summaryrefslogtreecommitdiffstats
path: root/WebKit/gtk/tests/testcopyandpaste.c
blob: 0ef91d1074ee6475e61fe67e28454aec9b5a55e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright (C) 2010 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2,1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <glib/gstdio.h>
#include <webkit/webkit.h>
#include <JavaScriptCore/JSStringRef.h>
#include <JavaScriptCore/JSContextRef.h>


#if GTK_CHECK_VERSION(2, 14, 0)

typedef struct {
    char* page;
    char* expectedContent;
} TestInfo;

typedef struct {
    GtkWidget* window;
    WebKitWebView* webView;
    GMainLoop* loop;
    TestInfo* info;
} CopyAndPasteFixture;

TestInfo*
test_info_new(const char* page, const char* expectedContent)
{
    TestInfo* info;
    info = g_slice_new0(TestInfo);
    info->page = g_strdup(page);
    if (expectedContent)
        info->expectedContent = g_strdup(expectedContent);
    return info;
}

void
test_info_destroy(TestInfo* info)
{
    g_free(info->page);
    g_free(info->expectedContent);
    g_slice_free(TestInfo, info);
}

static void copy_and_paste_fixture_setup(CopyAndPasteFixture* fixture, gconstpointer data)
{
    fixture->loop = g_main_loop_new(NULL, TRUE);

    fixture->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    fixture->webView = WEBKIT_WEB_VIEW(webkit_web_view_new());

    gtk_container_add(GTK_CONTAINER(fixture->window), GTK_WIDGET(fixture->webView));
}

static void copy_and_paste_fixture_teardown(CopyAndPasteFixture* fixture, gconstpointer data)
{
    gtk_widget_destroy(fixture->window);
    g_main_loop_unref(fixture->loop);
    test_info_destroy(fixture->info);
}

static void load_status_cb(WebKitWebView* webView, GParamSpec* spec, gpointer data)
{
    CopyAndPasteFixture* fixture = (CopyAndPasteFixture*)data;
    WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
    if (status != WEBKIT_LOAD_FINISHED)
        return;

    GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    gtk_clipboard_clear(clipboard);

    webkit_web_view_copy_clipboard(webView);

    gchar* text = gtk_clipboard_wait_for_text(clipboard);
    g_assert(text || !fixture->info->expectedContent);
    g_assert(!text || !strcmp(text, fixture->info->expectedContent));
    g_free(text);

    g_assert(!gtk_clipboard_wait_is_uris_available(clipboard));
    g_assert(!gtk_clipboard_wait_is_image_available(clipboard));

    g_main_loop_quit(fixture->loop);
}

gboolean map_event_cb(GtkWidget *widget, GdkEvent* event, gpointer data)
{
    gtk_widget_grab_focus(widget);
    CopyAndPasteFixture* fixture = (CopyAndPasteFixture*)data;
    webkit_web_view_load_string(fixture->webView, fixture->info->page,
                                "text/html", "utf-8", "file://");
    return FALSE;
}

static void test_copy_and_paste(CopyAndPasteFixture* fixture, gconstpointer data)
{
    fixture->info = (TestInfo*)data;
    g_signal_connect(fixture->window, "map-event",
                     G_CALLBACK(map_event_cb), fixture);

    gtk_widget_show(fixture->window);
    gtk_widget_show(GTK_WIDGET(fixture->webView));
    gtk_window_present(GTK_WINDOW(fixture->window));

    g_signal_connect(fixture->webView, "notify::load-status",
                     G_CALLBACK(load_status_cb), fixture);

    g_main_loop_run(fixture->loop);
}

static CopyAndPasteFixture* currentFixture;
static JSValueRef runPasteTestCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    // Simulate a paste keyboard sequence.
    GdkEvent event;
    memset(&event, 0, sizeof(event));
    event.key.keyval = gdk_unicode_to_keyval('v');
    event.key.state = GDK_CONTROL_MASK;
    event.key.window = gtk_widget_get_window(GTK_WIDGET(currentFixture->webView));
    GdkKeymapKey* keys;
    gint n_keys;
    if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), event.key.keyval, &keys, &n_keys)) {
        event.key.hardware_keycode = keys[0].keycode;
        g_free(keys);
    }
    event.key.type = GDK_KEY_PRESS;
    gtk_main_do_event(&event);
    event.key.type = GDK_KEY_RELEASE;
    gtk_main_do_event(&event);

    JSStringRef scriptString = JSStringCreateWithUTF8CString("document.body.innerHTML;");
    JSValueRef value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0);
    JSStringRelease(scriptString);

    g_assert(JSValueIsString(context, value));
    JSStringRef actual = JSValueToStringCopy(context, value, exception);
    g_assert(!exception || !*exception);
    g_assert(currentFixture->info->expectedContent);
    JSStringRef expected = JSStringCreateWithUTF8CString(currentFixture->info->expectedContent);
    g_assert(JSStringIsEqual(expected, actual));

    JSStringRelease(expected);
    JSStringRelease(actual);
    g_main_loop_quit(currentFixture->loop);
    return JSValueMakeUndefined(context);
}

