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 // Verify that the markup starts with the proper content-type meta tag prefix. 96 GtkSelectionData* selectionData = gtk_clipboard_wait_for_contents(clipboard, gdk_atom_intern("text/html", FALSE)); 97 if (selectionData) { 98 static const char* markupPrefix = "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"; 99 char* markup = g_strndup((const char*) gtk_selection_data_get_data(selectionData), 100 gtk_selection_data_get_length(selectionData)); 101 g_assert(strlen(markupPrefix) <= strlen(markup)); 102 g_assert(!strncmp(markupPrefix, markup, strlen(markupPrefix))); 103 g_free(markup); 104 } 105 106 g_assert(!gtk_clipboard_wait_is_uris_available(clipboard)); 107 g_assert(!gtk_clipboard_wait_is_image_available(clipboard)); 108 109 g_main_loop_quit(fixture->loop); 110 } 111 112 gboolean map_event_cb(GtkWidget *widget, GdkEvent* event, gpointer data) 113 { 114 gtk_widget_grab_focus(widget); 115 CopyAndPasteFixture* fixture = (CopyAndPasteFixture*)data; 116 webkit_web_view_load_string(fixture->webView, fixture->info->page, 117 "text/html", "utf-8", "file://"); 118 return FALSE; 119 } 120 121 static void test_copy_and_paste(CopyAndPasteFixture* fixture, gconstpointer data) 122 { 123 fixture->info = (TestInfo*)data; 124 g_signal_connect(fixture->window, "map-event", 125 G_CALLBACK(map_event_cb), fixture); 126 127 gtk_widget_show(fixture->window); 128 gtk_widget_show(GTK_WIDGET(fixture->webView)); 129 gtk_window_present(GTK_WINDOW(fixture->window)); 130 131 g_signal_connect(fixture->webView, "notify::load-status", 132 G_CALLBACK(load_status_cb), fixture); 133 134 g_main_loop_run(fixture->loop); 135 } 136 137 static CopyAndPasteFixture* currentFixture; 138 static JSValueRef runPasteTestCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) 139 { 140 // Simulate a paste keyboard sequence. 141 GdkEvent* event = gdk_event_new(GDK_KEY_PRESS); 142 event->key.keyval = gdk_unicode_to_keyval('v'); 143 event->key.state = GDK_CONTROL_MASK; 144 event->key.window = gtk_widget_get_window(GTK_WIDGET(currentFixture->webView)); 145 g_object_ref(event->key.window); 146 #ifndef GTK_API_VERSION_2 147 GdkDeviceManager* manager = gdk_display_get_device_manager(gdk_window_get_display(event->key.window)); 148 gdk_event_set_device(event, gdk_device_manager_get_client_pointer(manager)); 149 #endif 150 151 GdkKeymapKey* keys; 152 gint n_keys; 153 if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), event->key.keyval, &keys, &n_keys)) { 154 event->key.hardware_keycode = keys[0].keycode; 155 g_free(keys); 156 } 157 158 gtk_main_do_event(event); 159 event->key.type = GDK_KEY_RELEASE; 160 gtk_main_do_event(event); 161 gdk_event_free(event); 162 163 JSStringRef scriptString = JSStringCreateWithUTF8CString("document.body.innerHTML;"); 164 JSValueRef value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0); 165 JSStringRelease(scriptString); 166 167 g_assert(JSValueIsString(context, value)); 168 JSStringRef actual = JSValueToStringCopy(context, value, exception); 169 g_assert(!exception || !*exception); 170 g_assert(currentFixture->info->expectedContent); 171 JSStringRef expected = JSStringCreateWithUTF8CString(currentFixture->info->expectedContent); 172 g_assert(JSStringIsEqual(expected, actual)); 173 174 JSStringRelease(expected); 175 JSStringRelease(actual); 176 g_main_loop_quit(currentFixture->loop); 177 return JSValueMakeUndefined(context); 178 } 179 180 static void window_object_cleared_callback(WebKitWebView* web_view, WebKitWebFrame* web_frame, JSGlobalContextRef context, JSObjectRef window_object, gpointer data) 181 { 182 JSStringRef name = JSStringCreateWithUTF8CString("runTest"); 183 JSObjectRef testComplete = JSObjectMakeFunctionWithCallback(context, name, runPasteTestCallback); 184 JSObjectSetProperty(context, window_object, name, testComplete, kJSPropertyAttributeNone, 0); 185 JSStringRelease(name); 186 } 187 188 static void pasting_test_get_data_callback(GtkClipboard* clipboard, GtkSelectionData* selection_data, guint info, gpointer data) 189 { 190 gtk_selection_data_set(selection_data, gdk_atom_intern("text/html", FALSE), 8, (const guchar*) data, strlen((char*)data) + 1); 191 } 192 193 static void pasting_test_clear_data_callback(GtkClipboard* clipboard, gpointer data) 194 { 195 g_free(data); 196 } 197 198 static void test_pasting_markup(CopyAndPasteFixture* fixture, gconstpointer data) 199 { 200 fixture->info = (TestInfo*)data; 201 currentFixture = fixture; 202 203 GtkTargetList* targetList = gtk_target_list_new(0, 0); 204 gtk_target_list_add(targetList, gdk_atom_intern("text/html", FALSE), 0, 0); 205 206 int numberOfTargets = 1; 207 GtkTargetEntry* targetTable = gtk_target_table_new_from_list(targetList, &numberOfTargets); 208 gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), 209 targetTable, numberOfTargets, 210 pasting_test_get_data_callback, 211 pasting_test_clear_data_callback, 212 g_strdup(fixture->info->expectedContent)); 213 gtk_target_list_unref(targetList); 214 gtk_target_table_free(targetTable, numberOfTargets); 215 216 g_signal_connect(fixture->window, "map-event", 217 G_CALLBACK(map_event_cb), fixture); 218 g_signal_connect(fixture->webView, "window-object-cleared", 219 G_CALLBACK(window_object_cleared_callback), fixture); 220 221 gtk_widget_show(fixture->window); 222 gtk_widget_show(GTK_WIDGET(fixture->webView)); 223 gtk_window_present(GTK_WINDOW(fixture->window)); 224 225 g_main_loop_run(fixture->loop); 226 } 227 228 229 int main(int argc, char** argv) 230 { 231 g_thread_init(NULL); 232 gtk_test_init(&argc, &argv, NULL); 233 234 g_test_bug_base("https://bugs.webkit.org/"); 235 const char* selected_span_html = "<html><body>" 236 "<span id=\"mainspan\">All work and no play <span>make Jack a dull</span> boy.</span>" 237 "<script>document.getSelection().collapse();\n" 238 "document.getSelection().selectAllChildren(document.getElementById('mainspan'));\n" 239 "</script></body></html>"; 240 const char* no_selection_html = "<html><body>" 241 "<span id=\"mainspan\">All work and no play <span>make Jack a dull</span> boy</span>" 242 "<script>document.getSelection().collapse();\n" 243 "</script></body></html>"; 244 245 g_test_add("/webkit/copyandpaste/selection", CopyAndPasteFixture, 246 test_info_new(selected_span_html, "All work and no play make Jack a dull boy."), 247 copy_and_paste_fixture_setup, 248 test_copy_and_paste, 249 copy_and_paste_fixture_teardown); 250 g_test_add("/webkit/copyandpaste/no-selection", CopyAndPasteFixture, 251 test_info_new(no_selection_html, 0), 252 copy_and_paste_fixture_setup, 253 test_copy_and_paste, 254 copy_and_paste_fixture_teardown); 255 256 const char* paste_test_html = "<html>" 257 "<body onLoad=\"document.body.focus(); runTest();\" contentEditable=\"true\">" 258 "</body></html>"; 259 g_test_add("/webkit/copyandpaste/paste-markup", CopyAndPasteFixture, 260 test_info_new(paste_test_html, "bobby"), 261 copy_and_paste_fixture_setup, 262 test_pasting_markup, 263 copy_and_paste_fixture_teardown); 264 265 return g_test_run(); 266 } 267 268 #else 269 270 int main(int argc, char** argv) 271 { 272 g_critical("You will need at least GTK+ 2.14.0 to run the unit tests."); 273 return 0; 274 } 275 276 #endif 277