~ted/indicator-datetime/tests-fixes

« back to all changes in this revision

Viewing changes to tests/timedated-fixture.h

Use dbusmock's timedated template in our tests Fixes: #1560960
Approved by: Renato Araujo Oliveira Filho, PS Jenkins bot, Charles Kerr

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *   Charles Kerr <charles.kerr@canonical.com>
18
18
 */
19
19
 
20
 
#ifndef INDICATOR_DATETIME_TESTS_TIMEDATED_FIXTURE_H
21
 
#define INDICATOR_DATETIME_TESTS_TIMEDATED_FIXTURE_H
 
20
#pragma once
22
21
 
23
22
#include <datetime/actions-live.h>
24
23
 
25
 
#include "state-mock.h"
26
24
#include "glib-fixture.h"
27
25
 
28
 
using namespace unity::indicator::datetime;
29
 
 
30
 
class MockLiveActions: public LiveActions
31
 
{
32
 
public:
33
 
    std::string last_cmd;
34
 
    std::string last_url;
35
 
    explicit MockLiveActions(const std::shared_ptr<State>& state_in): LiveActions(state_in) {}
36
 
    ~MockLiveActions() {}
37
 
 
38
 
protected:
39
 
    void dispatch_url(const std::string& url) override { last_url = url; }
40
 
    void execute_command(const std::string& cmd) override { last_cmd = cmd; }
41
 
};
 
26
#include <datetime/dbus-shared.h>
 
27
#include <datetime/timezone.h>
42
28
 
43
29
/***
44
30
****
45
31
***/
46
32
 
47
 
using namespace unity::indicator::datetime;
48
 
 
49
 
class TimedateFixture: public GlibFixture
 
33
struct TimedatedFixture: public GlibFixture
50
34
{
51
35
private:
52
36
 
53
 
    typedef GlibFixture super;
54
 
 
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,
60
 
                                                GError ** /*error*/,
61
 
                                                gpointer gself)
62
 
 
63
 
    {
64
 
        auto self = static_cast<TimedateFixture*>(gself);
65
 
        g_debug("get_properties called");
66
 
        if (g_strcmp0(property_name, "Timezone") == 0)
67
 
        {
68
 
            g_debug("timezone requested, giving '%s'",
69
 
                    self->attempted_tzid.c_str());
70
 
            return g_variant_new_string(self->attempted_tzid.c_str());
71
 
        }
72
 
        return nullptr;
73
 
    }
74
 
 
75
 
 
76
 
    static void on_bus_acquired(GDBusConnection* conn,
77
 
                                const gchar* name,
78
 
                                gpointer gself)
79
 
    {
80
 
        auto self = static_cast<TimedateFixture*>(gself);
81
 
        g_debug("bus acquired: %s, connection is %p", name, conn);
82
 
 
83
 
        /* Set up a fake timedated which handles setting and getting the
84
 
         ** timezone
85
 
         */
86
 
        static const GDBusInterfaceVTable vtable = {
87
 
            timedate1_handle_method_call,
88
 
            timedate1_get_properties, /* GetProperty */
89
 
            nullptr, /* SetProperty */
90
 
        };
91
 
 
92
 
        self->connection = G_DBUS_CONNECTION(g_object_ref(G_OBJECT(conn)));
93
 
 
94
 
        GError* error = nullptr;
95
 
        self->object_register_id = g_dbus_connection_register_object(
96
 
            conn,
97
 
            "/org/freedesktop/timedate1",
98
 
            self->node_info->interfaces[0],
99
 
            &vtable,
100
 
            self,
101
 
            nullptr,
102
 
            &error);
103
 
        g_assert_no_error(error);
104
 
    }
105
 
 
106
 
    static void on_name_acquired(GDBusConnection* conn,
107
 
                                 const gchar*     name,
108
 
                                 gpointer         gself)
109
 
    {
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,
115
 
                nullptr,
116
 
                name,
117
 
                "/org/freedesktop/timedate1",
118
 
                "org.freedesktop.timedate1",
119
 
                nullptr,
120
 
                nullptr);
121
 
        g_main_loop_quit(self->loop);
122
 
    }
123
 
 
124
 
    static void on_name_lost(GDBusConnection* /*conn*/,
125
 
                             const gchar*     /*name*/,
126
 
                             gpointer           gself)
127
 
    {
128
 
        g_debug("on_name_lost");
129
 
        auto self = static_cast<TimedateFixture*>(gself);
130
 
        self->name_acquired = false;
131
 
    }
132
 
 
133
 
    static void on_bus_closed(GObject*       /*object*/,
134
 
                              GAsyncResult*    res,
135
 
                              gpointer         gself)
136
 
    {
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);
143
 
    }
144
 
 
145
 
    static void
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,
153
 
                                 gpointer                  gself)
154
 
    {
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));
158
 
 
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);
165
 
 
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,
170
 
                               "{sv}",
171
 
                               "Timezone",
172
 
                               g_variant_new_string(
173
 
                                   self->attempted_tzid.c_str()));
174
 
        g_dbus_connection_emit_signal (connection,
175
 
                                       NULL,
176
 
                                       object_path,
177
 
                                       "org.freedesktop.DBus.Properties",
178
 
                                       "PropertiesChanged",
179
 
                                       g_variant_new ("(sa{sv}as)",
180
 
                                                      interface_name,
181
 
                                                      builder,
182
 
                                                      NULL),
183
 
                                       &local_error);
