~ubuntu-branches/ubuntu/trusty/indicator-sync/trusty

« back to all changes in this revision

Viewing changes to test/test-client.cpp

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2012-08-28 16:25:25 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120828162525-v3mx9o332fqrseu8
Tags: 12.10.1-0ubuntu1
* New upstream release.
* debian/control:
  - rename gir1.2-syncclient-12.10 to gir1.2-syncmenu-0.1.
  - rename libindicator-sync-client1 to libsync-menu1.
  - rename libindicator-sync-client-dev to libsync-menu-dev.
  - add gir1.2-dbusmenu-glib-0.4 to Build-Depends.
  - add dbus-test-runner to Build-Depends.
* debian/*.install:
  - rename files as per package name changes.
  - update libsync-menu-dev.install to the library name.
  - update libsync-menu1.install to the library name.
  - update libsync-menu1.symbols to the library name.
  - rename /usr/include/indicator-sync-0.1 to /usr/include/sync-menu-0.1.
* debian/patches/upstream-soname-versioning.patch,
  debian/patches/upstream-pkgconfig-fixes.patch: dropped, no longer needed.
* debian/libsync-menu1.symbols:
  - update symbols that were renamed; keep the old ones as missing.
* debian/patches/upstream-watchers-client-test.patch: fix up the client test
  to avoid failing due to watchers not being allowed to connect.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <glib-object.h>
 
3
#include <gio/gio.h>
 
4
#include <glib.h>
 
5
 
 
6
#include <gtest/gtest.h>
 
7
 
 
8
#include "dbus-shared.h"
 
9
#include "sync-service-dbus.h"
 
10
#include "sync-menu/sync-app.h"
 
11
 
 
12
class ClientTest : public ::testing::Test
 
13
{
 
14
  protected:
 
15
 
 
16
    GTestDBus * test_dbus;
 
17
    GDBusConnection * session_bus;
 
18
    GMainLoop * main_loop;
 
19
    DbusSyncService * service_proxy;
 
20
 
 
21
    virtual void SetUp()
 
22
    {
 
23
      test_dbus = NULL;
 
24
      session_bus = NULL;
 
25
      main_loop = NULL;
 
26
      service_proxy = NULL;
 
27
 
 
28
      static bool ran_once_init = false;
 
29
      if (!ran_once_init)
 
30
        {
 
31
          g_type_init();
 
32
          g_unsetenv ("INDICATOR_ALLOW_NO_WATCHERS");
 
33
          g_unsetenv ("INDICATOR_SERVICE_REPLACE_MODE");
 
34
          ran_once_init = true;
 
35
        }
 
36
 
 
37
      main_loop = g_main_loop_new (NULL, FALSE);
 
38
      // pull up a test dbus that's pointed at our test .service file
 
39
      test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
 
40
      g_debug (G_STRLOC" service dir path is \"%s\"", INDICATOR_SERVICE_DIR);
 
41
      g_test_dbus_add_service_dir (test_dbus, INDICATOR_SERVICE_DIR);
 
42
 
 
43
      // allow the service to exist w/o a sync indicator
 
44
      g_setenv ("INDICATOR_ALLOW_NO_WATCHERS", "1", TRUE);
 
45
 
 
46
      g_test_dbus_up (test_dbus);
 
47
      g_debug (G_STRLOC" this test bus' address is \"%s\"", g_test_dbus_get_bus_address(test_dbus));
 
48
      session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
 
49
      g_debug (G_STRLOC" the dbus connection %p unique name is \"%s\"", session_bus, g_dbus_connection_get_unique_name(session_bus));
 
50
      g_debug (G_STRLOC" the dbus connection %p refcount is %d", session_bus, G_OBJECT(session_bus)->ref_count);
 
51
    }
 
52
 
 
53
    // undo SetUp
 
54
    virtual void TearDown()
 
55
    {
 
56
      g_clear_object (&session_bus);
 
57
      g_debug (G_STRLOC" tearing down the bus");
 
58
      g_test_dbus_down (test_dbus);
 
59
      g_clear_object (&test_dbus);
 
60
      g_clear_pointer (&main_loop, g_main_loop_unref);
 
61
    }
 
62
 
 
63
    void SetUpServiceProxy (bool autostart=false)
 
64
    {
 
65
      GError * err;
 
66
 
 
67
      ASSERT_TRUE (session_bus != NULL);
 
68
 
 
69
      int flags = G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES;
 
70
      if (!autostart)
 
71
        flags |= G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
 
72
 
 
73
      g_debug (G_STRLOC" the dbus connection %p refcount is %d", session_bus, G_OBJECT(session_bus)->ref_count);
 
74
      err = NULL;
 
75
      service_proxy = dbus_sync_service_proxy_new_sync (
 
76
        session_bus, 
 
77
        GDBusProxyFlags(flags),
 
78
        SYNC_SERVICE_DBUS_NAME,
 
79
        SYNC_SERVICE_DBUS_OBJECT,
 
80
        NULL, &err);
 
81
      if (err != NULL)
 
82
        g_error ("unable to create service proxy: %s", err->message);
 
83
      g_debug (G_STRLOC" the service proxy %p refcount is %d", service_proxy, G_OBJECT(service_proxy)->ref_count);
 
84
      g_debug (G_STRLOC" the dbus connection %p refcount is %d", session_bus, G_OBJECT(session_bus)->ref_count);
 
85
 
 
86
      ASSERT_TRUE (err == NULL);
 
87
      ASSERT_EQ (dbus_sync_service_get_client_count (service_proxy), 0);
 
88
    }
 
89
 
 
90
    bool ServiceProxyIsOwned () const
 
91
    {
 
92
      g_return_val_if_fail (service_proxy != NULL, FALSE);
 
93
 
 
94
      gchar * owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY(service_proxy));
 
95
      const bool is_owned = owner && *owner;
 
96
      g_free (owner);
 
97
      return is_owned;
 
98
    }
 
99
 
 
100
    void ServiceProxyShutdown ()
 
101
    {
 
102
      GError * err;
 
103
      GVariant * ret;
 
104
 
 
105
      ASSERT_TRUE (session_bus != NULL);
 
106
      ASSERT_TRUE (service_proxy != NULL);
 
107
 
 
108
      err = NULL;
 
109
      ret = g_dbus_connection_call_sync (
 
110
              session_bus,
 
111
              SYNC_SERVICE_DBUS_NAME,
 
112
              "/org/ayatana/indicator/service",
 
113
              "org.ayatana.indicator.service",
 
114
              "Shutdown", NULL,
 
115
              NULL,
 
116
              G_DBUS_CALL_FLAGS_NONE,
 
117
              -1, NULL, &err);
 
118
      g_clear_pointer (&ret, g_variant_unref);
 
119
 
 
120
      ASSERT_TRUE (err == NULL);
 
121
    }
 
122
 
 
123
    // undo SetUpServiceProxy
 
124
    void TearDownServiceProxy ()
 
125
    {
 
126
      ASSERT_TRUE (service_proxy != NULL);
 
127
 
 
128
      g_clear_object (&service_proxy);
 
129
    }
 
130
 
 
131
    void WaitForSignal (gpointer instance, const gchar * detailed_signal)
 
132
    {
 
133
      ASSERT_TRUE (instance != NULL);
 
134
      ASSERT_TRUE (main_loop != NULL);
 
135
 
 
136
      const gulong handler_id = g_signal_connect_swapped (instance, detailed_signal, G_CALLBACK(g_main_loop_quit), main_loop);
 
137
      g_main_loop_run (main_loop);
 
138
      g_signal_handler_disconnect (instance, handler_id);
 
139
    }
 
140
};
 
141
 
 
142
/***
 
143
****
 
144
***/
 
