From b1be45c3b8cef5fede101985a26363319f3a9645 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 4 Mar 2016 12:40:23 -0600 Subject: update glib/dbus test fixtures from indicator-datetime/15.10 branch --- tests/glib-fixture.h | 89 +++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 63 deletions(-) (limited to 'tests/glib-fixture.h') 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 * * 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 . - * - * Authors: - * Charles Kerr */ -#ifndef INDICATOR_TESTS_GLIB_FIXTURE_H -#define INDICATOR_TESTS_GLIB_FIXTURE_H +#pragma once #include @@ -32,75 +31,44 @@ class GlibFixture : public ::testing::Test { - private: - - GLogFunc realLogHandler; - - std::map expected_log; - std::map> 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(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 */ -- cgit v1.2.3 From d911528cfb367fac34a5764ad6bce339a12f56d0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 6 Mar 2016 23:00:42 -0600 Subject: add ADB server/client + tests --- tests/glib-fixture.h | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 3 deletions(-) (limited to 'tests/glib-fixture.h') diff --git a/tests/glib-fixture.h b/tests/glib-fixture.h index 41ac6e8..ccdeccd 100644 --- a/tests/glib-fixture.h +++ b/tests/glib-fixture.h @@ -19,7 +19,9 @@ #pragma once +#include // std::function #include +#include // std::shared_ptr #include #include @@ -74,7 +76,7 @@ class GlibFixture : public ::testing::Test static gboolean wait_for_signal__timeout(gpointer name) { - g_error("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name); + g_error("%s: timed out waiting for signal '%s'", G_STRLOC, static_cast(name)); return G_SOURCE_REMOVE; } @@ -88,7 +90,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 int timeout_seconds=5) + void wait_for_signal(gpointer o, const gchar * signal, guint timeout_seconds=5) { // wait for the signal or for timeout, whichever comes first const auto handler_id = g_signal_connect_swapped(o, signal, @@ -103,13 +105,99 @@ class GlibFixture : public ::testing::Test } /* convenience func to loop for N msec */ - void wait_msec(int msec=50) + void wait_msec(guint msec=50) { const auto id = g_timeout_add(msec, wait_msec__timeout, loop); g_main_loop_run(loop); g_source_remove(id); } + bool wait_for(std::function test_function, guint timeout_msec=1000) + { + auto timer = std::shared_ptr(g_timer_new(), [](GTimer* t){g_timer_destroy(t);}); + const auto timeout_sec = timeout_msec / 1000.0; + for (;;) { + if (test_function()) + return true; + //g_message("%f ... %f", g_timer_elapsed(timer.get(), nullptr), timeout_sec); + if (g_timer_elapsed(timer.get(), nullptr) >= timeout_sec) + return false; + wait_msec(); + } + } + + bool wait_for_name_owned(GDBusConnection* connection, + const gchar* name, + guint timeout_msec=1000, + GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) + { + struct Data { + GMainLoop* loop = nullptr; + bool owned = false; + }; + Data data; + + auto on_name_appeared = [](GDBusConnection* /*connection*/, + const gchar* /*name_*/, + const gchar* name_owner, + gpointer gdata) + { + if (name_owner == nullptr) + return; + auto tmp = static_cast(gdata); + tmp->owned = true; + g_main_loop_quit(tmp->loop); + }; + + const auto timeout_id = g_timeout_add(timeout_msec, wait_msec__timeout, loop); + data.loop = loop; + const auto watch_id = g_bus_watch_name_on_connection(connection, + name, + flags, + on_name_appeared, + nullptr, /* name_vanished */ + &data, + nullptr); /* user_data_free_func */ + g_main_loop_run(loop); + + g_bus_unwatch_name(watch_id); + g_source_remove(timeout_id); + + return data.owned; + } + + void EXPECT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection, + const gchar* name, + guint timeout_msec=1000, + GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) + { + EXPECT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name; + } + + void EXPECT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection, + const gchar* name, + guint timeout_msec=1000, + GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) + { + EXPECT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name; + } + + void ASSERT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection, + const gchar* name, + guint timeout_msec=1000, + GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) + { + ASSERT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name; + } + + void ASSERT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection, + const gchar* name, + guint timeout_msec=1000, + GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) + { + ASSERT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name; + } + GMainLoop * loop; }; -- cgit v1.2.3 From f8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 10 Mar 2016 12:13:20 -0600 Subject: cmake and test directory cleanup --- tests/glib-fixture.h | 203 --------------------------------------------------- 1 file changed, 203 deletions(-) delete mode 100644 tests/glib-fixture.h (limited to 'tests/glib-fixture.h') diff --git a/tests/glib-fixture.h b/tests/glib-fixture.h deleted file mode 100644 index ccdeccd..0000000 --- a/tests/glib-fixture.h +++ /dev/null @@ -1,203 +0,0 @@ -/* - * 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 . - */ - -#pragma once - -#include // std::function -#include -#include // std::shared_ptr - -#include -#include -#include - -#include - -#include // setlocale() - -class GlibFixture : public ::testing::Test -{ - public: - - virtual ~GlibFixture() =default; - - protected: - - virtual void SetUp() override - { - setlocale(LC_ALL, "C.UTF-8"); - - loop = g_main_loop_new(nullptr, false); - -#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() override - { - 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 - wait_for_signal__timeout(gpointer name) - { - g_error("%s: timed out waiting for signal '%s'", G_STRLOC, static_cast(name)); - return G_SOURCE_REMOVE; - } - - static gboolean - wait_msec__timeout(gpointer loop) - { - g_main_loop_quit(static_cast(loop)); - return G_SOURCE_CONTINUE; - } - - protected: - - /* convenience func to loop while waiting for a GObject's signal */ - void wait_for_signal(gpointer o, const gchar * signal, guint timeout_seconds=5) - { - // wait for the signal or for timeout, whichever comes first - const auto handler_id = g_signal_connect_swapped(o, signal, - G_CALLBACK(g_main_loop_quit), - loop); - const auto 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(guint msec=50) - { - const auto id = g_timeout_add(msec, wait_msec__timeout, loop); - g_main_loop_run(loop); - g_source_remove(id); - } - - bool wait_for(std::function test_function, guint timeout_msec=1000) - { - auto timer = std::shared_ptr(g_timer_new(), [](GTimer* t){g_timer_destroy(t);}); - const auto timeout_sec = timeout_msec / 1000.0; - for (;;) { - if (test_function()) - return true; - //g_message("%f ... %f", g_timer_elapsed(timer.get(), nullptr), timeout_sec); - if (g_timer_elapsed(timer.get(), nullptr) >= timeout_sec) - return false; - wait_msec(); - } - } - - bool wait_for_name_owned(GDBusConnection* connection, - const gchar* name, - guint timeout_msec=1000, - GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) - { - struct Data { - GMainLoop* loop = nullptr; - bool owned = false; - }; - Data data; - - auto on_name_appeared = [](GDBusConnection* /*connection*/, - const gchar* /*name_*/, - const gchar* name_owner, - gpointer gdata) - { - if (name_owner == nullptr) - return; - auto tmp = static_cast(gdata); - tmp->owned = true; - g_main_loop_quit(tmp->loop); - }; - - const auto timeout_id = g_timeout_add(timeout_msec, wait_msec__timeout, loop); - data.loop = loop; - const auto watch_id = g_bus_watch_name_on_connection(connection, - name, - flags, - on_name_appeared, - nullptr, /* name_vanished */ - &data, - nullptr); /* user_data_free_func */ - g_main_loop_run(loop); - - g_bus_unwatch_name(watch_id); - g_source_remove(timeout_id); - - return data.owned; - } - - void EXPECT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection, - const gchar* name, - guint timeout_msec=1000, - GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) - { - EXPECT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name; - } - - void EXPECT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection, - const gchar* name, - guint timeout_msec=1000, - GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) - { - EXPECT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name; - } - - void ASSERT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection, - const gchar* name, - guint timeout_msec=1000, - GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) - { - ASSERT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name; - } - - void ASSERT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection, - const gchar* name, - guint timeout_msec=1000, - GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START) - { - ASSERT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name; - } - - GMainLoop * loop; -}; - -- cgit v1.2.3