~codygarver/+junk/ind-sess

« back to all changes in this revision

Viewing changes to tests/gtest-dbus-fixture.h

  • Committer: Cody Garver
  • Date: 2014-04-03 17:08:08 UTC
  • Revision ID: cody@elementaryos.org-20140403170808-z56s93rorb1dzvmk
Initial import, version 12.10.5+14.04.20140324-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *   Charles Kerr <charles.kerr@canonical.com>
 
6
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include <glib.h>
 
21
#include <gio/gio.h>
 
22
 
 
23
#include <gtest/gtest.h>
 
24
 
 
25
/***
 
26
****
 
27
***/
 
28
 
 
29
class GTestDBusFixture : public ::testing::Test
 
30
{
 
31
  private:
 
32
 
 
33
    static void
 
34
    on_bus_opened (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
 
35
    {
 
36
      GTestDBusFixture * self = static_cast<GTestDBusFixture*>(gself);
 
37
 
 
38
      GError * err = 0;
 
39
      self->conn = g_bus_get_finish (res, &err);
 
40
      g_assert_no_error (err);
 
41
 
 
42
      g_main_loop_quit (self->loop);
 
43
    }
 
44
 
 
45
    static void
 
46
    on_bus_closed (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
 
47
    {
 
48
      GTestDBusFixture * self = static_cast<GTestDBusFixture*>(gself);
 
49
 
 
50
      GError * err = 0;
 
51
      g_dbus_connection_close_finish (self->conn, res, &err);
 
52
      g_assert_no_error (err);
 
53
 
 
54
      g_main_loop_quit (self->loop);
 
55
    }
 
56
 
 
57
  protected:
 
58
 
 
59
    virtual void SetUp ()
 
60
    {
 
61
      conn = 0;
 
62
      test_dbus = 0;
 
63
      loop = 0;
 
64
 
 
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);
 
68
 
 
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);
 
75
      const char * address;
 
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);
 
79
 
 
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);
 
83
    }
 
84
 
 
85
    virtual void TearDown()
 
86
    {
 
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);
 
91
 
 
92
      // tear down the test dbus
 
93
      g_test_dbus_down (test_dbus);
 
94
      g_clear_object (&test_dbus);
 
95
 
 
96
      g_clear_pointer (&loop, g_main_loop_unref);
 
97
    }
 
98
 
 
99
  private:
 
100
 
 
101
    static gboolean
 
102
    wait_for_signal__timeout(gpointer name)
 
103
    {
 
104
      g_error("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name);
 
105
      return G_SOURCE_REMOVE;
 
106
    }
 
107
 
 
108
    static gboolean
 
109
    wait_msec__timeout(gpointer loop)
 
110
    {
 
111
      g_main_loop_quit(static_cast<GMainLoop*>(loop));
 
112
      return G_SOURCE_CONTINUE;
 
113
    }
 
114
 
 
115
  protected:
 
116
 
 
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)
 
119
    {
 
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),
 
123
                                                       loop);
 
124
      const auto timeout_id = g_timeout_add_seconds(timeout_seconds,
 
125
                                                    wait_for_signal__timeout,
 
126
                                                    loop);
 
127
      g_main_loop_run(loop);
 
128
      g_source_remove(timeout_id);
 
129
      g_signal_handler_disconnect(o, handler_id);
 
130
    }
 
131
 
 
132
    /* convenience func to loop for N msec */
 
133
    void wait_msec(int msec=50)
 
134
    {
 
135
      const auto id = g_timeout_add(msec, wait_msec__timeout, loop);
 
136
      g_main_loop_run(loop);
 
137
      g_source_remove(id);
 
138
    }
 
139
 
 
140
    GMainLoop * loop;
 
141
    GTestDBus * test_dbus;
 
142
    GDBusConnection * conn;
 
143
};