summaryrefslogtreecommitdiff
path: root/src/tests/ibus-config.c
blob: 370212e97e3efed0b84dab90e5f1526917ceea00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */

#include <string.h>
#include "ibus.h"

static IBusBus *bus = NULL;
static int create_config_count = 0;

static void
finish_create_config_async_success (GObject      *source_object,
                                   GAsyncResult *res,
                                   gpointer      user_data)
{
    GMainLoop *loop = (GMainLoop *)user_data;
    GError *error = NULL;
    IBusConfig *config =
          ibus_config_new_async_finish (res, &error);

    g_assert (IBUS_IS_CONFIG (config));

    /* Since we reuse single D-Bus connection, we need to remove the
       default match rule for the next ibus_config_new() call.  */
    ibus_config_unwatch (config, NULL, NULL);
    g_object_unref (config);
    if (--create_config_count == 0)
        g_main_loop_quit (loop);
}

static void
finish_create_config_async_failed (GObject      *source_object,
                                   GAsyncResult *res,
                                   gpointer      user_data)
{
    GMainLoop *loop = (GMainLoop *)user_data;
    GError *error = NULL;
    IBusConfig *config =
            ibus_config_new_async_finish (res, &error);

    g_assert (config == NULL);
    g_assert (error != NULL);
    g_error_free (error);
    if (--create_config_count <= 0)
        g_main_loop_quit (loop);
}

static void
test_create_config_async (void)
{
    GMainLoop *loop = NULL;
    GCancellable *cancellable = NULL;

    /* create an IC */
    create_config_count = 1;
    loop = g_main_loop_new (NULL, TRUE);
    ibus_config_new_async (ibus_bus_get_connection (bus),
                           NULL,
                           finish_create_config_async_success,
                           loop);
    g_main_loop_run (loop);
    g_main_loop_unref (loop);

    /* call create, and then cancel */
    create_config_count = 1;
    loop = g_main_loop_new (NULL, TRUE);
    cancellable = g_cancellable_new ();

    ibus_config_new_async (ibus_bus_get_connection (bus),
                           cancellable,
                           finish_create_config_async_failed,
                           loop);
    g_cancellable_cancel (cancellable);
    g_object_unref (cancellable);
    g_main_loop_run (loop);
    g_main_loop_unref (loop);
}

static void
test_config_set_get (void)
{
    IBusConfig *config = ibus_config_new (ibus_bus_get_connection (bus),
                                          NULL,
                                          NULL);
    g_assert (config);

    ibus_config_set_value (config, "test", "v1", g_variant_new_int32(1));
    ibus_config_set_value (config, "test", "v2", g_variant_new_string("2"));

    GVariant *var;
    var = ibus_config_get_value (config, "test", "v1");
    g_assert (var);
    g_assert_cmpint (g_variant_get_int32(var), ==, 1);
    g_variant_unref (var);

    var = ibus_config_get_value (config, "test", "v2");
    g_assert (var);
    g_assert_cmpstr (g_variant_get_string(var, NULL), ==, "2");
    g_variant_unref (var);

    var = ibus_config_get_values (config, "test");
    g_assert (var);

    GVariantIter iter;
    gchar *name;
    GVariant *value;
    g_variant_iter_init (&iter, var);
    gint value_bits = 0;
    while (g_variant_iter_next (&iter, "{&sv}", &name, &value)) {
        if (g_strcmp0 (name, "v1") == 0) {
            g_assert_cmpint (g_variant_get_int32(value), ==, 1);
            value_bits |= 1;
        }
        else if (g_strcmp0 (name, "v2") == 0) {
            g_assert_cmpstr (g_variant_get_string(value, NULL), ==, "2");
            value_bits |= (1 << 1);
        }
        else {
            g_warning ("unknow value name=%s", name);
        }
        ibus_config_unset (config, "test", name);
        g_variant_unref (value);
    }
    g_assert_cmpint (value_bits, ==, 1 | (1 << 1));
    g_variant_unref (var);

    var = ibus_config_get_values (config, "test");
    g_assert (var);
    g_assert_cmpint (g_variant_n_children (var), ==, 0);
    g_variant_unref (var);

    /* Since we reuse single D-Bus connection, we need to remove the
       default match rule for the next ibus_config_new() call.  */
    ibus_config_unwatch (config, NULL, NULL);
    g_object_unref (config);
}