static void window_object_cleared_callback(WebKitWebView* web_view, WebKitWebFrame* web_frame, JSGlobalContextRef context, JSObjectRef window_object, gpointer data)
{
    JSStringRef name = JSStringCreateWithUTF8CString("runTest");
    JSObjectRef testComplete = JSObjectMakeFunctionWithCallback(context, name, runPasteTestCallback);
    JSObjectSetProperty(context, window_object, name, testComplete, kJSPropertyAttributeNone, 0);
    JSStringRelease(name);
}

static void pasting_test_get_data_callback(GtkClipboard* clipboard, GtkSelectionData* selection_data, guint info, gpointer data)
{
    gtk_selection_data_set(selection_data, gdk_atom_intern("text/html", FALSE), 8, (const guchar*) data, strlen((char*)data) + 1);
}

static void pasting_test_clear_data_callback(GtkClipboard* clipboard, gpointer data)
{
    g_free(data);
}

static void test_pasting_markup(CopyAndPasteFixture* fixture, gconstpointer data)
{
    fixture->info = (TestInfo*)data;
    currentFixture = fixture;

    GtkTargetList* targetList = gtk_target_list_new(0, 0);
    gtk_target_list_add(targetList, gdk_atom_intern("text/html", FALSE), 0, 0);

    int numberOfTargets = 1;
    GtkTargetEntry* targetTable = gtk_target_table_new_from_list(targetList, &numberOfTargets);
    gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
                                targetTable, numberOfTargets,
                                pasting_test_get_data_callback,
                                pasting_test_clear_data_callback,
                                g_strdup(fixture->info->expectedContent));
    gtk_target_list_unref(targetList);
    gtk_target_table_free(targetTable, numberOfTargets);

    g_signal_connect(fixture->window, "map-event",
                     G_CALLBACK(map_event_cb), fixture);
    g_signal_connect(fixture->webView, "window-object-cleared",
                     G_CALLBACK(window_object_cleared_callback), fixture);

    gtk_widget_show(fixture->window);
    gtk_widget_show(GTK_WIDGET(fixture->webView));
    gtk_window_present(GTK_WINDOW(fixture->window));

    g_main_loop_run(fixture->loop);
}


int main(int argc, char** argv)
{
    g_thread_init(NULL);
    gtk_test_init(&argc, &argv, NULL);

    g_test_bug_base("https://bugs.webkit.org/");
    const char* selected_span_html = "<html><body>"
        "<span id=\"mainspan\">All work and no play <span>make Jack a dull</span> boy.</span>"
        "<script>document.getSelection().collapse();\n"
        "document.getSelection().selectAllChildren(document.getElementById('mainspan'));\n"
        "</script></body></html>";
    const char* no_selection_html = "<html><body>"
        "<span id=\"mainspan\">All work and no play <span>make Jack a dull</span> boy</span>"
        "<script>document.getSelection().collapse();\n"
        "</script></body></html>";

    g_test_add("/webkit/copyandpaste/selection", CopyAndPasteFixture,
               test_info_new(selected_span_html, "All work and no play make Jack a dull boy."),
               copy_and_paste_fixture_setup,
               test_copy_and_paste,
               copy_and_paste_fixture_teardown);
    g_test_add("/webkit/copyandpaste/no-selection", CopyAndPasteFixture,
               test_info_new(no_selection_html, 0),
               copy_and_paste_fixture_setup,
               test_copy_and_paste,
               copy_and_paste_fixture_teardown);

    const char* paste_test_html = "<html>"
        "<body onLoad=\"document.body.focus(); runTest();\" contentEditable=\"true\">"
        "</body></html>";
    g_test_add("/webkit/copyandpaste/paste-markup", CopyAndPasteFixture,
               test_info_new(paste_test_html, "bobby"),
               copy_and_paste_fixture_setup,
               test_pasting_markup,
               copy_and_paste_fixture_teardown);

    return g_test_run();
}

#else

int main(int argc, char** argv)
{
    g_critical("You will need at least GTK+ 2.14.0 to run the unit tests.");
    return 0;
}

#endif