1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
/*
* Copyright 2013 Canonical Ltd.
* Copyright 2021 Robert Tari
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
* Robert Tari <robert@tari.in>
*/
#include <datetime/clock.h>
#include <datetime/clock-mock.h>
#include <datetime/timezone.h>
#include <notifications/dbus-shared.h>
#include "test-dbus-fixture.h"
#include "timezone-mock.h"
/***
****
***/
using namespace ayatana::indicator::datetime;
class ClockFixture: public TestDBusFixture
{
private:
typedef TestDBusFixture super;
};
TEST_F(ClockFixture, MinuteChangedSignalShouldTriggerOncePerMinute)
{
// start up a live clock
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
const DateTime now = clock.localtime();
const auto gnow = now.get();
auto gthen = g_date_time_add_minutes(gnow, 1);
int count = 0;
clock.minute_changed.connect([&count](){count++;});
const auto msec = g_date_time_difference(gthen,gnow) / 1000;
wait_msec(msec);
EXPECT_EQ(1, count);
g_date_time_unref(gthen);
}
/***
****
***/
#define TIMEZONE_FILE (SANDBOX"/timezone")
TEST_F(ClockFixture, HelloFixture)
{
auto timezone_ = std::make_shared<MockTimezone>();
timezone_->timezone.set("America/New_York");
LiveClock clock(timezone_);
}
TEST_F(ClockFixture, TimezoneChangeTriggersSkew)
{
auto timezone_ = std::make_shared<MockTimezone>();
timezone_->timezone.set("America/New_York");
LiveClock clock(timezone_);
#if GLIB_CHECK_VERSION(2, 68, 0)
auto tz_nyc = g_time_zone_new_identifier("America/New_York");
if (tz_nyc == NULL)
{
tz_nyc = g_time_zone_new_utc();
}
#else
auto tz_nyc = g_time_zone_new("America/New_York");
#endif
auto now_nyc = g_date_time_new_now(tz_nyc);
auto now = clock.localtime();
EXPECT_EQ(g_date_time_get_utc_offset(now_nyc), g_date_time_get_utc_offset(now.get()));
EXPECT_LE(abs(g_date_time_difference(now_nyc,now.get())), G_USEC_PER_SEC);
g_date_time_unref(now_nyc);
g_time_zone_unref(tz_nyc);
/// change the timezones!
clock.minute_changed.connect([this](){
g_main_loop_quit(loop);
});
g_idle_add([](gpointer gs){
static_cast<Timezone*>(gs)->timezone.set("America/Los_Angeles");
return G_SOURCE_REMOVE;
}, timezone_.get());
g_main_loop_run(loop);
#if GLIB_CHECK_VERSION(2, 68, 0)
auto tz_la = g_time_zone_new_identifier("America/Los_Angeles");
if (tz_la == NULL)
{
tz_la = g_time_zone_new_utc();
}
#else
auto tz_la = g_time_zone_new("America/Los_Angeles");
#endif
auto now_la = g_date_time_new_now(tz_la);
now = clock.localtime();
EXPECT_EQ(g_date_time_get_utc_offset(now_la), g_date_time_get_utc_offset(now.get()));
EXPECT_LE(abs(g_date_time_difference(now_la,now.get())), G_USEC_PER_SEC);
g_date_time_unref(now_la);
g_time_zone_unref(tz_la);
}
/***
****
***/
namespace
{
void on_login1_name_acquired(GDBusConnection * connection,
const gchar * /*name*/,
gpointer /*user_data*/)
{
g_dbus_connection_emit_signal(connection,
nullptr,
"/org/freedesktop/login1", // object path
"org.freedesktop.login1.Manager", // interface
"PrepareForSleep", // signal name
g_variant_new("(b)", FALSE),
nullptr);
}
}
/**
* Confirm that a "PrepareForSleep" event wil trigger a skew event
*/
TEST_F(ClockFixture, SleepTriggersSkew)
{
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;
clock.minute_changed.connect([&skewed, this](){
skewed = true;
g_main_loop_quit(loop);
return G_SOURCE_REMOVE;
});
auto name_tag = g_bus_own_name(G_BUS_TYPE_SYSTEM,
"org.freedesktop.login1",
G_BUS_NAME_OWNER_FLAGS_NONE,
nullptr /* bus acquired */,
on_login1_name_acquired,
nullptr /* name lost */,
nullptr /* user_data */,
nullptr /* user_data closure */);
g_main_loop_run(loop);
EXPECT_TRUE(skewed);
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);
}
|