2
* Copyright 2013 Canonical Ltd.
5
* Charles Kerr <charles.kerr@canonical.com>
7
* This program is free software: you can redistribute it and/or modify it
8
* under the terms of the GNU General Public License version 3, as published
9
* by the Free Software Foundation.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranties of
13
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
* PURPOSE. See the GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License along
17
* with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include <gtest/gtest.h>
29
class GTestDBusFixture : public ::testing::Test
34
on_bus_opened (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
36
GTestDBusFixture * self = static_cast<GTestDBusFixture*>(gself);
39
self->conn = g_bus_get_finish (res, &err);
40
g_assert_no_error (err);
42
g_main_loop_quit (self->loop);
46
on_bus_closed (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
48
GTestDBusFixture * self = static_cast<GTestDBusFixture*>(gself);
51
g_dbus_connection_close_finish (self->conn, res, &err);
52
g_assert_no_error (err);
54
g_main_loop_quit (self->loop);
65
g_setenv ("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE);
66
g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
67
g_debug ("SCHEMA_DIR is %s", SCHEMA_DIR);
69
// pull up a test dbus
70
loop = g_main_loop_new (NULL, FALSE);
71
test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
72
g_test_dbus_add_service_dir (test_dbus, INDICATOR_SERVICE_DIR);
73
g_debug ("INDICATOR_SERVICE_DIR is %s", INDICATOR_SERVICE_DIR);
74
g_test_dbus_up (test_dbus);
76
address = g_test_dbus_get_bus_address (test_dbus);
77
g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", address, TRUE);
78
g_debug ("test_dbus's address is %s", address);
80
// wait for the GDBusConnection before returning
81
g_bus_get (G_BUS_TYPE_SYSTEM, NULL, on_bus_opened, this);
82
g_main_loop_run (loop);
85
virtual void TearDown()
87
// close the bus connection
88
g_dbus_connection_close (conn, NULL, on_bus_closed, this);
89
g_main_loop_run (loop);
90
g_clear_object (&conn);
92
// tear down the test dbus
93
g_test_dbus_down (test_dbus);
94
g_clear_object (&test_dbus);
96
g_clear_pointer (&loop, g_main_loop_unref);
102
wait_for_signal__timeout(gpointer name)
104
g_error("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name);
105
return G_SOURCE_REMOVE;
109
wait_msec__timeout(gpointer loop)
111
g_main_loop_quit(static_cast<GMainLoop*>(loop));
112
return G_SOURCE_CONTINUE;
117
/* convenience func to loop while waiting for a GObject's signal */
118
void wait_for_signal(gpointer o, const gchar * signal, const int timeout_seconds=5)
120
// wait for the signal or for timeout, whichever comes first
121
const auto handler_id = g_signal_connect_swapped(o, signal,
122
G_CALLBACK(g_main_loop_quit),
124
const auto timeout_id = g_timeout_add_seconds(timeout_seconds,
125
wait_for_signal__timeout,
127
g_main_loop_run(loop);
128
g_source_remove(timeout_id);
129
g_signal_handler_disconnect(o, handler_id);
132
/* convenience func to loop for N msec */
133
void wait_msec(int msec=50)
135
const auto id = g_timeout_add(msec, wait_msec__timeout, loop);
136
g_main_loop_run(loop);
141
GTestDBus * test_dbus;
142
GDBusConnection * conn;