Add asynchronous APIs to ibusbus.h
[platform/upstream/ibus.git] / src / tests / ibus-bus.c
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2
3 #include <string.h>
4 #include "ibus.h"
5
6 static IBusBus *bus;
7
8 static gchar *
9 get_last_engine_id (const GList *engines)
10 {
11     const char *result = NULL;
12     for (; engines; engines = g_list_next (engines)) {
13         IBusEngineDesc *engine_desc = IBUS_ENGINE_DESC (engines->data);
14         g_assert (engine_desc);
15         result = ibus_engine_desc_get_name (engine_desc);
16     }
17     return g_strdup (result);
18 }
19
20 static void
21 print_engines (const GList *engines)
22 {
23     for (; engines; engines = g_list_next (engines)) {
24         IBusEngineDesc *engine_desc = IBUS_ENGINE_DESC (engines->data);
25         g_assert (engine_desc);
26 #if 0
27         g_debug ("%s (id:%s, icon:%s)",
28                  ibus_engine_desc_get_longname (engine_desc),
29                  ibus_engine_desc_get_name (engine_desc),
30                  ibus_engine_desc_get_icon (engine_desc));
31 #endif
32     }
33 }
34
35 static void
36 test_list_active_engines (void)
37 {
38     GList *engines;
39     IBUS_TYPE_ENGINE_DESC;
40
41     engines = ibus_bus_list_active_engines (bus);
42     print_engines (engines);
43
44     g_list_foreach (engines, (GFunc) g_object_unref, NULL);
45     g_list_free (engines);
46 }
47
48 static void
49 test_list_engines (void)
50 {
51     GList *engines;
52     IBUS_TYPE_ENGINE_DESC;
53
54     engines = ibus_bus_list_engines (bus);
55     print_engines (engines);
56
57     g_list_foreach (engines, (GFunc) g_object_unref, NULL);
58     g_list_free (engines);
59 }
60
61 static void
62 test_input_context (void)
63 {
64     GList *engines;
65     gchar *active_engine_name = NULL;
66     IBusInputContext *context;
67     IBusEngineDesc *engine_desc;
68     gchar *current_ic;
69
70     engines = ibus_bus_list_active_engines (bus);
71     if (engines == NULL)
72         return;
73     active_engine_name = get_last_engine_id (engines);
74     g_assert (active_engine_name);
75
76     context = ibus_bus_create_input_context (bus, "test");
77     ibus_input_context_set_capabilities (context, IBUS_CAP_FOCUS);
78     ibus_input_context_disable (context);
79     g_assert (ibus_input_context_is_enabled (context) == FALSE);
80     ibus_input_context_enable (context);
81     g_assert (ibus_input_context_is_enabled (context) == TRUE);
82     ibus_input_context_focus_in (context);
83     ibus_input_context_set_engine (context, active_engine_name);
84     current_ic = ibus_bus_current_input_context (bus);
85     g_assert (!strcmp (current_ic, g_dbus_proxy_get_object_path ((GDBusProxy *)context)));
86     engine_desc = ibus_input_context_get_engine (context);
87     g_assert (engine_desc);
88     g_assert (!strcmp (active_engine_name, ibus_engine_desc_get_name(engine_desc)));
89     g_free (current_ic);
90     g_object_unref (engine_desc);
91     g_object_unref (context);
92
93     g_free (active_engine_name);
94     g_list_foreach (engines, (GFunc) g_object_unref, NULL);
95     g_list_free (engines);
96 }
97
98 static void call_next_async_function (void);
99
100 static void
101 finish_request_name_async (GObject *source_object,
102                            GAsyncResult *res,
103                            gpointer user_data)
104 {
105     GError *error = NULL;
106     guint id = ibus_bus_request_name_async_finish (bus,
107                                                    res,
108                                                    &error);
109     g_assert (id != 0);
110     g_debug ("ibus_bus_request_name_async_finish: OK");
111     call_next_async_function ();
112 }
113
114 static void
115 start_request_name_async (void)
116 {
117     ibus_bus_request_name_async (bus,
118                                  "org.freedesktop.IBus.IBusBusTest",
119                                  0,
120                                  -1, /* timeout */
121                                  NULL, /* cancellable */
122                                  finish_request_name_async,
123                                  NULL); /* user_data */
124 }
125
126
127 static void
128 finish_name_has_owner_async (GObject *source_object,
129                              GAsyncResult *res,
130                              gpointer user_data)
131 {
132     GError *error = NULL;
133     gboolean has_owner = ibus_bus_name_has_owner_async_finish (bus,
134                                                                res,
135                                                                &error);
136     g_assert (has_owner);
137     g_debug ("ibus_bus_name_has_owner_async_finish: OK");
138     call_next_async_function ();
139 }
140
141 static void
142 start_name_has_owner_async (void)
143 {
144     ibus_bus_name_has_owner_async (bus,
145                                    "org.freedesktop.IBus.IBusBusTest",
146                                    -1, /* timeout */
147                                    NULL, /* cancellable */
148                                    finish_name_has_owner_async,
149                                    NULL); /* user_data */
150 }
151
152 static void
153 finish_get_name_owner_async (GObject *source_object,
154                              GAsyncResult *res,
155                              gpointer user_data)
156 {
157     GError *error = NULL;
158     gchar *owner = ibus_bus_get_name_owner_async_finish (bus,
159                                                          res,
160                                                          &error);
161     g_assert (owner);
162     g_free (owner);
163     g_debug ("ibus_bus_name_get_name_owner_async_finish: OK");
164     call_next_async_function ();
165 }
166
167 static void
168 start_get_name_owner_async (void)
169 {
170     ibus_bus_get_name_owner_async (bus,
171                                    "org.freedesktop.IBus.IBusBusTest",
172                                    -1, /* timeout */
173                                    NULL, /* cancellable */
174                                    finish_get_name_owner_async,
175                                    NULL); /* user_data */
176 }
177
178 static void
179 finish_release_name_async (GObject *source_object,
180                            GAsyncResult *res,
181                            gpointer user_data)
182 {
183     GError *error = NULL;
184     guint id = ibus_bus_release_name_async_finish (bus,
185                                                    res,
186                                                    &error);
187     g_assert (id != 0);
188     g_debug ("ibus_bus_release_name_async_finish: OK");
189     call_next_async_function ();
190 }
191
192 static void
193 start_release_name_async (void)
194 {
195     ibus_bus_release_name_async (bus,
196                                  "org.freedesktop.IBus.IBusBusTest",
197                                  -1, /* timeout */
198                                  NULL, /* cancellable */
199                                  finish_release_name_async,
200                                  NULL); /* user_data */
201 }
202
203 static void
204 finish_add_match_async (GObject *source_object,
205                         GAsyncResult *res,
206                         gpointer user_data)
207 {
208     GError *error = NULL;
209     gboolean result = ibus_bus_add_match_async_finish (bus,
210                                                        res,
211                                                        &error);
212     g_assert (result);
213     g_debug ("ibus_bus_add_match_finish: OK");
214     call_next_async_function ();
215 }
216
217 static void
218 start_add_match_async (void)
219 {
220     ibus_bus_add_match_async (bus,
221                               "type='signal'",
222                               -1, /* timeout */
223                               NULL, /* cancellable */
224                               finish_add_match_async,
225                               NULL); /* user_data */
226 }
227
228 static void
229 finish_remove_match_async (GObject *source_object,
230                            GAsyncResult *res,
231                            gpointer user_data)
232 {
233     GError *error = NULL;
234     gboolean result = ibus_bus_remove_match_async_finish (bus,
235                                                           res,
236                                                           &error);
237     g_assert (result);
238     g_debug ("ibus_bus_remove_match_finish: OK");
239     call_next_async_function ();
240 }
241
242 static void
243 start_remove_match_async (void)
244 {
245     ibus_bus_remove_match_async (bus,
246                                  "type='signal'",
247                                  -1, /* timeout */
248                                  NULL, /* cancellable */
249                                  finish_remove_match_async,
250                                  NULL); /* user_data */
251 }
252
253 static void
254 finish_create_input_context_async (GObject *source_object,
255                                    GAsyncResult *res,
256                                    gpointer user_data)
257 {
258     GError *error = NULL;
259     IBusInputContext *context = ibus_bus_create_input_context_async_finish (bus,
260                                                                             res,
261                                                                             &error);
262     g_assert (context != NULL);
263     g_object_unref (context);
264     g_debug ("ibus_bus_create_input_context_finish: OK");
265     call_next_async_function ();
266 }
267
268 static void
269 start_create_input_context_async (void)
270 {
271     ibus_bus_create_input_context_async (bus,
272                                          "test-async",
273                                          -1, /* timeout */
274                                          NULL, /* cancellable */
275                                          finish_create_input_context_async,
276                                          NULL); /* user_data */
277 }
278
279 static void
280 finish_current_input_context_async (GObject *source_object,
281                                     GAsyncResult *res,
282                                     gpointer user_data)
283 {
284     GError *error = NULL;
285     g_free (ibus_bus_current_input_context_async_finish (bus,
286                                                          res,
287                                                          &error));
288     // no null check.
289     g_debug ("ibus_bus_current_input_context_finish: OK");
290     call_next_async_function ();
291 }
292
293 static void
294 start_current_input_context_async (void)
295 {
296     ibus_bus_current_input_context_async (bus,
297                                           -1, /* timeout */
298                                           NULL, /* cancellable */
299                                           finish_current_input_context_async,
300                                           NULL); /* user_data */
301 }
302
303 static void
304 finish_list_engines_async (GObject *source_object,
305                            GAsyncResult *res,
306                            gpointer user_data)
307 {
308     GError *error = NULL;
309     GList *engines = ibus_bus_list_engines_async_finish (bus,
310                                                          res,
311                                                          &error);
312     // no null check.
313     g_list_foreach (engines, (GFunc) g_object_unref, NULL);
314     g_list_free (engines);
315     g_debug ("ibus_bus_list_engines_finish: OK");
316     call_next_async_function ();
317 }
318
319 static void
320 start_list_engines_async (void)
321 {
322     ibus_bus_list_engines_async (bus,
323                                  -1, /* timeout */
324                                  NULL, /* cancellable */
325                                  finish_list_engines_async,
326                                  NULL); /* user_data */
327 }
328
329 static void
330 finish_list_active_engines_async (GObject *source_object,
331                                   GAsyncResult *res,
332                                   gpointer user_data)
333 {
334     GError *error = NULL;
335     GList *engines = ibus_bus_list_active_engines_async_finish (bus,
336                                                                 res,
337                                                                 &error);
338     // no null check.
339     g_list_foreach (engines, (GFunc) g_object_unref, NULL);
340     g_list_free (engines);
341     g_debug ("ibus_bus_list_active_engines_finish: OK");
342     call_next_async_function ();
343 }
344
345 static void
346 start_list_active_engines_async (void)
347 {
348     ibus_bus_list_active_engines_async (bus,
349                                         -1, /* timeout */
350                                         NULL, /* cancellable */
351                                         finish_list_active_engines_async,
352                                         NULL); /* user_data */
353 }
354
355 static void
356 finish_get_use_sys_layout_async (GObject *source_object,
357                                  GAsyncResult *res,
358                                  gpointer user_data)
359 {
360     GError *error = NULL;
361     ibus_bus_get_use_sys_layout_async_finish (bus,
362                                               res,
363                                               &error);
364     g_debug ("ibus_bus_get_use_sys_layout_finish: OK");
365     call_next_async_function ();
366 }
367
368 static void
369 start_get_use_sys_layout_async (void)
370 {
371     ibus_bus_get_use_sys_layout_async (bus,
372                                        -1, /* timeout */
373                                        NULL, /* cancellable */
374                                        finish_get_use_sys_layout_async,
375                                        NULL); /* user_data */
376 }
377
378 static void
379 finish_get_use_global_engine_async (GObject *source_object,
380                                     GAsyncResult *res,
381                                     gpointer user_data)
382 {
383     GError *error = NULL;
384     ibus_bus_get_use_global_engine_async_finish (bus,
385                                                  res,
386                                                  &error);
387     g_debug ("ibus_bus_get_use_global_engine_finish: OK");
388     call_next_async_function ();
389 }
390
391 static void
392 start_get_use_global_engine_async (void)
393 {
394     ibus_bus_get_use_global_engine_async (bus,
395                                           -1, /* timeout */
396                                           NULL, /* cancellable */
397                                           finish_get_use_global_engine_async,
398                                           NULL); /* user_data */
399 }
400
401 static void
402 finish_is_global_engine_enabled_async (GObject *source_object,
403                                        GAsyncResult *res,
404                                        gpointer user_data)
405 {
406     GError *error = NULL;
407     ibus_bus_is_global_engine_enabled_async_finish (bus,
408                                                     res,
409                                                     &error);
410     g_debug ("ibus_bus_is_global_engine_enabled_finish: OK");
411     call_next_async_function ();
412 }
413
414 static void
415 start_is_global_engine_enabled_async (void)
416 {
417     ibus_bus_is_global_engine_enabled_async (bus,
418                                              -1, /* timeout */
419                                              NULL, /* cancellable */
420                                              finish_is_global_engine_enabled_async,
421                                              NULL); /* user_data */
422 }
423
424 static void
425 finish_get_global_engine_async (GObject *source_object,
426                                 GAsyncResult *res,
427                                 gpointer user_data)
428 {
429     GError *error = NULL;
430     IBusEngineDesc *desc = ibus_bus_get_global_engine_async_finish (bus,
431                                                                     res,
432                                                                     &error);
433     if (desc)
434         g_object_unref (desc);
435     g_debug ("ibus_bus_get_global_engine_finish: OK");
436     call_next_async_function ();
437 }
438
439 static void
440 start_get_global_engine_async (void)
441 {
442     ibus_bus_get_global_engine_async (bus,
443                                       -1, /* timeout */
444                                       NULL, /* cancellable */
445                                       finish_get_global_engine_async,
446                                       NULL); /* user_data */
447 }
448
449 static void
450 finish_set_global_engine_async (GObject *source_object,
451                                 GAsyncResult *res,
452                                 gpointer user_data)
453 {
454     GError *error = NULL;
455     ibus_bus_set_global_engine_async_finish (bus,
456                                              res,
457                                              &error);
458     g_debug ("ibus_bus_set_global_engine_finish: OK");
459     call_next_async_function ();
460 }
461
462 static void
463 start_set_global_engine_async (void)
464 {
465     ibus_bus_set_global_engine_async (bus,
466                                       "anthy",
467                                       -1, /* timeout */
468                                       NULL, /* cancellable */
469                                       finish_set_global_engine_async,
470                                       NULL); /* user_data */
471 }
472
473 static void
474 finish_exit_async (GObject *source_object,
475                    GAsyncResult *res,
476                    gpointer user_data)
477 {
478     GError *error = NULL;
479     gboolean result = ibus_bus_exit_async_finish (bus,
480                                                   res,
481                                                   &error);
482     g_assert (result);
483     g_debug ("ibus_bus_exit_finish: OK");
484     call_next_async_function ();
485 }
486
487 static void
488 start_exit_async (void)
489 {
490     ibus_bus_exit_async (bus,
491                          TRUE, /* restart */
492                          -1, /* timeout */
493                          NULL, /* cancellable */
494                          finish_exit_async,
495                          NULL); /* user_data */
496 }
497
498 static gboolean
499 test_async_apis_finish (gpointer user_data)
500 {
501     ibus_quit ();
502     return FALSE;
503 }
504
505 static void
506 test_async_apis (void)
507 {
508     g_debug ("start");
509     call_next_async_function ();
510     ibus_main ();
511 }
512
513 static void
514 call_next_async_function (void)
515 {
516     static void (*async_functions[])(void) = {
517         start_request_name_async,
518         start_name_has_owner_async,
519         start_get_name_owner_async,
520         start_release_name_async,
521         start_add_match_async,
522         start_remove_match_async,
523         start_create_input_context_async,
524         start_current_input_context_async,
525         // FIXME test ibus_bus_register_component_async.
526         start_list_engines_async,
527         start_list_active_engines_async,
528         start_get_use_sys_layout_async,
529         start_get_use_global_engine_async,
530         start_is_global_engine_enabled_async,
531         start_set_global_engine_async,
532         start_get_global_engine_async,
533         start_exit_async,
534     };
535     static guint index = 0;
536
537     // Use g_timeout_add to make sure test_async_apis finishes even if async_functions is empty.
538     if (index >= G_N_ELEMENTS (async_functions))
539         g_timeout_add (1, test_async_apis_finish, NULL);
540     else
541         (*async_functions[index++])();
542 }
543
544 gint
545 main (gint    argc,
546       gchar **argv)
547 {
548     gint result;
549     g_type_init ();
550     g_test_init (&argc, &argv, NULL);
551     ibus_init ();
552     bus = ibus_bus_new ();
553
554     g_test_add_func ("/ibus/list-engines", test_list_engines);
555     g_test_add_func ("/ibus/list-active-engines", test_list_active_engines);
556     g_test_add_func ("/ibus/async-apis", test_async_apis);
557
558     // FIXME This test does not pass if global engine is not available. Disabling it for now.
559     // g_test_add_func ("/ibus/input_context", test_input_context);
560
561     result = g_test_run ();
562     g_object_unref (bus);
563
564     return result;
565 }