From a3937830bf656d5a8e2f368757b947cef0c8b1de Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Tue, 1 Sep 2015 10:52:13 +0100 Subject: Rename FileTimezone to TimedatedTimezone --- tests/test-timezone-timedated.cpp | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/test-timezone-timedated.cpp (limited to 'tests/test-timezone-timedated.cpp') diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp new file mode 100644 index 0000000..7086e96 --- /dev/null +++ b/tests/test-timezone-timedated.cpp @@ -0,0 +1,73 @@ + +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * 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 . + */ + +#include "timedated-fixture.h" + +#include + +#include // fopen() +//#include // chmod() +#include // sync() + +using unity::indicator::datetime::TimedatedTimezone; + +/** + * Test that timezone-file picks up the initial value + */ +TEST_F(TimedateFixture, InitialValue) +{ + const std::string expected_timezone = "America/Chicago"; + set_timezone(expected_timezone); + TimedatedTimezone tz; + ASSERT_EQ(expected_timezone, tz.timezone.get()); +} + +/** + * Test that changing the tz after we are running works. + */ +TEST_F(TimedateFixture, 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); + + TimedatedTimezone tz; + ASSERT_EQ(initial_timezone, tz.timezone.get()); + + bool changed = false; + tz.timezone.changed().connect( + [&changed, this, l](const std::string& s){ + g_message("timezone changed to %s", s.c_str()); + changed = true; + g_main_loop_quit(l); + }); + + g_idle_add([](gpointer gself){ + static_cast(gself)->set_timezone("America/New_York"); + return G_SOURCE_REMOVE; + }, this); + + g_main_loop_run(l); + + ASSERT_TRUE(changed); + ASSERT_EQ(changed_timezone, tz.timezone.get()); +} -- cgit v1.2.3 From 29b5c4da1e44534352d29536bb6ad1c721406b8a Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Thu, 3 Sep 2015 10:54:20 +0100 Subject: Avoid nested GMainLoops by reading from the file on startup Restore some tests for this functionality. --- tests/test-timezone-timedated.cpp | 100 ++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 16 deletions(-) (limited to 'tests/test-timezone-timedated.cpp') 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 -#include // fopen() -//#include // chmod() -#include // 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()); +} -- cgit v1.2.3 From 046cd3a7dd67935eac5f8377996423c434f44419 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Wed, 9 Sep 2015 18:16:41 +0200 Subject: glib-fixture: fail tests on unexpected warnings This requires specifying which warnings are expected to be thrown (only test-timezone-file needed this for now). However, only fail on warnings in the Indicator-Datetime log domain so that we don't fail on gstreamer (or other library) warnings for now. --- tests/test-timezone-timedated.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tests/test-timezone-timedated.cpp') diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp index 5cfc311..7300649 100644 --- a/tests/test-timezone-timedated.cpp +++ b/tests/test-timezone-timedated.cpp @@ -69,8 +69,8 @@ TEST_F(TimezoneFixture, NoFile) remove(TIMEZONE_FILE); ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS)); + expectLogMessage(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "*No such file or directory*"); TimedatedTimezone tz(TIMEZONE_FILE); - testLogCount(G_LOG_LEVEL_WARNING, 1); } /** @@ -82,6 +82,7 @@ TEST_F(TimezoneFixture, DefaultValueNoFile) remove(TIMEZONE_FILE); ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS)); + expectLogMessage(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "*No such file or directory*"); TimedatedTimezone tz(TIMEZONE_FILE); ASSERT_EQ(expected_timezone, tz.timezone.get()); } -- cgit v1.2.3