1/*
2 * Copyright (C) 2009, 2010 Martin Robinson <mrobinson@webkit.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2,1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "autotoolsconfig.h"
21#include <errno.h>
22#include <unistd.h>
23#include <string.h>
24#include <glib/gstdio.h>
25#include <webkit/webkit.h>
26#include <JavaScriptCore/JSStringRef.h>
27#include <JavaScriptCore/JSContextRef.h>
28
29typedef struct {
30    char* page;
31    char* text;
32    gboolean shouldBeHandled;
33} TestInfo;
34
35typedef struct {
36    GtkWidget* window;
37    WebKitWebView* webView;
38    GMainLoop* loop;
39    TestInfo* info;
40} KeyEventFixture;
41
42TestInfo*
43test_info_new(const char* page, gboolean shouldBeHandled)
44{
45    TestInfo* info;
46
47    info = g_slice_new(TestInfo);
48    info->page = g_strdup(page);
49    info->shouldBeHandled = shouldBeHandled;
50    info->text = 0;
51
52    return info;
53}
54
55void
56test_info_destroy(TestInfo* info)
57{
58    g_free(info->page);
59    g_free(info->text);
60    g_slice_free(TestInfo, info);
61}
62
63static void key_event_fixture_setup(KeyEventFixture* fixture, gconstpointer data)
64{
65    fixture->loop = g_main_loop_new(NULL, TRUE);
66
67    fixture->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
68    fixture->webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
69
70    gtk_container_add(GTK_CONTAINER(fixture->window), GTK_WIDGET(fixture->webView));
71}
72
73static void key_event_fixture_teardown(KeyEventFixture* fixture, gconstpointer data)
74{
75    gtk_widget_destroy(fixture->window);
76    g_main_loop_unref(fixture->loop);
77    test_info_destroy(fixture->info);
78}
79
80static gboolean key_press_event_cb(WebKitWebView* webView, GdkEvent* event, gpointer data)
81{
82    KeyEventFixture* fixture = (KeyEventFixture*)data;
83    gboolean handled = GTK_WIDGET_GET_CLASS(fixture->webView)->key_press_event(GTK_WIDGET(fixture->webView), &event->key);
84    g_assert_cmpint(handled, ==, fixture->info->shouldBeHandled);
85
86    return FALSE;
87}
88
89static gboolean key_release_event_cb(WebKitWebView* webView, GdkEvent* event, gpointer data)
90{
91    // WebCore never seems to mark keyup events as handled.
92    KeyEventFixture* fixture = (KeyEventFixture*)data;
93    gboolean handled = GTK_WIDGET_GET_CLASS(fixture->webView)->key_press_event(GTK_WIDGET(fixture->webView), &event->key);
94    g_assert(!handled);
95
96    g_main_loop_quit(fixture->loop);
97
98    return FALSE;
99}
100
101static void test_keypress_events_load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
102{
103    KeyEventFixture* fixture = (KeyEventFixture*)data;
104    WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
105    if (status == WEBKIT_LOAD_FINISHED) {
106        g_signal_connect(fixture->webView, "key-press-event",
107                         G_CALLBACK(key_press_event_cb), fixture);
108        g_signal_connect(fixture->webView, "key-release-event",
109                         G_CALLBACK(key_release_event_cb), fixture);
110        if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
111                                      gdk_unicode_to_keyval('a'), 0))
112            g_assert_not_reached();
113    }
114
115}
116
117gboolean map_event_cb(GtkWidget *widget, GdkEvent* event, gpointer data)
118{
119    KeyEventFixture* fixture = (KeyEventFixture*)data;
120    webkit_web_view_load_string(fixture->webView, fixture->info->page,
121                                "text/html", "utf-8", "file://");
122    return FALSE;
123}
124
125static void setup_keyevent_test(KeyEventFixture* fixture, gconstpointer data, GCallback load_event_callback)
126{
127    fixture->info = (TestInfo*)data;
128    g_signal_connect(fixture->window, "map-event",
129                     G_CALLBACK(map_event_cb), fixture);
130
131    gtk_widget_grab_focus(GTK_WIDGET(fixture->webView));
132    gtk_widget_show(fixture->window);
133    gtk_widget_show(GTK_WIDGET(fixture->webView));
134    gtk_window_present(GTK_WINDOW(fixture->window));
135
136    g_signal_connect(fixture->webView, "notify::load-status",
137                     load_event_callback, fixture);
138
139    g_main_loop_run(fixture->loop);
140}
141
142static void test_keypress_events(KeyEventFixture* fixture, gconstpointer data)
143{
144    setup_keyevent_test(fixture, data, G_CALLBACK(test_keypress_events_load_status_cb));
145}
146
147static gboolean element_text_equal_to(JSContextRef context, const gchar* text)
148{
149    JSStringRef scriptString = JSStringCreateWithUTF8CString(
150      "window.document.getElementById(\"in\").value;");
151    JSValueRef value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0);
152    JSStringRelease(scriptString);
153
154    // If the value isn't a string, the element is probably a div
155    // so grab the innerText instead.
156    if (!JSValueIsString(context, value)) {
157        JSStringRef scriptString = JSStringCreateWithUTF8CString(
158          "window.document.getElementById(\"in\").innerText;");
159        value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0);
160        JSStringRelease(scriptString);
161    }
162
163    g_assert(JSValueIsString(context, value));
164    JSStringRef inputString = JSValueToStringCopy(context, value, 0);
165    g_assert(inputString);
166
167    gint size = JSStringGetMaximumUTF8CStringSize(inputString);
168    gchar* cString = g_malloc(size);
169    JSStringGetUTF8CString(inputString, cString, size);
170    JSStringRelease(inputString);
171
172    gboolean result = g_utf8_collate(cString, text) == 0;
173    g_free(cString);
174    return result;
175}
176
177static void test_ime_load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
178{
179    KeyEventFixture* fixture = (KeyEventFixture*)data;
180    WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
181    if (status != WEBKIT_LOAD_FINISHED)
182        return;
183
184    JSGlobalContextRef context = webkit_web_frame_get_global_context(
185        webkit_web_view_get_main_frame(webView));
186    g_assert(context);
187
188    GtkIMContext* imContext = 0;
189    g_object_get(webView, "im-context", &imContext, NULL);
190    g_assert(imContext);
191
192    // Test that commits that happen outside of key events
193    // change the text field immediately. This closely replicates
194    // the behavior of SCIM.
195    g_assert(element_text_equal_to(context, ""));
196    g_signal_emit_by_name(imContext, "commit", "a");
197    g_assert(element_text_equal_to(context, "a"));
198    g_signal_emit_by_name(imContext, "commit", "b");
199    g_assert(element_text_equal_to(context, "ab"));
200    g_signal_emit_by_name(imContext, "commit", "c");
201    g_assert(element_text_equal_to(context, "abc"));
202
203    g_object_unref(imContext);
204    g_main_loop_quit(fixture->loop);
205}
206
207static void test_ime(KeyEventFixture* fixture, gconstpointer data)
208{
209    setup_keyevent_test(fixture, data, G_CALLBACK(test_ime_load_status_cb));
210}
211
212static gboolean verify_contents(gpointer data)
213{
214    KeyEventFixture* fixture = (KeyEventFixture*)data;
215    JSGlobalContextRef context = webkit_web_frame_get_global_context(
216        webkit_web_view_get_main_frame(fixture->webView));
217    g_assert(context);
218
219    g_assert(element_text_equal_to(context, fixture->info->text));
220    g_main_loop_quit(fixture->loop);
221    return FALSE;
222}
223
224static void test_blocking_load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
225{
226    KeyEventFixture* fixture = (KeyEventFixture*)data;
227    WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
228    if (status != WEBKIT_LOAD_FINISHED)
229        return;
230
231    // The first keypress event should not modify the field.
232    fixture->info->text = g_strdup("bc");
233    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
234                                 gdk_unicode_to_keyval('a'), 0))
235        g_assert_not_reached();
236    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
237                                  gdk_unicode_to_keyval('b'), 0))
238        g_assert_not_reached();
239    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
240                                  gdk_unicode_to_keyval('c'), 0))
241        g_assert_not_reached();
242
243    g_idle_add(verify_contents, fixture);
244}
245
246static void test_blocking(KeyEventFixture* fixture, gconstpointer data)
247{
248    setup_keyevent_test(fixture, data, G_CALLBACK(test_blocking_load_status_cb));
249}
250
251#if defined(GDK_WINDOWING_X11)
252static void test_xim_load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
253{
254    KeyEventFixture* fixture = (KeyEventFixture*)data;
255    WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
256    if (status != WEBKIT_LOAD_FINISHED)
257        return;
258
259    GtkIMContext* imContext = 0;
260    g_object_get(webView, "im-context", &imContext, NULL);
261    g_assert(imContext);
262
263    gchar* originalId = g_strdup(gtk_im_multicontext_get_context_id(GTK_IM_MULTICONTEXT(imContext)));
264    gtk_im_multicontext_set_context_id(GTK_IM_MULTICONTEXT(imContext), "xim");
265
266    // Test that commits that happen outside of key events
267    // change the text field immediately. This closely replicates
268    // the behavior of SCIM.
269    fixture->info->text = g_strdup("debian");
270    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
271                                 gdk_unicode_to_keyval('d'), 0))
272        g_assert_not_reached();
273    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
274                             gdk_unicode_to_keyval('e'), 0))
275        g_assert_not_reached();
276    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
277                             gdk_unicode_to_keyval('b'), 0))
278        g_assert_not_reached();
279    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
280                             gdk_unicode_to_keyval('i'), 0))
281        g_assert_not_reached();
282    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
283                             gdk_unicode_to_keyval('a'), 0))
284        g_assert_not_reached();
285    if (!gtk_test_widget_send_key(GTK_WIDGET(fixture->webView),
286                             gdk_unicode_to_keyval('n'), 0))
287        g_assert_not_reached();
288
289    gtk_im_multicontext_set_context_id(GTK_IM_MULTICONTEXT(imContext), originalId);
290    g_free(originalId);
291    g_object_unref(imContext);
292
293    g_idle_add(verify_contents, fixture);
294}
295
296static void test_xim(KeyEventFixture* fixture, gconstpointer data)
297{
298    setup_keyevent_test(fixture, data, G_CALLBACK(test_xim_load_status_cb));
299}
300#endif
301
302int main(int argc, char** argv)
303{
304    gtk_test_init(&argc, &argv, NULL);
305
306    g_test_bug_base("https://bugs.webkit.org/");
307
308
309    // We'll test input on a slew of different node types. Key events to
310    // text inputs and editable divs should be marked as handled. Key events
311    // to buttons and links should not.
312    const char* textinput_html = "<html><body><input id=\"in\" type=\"text\">"
313        "<script>document.getElementById('in').focus();</script></body></html>";
314    const char* button_html = "<html><body><input id=\"in\" type=\"button\">"
315        "<script>document.getElementById('in').focus();</script></body></html>";
316    const char* link_html = "<html><body><a href=\"http://www.gnome.org\" id=\"in\">"
317        "LINKY MCLINKERSON</a><script>document.getElementById('in').focus();</script>"
318        "</body></html>";
319    const char* div_html = "<html><body><div id=\"in\" contenteditable=\"true\">"
320        "<script>document.getElementById('in').focus();</script></body></html>";
321
322    // These are similar to the blocks above, but they should block the first
323    // keypress modifying the editable node.
324    const char* textinput_html_blocking = "<html><body>"
325        "<input id=\"in\" type=\"text\" "
326        "onkeypress=\"if (first) {event.preventDefault();first=false;}\">"
327        "<script>first = true;\ndocument.getElementById('in').focus();</script>\n"
328        "</script></body></html>";
329    const char* div_html_blocking = "<html><body>"
330        "<div id=\"in\" contenteditable=\"true\" "
331        "onkeypress=\"if (first) {event.preventDefault();first=false;}\">"
332        "<script>first = true; document.getElementById('in').focus();</script>\n"
333        "</script></body></html>";
334
335    g_test_add("/webkit/keyevents/event-textinput", KeyEventFixture,
336               test_info_new(textinput_html, TRUE),
337               key_event_fixture_setup,
338               test_keypress_events,
339               key_event_fixture_teardown);
340    g_test_add("/webkit/keyevents/event-buttons", KeyEventFixture,
341               test_info_new(button_html, FALSE),
342               key_event_fixture_setup,
343               test_keypress_events,
344               key_event_fixture_teardown);
345    g_test_add("/webkit/keyevents/event-link", KeyEventFixture,
346               test_info_new(link_html, FALSE),
347               key_event_fixture_setup,
348               test_keypress_events,
349               key_event_fixture_teardown);
350    g_test_add("/webkit/keyevent/event-div", KeyEventFixture,
351               test_info_new(div_html, TRUE),
352               key_event_fixture_setup,
353               test_keypress_events,
354               key_event_fixture_teardown);
355    g_test_add("/webkit/keyevent/ime-textinput", KeyEventFixture,
356               test_info_new(textinput_html, TRUE),
357               key_event_fixture_setup,
358               test_ime,
359               key_event_fixture_teardown);
360    g_test_add("/webkit/keyevent/ime-div", KeyEventFixture,
361               test_info_new(div_html, TRUE),
362               key_event_fixture_setup,
363               test_ime,
364               key_event_fixture_teardown);
365    g_test_add("/webkit/keyevent/block-textinput", KeyEventFixture,
366               test_info_new(textinput_html_blocking, TRUE),
367               key_event_fixture_setup,
368               test_blocking,
369               key_event_fixture_teardown);
370    g_test_add("/webkit/keyevent/block-div", KeyEventFixture,
371               test_info_new(div_html_blocking, TRUE),
372               key_event_fixture_setup,
373               test_blocking,
374               key_event_fixture_teardown);
375#if defined(GDK_WINDOWING_X11)
376    g_test_add("/webkit/keyevent/xim-textinput", KeyEventFixture,
377               test_info_new(textinput_html, TRUE),
378               key_event_fixture_setup,
379               test_xim,
380               key_event_fixture_teardown);
381    g_test_add("/webkit/keyevent/xim-div", KeyEventFixture,
382               test_info_new(div_html, TRUE),
383               key_event_fixture_setup,
384               test_xim,
385               key_event_fixture_teardown);
386#endif
387
388    return g_test_run();
389}
390
391