From 172246c997d7b20d7e98fc25a7b23f4a79a0f6a6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 17 Dec 2013 22:02:06 -0600 Subject: add formatter + tests --- tests/test-formatter.cc | 270 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 tests/test-formatter.cc (limited to 'tests') diff --git a/tests/test-formatter.cc b/tests/test-formatter.cc new file mode 100644 index 0000000..641338b --- /dev/null +++ b/tests/test-formatter.cc @@ -0,0 +1,270 @@ + +/* + * 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 "clock-mock.h" +#include "glib-fixture.h" + +#include +#include + +#include + +#include +#include + +using unity::indicator::datetime::Clock; +using unity::indicator::datetime::MockClock; +using unity::indicator::datetime::PhoneFormatter; +using unity::indicator::datetime::DesktopFormatter; + +/*** +**** +***/ + +class FormatterFixture: public GlibFixture +{ + private: + + typedef GlibFixture super; + gchar * original_locale = nullptr; + + protected: + + GSettings * settings = nullptr; + + virtual void SetUp () + { + super::SetUp (); + + settings = g_settings_new (SETTINGS_INTERFACE); + + original_locale = g_strdup (setlocale (LC_TIME, NULL)); + } + + virtual void TearDown () + { + g_clear_object (&settings); + + setlocale (LC_TIME, original_locale); + g_clear_pointer (&original_locale, g_free); + + super::TearDown (); + } + + bool SetLocale (const char * expected_locale, const char * name) + { + setlocale (LC_TIME, expected_locale); + const char * actual_locale = setlocale (LC_TIME, NULL); + if (!g_strcmp0 (expected_locale, actual_locale)) + { + return true; + } + else + { + g_warning ("Unable to set locale to %s; skipping %s locale tests.", expected_locale, name); + return false; + } + } + + inline bool Set24hLocale () { return SetLocale ("C", "24h"); } + inline bool Set12hLocale () { return SetLocale ("en_US.utf8", "12h"); } +}; + + +/** + * Test the phone header format + */ +TEST_F (FormatterFixture, TestPhoneHeader) +{ + GDateTime * now = g_date_time_new_local (2020, 10, 31, 18, 30, 59); + std::shared_ptr mock (new MockClock(now)); + std::shared_ptr clock = std::dynamic_pointer_cast(mock); + + // test the default value in a 24h locale + if (Set24hLocale ()) + { + PhoneFormatter formatter (clock); + EXPECT_EQ (std::string("%H:%M"), formatter.headerFormat.get()); + EXPECT_EQ (std::string("18:30"), formatter.header.get()); + } + + // test the default value in a 12h locale + if (Set12hLocale ()) + { + PhoneFormatter formatter (clock); + EXPECT_EQ (std::string("%l:%M %p"), formatter.headerFormat.get()); + EXPECT_EQ (std::string(" 6:30 PM"), formatter.header.get()); + } +} + +#define EM_SPACE "\u2003" + +/** + * Test the default values of the desktop header format + */ +TEST_F (FormatterFixture, TestDesktopHeader) +{ + struct { + bool is_12h; + bool show_day; + bool show_date; + bool show_year; + const char * expected_format_string; + } test_cases[] = { + { false, false, false, false, "%H:%M" }, + { false, false, false, true, "%H:%M" }, // show_year is ignored iff show_date is false + { false, false, true, false, "%b %e" EM_SPACE "%H:%M" }, + { false, false, true, true, "%b %e %Y" EM_SPACE "%H:%M" }, + { false, true, false, false, "%a" EM_SPACE "%H:%M" }, + { false, true, false, true, "%a" EM_SPACE "%H:%M" }, // show_year is ignored iff show_date is false + { false, true, true, false, "%a %b %e" EM_SPACE "%H:%M" }, + { false, true, true, true, "%a %b %e %Y" EM_SPACE "%H:%M" }, + { true, false, false, false, "%l:%M %p" }, + { true, false, false, true, "%l:%M %p" }, // show_year is ignored iff show_date is false + { true, false, true, false, "%b %e" EM_SPACE "%l:%M %p" }, + { true, false, true, true, "%b %e %Y" EM_SPACE "%l:%M %p" }, + { true, true, false, false, "%a" EM_SPACE "%l:%M %p" }, + { true, true, false, true, "%a" EM_SPACE "%l:%M %p" }, // show_year is ignored iff show_date is false + { true, true, true, false, "%a %b %e" EM_SPACE "%l:%M %p" }, + { true, true, true, true, "%a %b %e %Y" EM_SPACE "%l:%M %p" } + }; + + GDateTime * now = g_date_time_new_local (2020, 10, 31, 18, 30, 59); + std::shared_ptr mock (new MockClock(now)); + std::shared_ptr clock = std::dynamic_pointer_cast(mock); + + for (int i=0, n=G_N_ELEMENTS(test_cases); i mock (new MockClock(test_cases[i].now)); + std::shared_ptr clock = std::dynamic_pointer_cast(mock); + DesktopFormatter f (clock); + + std::string fmt = f.getRelativeFormat (test_cases[i].then, test_cases[i].then_end); + ASSERT_STREQ (test_cases[i].expected_format_string, fmt.c_str()); + + g_clear_pointer (&test_cases[i].now, g_date_time_unref); + g_clear_pointer (&test_cases[i].then, g_date_time_unref); + g_clear_pointer (&test_cases[i].then_end, g_date_time_unref); + } + } + + g_date_time_unref (a); +} + + +/** + * Test the default values of the desktop header format + */ +TEST_F (FormatterFixture, TestEventTimes) +{ + GDateTime * day = g_date_time_new_local (2013, 1, 1, 13, 0, 0); + GDateTime * day_begin = g_date_time_new_local (2013, 1, 1, 13, 0, 0); + GDateTime * day_end = g_date_time_add_days (day_begin, 1); + GDateTime * tomorrow_begin = g_date_time_add_days (day_begin, 1); + GDateTime * tomorrow_end = g_date_time_add_days (tomorrow_begin, 1); + + struct { + bool is_12h; + GDateTime * now; + GDateTime * then; + GDateTime * then_end; + const char * expected_format_string; + } test_cases[] = { + { false, g_date_time_ref(day), g_date_time_ref(day_begin), g_date_time_ref(day_end), _("Today") }, + { true, g_date_time_ref(day), g_date_time_ref(day_begin), g_date_time_ref(day_end), _("Today") }, + { false, g_date_time_ref(day), g_date_time_ref(tomorrow_begin), g_date_time_ref(tomorrow_end), _("Tomorrow") }, + { true, g_date_time_ref(day), g_date_time_ref(tomorrow_begin), g_date_time_ref(tomorrow_end), _("Tomorrow") } + }; + + for (int i=0, n=G_N_ELEMENTS(test_cases); i mock (new MockClock(test_cases[i].now)); + std::shared_ptr clock = std::dynamic_pointer_cast(mock); + DesktopFormatter f (clock); + + std::string fmt = f.getRelativeFormat (test_cases[i].then, test_cases[i].then_end); + ASSERT_STREQ (test_cases[i].expected_format_string, fmt.c_str()); + + g_clear_pointer (&test_cases[i].now, g_date_time_unref); + g_clear_pointer (&test_cases[i].then, g_date_time_unref); + g_clear_pointer (&test_cases[i].then_end, g_date_time_unref); + } + } + + g_date_time_unref (tomorrow_end); + g_date_time_unref (tomorrow_begin); + g_date_time_unref (day_end); + g_date_time_unref (day_begin); + g_date_time_unref (day); +} + + -- cgit v1.2.3