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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
* Copyright 2013 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>
*/
#ifndef INDICATOR_DATETIME_TESTS_TIMEDATED_FIXTURE_H
#define INDICATOR_DATETIME_TESTS_TIMEDATED_FIXTURE_H
#include <datetime/actions-live.h>
#include "state-mock.h"
#include "glib-fixture.h"
using namespace unity::indicator::datetime;
class MockLiveActions: public LiveActions
{
public:
std::string last_cmd;
std::string last_url;
explicit MockLiveActions(const std::shared_ptr<State>& state_in): LiveActions(state_in) {}
~MockLiveActions() {}
protected:
void dispatch_url(const std::string& url) override { last_url = url; }
void execute_command(const std::string& cmd) override { last_cmd = cmd; }
};
/***
****
***/
using namespace unity::indicator::datetime;
class TimedateFixture: public GlibFixture
{
private:
typedef GlibFixture super;
static GVariant * timedate1_get_properties (GDBusConnection * /*connection*/ ,
const gchar * /*sender*/,
const gchar * /*object_path*/,
const gchar * /*interface_name*/,
const gchar *property_name,
GError ** /*error*/,
gpointer gself)
{
auto self = static_cast<TimedateFixture*>(gself);
g_debug("get_properties called");
if (g_strcmp0(property_name, "Timezone") == 0)
{
g_debug("timezone requested, giving '%s'",
self->attempted_tzid.c_str());
return g_variant_new_string(self->attempted_tzid.c_str());
}
return nullptr;
}
static void on_bus_acquired(GDBusConnection* conn,
const gchar* name,
gpointer gself)
{
auto self = static_cast<TimedateFixture*>(gself);
g_debug("bus acquired: %s, connection is %p", name, conn);
// Set up a mock GSD.
// All it really does is wait for calls to GetDevice and
// returns the get_devices_retval variant
static const GDBusInterfaceVTable vtable = {
timedate1_handle_method_call,
timedate1_get_properties, /* GetProperty */
nullptr, /* SetProperty */
};
self->connection = G_DBUS_CONNECTION(g_object_ref(G_OBJECT(conn)));
GError* error = nullptr;
self->object_register_id = g_dbus_connection_register_object(
conn,
"/org/freedesktop/timedate1",
self->node_info->interfaces[0],
&vtable,
self,
nullptr,
&error);
g_assert_no_error(error);
}
static void on_name_acquired(GDBusConnection* conn,
const gchar* name,
gpointer gself)
{
g_debug("on_name_acquired");
auto self = static_cast<TimedateFixture*>(gself);
self->name_acquired = true;
self->proxy = g_dbus_proxy_new_sync(conn,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
nullptr,
name,
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
nullptr,
nullptr);
g_main_loop_quit(self->loop);
}
static void on_name_lost(GDBusConnection* /*conn*/,
const gchar* /*name*/,
gpointer gself)
{
g_debug("on_name_lost");
auto self = static_cast<TimedateFixture*>(gself);
self->name_acquired = false;
}
static void on_bus_closed(GObject* /*object*/,
GAsyncResult* res,
gpointer gself)
{
g_debug("on_bus_closed");
auto self = static_cast<TimedateFixture*>(gself);
GError* err = nullptr;
g_dbus_connection_close_finish(self->connection, res, &err);
g_assert_no_error(err);
g_main_loop_quit(self->loop);
}
static void
timedate1_handle_method_call(GDBusConnection * connection,
const gchar * /*sender*/,
const gchar * object_path,
const gchar * interface_name,
const gchar * method_name,
GVariant * parameters,
GDBusMethodInvocation * invocation,
gpointer gself)
{
g_assert(!g_strcmp0(method_name, "SetTimezone"));
g_assert(g_variant_is_of_type(parameters, G_VARIANT_TYPE_TUPLE));
g_assert(2 == g_variant_n_children(parameters));
auto child = g_variant_get_child_value(parameters, 0);
g_assert(g_variant_is_of_type(child, G_VARIANT_TYPE_STRING));
auto self = static_cast<TimedateFixture*>(gself);
self->attempted_tzid = g_variant_get_string(child, nullptr);
g_debug("set tz (dbus side): '%s'", self->attempted_tzid.c_str());
g_dbus_method_invocation_return_value(invocation, nullptr);
/* Send PropertiesChanged */
GError * local_error = nullptr;
auto builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
g_variant_builder_add (builder,
"{sv}",
"Timezone",
g_variant_new_string(
self->attempted_tzid.c_str()));
g_dbus_connection_emit_signal (connection,
NULL,
object_path,
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
g_variant_new ("(sa{sv}as)",
interface_name,
builder,
NULL),
&local_error);
g_assert_no_error (local_error);
g_variant_unref(child);
g_main_loop_quit(self->loop);
}
protected:
std::shared_ptr<MockState> m_mock_state;
std::shared_ptr<State> m_state;
std::shared_ptr<MockLiveActions> m_live_actions;
std::shared_ptr<Actions> m_actions;
bool name_acquired;
std::string attempted_tzid;
GTestDBus* bus;
guint own_name;
GDBusConnection* connection;
GDBusNodeInfo* node_info;
int object_register_id;
GDBusProxy *proxy;
void SetUp()
{
super::SetUp();
g_debug("SetUp");
name_acquired = false;
attempted_tzid.clear();
connection = nullptr;
node_info = nullptr;
object_register_id = 0;
own_name = 0;
// bring up the test bus
bus = g_test_dbus_new(G_TEST_DBUS_NONE);
g_test_dbus_up(bus);
const auto address = g_test_dbus_get_bus_address(bus);
g_setenv("DBUS_SYSTEM_BUS_ADDRESS", address, true);
g_setenv("DBUS_SESSION_BUS_ADDRESS", address, true);
g_debug("test_dbus's address is %s", address);
// parse the org.freedesktop.timedate1 interface
const gchar introspection_xml[] =
"<node>"
" <interface name='org.freedesktop.timedate1'>"
" <property name='Timezone' type='s' access='read' />"
" <method name='SetTimezone'>"
" <arg name='timezone' type='s' direction='in'/>"
" <arg name='user_interaction' type='b' direction='in'/>"
" </method>"
" </interface>"
"</node>";
node_info = g_dbus_node_info_new_for_xml(introspection_xml, nullptr);
ASSERT_TRUE(node_info != nullptr);
ASSERT_TRUE(node_info->interfaces != nullptr);
ASSERT_TRUE(node_info->interfaces[0] != nullptr);
ASSERT_TRUE(node_info->interfaces[1] == nullptr);
ASSERT_STREQ("org.freedesktop.timedate1", node_info->interfaces[0]->name);
// own the bus
own_name = g_bus_own_name(G_BUS_TYPE_SYSTEM,
"org.freedesktop.timedate1",
G_BUS_NAME_OWNER_FLAGS_NONE,
on_bus_acquired, on_name_acquired, on_name_lost,
this, nullptr);
ASSERT_TRUE(object_register_id == 0);
ASSERT_FALSE(name_acquired);
ASSERT_TRUE(connection == nullptr);
g_main_loop_run(loop);
ASSERT_TRUE(object_register_id != 0);
ASSERT_TRUE(name_acquired);
ASSERT_TRUE(G_IS_DBUS_CONNECTION(connection));
// create the State and Actions
m_mock_state.reset(new MockState);
m_mock_state->settings.reset(new Settings);
m_state = std::dynamic_pointer_cast<State>(m_mock_state);
m_live_actions.reset(new MockLiveActions(m_state));
m_actions = std::dynamic_pointer_cast<Actions>(m_live_actions);
}
void TearDown()
{
g_debug("TearDown");
m_actions.reset();
m_live_actions.reset();
m_state.reset();
m_mock_state.reset();
g_dbus_connection_unregister_object(connection, object_register_id);
g_object_unref(proxy);
g_dbus_node_info_unref(node_info);
g_bus_unown_name(own_name);
g_dbus_connection_close(connection, nullptr, on_bus_closed, this);
g_main_loop_run(loop);
g_clear_object(&connection);
g_test_dbus_down(bus);
g_clear_object(&bus);
super::TearDown();
}
public:
void set_timezone(std::string tz)
{
g_debug("set_timezone: '%s'", tz.c_str());
g_dbus_proxy_call_sync(proxy,
"SetTimezone",
g_variant_new("(sb)",
tz.c_str(),
FALSE),
G_DBUS_CALL_FLAGS_NONE,
500,
nullptr,
nullptr);
}
};
#endif
|