From dd41db685c518acab1c8e3676f4292f66e4d0476 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 22 Jan 2014 09:06:01 -0600 Subject: copyediting: rename the test/ directory's .cc files to .cpp for consistency with src/ --- tests/test-settings.cpp | 197 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 tests/test-settings.cpp (limited to 'tests/test-settings.cpp') diff --git a/tests/test-settings.cpp b/tests/test-settings.cpp new file mode 100644 index 0000000..9e52197 --- /dev/null +++ b/tests/test-settings.cpp @@ -0,0 +1,197 @@ +/* + * 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 "glib-fixture.h" + +#include +#include + +using namespace unity::indicator::datetime; + +/*** +**** +***/ + +class SettingsFixture: public GlibFixture +{ +private: + typedef GlibFixture super; + +protected: + + std::shared_ptr m_live; + std::shared_ptr m_settings; + GSettings * m_gsettings; + + virtual void SetUp() + { + super::SetUp(); + + m_gsettings = g_settings_new(SETTINGS_INTERFACE); + m_live.reset(new LiveSettings); + m_settings = std::dynamic_pointer_cast(m_live); + } + + virtual void TearDown() + { + g_clear_object(&m_gsettings); + m_settings.reset(); + m_live.reset(); + + super::TearDown(); + } + + void TestBoolProperty(core::Property& property, const gchar* key) + { + EXPECT_EQ(g_settings_get_boolean(m_gsettings, key), property.get()); + g_settings_set_boolean(m_gsettings, key, false); + EXPECT_EQ(false, property.get()); + g_settings_set_boolean(m_gsettings, key, true); + EXPECT_EQ(true, property.get()); + + property.set(false); + EXPECT_EQ(false, g_settings_get_boolean(m_gsettings, key)); + property.set(true); + EXPECT_EQ(true, g_settings_get_boolean(m_gsettings, key)); + } + + void TestStringProperty(core::Property& property, const gchar* key) + { + gchar* tmp; + std::string str; + + tmp = g_settings_get_string(m_gsettings, key); + EXPECT_EQ(tmp, property.get()); + g_clear_pointer(&tmp, g_free); + + str = "a"; + g_settings_set_string(m_gsettings, key, str.c_str()); + EXPECT_EQ(str, property.get()); + + str = "b"; + g_settings_set_string(m_gsettings, key, str.c_str()); + EXPECT_EQ(str, property.get()); + + str = "a"; + property.set(str); + tmp = g_settings_get_string(m_gsettings, key); + EXPECT_EQ(str, tmp); + g_clear_pointer(&tmp, g_free); + + str = "b"; + property.set(str); + tmp = g_settings_get_string(m_gsettings, key); + EXPECT_EQ(str, tmp); + g_clear_pointer(&tmp, g_free); + } +}; + +/*** +**** +***/ + +TEST_F(SettingsFixture, HelloWorld) +{ + EXPECT_TRUE(true); +} + +TEST_F(SettingsFixture, BoolProperties) +{ + TestBoolProperty(m_settings->show_seconds, SETTINGS_SHOW_SECONDS_S); + TestBoolProperty(m_settings->show_calendar, SETTINGS_SHOW_CALENDAR_S); + TestBoolProperty(m_settings->show_clock, SETTINGS_SHOW_CLOCK_S); + TestBoolProperty(m_settings->show_date, SETTINGS_SHOW_DATE_S); + TestBoolProperty(m_settings->show_day, SETTINGS_SHOW_DAY_S); + TestBoolProperty(m_settings->show_detected_location, SETTINGS_SHOW_DETECTED_S); + TestBoolProperty(m_settings->show_events, SETTINGS_SHOW_EVENTS_S); + TestBoolProperty(m_settings->show_locations, SETTINGS_SHOW_LOCATIONS_S); + TestBoolProperty(m_settings->show_week_numbers, SETTINGS_SHOW_WEEK_NUMBERS_S); + TestBoolProperty(m_settings->show_year, SETTINGS_SHOW_YEAR_S); +} + +TEST_F(SettingsFixture, StringProperties) +{ + TestStringProperty(m_settings->custom_time_format, SETTINGS_CUSTOM_TIME_FORMAT_S); + TestStringProperty(m_settings->timezone_name, SETTINGS_TIMEZONE_NAME_S); +} + +TEST_F(SettingsFixture, TimeFormatMode) +{ + const auto key = SETTINGS_TIME_FORMAT_S; + const TimeFormatMode modes[] = { TIME_FORMAT_MODE_LOCALE_DEFAULT, + TIME_FORMAT_MODE_12_HOUR, + TIME_FORMAT_MODE_24_HOUR, + TIME_FORMAT_MODE_CUSTOM }; + + for(const auto& mode : modes) + { + g_settings_set_enum(m_gsettings, key, mode); + EXPECT_EQ(mode, m_settings->time_format_mode.get()); + } + + for(const auto& mode : modes) + { + m_settings->time_format_mode.set(mode); + EXPECT_EQ(mode, g_settings_get_enum(m_gsettings, key)); + } +} + +namespace +{ + std::vector strv_to_vector(const gchar** strv) + { + std::vector v; + for(int i=0; strv && strv[i]; i++) + v.push_back(strv[i]); + return v; + } +}; + +TEST_F(SettingsFixture, Locations) +{ + const auto key = SETTINGS_LOCATIONS_S; + + const gchar* astrv[] = {"America/Los_Angeles Oakland", "America/Chicago Oklahoma City", "Europe/London London", NULL}; + const gchar* bstrv[] = {"America/Denver", "Europe/London London", "Europe/Berlin Berlin", NULL}; + const std::vector av = strv_to_vector(astrv); + const std::vector bv = strv_to_vector(bstrv); + + g_settings_set_strv(m_gsettings, key, astrv); + EXPECT_EQ(av, m_settings->locations.get()); + g_settings_set_strv(m_gsettings, key, bstrv); + EXPECT_EQ(bv, m_settings->locations.get()); + + m_settings->locations.set(av); + auto tmp = g_settings_get_strv(m_gsettings, key); + auto vtmp = strv_to_vector((const gchar**)tmp); + g_strfreev(tmp); + EXPECT_EQ(av, vtmp); + + m_settings->locations.set(bv); + tmp = g_settings_get_strv(m_gsettings, key); + vtmp = strv_to_vector((const gchar**)tmp); + g_strfreev(tmp); + EXPECT_EQ(bv, vtmp); +} + +#if 0 + core::Property> locations; + core::Property time_format_mode; + +#endif -- cgit v1.2.3 From 572377478193fab7189e817f08487a17543ee32b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 22 Jan 2014 09:16:32 -0600 Subject: silence a minor compiler warning in test-settings --- tests/test-settings.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/test-settings.cpp') diff --git a/tests/test-settings.cpp b/tests/test-settings.cpp index 9e52197..df01cd7 100644 --- a/tests/test-settings.cpp +++ b/tests/test-settings.cpp @@ -61,14 +61,14 @@ protected: { EXPECT_EQ(g_settings_get_boolean(m_gsettings, key), property.get()); g_settings_set_boolean(m_gsettings, key, false); - EXPECT_EQ(false, property.get()); + EXPECT_FALSE(property.get()); g_settings_set_boolean(m_gsettings, key, true); - EXPECT_EQ(true, property.get()); + EXPECT_TRUE(property.get()); property.set(false); - EXPECT_EQ(false, g_settings_get_boolean(m_gsettings, key)); + EXPECT_FALSE(g_settings_get_boolean(m_gsettings, key)); property.set(true); - EXPECT_EQ(true, g_settings_get_boolean(m_gsettings, key)); + EXPECT_TRUE(g_settings_get_boolean(m_gsettings, key)); } void TestStringProperty(core::Property& property, const gchar* key) -- cgit v1.2.3 From 271b0fbf8b14a4f7a8f47de0e3a8751bd50676c3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 30 Jan 2014 18:40:13 -0600 Subject: copyediting: remove dead '#if 0''ed code --- src/locations.cpp | 23 ---------------- tests/test-settings.cpp | 6 ----- tests/test-timezone-geoclue.cpp | 59 ++++------------------------------------- 3 files changed, 5 insertions(+), 83 deletions(-) (limited to 'tests/test-settings.cpp') diff --git a/src/locations.cpp b/src/locations.cpp index c59f988..0690acd 100644 --- a/src/locations.cpp +++ b/src/locations.cpp @@ -54,29 +54,6 @@ Location::Location(const std::string& zone_, const std::string& name_): g_time_zone_unref (gzone); } -#if 0 -DateTime Location::localtime(const DateTime& reference_point) const -{ -GDateTime * g_date_time_to_timezone (GDateTime *datetime, - GTimeZone *tz); - auto gzone = g_time_zone_new(zone().c_str()); - const auto gtime = reference_point.get(); - auto glocal = g_date_time_new (gzone, - g_date_time_get_year(gtime), - g_date_time_get_month(gtime), - g_date_time_get_day_of_month(gtime), - g_date_time_get_hour(gtime), - g_date_time_get_minute(gtime), - g_date_time_get_seconds(gtime)); - DateTime local(glocal); - g_date_time_unref(glocal); - g_message("reference: %zu", (size_t)reference_point.to_unix(), (size_t)local.to_unix()); - //g_date_time_unref(gtime); - g_time_zone_unref(gzone); - return local; -} -#endif - } // namespace datetime } // namespace indicator } // namespace unity diff --git a/tests/test-settings.cpp b/tests/test-settings.cpp index df01cd7..980e7fa 100644 --- a/tests/test-settings.cpp +++ b/tests/test-settings.cpp @@ -189,9 +189,3 @@ TEST_F(SettingsFixture, Locations) g_strfreev(tmp); EXPECT_EQ(bv, vtmp); } - -#if 0 - core::Property> locations; - core::Property time_format_mode; - -#endif diff --git a/tests/test-timezone-geoclue.cpp b/tests/test-timezone-geoclue.cpp index 4bf08a7..3cc1393 100644 --- a/tests/test-timezone-geoclue.cpp +++ b/tests/test-timezone-geoclue.cpp @@ -21,64 +21,17 @@ #include -//#include - using unity::indicator::datetime::GeoclueTimezone; -/*** -**** -***/ - -class TimezoneGeoclueFixture : public GeoclueFixture -{ -}; - -#if 0 -namespace +// This test looks small because the interesting +// work is all happening in GeoclueFixture... +TEST_F(GeoclueFixture, ChangeDetected) { - struct EmitAddressChangedData - { - DbusTestDbusMock * mock = nullptr; - DbusTestDbusMockObject * obj_client = nullptr; - std::string timezone; - EmitAddressChangedData(DbusTestDbusMock * mock_, - DbusTestDbusMockObject * obj_client_, - const std::string& timezone_): mock(mock_), obj_client(obj_client_), timezone(timezone_) {} - }; - - gboolean emit_address_changed_idle(gpointer gdata) - { - auto data = static_cast(gdata); - - GError * error = nullptr; - dbus_test_dbus_mock_object_emit_signal(data->mock, data->obj_client, - "org.freedesktop.Geoclue.Address", - "AddressChanged", - G_VARIANT_TYPE("(ia{ss}(idd))"), - g_variant_new_parsed("(1385238033, {'timezone': 'America/Chicago'}, (3, 0.0, 0.0))"), - &error); - if (error) - { - g_warning("%s: %s", G_STRFUNC, error->message); - g_error_free(error); - } - - delete data; - return G_SOURCE_REMOVE; - } -} -#endif - -TEST_F(TimezoneGeoclueFixture, ChangeDetected) -{ -// const std::string timezone_1 = "America/Denver"; - const std::string timezone_2 = "America/Chicago"; - GeoclueTimezone tz; wait_msec(500); // wait for the bus to get set up EXPECT_EQ(timezone_1, tz.timezone.get()); - // start listening for a timezone change, then change the timezone + // Start listening for a timezone change, then change the timezone. bool changed = false; auto connection = tz.timezone.changed().connect( @@ -88,10 +41,8 @@ TEST_F(TimezoneGeoclueFixture, ChangeDetected) g_main_loop_quit(loop); }); + const std::string timezone_2 = "America/Chicago"; setGeoclueTimezoneOnIdle(timezone_2); - //g_timeout_add(50, emit_address_changed_idle, new EmitAddressChangedData(mock, obj_client, timezone_2.c_str())); g_main_loop_run(loop); EXPECT_EQ(timezone_2, tz.timezone.get()); } - - -- cgit v1.2.3