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/CMakeLists.txt | 2 +- tests/glib-fixture.h | 89 +++++++++++------------------------ tests/gtestdbus-fixture.h | 108 ------------------------------------------- tests/test-dbus-fixture.h | 106 ++++++++++++++++++++++++++++++++++++++++++ tests/test-rotation-lock.cpp | 6 +-- 5 files changed, 136 insertions(+), 175 deletions(-) delete mode 100644 tests/gtestdbus-fixture.h create mode 100644 tests/test-dbus-fixture.h (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 054a676..6c8be9b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,5 +31,5 @@ function(add_test_by_name name) endfunction() add_test_by_name(test-rotation-lock) -add_test (cppcheck cppcheck --enable=all -q --error-exitcode=2 --inline-suppr -I${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/tests) +add_test (cppcheck cppcheck --enable=all -USCHEMA_DIR --error-exitcode=2 --inline-suppr -I${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/tests) 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 */ diff --git a/tests/gtestdbus-fixture.h b/tests/gtestdbus-fixture.h deleted file mode 100644 index c592033..0000000 --- a/tests/gtestdbus-fixture.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2013 Canonical Ltd. - * - * 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 . - * - * Authors: - * Charles Kerr - */ - -#ifndef INDICATOR_TESTS_GTESTDBUS_FIXTURE_H -#define INDICATOR_TESTS_GTESTDBUS_FIXTURE_H - -#include "glib-fixture.h" - -/*** -**** -***/ - -class GTestDBusFixture: public GlibFixture -{ - public: - - GTestDBusFixture() =default; - virtual ~GTestDBusFixture() =default; - - explicit GTestDBusFixture(const std::vector& service_dirs_in): service_dirs(service_dirs_in) {} - - private: - - typedef GlibFixture super; - - static void - on_bus_opened (GObject* /*object*/, GAsyncResult * res, gpointer gself) - { - auto self = static_cast(gself); - - GError * err = 0; - self->bus = g_bus_get_finish (res, &err); - g_assert_no_error (err); - - g_main_loop_quit (self->loop); - } - - static void - on_bus_closed (GObject* /*object*/, GAsyncResult * res, gpointer gself) - { - auto self = static_cast(gself); - - GError * err = 0; - g_dbus_connection_close_finish (self->bus, res, &err); - g_assert_no_error (err); - - g_main_loop_quit (self->loop); - } - - protected: - - GTestDBus * test_dbus = nullptr; - GDBusConnection * bus = nullptr; - const std::vector service_dirs; - - virtual void SetUp () - { - super::SetUp (); - - // pull up a test dbus - test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE); - for (const auto& dir : service_dirs) - g_test_dbus_add_service_dir (test_dbus, dir.c_str()); - g_test_dbus_up (test_dbus); - const char * address = g_test_dbus_get_bus_address (test_dbus); - g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", address, true); - g_setenv ("DBUS_SESSION_BUS_ADDRESS", address, true); - g_debug ("test_dbus's address is %s", address); - - // wait for the GDBusConnection before returning - g_bus_get (G_BUS_TYPE_SYSTEM, nullptr, on_bus_opened, this); - g_main_loop_run (loop); - } - - virtual void TearDown () - { - wait_msec(); - - // close the system bus - g_dbus_connection_close(bus, nullptr, on_bus_closed, this); - g_main_loop_run(loop); - g_clear_object(&bus); - - // tear down the test dbus - g_test_dbus_down(test_dbus); - g_clear_object(&test_dbus); - - super::TearDown(); - } -}; - -#endif /* INDICATOR_TESTS_GTESTDBUS_FIXTURE_H */ diff --git a/tests/test-dbus-fixture.h b/tests/test-dbus-fixture.h new file mode 100644 index 0000000..3947e58 --- /dev/null +++ b/tests/test-dbus-fixture.h @@ -0,0 +1,106 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * 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 . + * + * Authors: + * Charles Kerr + */ + +#pragma once + +#include "glib-fixture.h" + +/*** +**** +***/ + +class TestDBusFixture: public GlibFixture +{ + public: + + TestDBusFixture() =default; + virtual ~TestDBusFixture() =default; + + explicit TestDBusFixture(const std::vector& service_dirs_in): service_dirs(service_dirs_in) {} + + private: + + typedef GlibFixture super; + + static void + on_bus_opened (GObject* /*object*/, GAsyncResult * res, gpointer gself) + { + auto self = static_cast(gself); + + GError * err = 0; + self->system_bus = g_bus_get_finish (res, &err); + g_assert_no_error (err); + + g_main_loop_quit (self->loop); + } + + static void + on_bus_closed (GObject* /*object*/, GAsyncResult * res, gpointer gself) + { + auto self = static_cast(gself); + + GError * err = 0; + g_dbus_connection_close_finish (self->system_bus, res, &err); + g_assert_no_error (err); + + g_main_loop_quit (self->loop); + } + + protected: + + GTestDBus * test_dbus = nullptr; + GDBusConnection * system_bus = nullptr; + const std::vector service_dirs; + + virtual void SetUp() override + { + super::SetUp (); + + // pull up a test dbus + test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE); + for (const auto& dir : service_dirs) + g_test_dbus_add_service_dir (test_dbus, dir.c_str()); + g_test_dbus_up (test_dbus); + const char * address = g_test_dbus_get_bus_address (test_dbus); + g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", address, true); + g_setenv ("DBUS_SESSION_BUS_ADDRESS", address, true); + g_debug ("test_dbus's address is %s", address); + + // wait for the GDBusConnection before returning + g_bus_get (G_BUS_TYPE_SYSTEM, nullptr, on_bus_opened, this); + g_main_loop_run (loop); + } + + virtual void TearDown() override + { + wait_msec(); + + // close the system bus + g_dbus_connection_close(system_bus, nullptr, on_bus_closed, this); + g_main_loop_run(loop); + g_clear_object(&system_bus); + + // tear down the test dbus + g_test_dbus_down(test_dbus); + g_clear_object(&test_dbus); + + super::TearDown(); + } +}; + diff --git a/tests/test-rotation-lock.cpp b/tests/test-rotation-lock.cpp index 946b1dd..b8661cc 100644 --- a/tests/test-rotation-lock.cpp +++ b/tests/test-rotation-lock.cpp @@ -17,14 +17,14 @@ * Charles Kerr */ -#include "gtestdbus-fixture.h" +#include "test-dbus-fixture.h" #include -class RotationLockFixture: public GTestDBusFixture +class RotationLockFixture: public TestDBusFixture { private: - typedef GTestDBusFixture super; + typedef TestDBusFixture super; protected: -- cgit v1.2.3