2010-08-30 Alejandro G. Castro <alex@igalia.com>
[WebKit-https.git] / WebKit / gtk / tests / testcopyandpaste.c
1 /*
2  * Copyright (C) 2010 Igalia S.L.
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 <errno.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <glib/gstdio.h>
24 #include <webkit/webkit.h>
25 #include <JavaScriptCore/JSStringRef.h>
26 #include <JavaScriptCore/JSContextRef.h>
27
28 #if GTK_CHECK_VERSION(2, 14, 0)
29
30 typedef struct {
31     char* page;
32     char* expectedContent;
33 } TestInfo;
34
35 typedef struct {
36     GtkWidget* window;
37     WebKitWebView* webView;
38     GMainLoop* loop;
39     TestInfo* info;
40 } CopyAndPasteFixture;
41
42 TestInfo*
43 test_info_new(const char* page, const char* expectedContent)
44 {
45     TestInfo* info;
46     info = g_slice_new0(TestInfo);
47     info->page = g_strdup(page);
48     if (expectedContent)
49         info->expectedContent = g_strdup(expectedContent);
50     return info;
51 }
52
53 void
54 test_info_destroy(TestInfo* info)
55 {
56     g_free(info->page);
57     g_free(info->expectedContent);
58     g_slice_free(TestInfo, info);
59 }
60
61 static void copy_and_paste_fixture_setup(CopyAndPasteFixture* fixture, gconstpointer data)
62 {
63     fixture->loop = g_main_loop_new(NULL, TRUE);
64
65     fixture->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
66     fixture->webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
67
68     gtk_container_add(GTK_CONTAINER(fixture->window), GTK_WIDGET(fixture->webView));
69 }
70
71 static void copy_and_paste_fixture_teardown(CopyAndPasteFixture* fixture, gconstpointer data)
72 {
73     gtk_widget_destroy(fixture->window);
74     g_main_loop_unref(fixture->loop);
75     test_info_destroy(fixture->info);
76 }
77
78 static void load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
79 {
80     CopyAndPasteFixture* fixture = (CopyAndPasteFixture*)data;
81     WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
82     if (status != WEBKIT_LOAD_FINISHED)
83         return;
84
85     GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
86     gtk_clipboard_clear(clipboard);
87
88     webkit_web_view_copy_clipboard(webView);
89
90     gchar* text = gtk_clipboard_wait_for_text(clipboard);
91     g_assert(text || !fixture->info->expectedContent);
92     g_assert(!text || !strcmp(text, fixture->info->expectedContent));
93     g_free(text);
94
95     g_assert(!gtk_clipboard_wait_is_uris_available(clipboard));
96     g_assert(!gtk_clipboard_wait_is_image_available(clipboard));
97
98     g_main_loop_quit(fixture->loop);
99 }
100
101 gboolean map_event_cb(GtkWidget *widget, GdkEvent* event, gpointer data)
102 {
103     gtk_widget_grab_focus(widget);
104     CopyAndPasteFixture* fixture = (CopyAndPasteFixture*)data;
105     webkit_web_view_load_string(fixture->webView, fixture->info->page,
106                                 "text/html", "utf-8", "file://");
107     return FALSE;
108 }
109
110 static void test_copy_and_paste(CopyAndPasteFixture* fixture, gconstpointer data)
111 {
112     fixture->info = (TestInfo*)data;
113     g_signal_connect(fixture->window, "map-event",
114                      G_CALLBACK(map_event_cb), fixture);
115
116     gtk_widget_show(fixture->window);
117     gtk_widget_show(GTK_WIDGET(fixture->webView));
118     gtk_window_present(GTK_WINDOW(fixture->window));
119
120     g_signal_connect(fixture->webView, "notify::load-status",
121                      G_CALLBACK(load_status_cb), fixture);
122
123     g_main_loop_run(fixture->loop);
124 }
125
126 static CopyAndPasteFixture* currentFixture;
127 static JSValueRef runPasteTestCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
128 {
129     // Simulate a paste keyboard sequence.
130     GdkEvent* event = gdk_event_new(GDK_KEY_PRESS);
131     event->key.keyval = gdk_unicode_to_keyval('v');
132     event->key.state = GDK_CONTROL_MASK;
133     event->key.window = gtk_widget_get_window(GTK_WIDGET(currentFixture->webView));
134     g_object_ref(event->key.window);
135 #ifndef GTK_API_VERSION_2
136     GdkDeviceManager* manager =  gdk_display_get_device_manager(gdk_drawable_get_display(event->key.window));
137     gdk_event_set_device(event, gdk_device_manager_get_client_pointer(manager));
138 #endif
139
140     GdkKeymapKey* keys;
141     gint n_keys;
142     if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), event->key.keyval, &keys, &n_keys)) {
143         event->key.hardware_keycode = keys[0].keycode;
144         g_free(keys);
145     }
146
147     gtk_main_do_event(event);
148     event->key.type = GDK_KEY_RELEASE;
149     gtk_main_do_event(event);
150     gdk_event_free(event);
151
152     JSStringRef scriptString = JSStringCreateWithUTF8CString("document.body.innerHTML;");
153     JSValueRef value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0);
154     JSStringRelease(scriptString);
155
156     g_assert(JSValueIsString(context, value));
157     JSStringRef actual = JSValueToStringCopy(context, value, exception);
158     g_assert(!exception || !*exception);
159     g_assert(currentFixture->info->expectedContent);
160     JSStringRef expected = JSStringCreateWithUTF8CString(currentFixture->info->expectedContent);
161     g_assert(JSStringIsEqual(expected, actual));
162
163     JSStringRelease(expected);
164     JSStringRelease(actual);
165     g_main_loop_quit(currentFixture->loop);
166     return JSValueMakeUndefined(context);
167 }
168
169 static void window_object_cleared_callback(WebKitWebView* web_view, WebKitWebFrame* web_frame, JSGlobalContextRef context, JSObjectRef window_object, gpointer data)
170 {
171     JSStringRef name = JSStringCreateWithUTF8CString("runTest");
172     JSObjectRef testComplete = JSObjectMakeFunctionWithCallback(context, name, runPasteTestCallback);
173     JSObjectSetProperty(context, window_object, name, testComplete, kJSPropertyAttributeNone, 0);
174     JSStringRelease(name);
175 }
176
177 static void pasting_test_get_data_callback(GtkClipboard* clipboard, GtkSelectionData* selection_data, guint info, gpointer data)
178 {
179     gtk_selection_data_set(selection_data, gdk_atom_intern("text/html", FALSE), 8, (const guchar*) data, strlen((char*)data) + 1);
180 }
181
182 static void pasting_test_clear_data_callback(GtkClipboard* clipboard, gpointer data)
183 {
184     g_free(data);
185 }
186
187 static void test_pasting_markup(CopyAndPasteFixture* fixture, gconstpointer data)
188 {
189     fixture->info = (TestInfo*)data;
190     currentFixture = fixture;
191
192     GtkTargetList* targetList = gtk_target_list_new(0, 0);
193     gtk_target_list_add(targetList, gdk_atom_intern("text/html", FALSE), 0, 0);
194
195     int numberOfTargets = 1;
196     GtkTargetEntry* targetTable = gtk_target_table_new_from_list(targetList, &numberOfTargets);
197     gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
198                                 targetTable, numberOfTargets,
199                                 pasting_test_get_data_callback,
200                                 pasting_test_clear_data_callback,
201                                 g_strdup(fixture->info->expectedContent));
202     gtk_target_list_unref(targetList);
203     gtk_target_table_free(targetTable, numberOfTargets);
204
205     g_signal_connect(fixture->window, "map-event",
206                      G_CALLBACK(map_event_cb), fixture);
207     g_signal_connect(fixture->webView, "window-object-cleared",
208                      G_CALLBACK(window_object_cleared_callback), fixture);
209
210     gtk_widget_show(fixture->window);
211     gtk_widget_show(GTK_WIDGET(fixture->webView));
212     gtk_window_present(GTK_WINDOW(fixture->window));
213
214     g_main_loop_run(fixture->loop);
215 }
216
217
218 int main(int argc, char** argv)
219 {
220     g_thread_init(NULL);
221     gtk_test_init(&argc, &argv, NULL);
222
223     g_test_bug_base("https://bugs.webkit.org/");
224     const char* selected_span_html = "<html><body>"
225         "<span id=\"mainspan\">All work and no play <span>make Jack a dull</span> boy.</span>"
226         "<script>document.getSelection().collapse();\n"
227         "document.getSelection().selectAllChildren(document.getElementById('mainspan'));\n"
228         "</script></body></html>";
229     const char* no_selection_html = "<html><body>"
230         "<span id=\"mainspan\">All work and no play <span>make Jack a dull</span> boy</span>"
231         "<script>document.getSelection().collapse();\n"
232         "</script></body></html>";
233
234     g_test_add("/webkit/copyandpaste/selection", CopyAndPasteFixture,
235                test_info_new(selected_span_html, "All work and no play make Jack a dull boy."),
236                copy_and_paste_fixture_setup,
237                test_copy_and_paste,
238                copy_and_paste_fixture_teardown);
239     g_test_add("/webkit/copyandpaste/no-selection", CopyAndPasteFixture,
240                test_info_new(no_selection_html, 0),
241                copy_and_paste_fixture_setup,
242                test_copy_and_paste,
243                copy_and_paste_fixture_teardown);
244
245     const char* paste_test_html = "<html>"
246         "<body onLoad=\"document.body.focus(); runTest();\" contentEditable=\"true\">"
247         "</body></html>";
248     g_test_add("/webkit/copyandpaste/paste-markup", CopyAndPasteFixture,
249                test_info_new(paste_test_html, "bobby"),
250                copy_and_paste_fixture_setup,
251                test_pasting_markup,
252                copy_and_paste_fixture_teardown);
253
254     return g_test_run();
255 }
256
257 #else
258
259 int main(int argc, char** argv)
260 {
261     g_critical("You will need at least GTK+ 2.14.0 to run the unit tests.");
262     return 0;
263 }
264
265 #endif