aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/CMakeLists.txt8
-rw-r--r--tests/glib-fixture.h119
-rw-r--r--tests/test-formatter.cc108
3 files changed, 235 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 60e0507..a10bcd0 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -31,4 +31,12 @@ include_directories (${CMAKE_CURRENT_BINARY_DIR})
include_directories (${SERVICE_DEPS_INCLUDE_DIRS})
include_directories (${DBUSTEST_INCLUDE_DIRS})
+add_definitions (-DSANDBOX="${CMAKE_CURRENT_BINARY_DIR}")
+
+# test-formatter
+set (TEST_NAME test-formatter)
+add_executable (${TEST_NAME} test-formatter.cc gschemas.compiled)
+add_test (${TEST_NAME} ${TEST_NAME})
+add_dependencies (${TEST_NAME} libindicatordatetimeservice)
+target_link_libraries (${TEST_NAME} indicatordatetimeservice gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS})
diff --git a/tests/glib-fixture.h b/tests/glib-fixture.h
new file mode 100644
index 0000000..4128033
--- /dev/null
+++ b/tests/glib-fixture.h
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2013 Canonical Ltd.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
+ *
+ * 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/>.
+ */
+
+#include <map>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <gio/gio.h>
+
+#include <gtest/gtest.h>
+
+class GlibFixture : public ::testing::Test
+{
+ private:
+
+ GLogFunc realLogHandler;
+
+ protected:
+
+ std::map<GLogLevelFlags,int> logCounts;
+
+ void testLogCount (GLogLevelFlags log_level, int expected G_GNUC_UNUSED)
+ {
+ ASSERT_EQ (expected, logCounts[log_level]);
+
+ logCounts.erase (log_level);
+ }
+
+ private:
+
+ static void default_log_handler (const gchar * log_domain G_GNUC_UNUSED,
+ GLogLevelFlags log_level,
+ const gchar * message G_GNUC_UNUSED,
+ gpointer self)
+ {
+ static_cast<GlibFixture*>(self)->logCounts[log_level]++;
+ }
+
+ protected:
+
+ virtual void SetUp ()
+ {
+ loop = g_main_loop_new (NULL, FALSE);
+
+ g_log_set_default_handler (default_log_handler, this);
+
+ // only use local, temporary settings
+ g_setenv ("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE);
+ g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
+ g_debug ("SCHEMA_DIR is %s", SCHEMA_DIR);
+ }
+
+ virtual void TearDown()
+ {
+ // confirm there aren't any unexpected log messages
+ ASSERT_EQ (0, logCounts[G_LOG_LEVEL_ERROR]);
+ ASSERT_EQ (0, logCounts[G_LOG_LEVEL_CRITICAL]);
+ ASSERT_EQ (0, logCounts[G_LOG_LEVEL_WARNING]);
+ ASSERT_EQ (0, logCounts[G_LOG_LEVEL_MESSAGE]);
+ ASSERT_EQ (0, logCounts[G_LOG_LEVEL_INFO]);
+
+ // revert to glib's log handler
+ g_log_set_default_handler (realLogHandler, this);
+
+ g_clear_pointer (&loop, g_main_loop_unref);
+ }
+
+ private:
+
+ static gboolean
+ wait_for_signal__timeout (gpointer name)
+ {
+ g_error ("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name);
+ return G_SOURCE_REMOVE;
+ }
+
+ protected:
+
+ /* convenience func to loop while waiting for a GObject's signal */
+ void wait_for_signal (gpointer o, const gchar * signal, const int timeout_seconds=5)
+ {
+ // wait for the signal or for timeout, whichever comes first
+ guint handler_id = g_signal_connect_swapped (o, signal,
+ G_CALLBACK(g_main_loop_quit),
+ loop);
+ gulong timeout_id = g_timeout_add_seconds (timeout_seconds,
+ wait_for_signal__timeout,
+ loop);
+ g_main_loop_run (loop);
+ g_source_remove (timeout_id);
+ g_signal_handler_disconnect (o, handler_id);
+ }
+
+ /* convenience func to loop for N msec */
+ void wait_msec (int msec=50)
+ {
+ guint id = g_timeout_add (msec, (GSourceFunc)g_main_loop_quit, loop);
+ g_main_loop_run (loop);
+ g_source_remove (id);
+ }
+
+ GMainLoop * loop;
+};
diff --git a/tests/test-formatter.cc b/tests/test-formatter.cc
new file mode 100644
index 0000000..488d9e1
--- /dev/null
+++ b/tests/test-formatter.cc
@@ -0,0 +1,108 @@
+
+/*
+ * Copyright 2013 Canonical Ltd.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
+ *
+ * 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/>.
+ */
+
+#include <string>
+#include <vector>
+
+#include <langinfo.h>
+#include <locale.h>
+
+#include <glib/gi18n.h>
+
+#include "utils.h"
+#include "settings-shared.h"
+
+#include "glib-fixture.h"
+
+/***
+****
+***/
+
+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)
+{
+ // test the default value in a 24h locale
+ if (Set24hLocale ())
+ {
+ const gchar * format = get_terse_header_time_format_string ();
+ ASSERT_NE (nullptr, format);
+ ASSERT_STREQ ("%H:%M", format);
+ }
+
+ // test the default value in a 12h locale
+ if (Set12hLocale ())
+ {
+ const gchar * format = get_terse_header_time_format_string ();
+ ASSERT_NE (nullptr, format);
+ ASSERT_STREQ ("%l:%M %p", format);
+ }
+}