145
 
 
146
 
 
147
TEST_F(ClientTest, TestCanStartService)
 
148
{
 
149
  ASSERT_TRUE (service_proxy == NULL);
 
150
  SetUpServiceProxy (true);
 
151
  ASSERT_TRUE (ServiceProxyIsOwned ());
 
152
  TearDownServiceProxy ();
 
153
}
 
154
 
 
155
 
 
156
TEST_F(ClientTest, AppCanStartService)
 
157
{
 
158
  SyncMenuApp * app;
 
159
  SetUpServiceProxy (false);
 
160
 
 
161
  app = sync_menu_app_new ("transmission-gtk.desktop");
 
162
  WaitForSignal (service_proxy, "notify::g-name-owner");
 
163
  ASSERT_TRUE (ServiceProxyIsOwned ());
 
164
 
 
165
  WaitForSignal (service_proxy, "notify::client-count");
 
166
  ASSERT_EQ (1, dbus_sync_service_get_client_count (service_proxy));
 
167
 
 
168
  ServiceProxyShutdown ();
 
169
  WaitForSignal (service_proxy, "notify::g-name-owner");
 
170
  ASSERT_FALSE (ServiceProxyIsOwned ());
 
171
  ASSERT_EQ (0, dbus_sync_service_get_client_count (service_proxy));
 
172
 
 
173
  TearDownServiceProxy ();
 
174
  g_clear_object (&app);
 
175
}