File: FrameTest.cpp

package info (click to toggle)
wpewebkit 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 421,720 kB
  • sloc: cpp: 3,670,389; javascript: 194,411; ansic: 165,592; python: 46,476; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; java: 1,993; sh: 1,948; lex: 1,327; pascal: 366; makefile: 85
file content (216 lines) | stat: -rw-r--r-- 9,434 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Copyright (C) 2013 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include "WebProcessTest.h"
#include <gio/gio.h>
#include <wtf/glib/GUniquePtr.h>

class WebKitFrameTest : public WebProcessTest {
public:
    static std::unique_ptr<WebProcessTest> create() { return std::unique_ptr<WebProcessTest>(new WebKitFrameTest()); }

private:
    bool testMainFrame(WebKitWebPage* page)
    {
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS
        WebKitFrame* frame = webkit_web_page_get_main_frame(page);
        G_GNUC_END_IGNORE_DEPRECATIONS
        g_assert_true(WEBKIT_IS_FRAME(frame));
        g_assert_true(webkit_frame_is_main_frame(frame));

        return true;
    }

    bool testURI(WebKitWebPage* page)
    {
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS
        WebKitFrame* frame = webkit_web_page_get_main_frame(page);
        G_GNUC_END_IGNORE_DEPRECATIONS
        g_assert_true(WEBKIT_IS_FRAME(frame));
        g_assert_cmpstr(webkit_web_page_get_uri(page), ==, webkit_frame_get_uri(frame));

        return true;
    }

    bool testJavaScriptContext(WebKitWebPage* page)
    {
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS
        WebKitFrame* frame = webkit_web_page_get_main_frame(page);
        G_GNUC_END_IGNORE_DEPRECATIONS
        g_assert_true(WEBKIT_IS_FRAME(frame));
#if PLATFORM(GTK) && !USE(GTK4)
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
        g_assert_nonnull(webkit_frame_get_javascript_global_context(frame));
        G_GNUC_END_IGNORE_DEPRECATIONS;
#endif

        GRefPtr<JSCContext> jsContext = adoptGRef(webkit_frame_get_js_context(frame));
        g_assert_true(JSC_IS_CONTEXT(jsContext.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsContext.get()));

        return true;
    }

    bool testJavaScriptValues(WebKitWebPage* page)
    {
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS
        WebKitFrame* frame = webkit_web_page_get_main_frame(page);
        G_GNUC_END_IGNORE_DEPRECATIONS
        g_assert_true(WEBKIT_IS_FRAME(frame));

        GRefPtr<JSCContext> jsContext = adoptGRef(webkit_frame_get_js_context(frame));
        g_assert_true(JSC_IS_CONTEXT(jsContext.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsContext.get()));

#if !ENABLE(2022_GLIB_API)
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
        WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
        g_assert_true(WEBKIT_DOM_IS_DOCUMENT(document));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(document));

        GRefPtr<JSCValue> jsDocument = adoptGRef(webkit_frame_get_js_value_for_dom_object(frame, WEBKIT_DOM_OBJECT(document)));
        g_assert_true(JSC_IS_VALUE(jsDocument.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsDocument.get()));
        g_assert_true(jsc_value_get_context(jsDocument.get()) == jsContext.get());
        G_GNUC_END_IGNORE_DEPRECATIONS;

        GRefPtr<JSCValue> value = adoptGRef(jsc_context_get_value(jsContext.get(), "document"));
        g_assert_true(value.get() == jsDocument.get());
#else
        GRefPtr<JSCValue> jsDocument = adoptGRef(jsc_context_get_value(jsContext.get(), "document"));
        g_assert_true(JSC_IS_VALUE(jsDocument.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsDocument.get()));
#endif

        GRefPtr<JSCValue> jsP = adoptGRef(jsc_value_object_invoke_method(jsDocument.get(), "getElementById", G_TYPE_STRING, "paragraph", G_TYPE_NONE));
        g_assert_true(JSC_IS_VALUE(jsP.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsP.get()));
        g_assert_true(jsc_value_is_object(jsP.get()));
        g_assert_true(jsc_value_get_context(jsP.get()) == jsContext.get());

#if ENABLE(2022_GLIB_API)
        GRefPtr<JSCValue> value = adoptGRef(jsc_context_evaluate(jsContext.get(), "document.getElementById('paragraph')", -1));
#else
        value = adoptGRef(jsc_context_evaluate(jsContext.get(), "document.getElementById('paragraph')", -1));
#endif
        g_assert_true(value.get() == jsP.get());

