1/*
2 * Copyright (C) 2012 Gustavo Noronha Silva <gns@gnome.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 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 "autotoolsconfig.h"
21#include "test_utils.h"
22
23#include <string.h>
24#include <glib.h>
25#include <gtk/gtk.h>
26#include <webkit/webkit.h>
27
28GMainLoop *loop;
29GtkWidget *window;
30
31static gboolean quitLoop(gpointer data)
32{
33    g_main_loop_quit(loop);
34    return TRUE;
35}
36
37/* Ignore simple translation-related messages and upgrade other
38 * messages to warnings.
39 */
40static gboolean consoleMessageCallback(WebKitWebView* webView, const char* message, unsigned int line, const char* sourceId)
41{
42    if (strstr(message, "Localized string") || strstr(message, "Protocol Error: the message is for non-existing domain 'Profiler'"))
43        return TRUE;
44
45    g_warning("Console: %s @%d: %s\n", sourceId, line, message);
46    return TRUE;
47}
48
49static WebKitWebView* inspectElementCallback(WebKitWebInspector *inspector, WebKitWebView *inspectedWebView, int *timesElementInspected)
50{
51    *timesElementInspected = *timesElementInspected + 1;
52
53    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
54
55    GtkWidget *newWebView = webkit_web_view_new();
56    gtk_container_add(GTK_CONTAINER(window), newWebView);
57
58    g_signal_connect(newWebView, "console-message",
59                     G_CALLBACK(consoleMessageCallback), NULL);
60
61    return WEBKIT_WEB_VIEW(newWebView);
62}
63
64static gboolean closeInspector(WebKitWebInspector *inspector, int *timesClosed)
65{
66    *timesClosed = *timesClosed + 1;
67
68    gtk_widget_destroy(window);
69    return TRUE;
70}
71
72static gboolean showInspector(WebKitWebInspector *inspector, gpointer data)
73{
74    g_idle_add(quitLoop, NULL);
75    return TRUE;
76}
77
78static void loadFinished(WebKitWebView *webView, WebKitWebFrame *frame, gboolean *isLoadFinished)
79{
80    *isLoadFinished = TRUE;
81    if (g_main_loop_is_running(loop))
82        g_main_loop_quit(loop);
83}
84
85static void test_webkit_web_inspector_close_and_inspect()
86{
87    WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
88    loop = g_main_loop_new(NULL, TRUE);
89
90    gboolean isLoadFinished = FALSE;
91    g_signal_connect(webView, "load-finished", G_CALLBACK(loadFinished), &isLoadFinished);
92    webkit_web_view_load_string(webView,
93                                "<html><body><p>woohoo</p></body></html>",
94                                "text/html", "UTF-8", "file://");
95    if (!isLoadFinished)
96        g_main_loop_run(loop);
97
98    g_object_set(webkit_web_view_get_settings(webView), "enable-developer-extras", TRUE, NULL);
99    WebKitWebInspector *inspector = webkit_web_view_get_inspector(webView);
100
101    int timesElementInspected = 0;
102    int timesClosed = 0;
103    g_object_connect(inspector,
104                     "signal::inspect-web-view", G_CALLBACK(inspectElementCallback), &timesElementInspected,
105                     "signal::show-window", G_CALLBACK(showInspector), NULL,
106                     "signal::close-window", G_CALLBACK(closeInspector), &timesClosed,
107                     NULL);
108
109    webkit_web_inspector_inspect_coordinates(inspector, 0.0, 0.0);
110    g_assert_cmpint(timesElementInspected, ==, 1);
111
112    g_main_loop_run(loop);
113
114    webkit_web_inspector_close(inspector);
115    g_assert_cmpint(timesClosed, ==, 1);
116
117    webkit_web_inspector_inspect_coordinates(inspector, 0.0, 0.0);
118    g_assert_cmpint(timesElementInspected, ==, 2);
119
120    g_main_loop_run(loop);
121
122    gtk_widget_destroy(GTK_WIDGET(webView));
123    g_assert_cmpint(timesClosed, ==, 2);
124
125    g_main_loop_unref(loop);
126}
127
128static void test_webkit_web_inspector_destroy_inspected_web_view()
129{
130    WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
131    loop = g_main_loop_new(NULL, TRUE);
132
133    gboolean isLoadFinished = FALSE;
134    g_signal_connect(webView, "load-finished", G_CALLBACK(loadFinished), &isLoadFinished);
135    webkit_web_view_load_string(webView,
136                                "<html><body><p>woohoo</p></body></html>",
137                                "text/html", "UTF-8", "file://");
138    if (!isLoadFinished)
139        g_main_loop_run(loop);
140
141    g_object_set(webkit_web_view_get_settings(webView), "enable-developer-extras", TRUE, NULL);
142    WebKitWebInspector *inspector = webkit_web_view_get_inspector(webView);
143
144    int timesElementInspected = 0;
145    int timesClosed = 0;
146    g_object_connect(inspector,
147                     "signal::inspect-web-view", G_CALLBACK(inspectElementCallback), &timesElementInspected,
148                     "signal::show-window", G_CALLBACK(showInspector), NULL,
149                     "signal::close-window", G_CALLBACK(closeInspector), &timesClosed,
150                     NULL);
151
152    webkit_web_inspector_inspect_coordinates(inspector, 0.0, 0.0);
153    g_assert_cmpint(timesElementInspected, ==, 1);
154
155    g_main_loop_run(loop);
156
157    gtk_widget_destroy(GTK_WIDGET(webView));
158    g_assert_cmpint(timesClosed, ==, 1);
159
160    g_main_loop_unref(loop);
161}
162
163int main(int argc, char** argv)
164{
165    gtk_test_init(&argc, &argv, NULL);
166
167    g_test_bug_base("https://bugs.webkit.org/");
168    g_test_add_func("/webkit/webinspector/destroy-inspected-web-view", test_webkit_web_inspector_destroy_inspected_web_view);
169    g_test_add_func("/webkit/webinspector/close-and-inspect", test_webkit_web_inspector_close_and_inspect);
170
171    return g_test_run();
172}
173