aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIain Lane <iain.lane@canonical.com>2015-09-03 10:54:20 +0100
committerIain Lane <iain.lane@canonical.com>2015-09-03 10:54:20 +0100
commit29b5c4da1e44534352d29536bb6ad1c721406b8a (patch)
treeb03e1ad209e51d57036c2706e2df764ed0ed8778 /tests
parent1da27f9088a759e78e88a390583d8646f2a82656 (diff)
downloadayatana-indicator-datetime-29b5c4da1e44534352d29536bb6ad1c721406b8a.tar.gz
ayatana-indicator-datetime-29b5c4da1e44534352d29536bb6ad1c721406b8a.tar.bz2
ayatana-indicator-datetime-29b5c4da1e44534352d29536bb6ad1c721406b8a.zip
Avoid nested GMainLoops by reading from the file on startup
Restore some tests for this functionality.
Diffstat (limited to 'tests')
-rw-r--r--tests/test-timezone-timedated.cpp100
-rw-r--r--tests/timedated-fixture.h3
2 files changed, 85 insertions, 18 deletions
diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp
index 7086e96..5cfc311 100644
--- a/tests/test-timezone-timedated.cpp
+++ b/tests/test-timezone-timedated.cpp
@@ -22,43 +22,99 @@
#include <datetime/timezone-timedated.h>
-#include <cstdio> // fopen()
-//#include <sys/stat.h> // chmod()
-#include <unistd.h> // sync()
-
using unity::indicator::datetime::TimedatedTimezone;
+/***
+****
+***/
+
+#define TIMEZONE_FILE (SANDBOX"/timezone")
+
+class TimezoneFixture: public TimedateFixture
+{
+ private:
+
+ typedef TimedateFixture super;
+
+ protected:
+
+ void SetUp() override
+ {
+ super::SetUp();
+ }
+
+ void TearDown() override
+ {
+ super::TearDown();
+ }
+
+ public:
+
+ /* convenience func to set the timezone file */
+ void set_file(const std::string& text)
+ {
+ g_debug("set_file %s %s", TIMEZONE_FILE, text.c_str());
+ auto fp = fopen(TIMEZONE_FILE, "w+");
+ fprintf(fp, "%s\n", text.c_str());
+ fclose(fp);
+ sync();
+ }
+};
+
/**
- * Test that timezone-file picks up the initial value
+ * Test that timezone-timedated warns, but doesn't crash, if the timezone file doesn't exist
*/
-TEST_F(TimedateFixture, InitialValue)
+TEST_F(TimezoneFixture, NoFile)
{
- const std::string expected_timezone = "America/Chicago";
- set_timezone(expected_timezone);
- TimedatedTimezone tz;
+ remove(TIMEZONE_FILE);
+ ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS));
+
+ TimedatedTimezone tz(TIMEZONE_FILE);
+ testLogCount(G_LOG_LEVEL_WARNING, 1);
+}
+
+/**
+ * Test that timezone-timedated gives a default of UTC if the file doesn't exist
+ */
+TEST_F(TimezoneFixture, DefaultValueNoFile)
+{
+ const std::string expected_timezone = "Etc/Utc";
+ remove(TIMEZONE_FILE);
+ ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS));
+
+ TimedatedTimezone tz(TIMEZONE_FILE);
ASSERT_EQ(expected_timezone, tz.timezone.get());
}
/**
+ * Test that timezone-timedated picks up the initial value
+ */
+TEST_F(TimezoneFixture, InitialValue)
+{
+ const std::string expected_timezone = "America/Chicago";
+ set_file(expected_timezone);
+ TimedatedTimezone tz(TIMEZONE_FILE);
+}
+
+/**
* Test that changing the tz after we are running works.
*/
-TEST_F(TimedateFixture, ChangedValue)
+TEST_F(TimezoneFixture, ChangedValue)
{
const std::string initial_timezone = "America/Chicago";
const std::string changed_timezone = "America/New_York";
- GMainLoop *l = g_main_loop_new(nullptr, FALSE);
- set_timezone(initial_timezone);
+ set_file(initial_timezone);
- TimedatedTimezone tz;
+ TimedatedTimezone tz(TIMEZONE_FILE);
ASSERT_EQ(initial_timezone, tz.timezone.get());
bool changed = false;
tz.timezone.changed().connect(
- [&changed, this, l](const std::string& s){
+ [&changed, this](const std::string& s){
g_message("timezone changed to %s", s.c_str());
changed = true;
- g_main_loop_quit(l);
+ g_main_loop_quit(loop);
});
g_idle_add([](gpointer gself){
@@ -66,8 +122,20 @@ TEST_F(TimedateFixture, ChangedValue)
return G_SOURCE_REMOVE;
}, this);
- g_main_loop_run(l);
+ g_main_loop_run(loop);
ASSERT_TRUE(changed);
ASSERT_EQ(changed_timezone, tz.timezone.get());
}
+
+/**
+ * Test that timezone-timedated picks up the initial value
+ */
+TEST_F(TimezoneFixture, IgnoreComments)
+{
+ const std::string comment = "# Created by cloud-init v. 0.7.5 on Thu, 24 Apr 2014 14:03:29 +0000";
+ const std::string expected_timezone = "Europe/Berlin";
+ set_file(comment + "\n" + expected_timezone);
+ TimedatedTimezone tz(TIMEZONE_FILE);
+ ASSERT_EQ(expected_timezone, tz.timezone.get());
+}
diff --git a/tests/timedated-fixture.h b/tests/timedated-fixture.h
index 0e89ea1..5ec425d 100644
--- a/tests/timedated-fixture.h
+++ b/tests/timedated-fixture.h
@@ -183,8 +183,6 @@ private:
&local_error);
g_assert_no_error (local_error);
g_variant_unref(child);
-
- g_main_loop_quit(self->loop);
}
protected:
@@ -215,6 +213,7 @@ protected:
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);