2
#include <glib-object.h>
6
#include <gtest/gtest.h>
8
#include "dbus-shared.h"
9
#include "sync-service-dbus.h"
10
#include "sync-menu/sync-app.h"
12
class ClientTest : public ::testing::Test
16
GTestDBus * test_dbus;
17
GDBusConnection * session_bus;
18
GMainLoop * main_loop;
19
DbusSyncService * service_proxy;
28
static bool ran_once_init = false;
32
g_unsetenv ("INDICATOR_ALLOW_NO_WATCHERS");
33
g_unsetenv ("INDICATOR_SERVICE_REPLACE_MODE");
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);
43
// allow the service to exist w/o a sync indicator
44
g_setenv ("INDICATOR_ALLOW_NO_WATCHERS", "1", TRUE);
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);
54
virtual void TearDown()
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);
63
void SetUpServiceProxy (bool autostart=false)
67
ASSERT_TRUE (session_bus != NULL);
69
int flags = G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES;
71
flags |= G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
73
g_debug (G_STRLOC" the dbus connection %p refcount is %d", session_bus, G_OBJECT(session_bus)->ref_count);
75
service_proxy = dbus_sync_service_proxy_new_sync (
77
GDBusProxyFlags(flags),
78
SYNC_SERVICE_DBUS_NAME,
79
SYNC_SERVICE_DBUS_OBJECT,
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);
86
ASSERT_TRUE (err == NULL);
87
ASSERT_EQ (dbus_sync_service_get_client_count (service_proxy), 0);
90
bool ServiceProxyIsOwned () const
92
g_return_val_if_fail (service_proxy != NULL, FALSE);
94
gchar * owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY(service_proxy));
95
const bool is_owned = owner && *owner;
100
void ServiceProxyShutdown ()
105
ASSERT_TRUE (session_bus != NULL);
106
ASSERT_TRUE (service_proxy != NULL);
109
ret = g_dbus_connection_call_sync (
111
SYNC_SERVICE_DBUS_NAME,
112
"/org/ayatana/indicator/service",
113
"org.ayatana.indicator.service",
116
G_DBUS_CALL_FLAGS_NONE,
118
g_clear_pointer (&ret, g_variant_unref);
120
ASSERT_TRUE (err == NULL);
123
// undo SetUpServiceProxy
124
void TearDownServiceProxy ()
126
ASSERT_TRUE (service_proxy != NULL);
128
g_clear_object (&service_proxy);
131
void WaitForSignal (gpointer instance, const gchar * detailed_signal)
133
ASSERT_TRUE (instance != NULL);
134
ASSERT_TRUE (main_loop != NULL);
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);
147
TEST_F(ClientTest, TestCanStartService)
149
ASSERT_TRUE (service_proxy == NULL);
150
SetUpServiceProxy (true);
151
ASSERT_TRUE (ServiceProxyIsOwned ());
152
TearDownServiceProxy ();
156
TEST_F(ClientTest, AppCanStartService)
159
SetUpServiceProxy (false);
161
app = sync_menu_app_new ("transmission-gtk.desktop");
162
WaitForSignal (service_proxy, "notify::g-name-owner");
163
ASSERT_TRUE (ServiceProxyIsOwned ());
165
WaitForSignal (service_proxy, "notify::client-count");
166
ASSERT_EQ (1, dbus_sync_service_get_client_count (service_proxy));
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));
173
TearDownServiceProxy ();
174
g_clear_object (&app);