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/glib/GUniquePtr.h>
26
27G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
28
29typedef struct _WebKitXPathNSResolver {
30 GObject parent;
31} WebKitXPathNSResolver;
32
33typedef struct _WebKitXPathNSResolverClass {
34 GObjectClass parentClass;
35} WebKitXPathNSResolverClass;
36
37static char* webkitXPathNSResolverLookupNamespaceURI(WebKitDOMXPathNSResolver* resolver, const char* prefix)
38{
39 if (!g_strcmp0(prefix, "foo"))
40 return g_strdup("http://www.example.com");
41
42 return nullptr;
43}
44
45static void webkitXPathNSResolverDOMXPathNSResolverIfaceInit(WebKitDOMXPathNSResolverIface* iface)
46{
47 iface->lookup_namespace_uri = webkitXPathNSResolverLookupNamespaceURI;
48}
49
50G_DEFINE_TYPE_WITH_CODE(WebKitXPathNSResolver, webkit_xpath_ns_resolver, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_XPATH_NS_RESOLVER, webkitXPathNSResolverDOMXPathNSResolverIfaceInit))
51
52static void webkit_xpath_ns_resolver_init(WebKitXPathNSResolver*)
53{
54}
55
56static void webkit_xpath_ns_resolver_class_init(WebKitXPathNSResolverClass*)
57{
58}
59
60class WebKitDOMXPathNSResolverTest : public WebProcessTest {
61public:
62 static std::unique_ptr<WebProcessTest> create() { return std::unique_ptr<WebProcessTest>(new WebKitDOMXPathNSResolverTest()); }
63
64private:
65 void evaluateFooChildTextAndCheckResult(WebKitDOMDocument* document, WebKitDOMXPathNSResolver* resolver)
66 {
67 WebKitDOMElement* documentElement = webkit_dom_document_get_document_element(document);
68 g_assert_true(WEBKIT_DOM_IS_ELEMENT(documentElement));
69 assertObjectIsDeletedWhenTestFinishes(G_OBJECT(documentElement));
70
71 GRefPtr<WebKitDOMXPathResult> result = adoptGRef(webkit_dom_document_evaluate(document, "foo:child/text()", WEBKIT_DOM_NODE(documentElement), resolver, WEBKIT_DOM_XPATH_RESULT_ORDERED_NODE_ITERATOR_TYPE, nullptr, nullptr));
72 g_assert_true(WEBKIT_DOM_IS_XPATH_RESULT(result.get()));
73 assertObjectIsDeletedWhenTestFinishes(G_OBJECT(result.get()));
74
75 WebKitDOMNode* nodeResult = webkit_dom_xpath_result_iterate_next(result.get(), nullptr);
76 g_assert_true(WEBKIT_DOM_IS_NODE(nodeResult));
77 assertObjectIsDeletedWhenTestFinishes(G_OBJECT(nodeResult));
78
79 GUniquePtr<char> nodeValue(webkit_dom_node_get_node_value(nodeResult));
80 g_assert_cmpstr(nodeValue.get(), ==, "SUCCESS");
81 }
82
83 bool testXPathNSResolverNative(WebKitWebPage* page)
84 {
85 WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
86 g_assert_true(WEBKIT_DOM_IS_DOCUMENT(document));
87 assertObjectIsDeletedWhenTestFinishes(G_OBJECT(document));
88
89 GRefPtr<WebKitDOMXPathNSResolver> resolver = adoptGRef(webkit_dom_document_create_ns_resolver(document, WEBKIT_DOM_NODE(webkit_dom_document_get_document_element(document))));
90 g_assert_true(WEBKIT_DOM_IS_XPATH_NS_RESOLVER(resolver.get()));
91 assertObjectIsDeletedWhenTestFinishes(G_OBJECT(resolver.get()));
92 evaluateFooChildTextAndCheckResult(document, resolver.get());
93
94 return true;
95 }
96
97 bool testXPathNSResolverCustom(WebKitWebPage* page)
98 {
99 WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
100 g_assert_true(WEBKIT_DOM_IS_DOCUMENT(document));
101 assertObjectIsDeletedWhenTestFinishes(G_OBJECT(document));
102
103 GRefPtr<WebKitDOMXPathNSResolver> resolver = adoptGRef(WEBKIT_DOM_XPATH_NS_RESOLVER(g_object_new(webkit_xpath_ns_resolver_get_type(), nullptr)));
104 assertObjectIsDeletedWhenTestFinishes(G_OBJECT(resolver.get()));
105 evaluateFooChildTextAndCheckResult(document, resolver.get());
106
107 return true;
108 }
109
110 bool runTest(const char* testName, WebKitWebPage* page) override
111 {
112 if (!strcmp(testName, "native"))
113 return testXPathNSResolverNative(page);
114 if (!strcmp(testName, "custom"))
115 return testXPathNSResolverCustom(page);
116
117 g_assert_not_reached();
118 return false;
119 }
120};
121
122static void __attribute__((constructor)) registerTests()
123{
124 REGISTER_TEST(WebKitDOMXPathNSResolverTest, "WebKitDOMXPathNSResolver/native");
125 REGISTER_TEST(WebKitDOMXPathNSResolverTest, "WebKitDOMXPathNSResolver/custom");
126}
127
128G_GNUC_END_IGNORE_DEPRECATIONS;
129