17
17
* Charles Kerr <charles.kerr@canonical.com>
20
#ifndef INDICATOR_DATETIME_TESTS_TIMEDATED_FIXTURE_H
21
#define INDICATOR_DATETIME_TESTS_TIMEDATED_FIXTURE_H
23
22
#include <datetime/actions-live.h>
25
#include "state-mock.h"
26
24
#include "glib-fixture.h"
28
using namespace unity::indicator::datetime;
30
class MockLiveActions: public LiveActions
35
explicit MockLiveActions(const std::shared_ptr<State>& state_in): LiveActions(state_in) {}
39
void dispatch_url(const std::string& url) override { last_url = url; }
40
void execute_command(const std::string& cmd) override { last_cmd = cmd; }
26
#include <datetime/dbus-shared.h>
27
#include <datetime/timezone.h>
47
using namespace unity::indicator::datetime;
49
class TimedateFixture: public GlibFixture
33
struct TimedatedFixture: public GlibFixture
53
typedef GlibFixture super;
55
static GVariant * timedate1_get_properties (GDBusConnection * /*connection*/ ,
56
const gchar * /*sender*/,
57
const gchar * /*object_path*/,
58
const gchar * /*interface_name*/,
59
const gchar *property_name,
64
auto self = static_cast<TimedateFixture*>(gself);
65
g_debug("get_properties called");
66
if (g_strcmp0(property_name, "Timezone") == 0)
68
g_debug("timezone requested, giving '%s'",
69
self->attempted_tzid.c_str());
70
return g_variant_new_string(self->attempted_tzid.c_str());
76
static void on_bus_acquired(GDBusConnection* conn,
80
auto self = static_cast<TimedateFixture*>(gself);
81
g_debug("bus acquired: %s, connection is %p", name, conn);
83
/* Set up a fake timedated which handles setting and getting the
86
static const GDBusInterfaceVTable vtable = {
87
timedate1_handle_method_call,
88
timedate1_get_properties, /* GetProperty */
89
nullptr, /* SetProperty */
92
self->connection = G_DBUS_CONNECTION(g_object_ref(G_OBJECT(conn)));
94
GError* error = nullptr;
95
self->object_register_id = g_dbus_connection_register_object(
97
"/org/freedesktop/timedate1",
98
self->node_info->interfaces[0],
103
g_assert_no_error(error);
106
static void on_name_acquired(GDBusConnection* conn,
110
g_debug("on_name_acquired");
111
auto self = static_cast<TimedateFixture*>(gself);
112
self->name_acquired = true;
113
self->proxy = g_dbus_proxy_new_sync(conn,
114
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
117
"/org/freedesktop/timedate1",
118
"org.freedesktop.timedate1",
121
g_main_loop_quit(self->loop);
124
static void on_name_lost(GDBusConnection* /*conn*/,
125
const gchar* /*name*/,
128
g_debug("on_name_lost");
129
auto self = static_cast<TimedateFixture*>(gself);
130
self->name_acquired = false;
133
static void on_bus_closed(GObject* /*object*/,
137
g_debug("on_bus_closed");
138
auto self = static_cast<TimedateFixture*>(gself);
139
GError* err = nullptr;
140
g_dbus_connection_close_finish(self->connection, res, &err);
141
g_assert_no_error(err);
142
g_main_loop_quit(self->loop);
146
timedate1_handle_method_call(GDBusConnection * connection,
147
const gchar * /*sender*/,
148
const gchar * object_path,
149
const gchar * interface_name,
150
const gchar * method_name,
151
GVariant * parameters,
152
GDBusMethodInvocation * invocation,
155
g_assert(!g_strcmp0(method_name, "SetTimezone"));
156
g_assert(g_variant_is_of_type(parameters, G_VARIANT_TYPE_TUPLE));
157
g_assert(2 == g_variant_n_children(parameters));
159
auto child = g_variant_get_child_value(parameters, 0);
160
g_assert(g_variant_is_of_type(child, G_VARIANT_TYPE_STRING));
161
auto self = static_cast<TimedateFixture*>(gself);
162
self->attempted_tzid = g_variant_get_string(child, nullptr);
163
g_debug("set tz (dbus side): '%s'", self->attempted_tzid.c_str());
164
g_dbus_method_invocation_return_value(invocation, nullptr);
166
/* Send PropertiesChanged */
167
GError * local_error = nullptr;
168
auto builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
169
g_variant_builder_add (builder,
172
g_variant_new_string(
173
self->attempted_tzid.c_str()));
174
g_dbus_connection_emit_signal (connection,
177
"org.freedesktop.DBus.Properties",
179
g_variant_new ("(sa{sv}as)",
184
g_assert_no_error (local_error);
185
g_variant_unref(child);
37
using super = GlibFixture;
190
std::shared_ptr<MockState> m_mock_state;
191
std::shared_ptr<State> m_state;
192
std::shared_ptr<MockLiveActions> m_live_actions;
193
std::shared_ptr<Actions> m_actions;
196
std::string attempted_tzid;
200
GDBusConnection* connection;
201
GDBusNodeInfo* node_info;
202
int object_register_id;
41
GDBusConnection* m_bus {};
42
GTestDBus* m_test_bus {};
44
virtual void SetUp() override
210
name_acquired = false;
211
attempted_tzid.clear();
212
connection = nullptr;
214
object_register_id = 0;
218
// bring up the test bus
219
bus = g_test_dbus_new(G_TEST_DBUS_NONE);
221
const auto address = g_test_dbus_get_bus_address(bus);
49
m_test_bus = g_test_dbus_new(G_TEST_DBUS_NONE);
50
g_test_dbus_up(m_test_bus);
51
const char * address = g_test_dbus_get_bus_address(m_test_bus);
222
52
g_setenv("DBUS_SYSTEM_BUS_ADDRESS", address, true);
223
53
g_setenv("DBUS_SESSION_BUS_ADDRESS", address, true);
224
54
g_debug("test_dbus's address is %s", address);
226
// parse the org.freedesktop.timedate1 interface
227
const gchar introspection_xml[] =
229
" <interface name='org.freedesktop.timedate1'>"
230
" <property name='Timezone' type='s' access='read' />"
231
" <method name='SetTimezone'>"
232
" <arg name='timezone' type='s' direction='in'/>"
233
" <arg name='user_interaction' type='b' direction='in'/>"
237
node_info = g_dbus_node_info_new_for_xml(introspection_xml, nullptr);
238
ASSERT_TRUE(node_info != nullptr);
239
ASSERT_TRUE(node_info->interfaces != nullptr);
240
ASSERT_TRUE(node_info->interfaces[0] != nullptr);
241
ASSERT_TRUE(node_info->interfaces[1] == nullptr);
242
ASSERT_STREQ("org.freedesktop.timedate1", node_info->interfaces[0]->name);
245
own_name = g_bus_own_name(G_BUS_TYPE_SYSTEM,
246
"org.freedesktop.timedate1",
247
G_BUS_NAME_OWNER_FLAGS_NONE,
248
on_bus_acquired, on_name_acquired, on_name_lost,
250
ASSERT_TRUE(object_register_id == 0);
251
ASSERT_FALSE(name_acquired);
252
ASSERT_TRUE(connection == nullptr);
253
g_main_loop_run(loop);
254
ASSERT_TRUE(object_register_id != 0);
255
ASSERT_TRUE(name_acquired);
256
ASSERT_TRUE(G_IS_DBUS_CONNECTION(connection));
258
// create the State and Actions
259
m_mock_state.reset(new MockState);
260
m_mock_state->settings.reset(new Settings);
261
m_state = std::dynamic_pointer_cast<State>(m_mock_state);
262
m_live_actions.reset(new MockLiveActions(m_state));
263
m_actions = std::dynamic_pointer_cast<Actions>(m_live_actions);
57
m_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr);
58
g_dbus_connection_set_exit_on_close(m_bus, FALSE);
59
g_object_add_weak_pointer(G_OBJECT(m_bus), (gpointer*)&m_bus);
62
virtual void TearDown() override
270
m_live_actions.reset();
272
m_mock_state.reset();
273
g_dbus_connection_unregister_object(connection, object_register_id);
274
g_object_unref(proxy);
275
g_dbus_node_info_unref(node_info);
276
g_bus_unown_name(own_name);
277
g_dbus_connection_close(connection, nullptr, on_bus_closed, this);
278
g_main_loop_run(loop);
279
g_clear_object(&connection);
280
g_test_dbus_down(bus);
281
g_clear_object(&bus);
65
bool bus_finished = false;
68
[](gpointer gbus_finished, GObject*){*static_cast<bool*>(gbus_finished) = true;},
71
g_clear_object(&m_bus);
72
EXPECT_TRUE(wait_for([&bus_finished](){return bus_finished;}));
74
// take down the GTestBus
75
g_clear_object(&m_test_bus);
283
77
super::TearDown();
286
void set_timezone(std::string tz)
288
g_debug("set_timezone: '%s'", tz.c_str());
289
g_dbus_proxy_call_sync(proxy,
291
g_variant_new("(sb)",
294
G_DBUS_CALL_FLAGS_NONE,
80
void start_timedate1(const std::string& tzid)
82
// start dbusmock with the timedated template
83
auto json_parameters = g_strdup_printf("{\"Timezone\": \"%s\"}", tzid.c_str());
84
const gchar* child_argv[] = {
85
"python3", "-m", "dbusmock",
86
"--template", "timedated",
87
"--parameters", json_parameters,
90
GError* error = nullptr;
91
g_spawn_async(nullptr, (gchar**)child_argv, nullptr, G_SPAWN_SEARCH_PATH, nullptr, nullptr, nullptr, &error);
92
g_assert_no_error(error);
93
g_free(json_parameters);
95
// wait for it to appear on the bus
96
wait_for_name_owned(m_bus, Bus::Timedate1::BUSNAME);
99
bool wait_for_tzid(const std::string& tzid, unity::indicator::datetime::Timezone& tz)
101
return wait_for([&tzid, &tz](){return tzid == tz.timezone.get();});
104
void set_timedate1_timezone(const std::string& tzid)
107
auto v = g_dbus_connection_call_sync(
109
Bus::Timedate1::BUSNAME,
110
Bus::Timedate1::ADDR,
111
Bus::Timedate1::IFACE,
112
Bus::Timedate1::Methods::SET_TIMEZONE,
113
g_variant_new("(sb)", tzid.c_str(), FALSE),
115
G_DBUS_CALL_FLAGS_NONE,
119
g_assert_no_error(error);
121
g_clear_pointer(&v, g_variant_unref);
124
std::string get_timedate1_timezone()
127
auto v = g_dbus_connection_call_sync(
129
Bus::Timedate1::BUSNAME,
130
Bus::Timedate1::ADDR,
131
Bus::Properties::IFACE,
132
Bus::Properties::Methods::GET,
133
g_variant_new("(ss)", Bus::Timedate1::IFACE, Bus::Timedate1::Properties::TIMEZONE),
134
G_VARIANT_TYPE("(v)"),
135
G_DBUS_CALL_FLAGS_NONE,
139
g_assert_no_error(error);
142
g_variant_get(v, "(v)", &tzv);
144
const char* tz = g_variant_get_string(tzv, nullptr);
148
g_clear_pointer(&tzv, g_variant_unref);
149
g_clear_pointer(&v, g_variant_unref);
154
#define EXPECT_TZID(expected_tzid, tmp) \
155
EXPECT_TRUE(wait_for_tzid(expected_tzid, tmp)) \
156
<< "expected " << expected_tzid \
157
<< " got " << tmp.timezone.get();