184
 
        g_assert_no_error (local_error);
185
 
        g_variant_unref(child);
186
 
    }
 
37
    using super = GlibFixture;
187
38
 
188
39
protected:
189
40
 
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;
194
 
 
195
 
    bool name_acquired;
196
 
    std::string attempted_tzid;
197
 
 
198
 
    GTestDBus* bus;
199
 
    guint own_name;
200
 
    GDBusConnection* connection;
201
 
    GDBusNodeInfo* node_info;
202
 
    int object_register_id;
203
 
    GDBusProxy *proxy;
204
 
 
205
 
    void SetUp()
 
41
    GDBusConnection* m_bus {};
 
42
    GTestDBus* m_test_bus {};
 
43
 
 
44
    virtual void SetUp() override
206
45
    {
207
46
        super::SetUp();
208
 
        g_debug("SetUp");
209
 
 
210
 
        name_acquired = false;
211
 
        attempted_tzid.clear();
212
 
        connection = nullptr;
213
 
        node_info = nullptr;
214
 
        object_register_id = 0;
215
 
        own_name = 0;
216
 
        proxy = nullptr;
217
 
 
218
 
        // bring up the test bus
219
 
        bus = g_test_dbus_new(G_TEST_DBUS_NONE);
220
 
        g_test_dbus_up(bus);
221
 
        const auto address = g_test_dbus_get_bus_address(bus);
 
47
 
 
48
        // use a fake 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);
225
55
 
226
 
        // parse the org.freedesktop.timedate1 interface
227
 
        const gchar introspection_xml[] =
228
 
            "<node>"
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'/>"
234
 
            "    </method>"
235
 
            "  </interface>"
236
 
            "</node>";
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);
243
 
 
244
 
        // own the bus
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,
249
 
                                  this, nullptr);
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));
257
 
 
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);
 
56
        // get the bus
 
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);
264
60
    }
265
61
 
266
 
    void TearDown()
 
62
    virtual void TearDown() override
267
63
    {
268
 
        g_debug("TearDown");
269
 
        m_actions.reset();
270
 
        m_live_actions.reset();
271
 
        m_state.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);
 
64
        // take down the bus
 
65
        bool bus_finished = false;
 
66
        g_object_weak_ref(
 
67
            G_OBJECT(m_bus),
 
68
            [](gpointer gbus_finished, GObject*){*static_cast<bool*>(gbus_finished) = true;},
 
69
            &bus_finished
 
70
        );
 
71
        g_clear_object(&m_bus);
 
72
        EXPECT_TRUE(wait_for([&bus_finished](){return bus_finished;}));
 
73
 
 
74
        // take down the GTestBus
 
75
        g_clear_object(&m_test_bus);
282
76
 
283
77
        super::TearDown();
284
78
    }
285
 
public:
286
 
    void set_timezone(std::string tz)
287
 
    {
288
 
        g_debug("set_timezone: '%s'", tz.c_str());
289
 
        g_dbus_proxy_call_sync(proxy,
290
 
                "SetTimezone",
291
 
                g_variant_new("(sb)",
292
 
                    tz.c_str(),
293
 
                    FALSE),
294
 
                G_DBUS_CALL_FLAGS_NONE,
295
 
                500,
296
 
                nullptr,
297
 
                nullptr);
 
79
 
 
80
    void start_timedate1(const std::string& tzid)
 
81
    {
 
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,
 
88
            nullptr
 
89
        };
 
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);
 
94
 
 
95
        // wait for it to appear on the bus
 
96
        wait_for_name_owned(m_bus, Bus::Timedate1::BUSNAME);
 
97
    }
 
98
 
 
99
    bool wait_for_tzid(const std::string& tzid, unity::indicator::datetime::Timezone& tz)
 
100
    {
 
101
        return wait_for([&tzid, &tz](){return tzid == tz.timezone.get();});
 
102
    }
 
103
 
 
104
    void set_timedate1_timezone(const std::string& tzid)
 
105
    {
 
106
        GError* error {};
 
107
        auto v = g_dbus_connection_call_sync(
 
108
            m_bus,
 
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),
 
114
            nullptr,
 
115
            G_DBUS_CALL_FLAGS_NONE,
 
116
            -1,
 
117
            nullptr,
 
118
            &error);
 
119
        g_assert_no_error(error);
 
120
 
 
121
        g_clear_pointer(&v, g_variant_unref);
 
122
    }
 
123
 
 
124
    std::string get_timedate1_timezone()
 
125
    {
 
126
        GError* error {};
 
127
        auto v = g_dbus_connection_call_sync(
 
128
            m_bus,
 
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,
 
136
            -1,
 
137
            nullptr,
 
138
            &error);
 
139
        g_assert_no_error(error);
 
140
 
 
141
        GVariant* tzv {}; 
 
142
        g_variant_get(v, "(v)", &tzv); 
 
143
        std::string tzid;
 
144
        const char* tz = g_variant_get_string(tzv, nullptr); 
 
145
        if (tz != nullptr)
 
146
            tzid = tz;
 
147
 
 
148
        g_clear_pointer(&tzv, g_variant_unref);
 
149
        g_clear_pointer(&v, g_variant_unref);
 
150
        return tzid;
298
151
    }
299
152
};
300
153
 
301
 
#endif
 
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();
 
158