2 * Copyright (C) 2009 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.
22 #include <glib/gstdio.h>
23 #include <webkit/webkit.h>
25 #if GTK_CHECK_VERSION(2, 14, 0)
32 static GMainLoop* loop;
35 WebKitWebView* webView;
37 } HitTestResultFixture;
40 test_info_new(const char* data, guint flag)
44 info = g_slice_new(TestInfo);
45 info->data = g_strdup(data);
52 test_info_destroy(TestInfo* info)
55 g_slice_free(TestInfo, info);
58 static void hit_test_result_fixture_setup(HitTestResultFixture* fixture, gconstpointer data)
60 fixture->webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
61 g_object_ref_sink(fixture->webView);
62 loop = g_main_loop_new(NULL, TRUE);
63 fixture->info = (TestInfo*)data;
66 static void hit_test_result_fixture_teardown(HitTestResultFixture* fixture, gconstpointer data)
68 g_object_unref(fixture->webView);
69 g_main_loop_unref(loop);
70 test_info_destroy(fixture->info);
74 load_status_cb(WebKitWebView* webView,
78 WebKitLoadStatus status = webkit_web_view_get_load_status(webView);
79 TestInfo* info = (TestInfo*)data;
81 if (status == WEBKIT_LOAD_FINISHED) {
82 WebKitHitTestResult* result;
84 GdkEvent* event = gdk_event_new(GDK_BUTTON_PRESS);
87 /* Close enough to 0,0 */
91 result = webkit_web_view_get_hit_test_result(webView, (GdkEventButton*) event);
92 gdk_event_free(event);
95 g_object_get(result, "context", &context, NULL);
96 g_assert(context & info->flag);
98 g_object_get(result, "inner-node", &node, NULL);
100 g_assert(WEBKIT_DOM_IS_NODE(node));
101 /* We can only test these node types at the moment. In the
102 * input case there seems to be an extra layer with a DIV on
103 * top of the input, which gets assigned to the inner-node.
105 if (info->flag == WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT)
106 g_assert(WEBKIT_DOM_IS_HTML_HTML_ELEMENT(node));
107 else if (info->flag == WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE)
108 g_assert(WEBKIT_DOM_IS_HTML_IMAGE_ELEMENT(node));
109 else if (info->flag == WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
110 /* The hit test will give us the inner text node, we want
112 WebKitDOMNode* parent = webkit_dom_node_get_parent_node(node);
113 g_assert(WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT(parent));
116 g_object_unref(result);
117 g_main_loop_quit(loop);
122 test_webkit_hit_test_result(HitTestResultFixture* fixture, gconstpointer data)
124 TestInfo* info = (TestInfo*)data;
125 GtkAllocation allocation = { 0, 0, 50, 50 };
127 webkit_web_view_load_string(fixture->webView,
132 gtk_widget_size_allocate(GTK_WIDGET(fixture->webView), &allocation);
133 g_signal_connect(fixture->webView, "notify::load-status", G_CALLBACK(load_status_cb), info);
134 g_main_loop_run(loop);
137 int main(int argc, char** argv)
139 gtk_test_init(&argc, &argv, NULL);
141 g_test_bug_base("https://bugs.webkit.org/");
143 g_test_add("/webkit/hittestresult/document", HitTestResultFixture,
144 test_info_new("<html><body><h1>WebKitGTK+!</h1></body></html>",
145 WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT),
146 hit_test_result_fixture_setup, test_webkit_hit_test_result, hit_test_result_fixture_teardown);
147 /* We hardcode all elements to be at 0,0 so that we know where to
148 * generate the button events */
149 g_test_add("/webkit/hittestresult/image", HitTestResultFixture,
150 test_info_new("<html><body><img style='position:absolute; left:0; top:0'src='0xdeadbeef' width=50 height=50></img></body></html>",
151 WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE),
152 hit_test_result_fixture_setup, test_webkit_hit_test_result, hit_test_result_fixture_teardown);
153 g_test_add("/webkit/hittestresult/editable", HitTestResultFixture,
154 test_info_new("<html><body><input style='position:absolute; left:0; top:0' size='35'></input>></body></html>",
155 WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE),
156 hit_test_result_fixture_setup, test_webkit_hit_test_result, hit_test_result_fixture_teardown);
157 g_test_add("/webkit/hittestresult/link", HitTestResultFixture,
158 test_info_new("<html><body><a style='position:absolute; left:0; top:0' href='http://www.example.com'>HELLO WORLD</a></body></html>",
159 WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK),
160 hit_test_result_fixture_setup, test_webkit_hit_test_result, hit_test_result_fixture_teardown);
162 return g_test_run ();
167 int main(int argc, char** argv)
169 g_critical("You will need at least GTK+ 2.14.0 to run the unit tests.");