summaryrefslogtreecommitdiff
path: root/tests/ibus-hangul.c
blob: 4375f90aae61cc63ca171f1c95afd4e2bb0d982e (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
#include <unistd.h>
#include <gtk/gtk.h>
#include "ibus.h"
#include "engine.h"

#define GREEN "\033[0;32m"
#define RED   "\033[0;31m"
#define NC    "\033[0m"

typedef struct {
    const gchar * input;
    const gchar * output;
}TestCase;

IBusBus *m_bus;
IBusEngine *m_engine;
gchar *m_srcdir;

IBusKeymap *keymap = NULL;

const guint m_switch_hangul[3] = {IBUS_Hangul, 122, 0};

const TestCase m_test_cases[] =
{
 {"rk ", "가"}
};

static gboolean window_focus_in_event_cb (GtkWidget     *entry,
                                          GdkEventFocus *event,
                                          gpointer       data);

static guint16 guess_keycode (IBusKeymap         *keymap,
			      guint               keyval,
			      guint32             modifiers)
{
    /* The IBusKeymap only have 256 entries here,
       Use Brute Force method to get keycode from keyval. */
    guint16 keycode = 0;
    for (; keycode < 256; ++keycode) {
	if (keyval == ibus_keymap_lookup_keysym (keymap, keycode, modifiers))
	    return keycode;
    }
    return 0;
}

static IBusEngine *
create_engine_cb (IBusFactory *factory,
                  const gchar *name,
                  gpointer     data)
{
    static int i = 1;
    gchar *engine_path =
            g_strdup_printf ("/org/freedesktop/IBus/engine/hangultest/%d",
                             i++);

    m_engine = ibus_engine_new_with_type (IBUS_TYPE_HANGUL_ENGINE,
                                          name,
                                          engine_path,
                                          ibus_bus_get_connection (m_bus));
    g_free (engine_path);

    return m_engine;
}

static gboolean
register_ibus_engine ()
{
    IBusFactory *factory;
    IBusComponent *component;
    IBusEngineDesc *desc;

    m_bus = ibus_bus_new ();
    if (!ibus_bus_is_connected (m_bus)) {
        g_critical ("ibus-daemon is not running.");
        return FALSE;
    }
    factory = ibus_factory_new (ibus_bus_get_connection (m_bus));
    g_signal_connect (factory, "create-engine",
                      G_CALLBACK (create_engine_cb), NULL);

    component = ibus_component_new (
            "org.freedesktop.IBus.HangulTest",
            "Hangul Engine Test",
            "1.5.1",
            "GPL",
            "Peng Huang <shawn.p.huang@gmail.com>",
            "https://github.com/ibus/ibus/wiki",
            "",
            "ibus-hangul");
    desc = ibus_engine_desc_new (
            "hangultest",
            "Hangul Test",
            "Hangul Test",
            "en",
            "GPL",
            "Peng Huang <shawn.p.huang@gmail.com>",
            "ibus-hangul",
            "us");
    ibus_component_add_engine (component, desc);
    ibus_bus_register_component (m_bus, component);

    return TRUE;
}

static gboolean
finit (gpointer data)
{
    g_test_incomplete ("time out");
    gtk_main_quit ();
    return FALSE;
}

static void
set_engine_cb (GObject *object, GAsyncResult *res, gpointer data)
{
    IBusBus *bus = IBUS_BUS (object);
    GtkWidget *entry = GTK_WIDGET (data);
    GError *error = NULL;
    gint i;
    const gchar *p = NULL;

    if (!ibus_bus_set_global_engine_async_finish (bus, res, &error)) {
        gchar *msg = g_strdup_printf ("set engine failed: %s", error->message);
        g_test_incomplete (msg);
        g_free (msg);
        g_error_free (error);
        return;
    }

    {
        /* Switch hangul mode. */
        guint keyval = m_switch_hangul[0];
        guint keycode = m_switch_hangul[1];
        guint modifiers = m_switch_hangul[2];
        gboolean retval;

        if (keyval == 0) {
            g_test_incomplete ("ibus-hangul switch key is not set correctly.");
            return;
        }
        g_signal_emit_by_name (m_engine, "process-key-event",
                               keyval, keycode, modifiers, &retval);
        modifiers |= IBUS_RELEASE_MASK;
        sleep(1);
        g_signal_emit_by_name (m_engine, "process-key-event",
                               keyval, keycode, modifiers, &retval);
    }

    {
        /* Run test cases */
        for (i = 0;
             i < G_N_ELEMENTS(m_test_cases);
             i++) {
            for (p = m_test_cases[i].input; *p; p++) {
                gboolean retval;
                guint keyval = *p;
                guint modifiers = 0;
                guint keycode = guess_keycode (keymap, keyval, modifiers);

                if (keyval == 0)
                    break;
                g_signal_emit_by_name (m_engine, "process-key-event",
                                       keyval, keycode, modifiers, &retval);
                modifiers |= IBUS_RELEASE_MASK;
                sleep(1);
                g_signal_emit_by_name (m_engine, "process-key-event",
                                       keyval, keycode, modifiers, &retval);
            }
        }
    }

    g_signal_handlers_disconnect_by_func (entry,
                                          G_CALLBACK (window_focus_in_event_cb),
                                          NULL);
    g_timeout_add_seconds (10, finit, NULL);
}

static gboolean
window_focus_in_event_cb (GtkWidget *entry, GdkEventFocus *event, gpointer data)
{
    g_assert (m_bus != NULL);
    ibus_bus_set_global_engine_async (m_bus,
                                      "hangultest",
                                      -1,
                                      NULL,
                                      set_engine_cb,
                                      entry);
    return FALSE;
}

static void
window_inserted_text_cb (GtkEntryBuffer *buffer,
                         guint           position,
                         const gchar    *chars,
                         guint           nchars,
                         gpointer        data)
{
/* https://gitlab.gnome.org/GNOME/gtk/commit/9981f46e0b
 * The latest GTK does not emit "inserted-text" when the text is "".
 */
#if !GTK_CHECK_VERSION (3, 22, 16)
    static int n_loop = 0;
#endif
    static guint index = 0;
    gunichar code = g_utf8_get_char (chars);
    const gchar *test;
    GtkEntry *entry = GTK_ENTRY (data);

#if !GTK_CHECK_VERSION (3, 22, 16)
    if (n_loop % 2 == 1) {
        n_loop = 0;
        return;
    }
#endif

    {
        /* Run test case */
        const gchar *p = chars;
        const gchar *output = m_test_cases[index].output;
        guint j = 0;
        gboolean valid_output = TRUE;

        if (0 != g_strcmp0 (p, output))
            valid_output = FALSE;
        index++;

        if (valid_output) {
            test = GREEN "PASS" NC;
        } else {
            test = RED "FAIL" NC;
            g_test_fail ();
        }
        g_print ("%05d %s expected: %s typed: %s\n",
                 index, test, output, p);
    }

    if (index == G_N_ELEMENTS(m_test_cases)) {
        gtk_main_quit ();
        return;
    }

#if !GTK_CHECK_VERSION (3, 22, 16)
    n_loop++;
#endif
    gtk_entry_set_text (entry, "");
}

static void
create_window ()
{
    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    GtkWidget *entry = gtk_entry_new ();
    GtkEntryBuffer *buffer;

    g_signal_connect (window, "destroy",
                      G_CALLBACK (gtk_main_quit), NULL);
    g_signal_connect (entry, "focus-in-event",
                      G_CALLBACK (window_focus_in_event_cb), NULL);
    buffer = gtk_entry_get_buffer (GTK_ENTRY (entry));
    g_signal_connect (buffer, "inserted-text",
                      G_CALLBACK (window_inserted_text_cb), entry);
    gtk_container_add (GTK_CONTAINER (window), entry);
    gtk_widget_show_all (window);
}

static void
test_hangul (void)
{
    GLogLevelFlags flags;
    if (!register_ibus_engine ()) {
        g_test_fail ();
        return;
    }

    ibus_hangul_init (m_bus);

    create_window ();
    /* FIXME:
     * IBusIMContext opens GtkIMContextSimple as the slave and
     * GtkIMContextSimple opens the compose table on el_GR.UTF-8, and the
     * multiple outputs in el_GR's compose causes a warning in gtkcomposetable
     * and the warning always causes a fatal in GTest:
     " "GTK+ supports to output one char only: "
     */
    flags = g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
    gtk_main ();
    g_log_set_always_fatal (flags);
}

int
main (int argc, char *argv[])
{
    const gchar *test_name;
    gchar *test_path;

    /* Run test cases with X Window. */
    g_setenv ("GDK_BACKEND", "x11", TRUE);

    ibus_init ();
    /* Avoid a warning of "AT-SPI: Could not obtain desktop path or name"
     * with gtk_main().
     */
    g_setenv ("NO_AT_BRIDGE", "1", TRUE);
    g_test_init (&argc, &argv, NULL);
    gtk_init (&argc, &argv);

    m_srcdir = argc > 1 ? g_strdup (argv[1]) : g_strdup (".");

#if GLIB_CHECK_VERSION (2, 58, 0)
    test_name = g_get_language_names_with_category ("LC_CTYPE")[0];
#else
    test_name = g_getenv ("LANG");
#endif

    test_path = g_build_filename ("/ibus-hangul", test_name, NULL);
    g_test_add_func (test_path, test_hangul);
    g_free (test_path);

    keymap = ibus_keymap_get("us");

    return g_test_run ();
}