diff options
-rw-r--r-- | tests/test-timezone-timedated.cpp | 98 | ||||
-rw-r--r-- | tests/timedated-fixture.h | 331 |
2 files changed, 96 insertions, 333 deletions
diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp index e613806..8c56459 100644 --- a/tests/test-timezone-timedated.cpp +++ b/tests/test-timezone-timedated.cpp @@ -18,110 +18,26 @@ * Ted Gould <ted.gould@canonical.com> */ -#include "glib-fixture.h" +#include "timedated-fixture.h" -#include <datetime/dbus-shared.h> #include <datetime/timezone-timedated.h> -#include <gio/gio.h> - - using namespace ayatana::indicator::datetime; - -struct Timedate1Fixture: public GlibFixture -{ -private: - - typedef GlibFixture super; - -protected: - - GDBusConnection* m_bus {}; - GTestDBus* m_test_bus {}; - - void SetUp() override - { - super::SetUp(); - - // use a fake bus - m_test_bus = g_test_dbus_new(G_TEST_DBUS_NONE); - g_test_dbus_up(m_test_bus); - const char * address = g_test_dbus_get_bus_address(m_test_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); - - // get the bus - m_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr); - g_dbus_connection_set_exit_on_close(m_bus, FALSE); - g_object_add_weak_pointer(G_OBJECT(m_bus), (gpointer*)&m_bus); - } - - void TearDown() override - { - g_clear_object(&m_bus); - g_clear_object(&m_test_bus); - - super::TearDown(); - } - - void start_timedate1(const std::string& tzid) - { - // start dbusmock with the timedated template - auto json_parameters = g_strdup_printf("{\"Timezone\": \"%s\"}", tzid.c_str()); - const gchar* child_argv[] = { "python3", "-m", "dbusmock", "--template", "timedated", "--parameters", json_parameters, nullptr }; - GError* error = nullptr; - g_spawn_async(nullptr, (gchar**)child_argv, nullptr, G_SPAWN_SEARCH_PATH, nullptr, nullptr, nullptr, &error); - g_assert_no_error(error); - g_free(json_parameters); - - // wait for it to appear on the bus - wait_for_name_owned(m_bus, Bus::Timedate1::BUSNAME); - } - - bool wait_for_tzid(const std::string& tzid, Timezone& tz) - { - return wait_for([&tzid, &tz](){return tzid == tz.timezone.get();}); - } - - void set_timedate1_timezone(const std::string& tzid) - { - GError* error {}; - auto v = g_dbus_connection_call_sync( - m_bus, - Bus::Timedate1::BUSNAME, - Bus::Timedate1::ADDR, - Bus::Timedate1::IFACE, - Bus::Timedate1::Methods::SET_TIMEZONE, - g_variant_new("(sb)", tzid.c_str(), FALSE), - nullptr, - G_DBUS_CALL_FLAGS_NONE, - -1, - nullptr, - &error); - g_clear_pointer(&v, g_variant_unref); - g_assert_no_error(error); - } -}; - -#define EXPECT_TZID(expected_tzid, tmp) \ - EXPECT_TRUE(wait_for_tzid(expected_tzid, tmp)) \ - << "expected " << expected_tzid \ - << " got " << tmp.timezone.get(); +using TestTimedatedFixture = TimedatedFixture; /*** **** ***/ -TEST_F(Timedate1Fixture, HelloWorld) +TEST_F(TestTimedatedFixture, HelloWorld) { } /** * Test that the tzid is right if timedated isn't available */ -TEST_F(Timedate1Fixture, DefaultTimezone) +TEST_F(TestTimedatedFixture, DefaultTimezone) { const std::string expected_tzid{"Etc/Utc"}; @@ -132,7 +48,7 @@ TEST_F(Timedate1Fixture, DefaultTimezone) /** * Test that the tzid is right if timedated shows BEFORE we start */ -TEST_F(Timedate1Fixture, Timedate1First) +TEST_F(TestTimedatedFixture, Timedate1First) { const std::string expected_tzid{"America/Chicago"}; @@ -144,7 +60,7 @@ TEST_F(Timedate1Fixture, Timedate1First) /** * Test that the tzid is right if timedated shows AFTER we start */ -TEST_F(Timedate1Fixture, Timedate1Last) +TEST_F(TestTimedatedFixture, Timedate1Last) { const std::string expected_tzid("America/Los_Angeles"); @@ -156,7 +72,7 @@ TEST_F(Timedate1Fixture, Timedate1Last) /** * Test that the tzid is right if timedated's property changes */ -TEST_F(Timedate1Fixture, TimezoneChange) +TEST_F(TestTimedatedFixture, TimezoneChange) { const std::vector<std::string> expected_tzids{"America/Los_Angeles", "America/Chicago", "Etc/Utc"}; diff --git a/tests/timedated-fixture.h b/tests/timedated-fixture.h index 00269e0..3aff986 100644 --- a/tests/timedated-fixture.h +++ b/tests/timedated-fixture.h @@ -17,285 +17,132 @@ * Charles Kerr <charles.kerr@canonical.com> */ -#ifndef INDICATOR_DATETIME_TESTS_TIMEDATED_FIXTURE_H -#define INDICATOR_DATETIME_TESTS_TIMEDATED_FIXTURE_H +#pragma once #include <datetime/actions-live.h> -#include "state-mock.h" #include "glib-fixture.h" -using namespace ayatana::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; } -}; +#include <datetime/dbus-shared.h> +#include <datetime/timezone.h> /*** **** ***/ -using namespace ayatana::indicator::datetime; - -class TimedateFixture: public GlibFixture +struct TimedatedFixture: 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) + using super = GlibFixture; - { - 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; - } +protected: + GDBusConnection* m_bus {}; + GTestDBus* m_test_bus {}; - static void on_bus_acquired(GDBusConnection* conn, - const gchar* name, - gpointer gself) + virtual void SetUp() override { - auto self = static_cast<TimedateFixture*>(gself); - g_debug("bus acquired: %s, connection is %p", name, conn); - - /* Set up a fake timedated which handles setting and getting the - ** timezone - */ - static const GDBusInterfaceVTable vtable = { - timedate1_handle_method_call, - timedate1_get_properties, /* GetProperty */ - nullptr, /* SetProperty */ - }; + super::SetUp(); - self->connection = G_DBUS_CONNECTION(g_object_ref(G_OBJECT(conn))); + // use a fake bus + m_test_bus = g_test_dbus_new(G_TEST_DBUS_NONE); + g_test_dbus_up(m_test_bus); + const char * address = g_test_dbus_get_bus_address(m_test_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); - 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); + // get the bus + m_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr); + g_dbus_connection_set_exit_on_close(m_bus, FALSE); + g_object_add_weak_pointer(G_OBJECT(m_bus), (gpointer*)&m_bus); } - static void on_name_acquired(GDBusConnection* conn, - const gchar* name, - gpointer gself) + virtual void TearDown() override { - 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); - } + g_clear_object(&m_bus); + g_clear_object(&m_test_bus); - 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; + super::TearDown(); } - static void on_bus_closed(GObject* /*object*/, - GAsyncResult* res, - gpointer gself) + void start_timedate1(const std::string& tzid) { - 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); + // start dbusmock with the timedated template + auto json_parameters = g_strdup_printf("{\"Timezone\": \"%s\"}", tzid.c_str()); + const gchar* child_argv[] = { + "python3", "-m", "dbusmock", + "--template", "timedated", + "--parameters", json_parameters, + nullptr + }; + GError* error = nullptr; + g_spawn_async(nullptr, (gchar**)child_argv, nullptr, G_SPAWN_SEARCH_PATH, nullptr, nullptr, nullptr, &error); + g_assert_no_error(error); + g_free(json_parameters); + + // wait for it to appear on the bus + wait_for_name_owned(m_bus, Bus::Timedate1::BUSNAME); } - 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) + bool wait_for_tzid(const std::string& tzid, ayatana::indicator::datetime::Timezone& tz) { - 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); + return wait_for([&tzid, &tz](){return tzid == tz.timezone.get();}); } -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() + void set_timedate1_timezone(const std::string& tzid) { - super::SetUp(); - g_debug("SetUp"); - - name_acquired = false; - attempted_tzid.clear(); - connection = nullptr; - node_info = nullptr; - object_register_id = 0; - own_name = 0; - proxy = nullptr; - - // 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)); + GError* error {}; + auto v = g_dbus_connection_call_sync( + m_bus, + Bus::Timedate1::BUSNAME, + Bus::Timedate1::ADDR, + Bus::Timedate1::IFACE, + Bus::Timedate1::Methods::SET_TIMEZONE, + g_variant_new("(sb)", tzid.c_str(), FALSE), + nullptr, + G_DBUS_CALL_FLAGS_NONE, + -1, + nullptr, + &error); + g_assert_no_error(error); - // 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); + g_clear_pointer(&v, g_variant_unref); } - void TearDown() + std::string get_timedate1_timezone() { - 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); + GError* error {}; + auto v = g_dbus_connection_call_sync( + m_bus, + Bus::Timedate1::BUSNAME, + Bus::Timedate1::ADDR, + Bus::Properties::IFACE, + Bus::Properties::Methods::GET, + g_variant_new("(ss)", Bus::Timedate1::IFACE, Bus::Timedate1::Properties::TIMEZONE), + G_VARIANT_TYPE("(v)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + nullptr, + &error); + g_assert_no_error(error); - 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); + GVariant* tzv {}; + g_variant_get(v, "(v)", &tzv); + std::string tzid; + const char* tz = g_variant_get_string(tzv, nullptr); + if (tz != nullptr) + tzid = tz; + + g_clear_pointer(&tzv, g_variant_unref); + g_clear_pointer(&v, g_variant_unref); + return tzid; } }; -#endif +#define EXPECT_TZID(expected_tzid, tmp) \ + EXPECT_TRUE(wait_for_tzid(expected_tzid, tmp)) \ + << "expected " << expected_tzid \ + << " got " << tmp.timezone.get(); + |