2
* Copyright © 2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Thomas Voß <thomas.voss@canonical.com>
19
#include "test_harness.h"
20
#include "../cross_process_sync.h"
21
#include "../fork_and_run.h"
23
#include <com/ubuntu/content/hub.h>
24
#include <com/ubuntu/content/peer.h>
25
#include <com/ubuntu/content/scope.h>
26
#include <com/ubuntu/content/store.h>
27
#include <com/ubuntu/content/type.h>
29
#include "com/ubuntu/content/peer_registry.h"
30
#include "com/ubuntu/content/service.h"
31
#include "com/ubuntu/content/serviceadaptor.h"
33
#include <gmock/gmock.h>
34
#include <gtest/gtest.h>
36
#include <QCoreApplication>
37
#include <QtDBus/QDBusConnection>
38
#include <QStandardPaths>
39
#include <QtTest/QTest>
43
namespace cuc = com::ubuntu::content;
44
namespace cucd = com::ubuntu::content::detail;
46
void PrintTo(const QString& s, ::std::ostream* os) {
47
*os << std::string(qPrintable(s));
52
QString service_name{"com.ubuntu.content.Service"};
54
struct MockedPeerRegistry : public cucd::PeerRegistry
56
MockedPeerRegistry() : cucd::PeerRegistry()
58
using namespace ::testing;
60
ON_CALL(*this, default_peer_for_type(_)).WillByDefault(Return(cuc::Peer::unknown()));
61
ON_CALL(*this, install_default_peer_for_type(_,_)).WillByDefault(Return(false));
62
ON_CALL(*this, install_peer_for_type(_,_)).WillByDefault(Return(false));
65
MOCK_METHOD1(default_peer_for_type, cuc::Peer(cuc::Type t));
66
MOCK_METHOD2(enumerate_known_peers_for_type, void(cuc::Type, const std::function<void(const cuc::Peer&)>&));
68
MOCK_METHOD2(install_default_peer_for_type, bool(cuc::Type, cuc::Peer));
69
MOCK_METHOD2(install_peer_for_type, bool(cuc::Type, cuc::Peer));
73
TEST(Hub, stores_are_reported_correctly_to_clients)
75
test::CrossProcessSync sync;
77
auto parent = [&sync]()
80
QCoreApplication app{argc, nullptr};
82
QDBusConnection connection = QDBusConnection::sessionBus();
84
QSharedPointer<cucd::PeerRegistry> registry{new MockedPeerRegistry{}};
85
auto implementation = new cucd::Service(registry, &app);
86
new ServiceAdaptor(implementation);
88
ASSERT_TRUE(connection.registerService(service_name));
89
ASSERT_TRUE(connection.registerObject("/", implementation));
95
connection.unregisterObject("/");
96
connection.unregisterService(service_name);
98
delete implementation;
101
auto child = [&sync]()
103
sync.wait_for_signal_ready();
105
test::TestHarness harness;
106
harness.add_test_case([]()
108
auto hub = cuc::Hub::Client::instance();
110
auto system_wide_pictures_store = hub->store_for_scope_and_type(cuc::Scope::system, cuc::Type::Known::pictures());
111
auto system_wide_music_store = hub->store_for_scope_and_type(cuc::Scope::system, cuc::Type::Known::music());
112
auto system_wide_documents_store = hub->store_for_scope_and_type(cuc::Scope::system, cuc::Type::Known::documents());
114
auto users_pictures_store = hub->store_for_scope_and_type(cuc::Scope::user, cuc::Type::Known::pictures());
115
auto users_music_store = hub->store_for_scope_and_type(cuc::Scope::user, cuc::Type::Known::music());
116
auto users_documents_store = hub->store_for_scope_and_type(cuc::Scope::user, cuc::Type::Known::documents());
118
auto apps_pictures_store = hub->store_for_scope_and_type(cuc::Scope::app, cuc::Type::Known::pictures());
119
auto apps_music_store = hub->store_for_scope_and_type(cuc::Scope::app, cuc::Type::Known::music());
120
auto apps_documents_store = hub->store_for_scope_and_type(cuc::Scope::app, cuc::Type::Known::documents());
122
EXPECT_EQ(system_wide_pictures_store->uri(), "/content/Pictures");
123
EXPECT_EQ(system_wide_music_store->uri(), "/content/Music");
124
EXPECT_EQ(system_wide_documents_store->uri(), "/content/Documents");
126
auto ups = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
127
auto ums = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
128
auto uds = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
129
EXPECT_EQ(users_pictures_store->uri(), ups);
130
EXPECT_EQ(users_music_store->uri(), ums);
131
EXPECT_EQ(users_documents_store->uri(), uds);
133
auto aps = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/Pictures";
134
auto ams = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/Music";
135
auto ads = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/Documents";
136
EXPECT_EQ(apps_pictures_store->uri(), aps);
137
EXPECT_EQ(apps_music_store->uri(), ams);
138
EXPECT_EQ(apps_documents_store->uri(), ads);
142
EXPECT_EQ(0, QTest::qExec(std::addressof(harness)));
145
EXPECT_EQ(EXIT_SUCCESS, test::fork_and_run(child, parent));