aboutsummaryrefslogtreecommitdiff
path: root/tests/test-clock.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test-clock.cpp')
-rw-r--r--tests/test-clock.cpp92
1 files changed, 77 insertions, 15 deletions
diff --git a/tests/test-clock.cpp b/tests/test-clock.cpp
index 82f8cb0..5601d35 100644
--- a/tests/test-clock.cpp
+++ b/tests/test-clock.cpp
@@ -18,9 +18,13 @@
*/
#include <datetime/clock.h>
-#include <datetime/timezones.h>
+#include <datetime/clock-mock.h>
+#include <datetime/timezone.h>
+
+#include <notifications/dbus-shared.h>
#include "test-dbus-fixture.h"
+#include "timezone-mock.h"
/***
****
@@ -37,9 +41,9 @@ class ClockFixture: public TestDBusFixture
TEST_F(ClockFixture, MinuteChangedSignalShouldTriggerOncePerMinute)
{
// start up a live clock
- std::shared_ptr<Timezones> zones(new Timezones);
- zones->timezone.set("America/New_York");
- LiveClock clock(zones);
+ auto timezone_ = std::make_shared<MockTimezone>();
+ timezone_->timezone.set("America/New_York");
+ LiveClock clock(timezone_);
wait_msec(500); // wait for the bus to set up
// count how many times clock.minute_changed() is emitted over the next minute
@@ -62,17 +66,17 @@ TEST_F(ClockFixture, MinuteChangedSignalShouldTriggerOncePerMinute)
TEST_F(ClockFixture, HelloFixture)
{
- std::shared_ptr<Timezones> zones(new Timezones);
- zones->timezone.set("America/New_York");
- LiveClock clock(zones);
+ auto timezone_ = std::make_shared<MockTimezone>();
+ timezone_->timezone.set("America/New_York");
+ LiveClock clock(timezone_);
}
TEST_F(ClockFixture, TimezoneChangeTriggersSkew)
{
- std::shared_ptr<Timezones> zones(new Timezones);
- zones->timezone.set("America/New_York");
- LiveClock clock(zones);
+ auto timezone_ = std::make_shared<MockTimezone>();
+ timezone_->timezone.set("America/New_York");
+ LiveClock clock(timezone_);
auto tz_nyc = g_time_zone_new("America/New_York");
auto now_nyc = g_date_time_new_now(tz_nyc);
@@ -87,9 +91,9 @@ TEST_F(ClockFixture, TimezoneChangeTriggersSkew)
g_main_loop_quit(loop);
});
g_idle_add([](gpointer gs){
- static_cast<Timezones*>(gs)->timezone.set("America/Los_Angeles");
+ static_cast<Timezone*>(gs)->timezone.set("America/Los_Angeles");
return G_SOURCE_REMOVE;
- }, zones.get());
+ }, timezone_.get());
g_main_loop_run(loop);
auto tz_la = g_time_zone_new("America/Los_Angeles");
@@ -127,9 +131,9 @@ namespace
*/
TEST_F(ClockFixture, SleepTriggersSkew)
{
- std::shared_ptr<Timezones> zones(new Timezones);
- zones->timezone.set("America/New_York");
- LiveClock clock(zones);
+ auto timezone_ = std::make_shared<MockTimezone>();
+ timezone_->timezone.set("America/New_York");
+ LiveClock clock(timezone_);
wait_msec(250); // wait for the bus to set up
bool skewed = false;
@@ -152,3 +156,61 @@ TEST_F(ClockFixture, SleepTriggersSkew)
g_bus_unown_name(name_tag);
}
+
+namespace
+{
+ void on_powerd_name_acquired(GDBusConnection * /*connection*/,
+ const gchar * /*name*/,
+ gpointer is_owned)
+ {
+ *static_cast<bool*>(is_owned) = true;
+ }
+}
+
+/**
+ * Confirm that powerd's SysPowerStateChange triggers
+ * a timestamp change
+ */
+TEST_F(ClockFixture, SysPowerStateChange)
+{
+ // set up the mock clock
+ bool minute_changed = false;
+ auto clock = std::make_shared<MockClock>(DateTime::NowLocal());
+ clock->minute_changed.connect([&minute_changed]() {
+ minute_changed = true;
+ });
+
+ // control test -- minute_changed shouldn't get triggered
+ // when the clock is silently changed
+ gboolean is_owned = false;
+ auto tag = g_bus_own_name_on_connection(system_bus,
+ BUS_POWERD_NAME,
+ G_BUS_NAME_OWNER_FLAGS_NONE,
+ on_powerd_name_acquired,
+ nullptr,
+ &is_owned /* user_data */,
+ nullptr /* user_data closure */);
+ const DateTime not_now {DateTime::Local(1999, 12, 31, 23, 59, 59)};
+ clock->set_localtime_quietly(not_now);
+ wait_msec();
+ ASSERT_TRUE(is_owned);
+ ASSERT_FALSE(minute_changed);
+
+ // now for the actual test,
+ // confirm that SysPowerStateChange triggers a minute_changed() signal
+ GError * error = nullptr;
+ auto emitted = g_dbus_connection_emit_signal(system_bus,
+ nullptr,
+ BUS_POWERD_PATH,
+ BUS_POWERD_INTERFACE,
+ "SysPowerStateChange",
+ g_variant_new("(i)", 1),
+ &error);
+ wait_msec();
+ EXPECT_TRUE(emitted);
+ EXPECT_EQ(nullptr, error);
+ EXPECT_TRUE(minute_changed);
+
+ // cleanup
+ g_bus_unown_name(tag);
+}