2 * Copyright (C) 2010 Igalia S.L.
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.
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.
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.
23 #include <glib/gstdio.h>
24 #include <webkit/webkit.h>
25 #include <JavaScriptCore/JSStringRef.h>
26 #include <JavaScriptCore/JSContextRef.h>
29 #if GTK_CHECK_VERSION(2, 14, 0)
33 char* expectedContent;
38 WebKitWebView* webView;
41 } CopyAndPasteFixture;
44 test_info_new(const char* page, const char* expectedContent)
47 info = g_slice_new0(TestInfo);
48 info->page = g_strdup(page);
50 info->expectedContent = g_strdup(expectedContent);
55 test_info_destroy(TestInfo* info)
58 g_free(info->expectedContent);
59 g_slice_free(TestInfo, info);
62 static void copy_and_paste_fixture_setup(CopyAndPasteFixture* fixture, gconstpointer data)
64 fixture->loop = g_main_loop_new(NULL, TRUE);
66 fixture->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
67 fixture->webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
69 gtk_container_add(GTK_CONTAINER(fixture->window), GTK_WIDGET(fixture->webView));
72 static void copy_and_paste_fixture_teardown(CopyAndPasteFixture* fixture, gconstpointer data)
74 gtk_widget_destroy(fixture->window);
75 g_main_loop_unref(fixture->loop);
76 test_info_destroy(fixture->info);
79 static void load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
81 CopyAndPasteFixture* fixture = (CopyAndPasteFixture*)data;
82 WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
83 if (status != WEBKIT_LOAD_FINISHED)
86 GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
87 gtk_clipboard_clear(clipboard);
89 webkit_web_view_copy_clipboard(webView);
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));
96 g_assert(!gtk_clipboard_wait_is_uris_available(clipboard));
97 g_assert(!gtk_clipboard_wait_is_image_available(clipboard));
99 g_main_loop_quit(fixture->loop);
102 gboolean map_event_cb(GtkWidget *widget, GdkEvent* event, gpointer data)
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://");
111 static void test_copy_and_paste(CopyAndPasteFixture* fixture, gconstpointer data)
113 fixture->info = (TestInfo*)data;
114 g_signal_connect(fixture->window, "map-event",
115 G_CALLBACK(map_event_cb), fixture);
117 gtk_widget_show(fixture->window);
118 gtk_widget_show(GTK_WIDGET(fixture->webView));
119 gtk_window_present(GTK_WINDOW(fixture->window));
121 g_signal_connect(fixture->webView, "notify::load-status",
122 G_CALLBACK(load_status_cb), fixture);
124 g_main_loop_run(fixture->loop);
127 static CopyAndPasteFixture* currentFixture;
128 static JSValueRef runPasteTestCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
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))));
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;
146 gtk_main_do_event(event);
147 event->key.type = GDK_KEY_RELEASE;
148 gtk_main_do_event(event);
149 gdk_event_free(event);
151 JSStringRef scriptString = JSStringCreateWithUTF8CString("document.body.innerHTML;");
152 JSValueRef value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0);
153 JSStringRelease(scriptString);
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));
162 JSStringRelease(expected);
163 JSStringRelease(actual);
164 g_main_loop_quit(currentFixture->loop);
165 return JSValueMakeUndefined(context);
168 static void window_object_cleared_callback(WebKitWebView* web_view, WebKitWebFrame* web_frame, JSGlobalContextRef context, JSObjectRef window_object, gpointer data)
170 JSStringRef name = JSStringCreateWithUTF8CString("runTest");
171 JSObjectRef testComplete = JSObjectMakeFunctionWithCallback(context, name, runPasteTestCallback);
172 JSObjectSetProperty(context, window_object, name, testComplete, kJSPropertyAttributeNone, 0);
173 JSStringRelease(name);
176 static void pasting_test_get_data_callback(GtkClipboard* clipboard, GtkSelectionData* selection_data, guint info, gpointer data)
178 gtk_selection_data_set(selection_data, gdk_atom_intern("text/html", FALSE), 8, (const guchar*) data, strlen((char*)data) + 1);
181 static void pasting_test_clear_data_callback(GtkClipboard* clipboard, gpointer data)
186 static void test_pasting_markup(CopyAndPasteFixture* fixture, gconstpointer data)
188 fixture->info = (TestInfo*)data;
189 currentFixture = fixture;
191 GtkTargetList* targetList = gtk_target_list_new(0, 0);
192 gtk_target_list_add(targetList, gdk_atom_intern("text/html", FALSE), 0, 0);
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);
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);
209 gtk_widget_show(fixture->window);
210 gtk_widget_show(GTK_WIDGET(fixture->webView));
211 gtk_window_present(GTK_WINDOW(fixture->window));
213 g_main_loop_run(fixture->loop);
217 int main(int argc, char** argv)
220 gtk_test_init(&argc, &argv, NULL);
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>";
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,
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,
242 copy_and_paste_fixture_teardown);
244 const char* paste_test_html = "<html>"
245 "<body onLoad=\"document.body.focus(); runTest();\" contentEditable=\"true\">"
247 g_test_add("/webkit/copyandpaste/paste-markup", CopyAndPasteFixture,
248 test_info_new(paste_test_html, "bobby"),
249 copy_and_paste_fixture_setup,
251 copy_and_paste_fixture_teardown);
258 int main(int argc, char** argv)
260 g_critical("You will need at least GTK+ 2.14.0 to run the unit tests.");