diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2016-03-04 12:40:23 -0600 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2016-03-04 12:40:23 -0600 |
commit | b1be45c3b8cef5fede101985a26363319f3a9645 (patch) | |
tree | ba5d2d564414c3d0ad1f076a8839e9e701d5436b /tests/glib-fixture.h | |
parent | 9cf0f21373db99aaece2795bb7d947ba18b96957 (diff) | |
download | ayatana-indicator-display-b1be45c3b8cef5fede101985a26363319f3a9645.tar.gz ayatana-indicator-display-b1be45c3b8cef5fede101985a26363319f3a9645.tar.bz2 ayatana-indicator-display-b1be45c3b8cef5fede101985a26363319f3a9645.zip |
update glib/dbus test fixtures from indicator-datetime/15.10 branch
Diffstat (limited to 'tests/glib-fixture.h')
-rw-r--r-- | tests/glib-fixture.h | 89 |
1 files changed, 26 insertions, 63 deletions
diff --git a/tests/glib-fixture.h b/tests/glib-fixture.h index 65d2921..41ac6e8 100644 --- a/tests/glib-fixture.h +++ b/tests/glib-fixture.h @@ -1,5 +1,8 @@ /* - * Copyright 2014 Canonical Ltd. + * 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 @@ -12,13 +15,9 @@ * * You should have received a copy of the GNU General Public License along * with this program. If not, see <http://www.gnu.org/licenses/>. - * - * Authors: - * Charles Kerr <charles.kerr@canonical.com> */ -#ifndef INDICATOR_TESTS_GLIB_FIXTURE_H -#define INDICATOR_TESTS_GLIB_FIXTURE_H +#pragma once #include <map> @@ -32,75 +31,44 @@ class GlibFixture : public ::testing::Test { - private: - - GLogFunc realLogHandler; - - std::map<GLogLevelFlags,size_t> expected_log; - std::map<GLogLevelFlags,std::vector<std::string>> log; - - void test_log_counts() - { - const GLogLevelFlags levels_to_test[] = { G_LOG_LEVEL_ERROR, - G_LOG_LEVEL_CRITICAL, - G_LOG_LEVEL_MESSAGE, - G_LOG_LEVEL_WARNING }; - - for(const auto& level : levels_to_test) - { - const auto& v = log[level]; - const auto n = v.size(); - - EXPECT_EQ(expected_log[level], n); - - if (expected_log[level] != n) - for (size_t i=0; i<n; ++i) - g_print("%d %s\n", (n+1), v[i].c_str()); - } - - expected_log.clear(); - log.clear(); - } + public: - static void default_log_handler(const gchar * log_domain, - GLogLevelFlags log_level, - const gchar * message, - gpointer self) - { - char* tmp = g_strdup_printf ("%s:%d \"%s\"", log_domain, (int)log_level, message); - static_cast<GlibFixture*>(self)->log[log_level].push_back(tmp); - g_free(tmp); - } + virtual ~GlibFixture() =default; protected: - void increment_expected_errors(GLogLevelFlags level, size_t n=1) - { - expected_log[level] += n; - } - - virtual void SetUp() + virtual void SetUp() override { setlocale(LC_ALL, "C.UTF-8"); loop = g_main_loop_new(nullptr, false); - g_log_set_default_handler(default_log_handler, this); - +#ifdef SCHEMA_DIR + // only use local, temporary settings + g_assert(g_setenv("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, true)); g_assert(g_setenv("GSETTINGS_BACKEND", "memory", true)); + g_debug("SCHEMA_DIR is %s", SCHEMA_DIR); +#endif + + // fail on unexpected messages from this domain + g_log_set_fatal_mask(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING); g_unsetenv("DISPLAY"); + } - virtual void TearDown() + virtual void TearDown() override { - test_log_counts(); - - g_log_set_default_handler(realLogHandler, this); + g_test_assert_expected_messages (); g_clear_pointer(&loop, g_main_loop_unref); } + void expectLogMessage (const gchar *domain, GLogLevelFlags level, const gchar *pattern) + { + g_test_expect_message (domain, level, pattern); + } + private: static gboolean @@ -120,7 +88,7 @@ class GlibFixture : public ::testing::Test protected: /* convenience func to loop while waiting for a GObject's signal */ - void wait_for_signal(gpointer o, const gchar * signal, const guint timeout_seconds=5) + void wait_for_signal(gpointer o, const gchar * signal, const int timeout_seconds=5) { // wait for the signal or for timeout, whichever comes first const auto handler_id = g_signal_connect_swapped(o, signal, @@ -135,7 +103,7 @@ class GlibFixture : public ::testing::Test } /* convenience func to loop for N msec */ - void wait_msec(guint msec=50) + void wait_msec(int msec=50) { const auto id = g_timeout_add(msec, wait_msec__timeout, loop); g_main_loop_run(loop); @@ -143,10 +111,5 @@ class GlibFixture : public ::testing::Test } GMainLoop * loop; - - public: - - virtual ~GlibFixture() =default; }; -#endif /* INDICATOR_TESTS_GLIB_FIXTURE_H */ |