aboutsummaryrefslogtreecommitdiff
path: root/tests/gtest-dbus-fixture.h
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2013-03-22 16:34:34 -0500
committerCharles Kerr <charles.kerr@canonical.com>2013-03-22 16:34:34 -0500
commitae39f7001e5603010afc02de29787ade6d48ef14 (patch)
tree74c303a86603134fc2b86d1c428475a60e455e3f /tests/gtest-dbus-fixture.h
parente4e327f139dd139a91893fc7f19061a37d4b47e9 (diff)
downloadayatana-indicator-session-ae39f7001e5603010afc02de29787ade6d48ef14.tar.gz
ayatana-indicator-session-ae39f7001e5603010afc02de29787ade6d48ef14.tar.bz2
ayatana-indicator-session-ae39f7001e5603010afc02de29787ade6d48ef14.zip
port indicator-session to GMenu/cmake. Code coverage increased from 0% to 95.4%.
Diffstat (limited to 'tests/gtest-dbus-fixture.h')
-rw-r--r--tests/gtest-dbus-fixture.h134
1 files changed, 134 insertions, 0 deletions
diff --git a/tests/gtest-dbus-fixture.h b/tests/gtest-dbus-fixture.h
new file mode 100644
index 0000000..e6cd9c7
--- /dev/null
+++ b/tests/gtest-dbus-fixture.h
@@ -0,0 +1,134 @@
+/*
+ * 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 <glib.h>
+#include <gio/gio.h>
+
+#include <gtest/gtest.h>
+
+/***
+****
+***/
+
+class GTestDBusFixture : public ::testing::Test
+{
+ private:
+
+ static void
+ on_bus_opened (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
+ {
+ GTestDBusFixture * self = static_cast<GTestDBusFixture*>(gself);
+
+ GError * err = 0;
+ self->conn = g_bus_get_finish (res, &err);
+ g_assert_no_error (err);
+
+ g_main_loop_quit (self->loop);
+ }
+
+ static void
+ on_bus_closed (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
+ {
+ GTestDBusFixture * self = static_cast<GTestDBusFixture*>(gself);
+
+ GError * err = 0;
+ g_dbus_connection_close_finish (self->conn, res, &err);
+ g_assert_no_error (err);
+
+ g_main_loop_quit (self->loop);
+ }
+
+ 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:
+
+ virtual void SetUp ()
+ {
+ conn = 0;
+ test_dbus = 0;
+ loop = 0;
+
+ g_setenv ("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE);
+ g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
+ g_debug ("SCHEMA_DIR is %s", SCHEMA_DIR);
+
+ // pull up a test dbus
+ loop = g_main_loop_new (NULL, FALSE);
+ test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
+ g_test_dbus_add_service_dir (test_dbus, INDICATOR_SERVICE_DIR);
+ g_debug ("INDICATOR_SERVICE_DIR is %s", INDICATOR_SERVICE_DIR);
+ g_test_dbus_up (test_dbus);
+ const char * address;
+ address = g_test_dbus_get_bus_address (test_dbus);
+ g_setenv ("DBUS_SYSTEM_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, NULL, on_bus_opened, this);
+ g_main_loop_run (loop);
+ }
+
+ virtual void TearDown()
+ {
+ // close the bus connection
+ g_dbus_connection_close (conn, NULL, on_bus_closed, this);
+ g_main_loop_run (loop);
+ g_clear_object (&conn);
+
+ // tear down the test dbus
+ g_test_dbus_down (test_dbus);
+ g_clear_object (&test_dbus);
+
+ g_clear_pointer (&loop, g_main_loop_unref);
+ }
+
+ /* 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; // arbitrary
+
+ // 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)
+ {
+ guint id = g_timeout_add (msec, (GSourceFunc)g_main_loop_quit, loop);
+ g_main_loop_run (loop);
+ g_source_remove (id);
+ }
+
+ GMainLoop * loop;
+ GTestDBus * test_dbus;
+ GDBusConnection * conn;
+};