~ubuntu-branches/ubuntu/quantal/ibus/quantal

« back to all changes in this revision

Viewing changes to src/tests/ibus-bus.c

  • Committer: Bazaar Package Importer
  • Author(s): Barry Warsaw
  • Date: 2011-08-11 17:00:57 UTC
  • mfrom: (6.2.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110811170057-6dmbfs4s3cchzl7x
Tags: 1.3.99.20110419-1ubuntu1
* Merge with Debian unstable.  Remaining Ubuntu changes:
  - Indicator support:
    + Add 05_appindicator.patch: Use an indicator rather than a notification
      icon.
    + debian/control: Recommend python-appindicator.
  - debian/control: Install im-switch instead of im-config by default.
  - debian/README.source: Removed, it was outdated and no longer correct
  - debian/patches/01_ubuntu_desktop: Fix "Desktop entry needs the
    X-Ubuntu-Gettext-Domain key"  (LP: #457632)
  - debian/patches/02_title_update.patch: Rename "IBus Preferences" to
    "Keyboard Input Methods"
  - debian/patches/06_locale_parser.patch: Cherry-picked from upstream.

Show diffs side-by-side

added added

removed removed

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