typedef struct {
    GMainLoop *loop;
    guint timeout_id;
    gchar *section;
    gchar *name;
} WatchData;

typedef struct {
    gchar *section;
    gchar *name;
} WatchKey;

typedef struct {
    WatchKey *watched;          /* watched keys (null-terminated) */
    WatchKey *changed;          /* changed keys (null-terminated) */
    WatchKey *notified;         /* notified keys (same length as
                                   changed, not null-terminated) */
} WatchTestData;

static WatchKey default_watched[] = {
    { NULL }
};
static WatchKey default_changed[] = {
    { "test/s1", "n1" },
    { "test/s1", "n2" },
    { "test/s2", "n1" },
    { "test/s2", "n2" },
    { NULL }
};
static WatchKey default_notified[] = {
    { "test/s1", "n1" },
    { "test/s1", "n2" },
    { "test/s2", "n1" },
    { "test/s2", "n2" }
};
static WatchTestData default_data = {
    default_watched,
    default_changed,
    default_notified
};

static WatchKey section_watched[] = {
    { "test/s1", NULL },
    { NULL }
};
static WatchKey section_notified[] = {
    { "test/s1", "n1" },
    { "test/s1", "n2" },
    { NULL, NULL },
    { NULL, NULL },
};
static WatchTestData section_data = {
    section_watched,
    default_changed,
    section_notified
};

static WatchKey section_multiple_watched[] = {
    { "test/s1", NULL },
    { "test/s2", NULL },
    { NULL }
};
static WatchKey section_multiple_notified[] = {
    { "test/s1", "n1" },
    { "test/s1", "n2" },
    { "test/s2", "n1" },
    { "test/s2", "n2" },
};
static WatchTestData section_multiple_data = {
    section_multiple_watched,
    default_changed,
    section_multiple_notified
};

static WatchKey section_name_watched[] = {
    { "test/s1", "n1" },
    { NULL }
};
static WatchKey section_name_notified[] = {
    { "test/s1", "n1" },
    { NULL, NULL },
    { NULL, NULL },
    { NULL, NULL },
};
static WatchTestData section_name_data = {
    section_name_watched,
    default_changed,
    section_name_notified
};

static WatchKey section_name_multiple_watched[] = {
    { "test/s1", "n1" },
    { "test/s2", "n2" },
    { NULL }
};
static WatchKey section_name_multiple_notified[] = {
    { "test/s1", "n1" },
    { NULL, NULL },
    { NULL, NULL },
    { "test/s2", "n2" },
};
static WatchTestData section_name_multiple_data = {
    section_name_multiple_watched,
    default_changed,
    section_name_multiple_notified
};

typedef struct {
    IBusConfig *config;
    WatchData data;
} WatchFixture;

static void
value_changed_cb (IBusConfig  *config,
                  const gchar *section,
                  const gchar *name,
                  GVariant    *value,
                  gpointer     user_data)
{
    WatchData *data = (WatchData *) user_data;

    data->section = g_strdup (section);
    data->name = g_strdup (name);

    g_main_loop_quit (data->loop);
}

static gboolean
timeout_cb (gpointer user_data)
{
    WatchData *data = (WatchData *) user_data;
    g_main_loop_quit (data->loop);
    data->timeout_id = 0;
    return FALSE;
}

