~ted/indicator-datetime/tests-fixes

« back to all changes in this revision

Viewing changes to tests/test-timezone-timedated.cpp

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:
18
18
 *   Ted Gould <ted.gould@canonical.com>
19
19
 */
20
20
 
21
 
#include "glib-fixture.h"
 
21
#include "timedated-fixture.h"
22
22
 
23
 
#include <datetime/dbus-shared.h>
24
23
#include <datetime/timezone-timedated.h>
25
24
 
26
 
#include <gio/gio.h>
27
 
 
28
 
 
29
25
using namespace unity::indicator::datetime;
30
26
 
31
 
 
32
 
struct Timedate1Fixture: public GlibFixture
33
 
{
34
 
private:
35
 
 
36
 
    typedef GlibFixture super;
37
 
 
38
 
protected:
39
 
 
40
 
    GDBusConnection* m_bus {};
41
 
    GTestDBus* m_test_bus {};
42
 
 
43
 
    void SetUp() override
44
 
    {
45
 
        super::SetUp();
46
 
 
47
 
        // use a fake bus
48
 
        m_test_bus = g_test_dbus_new(G_TEST_DBUS_NONE);
49
 
        g_test_dbus_up(m_test_bus);
50
 
        const char * address = g_test_dbus_get_bus_address(m_test_bus);
51
 
        g_setenv("DBUS_SYSTEM_BUS_ADDRESS", address, true);
52
 
        g_setenv("DBUS_SESSION_BUS_ADDRESS", address, true);
53
 
        g_debug("test_dbus's address is %s", address);
54
 
 
55
 
        // get the bus
56
 
        m_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr);
57
 
        g_dbus_connection_set_exit_on_close(m_bus, FALSE);
58
 
    }
59
 
 
60
 
    void TearDown() override
61
 
    {
62
 
        // tear down the bus
63
 
        bool bus_finished = false;
64
 
        g_object_weak_ref(
65
 
            G_OBJECT(m_bus),
66
 
            [](gpointer gbus_finished, GObject*){*static_cast<bool*>(gbus_finished) = true;},
67
 
            &bus_finished
68
 
        );
69
 
        g_clear_object(&m_bus);
70
 
        EXPECT_TRUE(wait_for([&bus_finished](){return bus_finished;}));
71
 
 
72
 
        // tear down test bus
73
 
        g_clear_object(&m_test_bus);
74
 
 
75
 
        super::TearDown();
76
 
    }
77
 
 
78
 
    void start_timedate1(const std::string& tzid)
79
 
    {
80
 
        // start dbusmock with the timedated template
81
 
        auto json_parameters = g_strdup_printf("{\"Timezone\": \"%s\"}", tzid.c_str());
82
 
        const gchar* child_argv[] = { "python3", "-m", "dbusmock", "--template", "timedated", "--parameters", json_parameters, nullptr };
83
 
        GError* error = nullptr;
84
 
        g_spawn_async(nullptr, (gchar**)child_argv, nullptr, G_SPAWN_SEARCH_PATH, nullptr, nullptr, nullptr, &error);
85
 
        g_assert_no_error(error);
86
 
        g_free(json_parameters);
87
 
 
88
 
        // wait for it to appear on the bus
89
 
        wait_for_name_owned(m_bus, Bus::Timedate1::BUSNAME);
90
 
    }
91
 
 
92
 
    bool wait_for_tzid(const std::string& tzid, Timezone& tz)
93
 
    {
94
 
        return wait_for([&tzid, &tz](){return tzid == tz.timezone.get();});
95
 
    }
96
 
 
97
 
    void set_timedate1_timezone(const std::string& tzid)
98
 
    {
99
 
        GError* error {};
100
 
        auto v = g_dbus_connection_call_sync(
101
 
            m_bus,
102
 
            Bus::Timedate1::BUSNAME,
103
 
            Bus::Timedate1::ADDR,
104
 
            Bus::Timedate1::IFACE,
105
 
            Bus::Timedate1::Methods::SET_TIMEZONE,
106
 
            g_variant_new("(sb)", tzid.c_str(), FALSE),
107
 
            nullptr,
108
 
            G_DBUS_CALL_FLAGS_NONE,
109
 
            -1,
110
 
            nullptr,
111
 
            &error);
112
 
        g_clear_pointer(&v, g_variant_unref);
113
 
        g_assert_no_error(error);
114
 
    }
115
 
};
116
 
 
117
 
#define EXPECT_TZID(expected_tzid, tmp) \
118
 
    EXPECT_TRUE(wait_for_tzid(expected_tzid, tmp)) \
119
 
        << "expected " << expected_tzid \
120
 
        << " got " << tmp.timezone.get();
 
27
using TestTimedatedFixture = TimedatedFixture;
121
28
 
122
29
/***
123
30
****
124
31
***/
125
32
 
126
 
TEST_F(Timedate1Fixture, HelloWorld)
 
33
TEST_F(TestTimedatedFixture, HelloWorld)
127
34
{
128
35
}
129
36
 
130
37
/**
131
38
 * Test that the tzid is right if timedated isn't available
132
39
 */
133
 
TEST_F(Timedate1Fixture, DefaultTimezone)
 
40
TEST_F(TestTimedatedFixture, DefaultTimezone)
134
41
{
135
42
    const std::string expected_tzid{"Etc/Utc"};
136
43
 
141
48
/**
142
49
 * Test that the tzid is right if timedated shows BEFORE we start
143
50
 */
144
 
TEST_F(Timedate1Fixture, Timedate1First)
 
51
TEST_F(TestTimedatedFixture, Timedate1First)
145
52
{
146
53
    const std::string expected_tzid{"America/Chicago"};
147
54
 
153
60
/**
154
61
 * Test that the tzid is right if timedated shows AFTER we start
155
62
 */
156
 
TEST_F(Timedate1Fixture, Timedate1Last)
 
63
TEST_F(TestTimedatedFixture, Timedate1Last)
157
64
{
158
65
    const std::string expected_tzid("America/Los_Angeles");
159
66
 
165
72
/**
166
73
 * Test that the tzid is right if timedated's property changes
167
74
 */
168
 
TEST_F(Timedate1Fixture, TimezoneChange)
 
75
TEST_F(TestTimedatedFixture, TimezoneChange)
169
76
{
170
77
    const std::vector<std::string> expected_tzids{"America/Los_Angeles", "America/Chicago", "Etc/Utc"};
171
78