1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*
* Copyright 2016 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*/
#include <tests/utils/qt-fixture.h>
#include <tests/utils/gtest-print-helpers.h>
#include <src/dbus-names.h>
#include <src/greeter.h>
#include <libqtdbustest/DBusTestRunner.h>
#include <libqtdbustest/QProcessDBusService.h>
#include <libqtdbusmock/DBusMock.h>
class GreeterFixture: public QtFixture
{
private:
using super = QtFixture;
public:
GreeterFixture() =default;
~GreeterFixture() =default;
protected:
std::shared_ptr<QtDBusTest::DBusTestRunner> m_dbus_runner;
std::shared_ptr<QtDBusMock::DBusMock> m_dbus_mock;
GDBusConnection* m_bus {};
void SetUp() override
{
super::SetUp();
// use a fresh bus for each test run
m_dbus_runner.reset(new QtDBusTest::DBusTestRunner());
m_dbus_mock.reset(new QtDBusMock::DBusMock(*m_dbus_runner.get()));
GError* error {};
m_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, nullptr, &error);
g_assert_no_error(error);
g_dbus_connection_set_exit_on_close(m_bus, FALSE);
}
void TearDown() override
{
g_clear_object(&m_bus);
super::TearDown();
}
void start_greeter_service(bool is_active)
{
// set a watcher to look for our mock greeter to appear
bool owned {};
QDBusServiceWatcher watcher(
DBusNames::Greeter::NAME,
m_dbus_runner->sessionConnection()
);
QObject::connect(
&watcher,
&QDBusServiceWatcher::serviceRegistered,
[&owned](const QString&){owned = true;}
);
// start the mock greeter
QVariantMap parameters;
parameters["IsActive"] = QVariant(is_active);
m_dbus_mock->registerTemplate(
DBusNames::Greeter::NAME,
GREETER_TEMPLATE,
parameters,
QDBusConnection::SessionBus
);
m_dbus_runner->startServices();
// wait for the watcher
ASSERT_TRUE(wait_for([&owned]{return owned;}));
}
};
#define ASSERT_PROPERTY_EQ_EVENTUALLY(expected_in, property_in) \
do { \
const auto& e = expected_in; \
const auto& p = property_in; \
ASSERT_TRUE(wait_for([e, &p](){return e == p.get();})) \
<< "expected " << e << " but got " << p.get(); \
} while(0)
/**
* Test startup timing by looking at four different cases:
* [unity greeter shows up on bus (before, after) we start listening]
* x [unity greeter is (active, inactive)]
*/
TEST_F(GreeterFixture, ActiveServiceStartsBeforeWatcher)
{
constexpr bool is_active {true};
constexpr Greeter::State expected {Greeter::State::ACTIVE};
start_greeter_service(is_active);
Greeter greeter;
ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
}
TEST_F(GreeterFixture, WatcherStartsBeforeActiveService)
{
constexpr bool is_active {true};
constexpr Greeter::State expected {Greeter::State::ACTIVE};
Greeter greeter;
start_greeter_service(is_active);
ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
}
TEST_F(GreeterFixture, InactiveServiceStartsBeforeWatcher)
{
constexpr bool is_active {false};
constexpr Greeter::State expected {Greeter::State::INACTIVE};
start_greeter_service(is_active);
Greeter greeter;
ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
}
TEST_F(GreeterFixture, WatcherStartsBeforeInactiveService)
{
constexpr bool is_active {false};
constexpr Greeter::State expected {Greeter::State::INACTIVE};
Greeter greeter;
start_greeter_service(is_active);
ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
}
|