aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/datetime/clock-mock.h12
-rw-r--r--tests/test-clock.cpp61
2 files changed, 71 insertions, 2 deletions
diff --git a/include/datetime/clock-mock.h b/include/datetime/clock-mock.h
index fb9b52f..0e24377 100644
--- a/include/datetime/clock-mock.h
+++ b/include/datetime/clock-mock.h
@@ -41,15 +41,23 @@ public:
DateTime localtime() const { return m_localtime; }
- void set_localtime(const DateTime& dt) {
+ void set_localtime(const DateTime& dt)
+ {
const auto old = m_localtime;
- m_localtime = dt;
+
+ set_localtime_quietly(dt);
+
if (!DateTime::is_same_minute(old, m_localtime))
minute_changed();
if (!DateTime::is_same_day(old, m_localtime))
date_changed();
}
+ void set_localtime_quietly(const DateTime& dt)
+ {
+ m_localtime = dt;
+ }
+
private:
DateTime m_localtime;
};
diff --git a/tests/test-clock.cpp b/tests/test-clock.cpp
index 82f8cb0..62281fb 100644
--- a/tests/test-clock.cpp
+++ b/tests/test-clock.cpp
@@ -18,8 +18,11 @@
*/
#include <datetime/clock.h>
+#include <datetime/clock-mock.h>
#include <datetime/timezones.h>
+#include <notifications/dbus-shared.h>
+
#include "test-dbus-fixture.h"
/***
@@ -152,3 +155,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);
+}