static void
change_and_test (IBusConfig  *config,
                 const gchar *section,
                 const gchar *name,
                 const gchar *expected_section,
                 const gchar *expected_name,
                 WatchData   *data)
{
    gboolean retval;
    GVariant *var;

    data->section = NULL;
    data->name = NULL;

    /* Unset won't notify value-changed signal. */
    var = ibus_config_get_values (config, section);
    if (var != NULL) {
        GVariant *value = g_variant_lookup_value (var, name,
                                                  G_VARIANT_TYPE_VARIANT);
        if (value != NULL) {
            ibus_config_unset (config, section, name);
            g_variant_unref (value);
        }
        g_variant_unref (var);
    }

    data->timeout_id = g_timeout_add (1, timeout_cb, data);
    g_main_loop_run (data->loop);
    if (data->timeout_id != 0) {
        g_source_remove (data->timeout_id);
    }

    retval = ibus_config_set_value (config, section, name,
                                    g_variant_new_int32 (1));
    g_assert (retval);

    data->timeout_id = g_timeout_add (1, timeout_cb, data);
    g_main_loop_run (data->loop);
    if (data->timeout_id != 0) {
        g_source_remove (data->timeout_id);
    }

    g_assert_cmpstr (data->section, ==, expected_section);
    g_assert_cmpstr (data->name, ==, expected_name);

    g_free (data->section);
    g_free (data->name);
}

static void
watch_fixture_setup (WatchFixture *fixture, gconstpointer user_data)
{
    fixture->config = ibus_config_new (ibus_bus_get_connection (bus),
                                       NULL,
                                       NULL);
    g_assert (fixture->config);

    fixture->data.loop = g_main_loop_new (NULL, FALSE);
    g_signal_connect (fixture->config, "value-changed",
                      G_CALLBACK (value_changed_cb), &fixture->data);
}

static void
watch_fixture_teardown (WatchFixture *fixture, gconstpointer user_data)
{
    g_main_loop_unref (fixture->data.loop);

    ibus_proxy_destroy (IBUS_PROXY (fixture->config));
    g_object_unref (fixture->config);
}

static void
test_config_watch (WatchFixture *fixture, gconstpointer user_data)
{
    const WatchTestData *data = user_data;
    gint i;

    for (i = 0; data->watched[i].section != NULL; i++) {
        ibus_config_watch (fixture->config,
                           data->watched[i].section,
                           data->watched[i].name);
    }
    for (i = 0; data->changed[i].section != NULL; i++) {
        change_and_test (fixture->config,
                         data->changed[i].section,
                         data->changed[i].name,
                         data->notified[i].section,
                         data->notified[i].name,
                         &fixture->data);
    }
    for (i = 0; data->watched[i].section != NULL; i++) {
        ibus_config_unwatch (fixture->config,
                             data->watched[i].section,
                             data->watched[i].name);
    }
    if (i > 0) {
        /* Check if the above unwatch takes effect. */
        for (i = 0; data->changed[i].section != NULL; i++) {
            change_and_test (fixture->config,
                             data->changed[i].section,
                             data->changed[i].name,
                             NULL,
                             NULL,
                             &fixture->data);
        }
    } else {
        /* Since we reuse single D-Bus connection, we need to remove the
           default match rule for the next ibus_config_new() call.  */
        ibus_config_unwatch (fixture->config, NULL, NULL);
    }
}

gint
main (gint    argc,
      gchar **argv)
{
    gint result;
    ibus_init ();
    g_test_init (&argc, &argv, NULL);
    bus = ibus_bus_new ();

    g_test_add ("/ibus/config-watch/default",
                WatchFixture,
                &default_data,
                watch_fixture_setup,
                test_config_watch,
                watch_fixture_teardown);

    g_test_add ("/ibus/config-watch/section",
                WatchFixture,
                &section_data,
                watch_fixture_setup,
                test_config_watch,
                watch_fixture_teardown);

    g_test_add ("/ibus/config-watch/section-multiple",
                WatchFixture,
                &section_multiple_data,
                watch_fixture_setup,
                test_config_watch,
                watch_fixture_teardown);

    g_test_add ("/ibus/config-watch/section-name",
                WatchFixture,
                &section_name_data,
                watch_fixture_setup,
                test_config_watch,
                watch_fixture_teardown);

    g_test_add ("/ibus/config-watch/section-name-multiple",
                WatchFixture,
                &section_name_multiple_data,
                watch_fixture_setup,
                test_config_watch,
                watch_fixture_teardown);

    g_test_add_func ("/ibus/create-config-async", test_create_config_async);
    g_test_add_func ("/ibus/config-set-get", test_config_set_get);

    result = g_test_run ();
    g_object_unref (bus);

    return result;
}