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, querying_default_peer_returns_correct_value)
75
using namespace ::testing;
77
test::CrossProcessSync sync;
79
QString default_peer_id{"com.does.not.exist.anywhere.application"};
81
auto parent = [&sync, default_peer_id]()
84
QCoreApplication app{argc, nullptr};
86
QDBusConnection connection = QDBusConnection::sessionBus();
88
auto mock = new MockedPeerRegistry{};
89
EXPECT_CALL(*mock, default_peer_for_type(_)).
91
WillRepeatedly(Return(cuc::Peer{default_peer_id}));
93
QSharedPointer<cucd::PeerRegistry> registry{mock};
95
auto implementation = new cucd::Service(registry, &app);
96
new ServiceAdaptor(implementation);
98
ASSERT_TRUE(connection.registerService(service_name));
99
ASSERT_TRUE(connection.registerObject("/", implementation));
105
connection.unregisterObject("/");
106
connection.unregisterService(service_name);
109
auto child = [&sync, default_peer_id]()
111
sync.wait_for_signal_ready();
114
QCoreApplication app(argc, nullptr);
116
auto hub = cuc::Hub::Client::instance();
118
test::TestHarness harness;
119
harness.add_test_case([hub, default_peer_id]()
121
EXPECT_EQ(default_peer_id, hub->default_peer_for_type(cuc::Type::Known::documents()).id());
124
EXPECT_EQ(0, QTest::qExec(std::addressof(harness)));
130
EXPECT_TRUE(test::fork_and_run(child, parent) != EXIT_FAILURE);