[GTK] Use custom JavaScript instead of DBus to implement WebProcess tests
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit2Gtk / DOMXPathNSResolverTest.cpp
1 /*
2  * Copyright (C) 2014 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 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 "config.h"
21
22 #include "WebProcessTest.h"
23 #include <gio/gio.h>
24 #include <webkit2/webkit-web-extension.h>
25 #include <wtf/gobject/GUniquePtr.h>
26
27 typedef struct _WebKitXPathNSResolver {
28     GObject parent;
29 } WebKitXPathNSResolver;
30
31 typedef struct _WebKitXPathNSResolverClass {
32     GObjectClass parentClass;
33 } WebKitXPathNSResolverClass;
34
35 static char* webkitXPathNSResolverLookupNamespaceURI(WebKitDOMXPathNSResolver* resolver, const char* prefix)
36 {
37     if (!g_strcmp0(prefix, "foo"))
38         return g_strdup("http://www.example.com");
39
40     return nullptr;
41 }
42
43 static void webkitXPathNSResolverDOMXPathNSResolverIfaceInit(WebKitDOMXPathNSResolverIface* iface)
44 {
45     iface->lookup_namespace_uri = webkitXPathNSResolverLookupNamespaceURI;
46 }
47
48 G_DEFINE_TYPE_WITH_CODE(WebKitXPathNSResolver, webkit_xpath_ns_resolver, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_XPATH_NS_RESOLVER, webkitXPathNSResolverDOMXPathNSResolverIfaceInit))
49
50 static void webkit_xpath_ns_resolver_init(WebKitXPathNSResolver*)
51 {
52 }
53
54 static void webkit_xpath_ns_resolver_class_init(WebKitXPathNSResolverClass*)
55 {
56 }
57
58 class WebKitDOMXPathNSResolverTest : public WebProcessTest {
59 public:
60     static std::unique_ptr<WebProcessTest> create() { return std::unique_ptr<WebProcessTest>(new WebKitDOMXPathNSResolverTest()); }
61
62 private:
63     void evaluateFooChildTextAndCheckResult(WebKitDOMDocument* document, WebKitDOMXPathNSResolver* resolver)
64     {
65         WebKitDOMElement* documentElement = webkit_dom_document_get_document_element(document);
66         g_assert(WEBKIT_DOM_IS_ELEMENT(documentElement));
67
68         WebKitDOMXPathResult* result = webkit_dom_document_evaluate(document, "foo:child/text()", WEBKIT_DOM_NODE(documentElement), resolver, WEBKIT_DOM_XPATH_RESULT_ORDERED_NODE_ITERATOR_TYPE, nullptr, nullptr);
69         g_assert(WEBKIT_DOM_IS_XPATH_RESULT(result));
70
71         WebKitDOMNode* nodeResult = webkit_dom_xpath_result_iterate_next(result, nullptr);
72         g_assert(WEBKIT_DOM_IS_NODE(nodeResult));
73
74         GUniquePtr<char> nodeValue(webkit_dom_node_get_node_value(nodeResult));
75         g_assert_cmpstr(nodeValue.get(), ==, "SUCCESS");
76     }
77
78     bool testXPathNSResolverNative(WebKitWebPage* page)
79     {
80         WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
81         g_assert(WEBKIT_DOM_IS_DOCUMENT(document));
82
83         WebKitDOMXPathNSResolver* resolver = webkit_dom_document_create_ns_resolver(document, WEBKIT_DOM_NODE(webkit_dom_document_get_document_element(document)));
84         g_assert(WEBKIT_DOM_IS_XPATH_NS_RESOLVER(resolver));
85         evaluateFooChildTextAndCheckResult(document, resolver);
86
87         return true;
88     }
89
90     bool testXPathNSResolverCustom(WebKitWebPage* page)
91     {
92         WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
93         g_assert(WEBKIT_DOM_IS_DOCUMENT(document));
94
95         GRefPtr<WebKitDOMXPathNSResolver> resolver = adoptGRef(WEBKIT_DOM_XPATH_NS_RESOLVER(g_object_new(webkit_xpath_ns_resolver_get_type(), nullptr)));
96         evaluateFooChildTextAndCheckResult(document, resolver.get());
97
98         return true;
99     }
100
101     bool runTest(const char* testName, WebKitWebPage* page) override
102     {
103         if (!strcmp(testName, "native"))
104             return testXPathNSResolverNative(page);
105         if (!strcmp(testName, "custom"))
106             return testXPathNSResolverCustom(page);
107
108         g_assert_not_reached();
109         return false;
110     }
111 };
112
113 static void __attribute__((constructor)) registerTests()
114 {
115     REGISTER_TEST(WebKitDOMXPathNSResolverTest, "WebKitDOMXPathNSResolver/native");
116     REGISTER_TEST(WebKitDOMXPathNSResolverTest, "WebKitDOMXPathNSResolver/custom");
117 }