1 /* Tests of TpDebugClient
2  *
3  * Copyright © 2010 Collabora Ltd. <http://www.collabora.co.uk/>
4  *
5  * Copying and distribution of this file, with or without modification,
6  * are permitted in any medium without royalty provided the copyright
7  * notice and this notice are preserved.
8  */
9 
10 #include "config.h"
11 
12 #include <string.h>
13 
14 #include <telepathy-glib/telepathy-glib.h>
15 #include <telepathy-glib/debug-sender.h>
16 
17 #include "tests/lib/util.h"
18 
19 typedef struct {
20     GMainLoop *mainloop;
21     TpDBusDaemon *dbus;
22 
23     /* Service side object */
24     TpDebugSender *sender;
25 
26     /* Client side object */
27     TpDebugClient *client;
28 
29     GPtrArray *messages;
30     TpDebugMessage *message;
31     GError *error /* initialized where needed */;
32     gint wait;
33 } Test;
34 
35 static void
setup(Test * test,gconstpointer data)36 setup (Test *test,
37        gconstpointer data)
38 {
39   test->mainloop = g_main_loop_new (NULL, FALSE);
40   test->dbus = tp_tests_dbus_daemon_dup_or_die ();
41 
42   test->error = NULL;
43 
44   test->sender = tp_debug_sender_dup ();
45   g_assert (test->sender != NULL);
46 
47   test->client = tp_debug_client_new (test->dbus,
48       tp_dbus_daemon_get_unique_name (test->dbus), &test->error);
49   g_assert_no_error (test->error);
50 }
51 
52 static void
teardown(Test * test,gconstpointer data)53 teardown (Test *test,
54           gconstpointer data)
55 {
56   g_clear_error (&test->error);
57 
58   tp_clear_object (&test->dbus);
59   g_main_loop_unref (test->mainloop);
60   test->mainloop = NULL;
61 
62   tp_clear_object (&test->sender);
63   tp_clear_object (&test->client);
64 
65   tp_clear_pointer (&test->messages, g_ptr_array_unref);
66   tp_clear_object (&test->message);
67 }
68 
69 static void
test_creation(Test * test,gconstpointer data G_GNUC_UNUSED)70 test_creation (Test *test,
71     gconstpointer data G_GNUC_UNUSED)
72 {
73   g_assert (TP_IS_DEBUG_CLIENT (test->client));
74 }
75 
76 static void
invalidated_cb(TpProxy * proxy,guint domain,gint code,gchar * message,Test * test)77 invalidated_cb (TpProxy *proxy,
78     guint domain,
79     gint code,
80     gchar *message,
81     Test *test)
82 {
83   test->wait--;
84   if (test->wait <= 0)
85     g_main_loop_quit (test->mainloop);
86 }
87 
88 static void
test_invalidated(Test * test,gconstpointer data G_GNUC_UNUSED)89 test_invalidated (Test *test,
90     gconstpointer data G_GNUC_UNUSED)
91 {
92   g_signal_connect (test->client, "invalidated",
93       G_CALLBACK (invalidated_cb), test);
94 
95   tp_clear_object (&test->sender);
96 
97   test->wait = 1;
98   g_main_loop_run (test->mainloop);
99   g_assert_no_error (test->error);
100 }
101 
102 static void
proxy_prepare_cb(GObject * source,GAsyncResult * result,gpointer user_data)103 proxy_prepare_cb (GObject *source,
104     GAsyncResult *result,
105     gpointer user_data)
106 {
107   Test *test = user_data;
108 
109   tp_proxy_prepare_finish (source, result, &test->error);
110 
111   test->wait--;
112   if (test->wait <= 0)
113     g_main_loop_quit (test->mainloop);
114 }
115 
116 static void
test_core_feature(Test * test,gconstpointer data G_GNUC_UNUSED)117 test_core_feature (Test *test,
118     gconstpointer data G_GNUC_UNUSED)
119 {
120   GQuark features[] = { TP_DEBUG_CLIENT_FEATURE_CORE, 0 };
121 
122   g_object_set (test->sender, "enabled", TRUE, NULL);
123 
124   /* feature is not prepared yet */
125   g_assert (!tp_debug_client_is_enabled (test->client));
126 
127   tp_proxy_prepare_async (test->client, features, proxy_prepare_cb, test);
128 
129   test->wait = 1;
130   g_main_loop_run (test->mainloop);
131   g_assert_no_error (test->error);
132 
133   g_assert (tp_debug_client_is_enabled (test->client));
134 }
135 
136 static void
set_enabled_cb(GObject * source,GAsyncResult * result,gpointer user_data)137 set_enabled_cb (GObject *source,
138     GAsyncResult *result,
139     gpointer user_data)
140 {
141   Test *test = user_data;
142 
143   tp_debug_client_set_enabled_finish (TP_DEBUG_CLIENT (source),
144       result, &test->error);
145 
146   test->wait--;
147   if (test->wait <= 0)
148     g_main_loop_quit (test->mainloop);
149 }
150 
151 static void
test_set_enabled(Test * test,gconstpointer data G_GNUC_UNUSED)152 test_set_enabled (Test *test,
153     gconstpointer data G_GNUC_UNUSED)
154 {
155   gboolean enabled;
156 
157   g_object_get (test->sender, "enabled", &enabled, NULL);
158   g_assert (!enabled);
159 
160   /* Enable */
161   tp_debug_client_set_enabled_async (test->client, TRUE, set_enabled_cb, test);
162 
163   test->wait = 1;
164   g_main_loop_run (test->mainloop);
165   g_assert_no_error (test->error);
166 
167   g_object_get (test->sender, "enabled", &enabled, NULL);
168   g_assert (enabled);
169 
170   /* Disable */
171   tp_debug_client_set_enabled_async (test->client, FALSE, set_enabled_cb, test);
172 
173   test->wait = 1;
174   g_main_loop_run (test->mainloop);
175   g_assert_no_error (test->error);
176 
177   g_object_get (test->sender, "enabled", &enabled, NULL);
178   g_assert (!enabled);
179 }
180 
181 static void
get_messages_cb(GObject * source,GAsyncResult * result,gpointer user_data)182 get_messages_cb (GObject *source,
183     GAsyncResult *result,
184     gpointer user_data)
185 {
186   Test *test = user_data;
187 
188   tp_clear_pointer (&test->messages, g_ptr_array_unref);
189 
190   test->messages = tp_debug_client_get_messages_finish (
191       TP_DEBUG_CLIENT (source), result, &test->error);
192 
193   test->wait--;
194   if (test->wait <= 0)
195     g_main_loop_quit (test->mainloop);
196 }
197 
198 static void
test_get_messages(Test * test,gconstpointer data G_GNUC_UNUSED)199 test_get_messages (Test *test,
200     gconstpointer data G_GNUC_UNUSED)
201 {
202   GDateTime *time1, *time2, *t;
203   GTimeVal time_val;
204   TpDebugMessage *msg;
205 
206   time1 = g_date_time_new_now_utc ();
207   g_date_time_to_timeval (time1, &time_val);
208 
209   tp_debug_sender_add_message (test->sender, &time_val, "domain1",
210       G_LOG_LEVEL_MESSAGE, "message1\n");
211 
212   time2 = g_date_time_new_now_local ();
213   g_date_time_to_timeval (time2, &time_val);
214 
215   tp_debug_sender_add_message (test->sender, &time_val, "domain2/category",
216       G_LOG_LEVEL_DEBUG, "message2");
217 
218   tp_debug_client_get_messages_async (test->client, get_messages_cb, test);
219 
220   test->wait = 1;
221   g_main_loop_run (test->mainloop);
222   g_assert_no_error (test->error);
223 
224   g_assert (test->messages != NULL);
225   g_assert_cmpuint (test->messages->len, ==, 2);
226 
227   /* first message */
228   msg = g_ptr_array_index (test->messages, 0);
229   g_assert (TP_IS_DEBUG_MESSAGE (msg));
230 
231   t = tp_debug_message_get_time (msg);
232   g_assert (t != NULL);
233   /* Don't use g_date_time_equal() as the gouble -> GDateTime conversion in
234    * _tp_debug_message_new() may result in a difference of one (!)
235    * millisecond */
236   g_assert_cmpuint (g_date_time_to_unix (t), ==, g_date_time_to_unix (time1));
237 
238   g_assert_cmpstr (tp_debug_message_get_domain (msg), ==, "domain1");
239   g_assert (tp_debug_message_get_category (msg) == NULL);
240   g_assert_cmpuint (tp_debug_message_get_level (msg), ==, G_LOG_LEVEL_MESSAGE);
241   g_assert_cmpstr (tp_debug_message_get_message (msg), ==, "message1");
242 
243   /* second message */
244   msg = g_ptr_array_index (test->messages, 1);
245   g_assert (TP_IS_DEBUG_MESSAGE (msg));
246 
247   t = tp_debug_message_get_time (msg);
248   g_assert (t != NULL);
249   g_assert_cmpuint (g_date_time_to_unix (t), ==, g_date_time_to_unix (time2));
250 
251   g_assert_cmpstr (tp_debug_message_get_domain (msg), ==, "domain2");
252   g_assert_cmpstr (tp_debug_message_get_category (msg), ==, "category");
253   g_assert_cmpuint (tp_debug_message_get_level (msg), ==, G_LOG_LEVEL_DEBUG);
254   g_assert_cmpstr (tp_debug_message_get_message (msg), ==, "message2");
255 }
256 
257 static void
new_debug_message_cb(TpDebugClient * client,TpDebugMessage * message,Test * test)258 new_debug_message_cb (TpDebugClient *client,
259     TpDebugMessage *message,
260     Test *test)
261 {
262   tp_clear_object (&test->message);
263 
264   test->message = g_object_ref (message);
265 
266   test->wait--;
267   if (test->wait <= 0)
268     g_main_loop_quit (test->mainloop);
269 
270 }
271 
272 static void
test_new_debug_message(Test * test,gconstpointer data G_GNUC_UNUSED)273 test_new_debug_message (Test *test,
274     gconstpointer data G_GNUC_UNUSED)
275 {
276   g_signal_connect (test->client, "new-debug-message",
277       G_CALLBACK (new_debug_message_cb), test);
278 
279   g_object_set (test->sender, "enabled", TRUE, NULL);
280 
281   tp_debug_sender_add_message (test->sender, NULL, "domain",
282       G_LOG_LEVEL_DEBUG, "new message");
283 
284   test->wait = 1;
285   g_main_loop_run (test->mainloop);
286   g_assert_no_error (test->error);
287 
288   g_assert (TP_IS_DEBUG_MESSAGE (test->message));
289 
290   g_assert_cmpstr (tp_debug_message_get_domain (test->message), ==, "domain");
291   g_assert_cmpuint (tp_debug_message_get_level (test->message), ==,
292       G_LOG_LEVEL_DEBUG);
293   g_assert_cmpstr (tp_debug_message_get_message (test->message), ==,
294       "new message");
295 }
296 
297 static void
test_get_messages_failed(Test * test,gconstpointer data G_GNUC_UNUSED)298 test_get_messages_failed (Test *test,
299     gconstpointer data G_GNUC_UNUSED)
300 {
301   /* Remove debug service */
302   tp_clear_object (&test->sender);
303 
304   tp_debug_client_get_messages_async (test->client, get_messages_cb, test);
305 
306   test->wait = 1;
307   g_main_loop_run (test->mainloop);
308   g_assert_error (test->error, DBUS_GERROR, DBUS_GERROR_UNKNOWN_METHOD);
309 
310   g_assert (test->messages == NULL);
311 }
312 
313 int
main(int argc,char ** argv)314 main (int argc,
315       char **argv)
316 {
317   tp_tests_init (&argc, &argv);
318   g_test_bug_base ("http://bugs.freedesktop.org/show_bug.cgi?id=");
319 
320   g_test_add ("/debug-client/creation", Test, NULL, setup,
321       test_creation, teardown);
322   g_test_add ("/debug-client/invalidated", Test, NULL, setup,
323       test_invalidated, teardown);
324   g_test_add ("/debug-client/core-feature", Test, NULL, setup,
325       test_core_feature, teardown);
326   g_test_add ("/debug-client/set-enabled", Test, NULL, setup,
327       test_set_enabled, teardown);
328   g_test_add ("/debug-client/get-messages", Test, NULL, setup,
329       test_get_messages, teardown);
330   g_test_add ("/debug-client/new-debug-message", Test, NULL, setup,
331       test_new_debug_message, teardown);
332   g_test_add ("/debug-client/get-messages-failed", Test, NULL, setup,
333       test_get_messages_failed, teardown);
334 
335   return tp_tests_run_with_bus ();
336 }
337