        value = adoptGRef(jsc_value_object_get_property(jsP.get(), "innerText"));
        g_assert_true(JSC_IS_VALUE(value.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(value.get()));
        g_assert_true(jsc_value_is_string(value.get()));
        GUniquePtr<char> strValue(jsc_value_to_string(value.get()));
        g_assert_cmpstr(strValue.get(), ==, "This is a test");

        GRefPtr<JSCValue> jsImage = adoptGRef(jsc_value_object_invoke_method(jsDocument.get(), "getElementById", G_TYPE_STRING, "image", G_TYPE_NONE));
        g_assert_true(JSC_IS_VALUE(jsImage.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsImage.get()));
        g_assert_true(jsc_value_is_object(jsImage.get()));

#if !ENABLE(2022_GLIB_API)
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
        WebKitDOMNode* image = webkit_dom_node_for_js_value(jsImage.get());
        g_assert_true(WEBKIT_DOM_IS_ELEMENT(image));
        G_GNUC_END_IGNORE_DEPRECATIONS;
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(image));
#endif
#if PLATFORM(GTK) && !USE(GTK4)
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
        g_assert_true(webkit_dom_document_get_element_by_id(document, "image") == WEBKIT_DOM_ELEMENT(image));
        G_GNUC_END_IGNORE_DEPRECATIONS;
#endif

        return true;
    }

    static void willSubmitFormCallback(WebKitWebFormManager*, JSCValue*, WebKitFrame* sourceFrame, WebKitFrame*, gpointer userData)
    {
        // The form is submitted from a subframe.
        g_assert_false(webkit_frame_is_main_frame(sourceFrame));

        auto* test = static_cast<WebKitFrameTest*>(userData);
        g_main_loop_quit(test->m_mainLoop.get());
    }

    bool testSubframe(WebKitWebPage* page)
    {
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS
        WebKitFrame* mainFrame = webkit_web_page_get_main_frame(page);
        G_GNUC_END_IGNORE_DEPRECATIONS
        g_assert_true(WEBKIT_IS_FRAME(mainFrame));

        GRefPtr<JSCContext> jsContext = adoptGRef(webkit_frame_get_js_context(mainFrame));
        g_assert_true(JSC_IS_CONTEXT(jsContext.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsContext.get()));

        GRefPtr<JSCValue> jsParentDocument = adoptGRef(jsc_context_get_value(jsContext.get(), "document"));
        g_assert_true(JSC_IS_VALUE(jsParentDocument.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsParentDocument.get()));

        GRefPtr<JSCValue> subframe = adoptGRef(jsc_value_object_invoke_method(jsParentDocument.get(), "getElementById", G_TYPE_STRING, "frame", G_TYPE_NONE));
        g_assert_true(JSC_IS_VALUE(subframe.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(subframe.get()));

        GRefPtr<JSCValue> contentWindow = adoptGRef(jsc_value_object_get_property(subframe.get(), "contentWindow"));
        g_assert_true(JSC_IS_VALUE(contentWindow.get()));
        s_watcher.assertObjectIsDeletedWhenTestFinishes(G_OBJECT(contentWindow.get()));

        GRefPtr<JSCValue> undefined = adoptGRef(jsc_value_object_invoke_method(contentWindow.get(), "postMessage", G_TYPE_STRING, "submit the form!", G_TYPE_STRING, "*", G_TYPE_NONE));
        g_assert_true(JSC_IS_VALUE(undefined.get()));
        g_assert_true(jsc_value_is_undefined(undefined.get()));

        auto* formManager = webkit_web_page_get_form_manager(page, nullptr);
        g_signal_connect(formManager, "will-submit-form", reinterpret_cast<GCallback>(willSubmitFormCallback), this);

        m_mainLoop = adoptGRef(g_main_loop_new(nullptr, FALSE));
        g_main_loop_run(m_mainLoop.get());

        return true;
    }

    bool runTest(const char* testName, WebKitWebPage* page) override
    {
        if (!strcmp(testName, "main-frame"))
            return testMainFrame(page);
        if (!strcmp(testName, "uri"))
            return testURI(page);
        if (!strcmp(testName, "javascript-context"))
            return testJavaScriptContext(page);
        if (!strcmp(testName, "javascript-values"))
            return testJavaScriptValues(page);
        if (!strcmp(testName, "subframe"))
            return testSubframe(page);

        g_assert_not_reached();
        return false;
    }

    GRefPtr<GMainLoop> m_mainLoop;
};

static void __attribute__((constructor)) registerTests()
{
    REGISTER_TEST(WebKitFrameTest, "WebKitFrame/main-frame");
    REGISTER_TEST(WebKitFrameTest, "WebKitFrame/uri");
    REGISTER_TEST(WebKitFrameTest, "WebKitFrame/javascript-context");
    REGISTER_TEST(WebKitFrameTest, "WebKitFrame/javascript-values");
    REGISTER_TEST(WebKitFrameTest, "WebKitFrame/subframe");
}