From 5b5546a50c9a323942ca5abae81563ae2c709042 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 13 Dec 2013 09:48:35 -0600 Subject: in tests/, add rules to make the Google Test library --- tests/CMakeLists.txt | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 682896b..e6b3e2d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,12 @@ +# build libgtest +add_library (gtest STATIC + ${GTEST_SOURCE_DIR}/gtest-all.cc + ${GTEST_SOURCE_DIR}/gtest_main.cc) +set_target_properties (gtest PROPERTIES INCLUDE_DIRECTORIES ${INCLUDE_DIRECTORIES} ${GTEST_INCLUDE_DIR}) +set_target_properties (gtest PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS} -w) + +SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -g ${CC_WARNING_ARGS}") + # build the necessary schemas set_directory_properties (PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES gschemas.compiled) -- cgit v1.2.3 From 40f91368f6ac0bb3ebf86e349be0779e6f2746b8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 13 Dec 2013 09:49:40 -0600 Subject: in tests/, fix typo in schema generator rules --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e6b3e2d..0a3284b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -21,7 +21,7 @@ execute_process (COMMAND ${PKG_CONFIG_EXECUTABLE} gio-2.0 --variable glib_compil OUTPUT_VARIABLE COMPILE_SCHEMA_EXECUTABLE OUTPUT_STRIP_TRAILING_WHITESPACE) add_custom_command (OUTPUT gschemas.compiled - DEPENDS ${CMAKE_SOURCE_DIR}/data/com.canonical.indicator.session.gschema.xml + DEPENDS ${CMAKE_SOURCE_DIR}/data/com.canonical.indicator.datetime.gschema.xml COMMAND cp -f ${CMAKE_SOURCE_DIR}/data/*gschema.xml ${SCHEMA_DIR} COMMAND ${COMPILE_SCHEMA_EXECUTABLE} ${SCHEMA_DIR}) -- cgit v1.2.3 From 75b0a871e757b1e71539bd0bd8d52065bd2a85ec Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 13 Dec 2013 09:50:27 -0600 Subject: in tests/, fix the include dirs to look at the source and binary dirs so we can pick up autogenerated headers --- tests/CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0a3284b..60e0507 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -25,8 +25,10 @@ add_custom_command (OUTPUT gschemas.compiled COMMAND cp -f ${CMAKE_SOURCE_DIR}/data/*gschema.xml ${SCHEMA_DIR} COMMAND ${COMPILE_SCHEMA_EXECUTABLE} ${SCHEMA_DIR}) -# look for hearder in our src dir, and also in the directories where we autogenerate files... +# look for headers in our src dir, and also in the directories where we autogenerate files... include_directories (${CMAKE_SOURCE_DIR}/src) -include_directories (${CMAKE_CURRENT_BINARY_DIR} ${SERVICE_INCLUDE_DIRS}) +include_directories (${CMAKE_CURRENT_BINARY_DIR}) +include_directories (${SERVICE_DEPS_INCLUDE_DIRS}) +include_directories (${DBUSTEST_INCLUDE_DIRS}) -- cgit v1.2.3 From 8c89aa772071eaa5f4c2b8bc37cb346d80a0ad6e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 13 Dec 2013 09:51:20 -0600 Subject: Add tests to confirm the phone profile header's time format is sensitive to 12/24hr locales (bug #1256061) --- tests/CMakeLists.txt | 8 ++++ tests/glib-fixture.h | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test-formatter.cc | 108 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 235 insertions(+) create mode 100644 tests/glib-fixture.h create mode 100644 tests/test-formatter.cc (limited to 'tests') 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 + * + * 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 + +#include +#include +#include + +#include + +class GlibFixture : public ::testing::Test +{ + private: + + GLogFunc realLogHandler; + + protected: + + std::map 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(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 + * + * 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 +#include + +#include +#include + +#include + +#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); + } +} -- cgit v1.2.3 From 54cf4f28f2284ee2700e6806b2ab1cbba355dfba Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 13 Dec 2013 10:01:39 -0600 Subject: in tests/, remove a couple of rules that got pulled in from the dev branch but aren't necessary here --- tests/CMakeLists.txt | 3 --- 1 file changed, 3 deletions(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a10bcd0..23f96b9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,9 +29,6 @@ add_custom_command (OUTPUT gschemas.compiled include_directories (${CMAKE_SOURCE_DIR}/src) 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) -- cgit v1.2.3 From 77608001413236f5decbfabea3392137127bd1c1 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 13 Dec 2013 10:36:46 -0600 Subject: in test-formatter, the 12h/24h unit test doesn't need our gschema, so remove it from this MP --- tests/CMakeLists.txt | 2 +- tests/test-formatter.cc | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 23f96b9..6564a25 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -32,7 +32,7 @@ include_directories (${SERVICE_DEPS_INCLUDE_DIRS}) # test-formatter set (TEST_NAME test-formatter) -add_executable (${TEST_NAME} test-formatter.cc gschemas.compiled) +add_executable (${TEST_NAME} test-formatter.cc) 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/test-formatter.cc b/tests/test-formatter.cc index 488d9e1..6a408ab 100644 --- a/tests/test-formatter.cc +++ b/tests/test-formatter.cc @@ -18,16 +18,12 @@ * with this program. If not, see . */ -#include -#include - #include #include #include #include "utils.h" -#include "settings-shared.h" #include "glib-fixture.h" @@ -44,21 +40,15 @@ class FormatterFixture: public GlibFixture 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); -- cgit v1.2.3 From bb0c9077bbdf00adf7b737a84730959250d91200 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 13 Dec 2013 10:44:37 -0600 Subject: let's see what g_warning jenkins found. --- tests/glib-fixture.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/glib-fixture.h b/tests/glib-fixture.h index 4128033..c6ecc68 100644 --- a/tests/glib-fixture.h +++ b/tests/glib-fixture.h @@ -44,11 +44,12 @@ class GlibFixture : public ::testing::Test private: - static void default_log_handler (const gchar * log_domain G_GNUC_UNUSED, + static void default_log_handler (const gchar * log_domain, GLogLevelFlags log_level, - const gchar * message G_GNUC_UNUSED, + const gchar * message, gpointer self) { + g_print ("%s - %d - %s", log_domain, (int)log_level, message); static_cast(self)->logCounts[log_level]++; } -- cgit v1.2.3