summaryrefslogtreecommitdiffstats
path: root/WebKit/gtk/tests/testkeyevents.c
blob: b41a0328204e158090869d7d846c4e9df1b0212a (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
/*
 * Copyright (C) 2009, 2010 Martin Robinson <mrobinson@webkit.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2,1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <glib/gstdio.h>
#include <webkit/webkit.h>
#include <JavaScriptCore/JSStringRef.h>
#include <JavaScriptCore/JSContextRef.h>


#if GTK_CHECK_VERSION(2, 14, 0)

typedef struct {
    char* page;
    char* text;
    gboolean shouldBeHandled;
} TestInfo;

typedef struct {
    GtkWidget* window;
    WebKitWebView* webView;
    GMainLoop* loop;
    TestInfo* info;
} KeyEventFixture;

TestInfo*
test_info_new(const char* page, gboolean shouldBeHandled)
{
    TestInfo* info;

    info = g_slice_new(TestInfo);
    info->page = g_strdup(page);
    info->shouldBeHandled = shouldBeHandled;
    info->text = 0;

    return info;
}

void
test_info_destroy(TestInfo* info)
{
    g_free(info->page);
    g_free(info->text);
    g_slice_free(TestInfo, info);
}

static void key_event_fixture_setup(KeyEventFixture* fixture, gconstpointer data)
{
    fixture->loop = g_main_loop_new(NULL, TRUE);

    fixture->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    fixture->webView = WEBKIT_WEB_VIEW(webkit_web_view_new());

    gtk_container_add(GTK_CONTAINER(fixture->window), GTK_WIDGET(fixture->webView));
}

static void key_event_fixture_teardown(KeyEventFixture* fixture, gconstpointer data)
{
    gtk_widget_destroy(fixture->window);
    g_main_loop_unref(fixture->loop);
    test_info_destroy(fixture->info);
}

static gboolean key_press_event_cb(WebKitWebView* webView, GdkEvent* event, gpointer data)
{
    KeyEventFixture* fixture = (KeyEventFixture*)data;
    gboolean handled = GTK_WIDGET_GET_CLASS(fixture->webView)->key_press_event(GTK_WIDGET(fixture->webView), &event->key);
    g_assert_cmpint(handled, ==, fixture->info->shouldBeHandled);

    return FALSE;
}

static gboolean key_release_event_cb(WebKitWebView* webView, GdkEvent* event, gpointer data)
{
    // WebCore never seems to mark keyup events as handled.
    KeyEventFixture* fixture = (KeyEventFixture*)data;
    gboolean handled = GTK_WIDGET_GET_CLASS(fixture->webView)->key_press_event(GTK_WIDGET(fixture->webView), &event->key);
    g_assert(!handled);

    g_main_loop_quit(fixture->loop);

    return FALSE;
}

static void test_keypress_events_load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
{
    KeyEventFixture* fixture = (KeyEventFixture*)data;
    WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
    if (status == WEBKIT_LOAD_FINISHED) {
        g_signal_connect(fixture->webView, "key-press-event",
                         G_CALLBACK(key_press_event_cb), fixture);
        g_signal_connect(fixture->webView, "key-release-event",
                         G_CALLBACK(key_release_event_cb), fixture);
        if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                                      gdk_unicode_to_keyval('a'), 0))
            g_assert_not_reached();
    }

}

gboolean map_event_cb(GtkWidget *widget, GdkEvent* event, gpointer data)
{
    gtk_widget_grab_focus(widget);
    KeyEventFixture* fixture = (KeyEventFixture*)data;
    webkit_web_view_load_string(fixture->webView, fixture->info->page,
                                "text/html", "utf-8", "file://");
    return FALSE;
}

static void setup_keyevent_test(KeyEventFixture* fixture, gconstpointer data, GCallback load_event_callback)
{
    fixture->info = (TestInfo*)data;
    g_signal_connect(fixture->window, "map-event",
                     G_CALLBACK(map_event_cb), fixture);

    gtk_widget_show(fixture->window);
    gtk_widget_show(GTK_WIDGET(fixture->webView));
    gtk_window_present(GTK_WINDOW(fixture->window));

    g_signal_connect(fixture->webView, "notify::load-status",
                     load_event_callback, fixture);

    g_main_loop_run(fixture->loop);
}

static void test_keypress_events(KeyEventFixture* fixture, gconstpointer data)
{
    setup_keyevent_test(fixture, data, G_CALLBACK(test_keypress_events_load_status_cb));
}

static gboolean element_text_equal_to(JSContextRef context, const gchar* text)
{
    JSStringRef scriptString = JSStringCreateWithUTF8CString(
      "window.document.getElementById(\"in\").value;");
    JSValueRef value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0);
    JSStringRelease(scriptString);

    // If the value isn't a string, the element is probably a div
    // so grab the innerText instead.
    if (!JSValueIsString(context, value)) {
        JSStringRef scriptString = JSStringCreateWithUTF8CString(
          "window.document.getElementById(\"in\").innerText;");
        value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0);
        JSStringRelease(scriptString);
    }

    g_assert(JSValueIsString(context, value));
    JSStringRef inputString = JSValueToStringCopy(context, value, 0);
    g_assert(inputString);

    gint size = JSStringGetMaximumUTF8CStringSize(inputString);
    gchar* cString = g_malloc(size);
    JSStringGetUTF8CString(inputString, cString, size);
    JSStringRelease(inputString);

    gboolean result = g_utf8_collate(cString, text) == 0;
    g_free(cString);
    return result;
}

static void test_ime_load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
{
    KeyEventFixture* fixture = (KeyEventFixture*)data;
    WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
    if (status != WEBKIT_LOAD_FINISHED)
        return;

    JSGlobalContextRef context = webkit_web_frame_get_global_context(
        webkit_web_view_get_main_frame(webView));
    g_assert(context);

    GtkIMContext* imContext = 0;
    g_object_get(webView, "im-context", &imContext, NULL);
    g_assert(imContext);

    // Test that commits that happen outside of key events
    // change the text field immediately. This closely replicates
    // the behavior of SCIM.
    g_assert(element_text_equal_to(context, ""));
    g_signal_emit_by_name(imContext, "commit", "a");
    g_assert(element_text_equal_to(context, "a"));
    g_signal_emit_by_name(imContext, "commit", "b");
    g_assert(element_text_equal_to(context, "ab"));
    g_signal_emit_by_name(imContext, "commit", "c");
    g_assert(element_text_equal_to(context, "abc"));

    g_object_unref(imContext);
    g_main_loop_quit(fixture->loop);
}

static void test_ime(KeyEventFixture* fixture, gconstpointer data)
{
    setup_keyevent_test(fixture, data, G_CALLBACK(test_ime_load_status_cb));
}

static gboolean verify_contents(gpointer data)
{
    KeyEventFixture* fixture = (KeyEventFixture*)data;
    JSGlobalContextRef context = webkit_web_frame_get_global_context(
        webkit_web_view_get_main_frame(fixture->webView));
    g_assert(context);

    g_assert(element_text_equal_to(context, fixture->info->text));
    g_main_loop_quit(fixture->loop);
    return FALSE;
}

static void test_blocking_load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
{
    KeyEventFixture* fixture = (KeyEventFixture*)data;
    WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
    if (status != WEBKIT_LOAD_FINISHED)
        return;

    // The first keypress event should not modify the field.
    fixture->info->text = g_strdup("bc");
    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                                 gdk_unicode_to_keyval('a'), 0))
        g_assert_not_reached();
    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                                  gdk_unicode_to_keyval('b'), 0))
        g_assert_not_reached();
    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                                  gdk_unicode_to_keyval('c'), 0))
        g_assert_not_reached();

    g_idle_add(verify_contents, fixture);
}

static void test_blocking(KeyEventFixture* fixture, gconstpointer data)
{
    setup_keyevent_test(fixture, data, G_CALLBACK(test_blocking_load_status_cb));
}

#if defined(GDK_WINDOWING_X11) && GTK_CHECK_VERSION(2, 16, 0)
static void test_xim_load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
{
    KeyEventFixture* fixture = (KeyEventFixture*)data;
    WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
    if (status != WEBKIT_LOAD_FINISHED)
        return;

    GtkIMContext* imContext = 0;
    g_object_get(webView, "im-context", &imContext, NULL);
    g_assert(imContext);

    gchar* originalId = g_strdup(gtk_im_multicontext_get_context_id(GTK_IM_MULTICONTEXT(imContext)));
    gtk_im_multicontext_set_context_id(GTK_IM_MULTICONTEXT(imContext), "xim");

    // Test that commits that happen outside of key events
    // change the text field immediately. This closely replicates
    // the behavior of SCIM.
    fixture->info->text = g_strdup("debian");
    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                                 gdk_unicode_to_keyval('d'), 0))
        g_assert_not_reached();
    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                             gdk_unicode_to_keyval('e'), 0))
        g_assert_not_reached();
    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                             gdk_unicode_to_keyval('b'), 0))
        g_assert_not_reached();
    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                             gdk_unicode_to_keyval('i'), 0))
        g_assert_not_reached();
    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                             gdk_unicode_to_keyval('a'), 0))
        g_assert_not_reached();
    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
                             gdk_unicode_to_keyval('n'), 0))
        g_assert_not_reached();

    gtk_im_multicontext_set_context_id(GTK_IM_MULTICONTEXT(imContext), originalId);
    g_free(originalId);
    g_object_unref(imContext);

    g_idle_add(verify_contents, fixture);
}

static void test_xim(KeyEventFixture* fixture, gconstpointer data)
{
    setup_keyevent_test(fixture, data, G_CALLBACK(test_xim_load_status_cb));
}
#endif

int main(int argc, char** argv)
{
    g_thread_init(NULL);
    gtk_test_init(&argc, &argv, NULL);

    g_test_bug_base("https://bugs.webkit.org/");


    // We'll test input on a slew of different node types. Key events to
    // text inputs and editable divs should be marked as handled. Key events
    // to buttons and links should not.
    const char* textinput_html = "<html><body><input id=\"in\" type=\"text\">"
        "<script>document.getElementById('in').focus();</script></body></html>";
    const char* button_html = "<html><body><input id=\"in\" type=\"button\">"
        "<script>document.getElementById('in').focus();</script></body></html>";
    const char* link_html = "<html><body><a href=\"http://www.gnome.org\" id=\"in\">"
        "LINKY MCLINKERSON</a><script>document.getElementById('in').focus();</script>"
        "</body></html>";
    const char* div_html = "<html><body><div id=\"in\" contenteditable=\"true\">"
        "<script>document.getElementById('in').focus();</script></body></html>";

    // These are similar to the blocks above, but they should block the first
    // keypress modifying the editable node.
    const char* textinput_html_blocking = "<html><body>"
        "<input id=\"in\" type=\"text\" "
        "onkeypress=\"if (first) {event.preventDefault();first=false;}\">"
        "<script>first = true;\ndocument.getElementById('in').focus();</script>\n"
        "</script></body></html>";
    const char* div_html_blocking = "<html><body>"
        "<div id=\"in\" contenteditable=\"true\" "
        "onkeypress=\"if (first) {event.preventDefault();first=false;}\">"
        "<script>first = true; document.getElementById('in').focus();</script>\n"
        "</script></body></html>";

    g_test_add("/webkit/keyevents/event-textinput", KeyEventFixture,
               test_info_new(textinput_html, TRUE),
               key_event_fixture_setup,
               test_keypress_events,
               key_event_fixture_teardown);
    g_test_add("/webkit/keyevents/event-buttons", KeyEventFixture,
               test_info_new(button_html, FALSE),
               key_event_fixture_setup,
               test_keypress_events,
               key_event_fixture_teardown);
    g_test_add("/webkit/keyevents/event-link", KeyEventFixture,
               test_info_new(link_html, FALSE),
               key_event_fixture_setup,
               test_keypress_events,
               key_event_fixture_teardown);
    g_test_add("/webkit/keyevent/event-div", KeyEventFixture,
               test_info_new(div_html, TRUE),
               key_event_fixture_setup,
               test_keypress_events,
               key_event_fixture_teardown);
    g_test_add("/webkit/keyevent/ime-textinput", KeyEventFixture,
               test_info_new(textinput_html, TRUE),
               key_event_fixture_setup,
               test_ime,
               key_event_fixture_teardown);
    g_test_add("/webkit/keyevent/ime-div", KeyEventFixture,
               test_info_new(div_html, TRUE),
               key_event_fixture_setup,
               test_ime,
               key_event_fixture_teardown);
    g_test_add("/webkit/keyevent/block-textinput", KeyEventFixture,
               test_info_new(textinput_html_blocking, TRUE),
               key_event_fixture_setup,
               test_blocking,
               key_event_fixture_teardown);
    g_test_add("/webkit/keyevent/block-div", KeyEventFixture,
               test_info_new(div_html_blocking, TRUE),
               key_event_fixture_setup,
               test_blocking,
               key_event_fixture_teardown);
#if defined(GDK_WINDOWING_X11) && GTK_CHECK_VERSION(2, 16, 0)
    g_test_add("/webkit/keyevent/xim-textinput", KeyEventFixture,
               test_info_new(textinput_html, TRUE),
               key_event_fixture_setup,
               test_xim,
               key_event_fixture_teardown);
    g_test_add("/webkit/keyevent/xim-div", KeyEventFixture,
               test_info_new(div_html, TRUE),
               key_event_fixture_setup,
               test_xim,
               key_event_fixture_teardown);
#endif

    return g_test_run();
}

#else

int main(int argc, char** argv)
{
    g_critical("You will need at least GTK+ 2.14.0 to run the unit tests.");
    return 0;
}

#endif