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