f3ef5cfa947dd24d15a31fcedf16c2f3a7ac5aef
[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 PassOwnPtr<WebProcessTest> create() { return adoptPtr(new WebKitDOMXPathNSResolverTest()); }
61
62 private:
63     guint64 webPageFromArgs(GVariant* args)
64     {
65         GVariantIter iter;
66         g_variant_iter_init(&iter, args);
67
68         const char* key;
69         GVariant* value;
70         while (g_variant_iter_loop(&iter, "{&sv}", &key, &value)) {
71             if (!strcmp(key, "pageID") && g_variant_classify(value) == G_VARIANT_CLASS_UINT64)
72                 return g_variant_get_uint64(value);
73         }
74
75         g_assert_not_reached();
76         return 0;
77     }
78
79     void evaluateFooChildTextAndCheckResult(WebKitDOMDocument* document, WebKitDOMXPathNSResolver* resolver)
80     {
81         WebKitDOMElement* documentElement = webkit_dom_document_get_document_element(document);
82         g_assert(WEBKIT_DOM_IS_ELEMENT(documentElement));
83
84         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);
85         g_assert(WEBKIT_DOM_IS_XPATH_RESULT(result));
86
87         WebKitDOMNode* nodeResult = webkit_dom_xpath_result_iterate_next(result, nullptr);
88         g_assert(WEBKIT_DOM_IS_NODE(nodeResult));
89
90         GUniquePtr<char> nodeValue(webkit_dom_node_get_node_value(nodeResult));
91         g_assert_cmpstr(nodeValue.get(), ==, "SUCCESS");
92     }
93
94     bool testXPathNSResolverNative(WebKitWebExtension* extension, GVariant* args)
95     {
96         WebKitWebPage* page = webkit_web_extension_get_page(extension, webPageFromArgs(args));
97         g_assert(WEBKIT_IS_WEB_PAGE(page));
98         WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
99         g_assert(WEBKIT_DOM_IS_DOCUMENT(document));
100
101         WebKitDOMXPathNSResolver* resolver = webkit_dom_document_create_ns_resolver(document, WEBKIT_DOM_NODE(webkit_dom_document_get_document_element(document)));
102         g_assert(WEBKIT_DOM_IS_XPATH_NS_RESOLVER(resolver));
103         evaluateFooChildTextAndCheckResult(document, resolver);
104
105         return true;
106     }
107
108     bool testXPathNSResolverCustom(WebKitWebExtension* extension, GVariant* args)
109     {
110         WebKitWebPage* page = webkit_web_extension_get_page(extension, webPageFromArgs(args));
111         g_assert(WEBKIT_IS_WEB_PAGE(page));
112         WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
113         g_assert(WEBKIT_DOM_IS_DOCUMENT(document));
114
115         GRefPtr<WebKitDOMXPathNSResolver> resolver = adoptGRef(WEBKIT_DOM_XPATH_NS_RESOLVER(g_object_new(webkit_xpath_ns_resolver_get_type(), nullptr)));
116         evaluateFooChildTextAndCheckResult(document, resolver.get());
117
118         return true;
119     }
120
121     virtual bool runTest(const char* testName, WebKitWebExtension* extension, GVariant* args)
122     {
123         if (!strcmp(testName, "native"))
124             return testXPathNSResolverNative(extension, args);
125         if (!strcmp(testName, "custom"))
126             return testXPathNSResolverCustom(extension, args);
127
128         g_assert_not_reached();
129         return false;
130     }
131 };
132
133 static void __attribute__((constructor)) registerTests()
134 {
135     REGISTER_TEST(WebKitDOMXPathNSResolverTest, "WebKitDOMXPathNSResolver/native");
136     REGISTER_TEST(WebKitDOMXPathNSResolverTest, "WebKitDOMXPathNSResolver/custom");
137 }