| 1 | /* |
| 2 | * Copyright (C) 2013 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 <wtf/glib/GUniquePtr.h> |
| 25 | |
| 26 | class WebKitFrameTest : public WebProcessTest { |
| 27 | public: |
| 28 | static std::unique_ptr<WebProcessTest> create() { return std::unique_ptr<WebProcessTest>(new WebKitFrameTest()); } |
| 29 | |
| 30 | private: |
| 31 | bool testMainFrame(WebKitWebPage* page) |
| 32 | { |
| 33 | WebKitFrame* frame = webkit_web_page_get_main_frame(page); |
| 34 | g_assert_true(WEBKIT_IS_FRAME(frame)); |
| 35 | g_assert_true(webkit_frame_is_main_frame(frame)); |
| 36 | |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | bool testURI(WebKitWebPage* page) |
| 41 | { |
| 42 | WebKitFrame* frame = webkit_web_page_get_main_frame(page); |
| 43 | g_assert_true(WEBKIT_IS_FRAME(frame)); |
| 44 | g_assert_cmpstr(webkit_web_page_get_uri(page), ==, webkit_frame_get_uri(frame)); |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | bool testJavaScriptContext(WebKitWebPage* page) |
| 50 | { |
| 51 | WebKitFrame* frame = webkit_web_page_get_main_frame(page); |
| 52 | g_assert_true(WEBKIT_IS_FRAME(frame)); |
| 53 | #if PLATFORM(GTK) |
| 54 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
| 55 | g_assert_nonnull(webkit_frame_get_javascript_global_context(frame)); |
| 56 | G_GNUC_END_IGNORE_DEPRECATIONS; |
| 57 | #endif |
| 58 | |
| 59 | GRefPtr<JSCContext> jsContext = adoptGRef(webkit_frame_get_js_context(frame)); |
| 60 | g_assert_true(JSC_IS_CONTEXT(jsContext.get())); |
| 61 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsContext.get())); |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | bool testJavaScriptValues(WebKitWebPage* page) |
| 67 | { |
| 68 | WebKitFrame* frame = webkit_web_page_get_main_frame(page); |
| 69 | g_assert_true(WEBKIT_IS_FRAME(frame)); |
| 70 | |
| 71 | GRefPtr<JSCContext> jsContext = adoptGRef(webkit_frame_get_js_context(frame)); |
| 72 | g_assert_true(JSC_IS_CONTEXT(jsContext.get())); |
| 73 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsContext.get())); |
| 74 | |
| 75 | WebKitDOMDocument* document = webkit_web_page_get_dom_document(page); |
| 76 | g_assert_true(WEBKIT_DOM_IS_DOCUMENT(document)); |
| 77 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(document)); |
| 78 | |
| 79 | GRefPtr<JSCValue> jsDocument = adoptGRef(webkit_frame_get_js_value_for_dom_object(frame, WEBKIT_DOM_OBJECT(document))); |
| 80 | g_assert_true(JSC_IS_VALUE(jsDocument.get())); |
| 81 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsDocument.get())); |
| 82 | g_assert_true(jsc_value_get_context(jsDocument.get()) == jsContext.get()); |
| 83 | |
| 84 | GRefPtr<JSCValue> value = adoptGRef(jsc_context_get_value(jsContext.get(), "document" )); |
| 85 | g_assert_true(value.get() == jsDocument.get()); |
| 86 | |
| 87 | #if PLATFORM(GTK) |
| 88 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
| 89 | WebKitDOMElement* p = webkit_dom_document_get_element_by_id(document, "paragraph" ); |
| 90 | G_GNUC_END_IGNORE_DEPRECATIONS; |
| 91 | g_assert_true(WEBKIT_DOM_IS_ELEMENT(p)); |
| 92 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(p)); |
| 93 | |
| 94 | GRefPtr<JSCValue> jsP = adoptGRef(webkit_frame_get_js_value_for_dom_object(frame, WEBKIT_DOM_OBJECT(p))); |
| 95 | #else |
| 96 | GRefPtr<JSCValue> jsP = adoptGRef(jsc_value_object_invoke_method(jsDocument.get(), "getElementById" , G_TYPE_STRING, "paragraph" , G_TYPE_NONE)); |
| 97 | #endif |
| 98 | g_assert_true(JSC_IS_VALUE(jsP.get())); |
| 99 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsP.get())); |
| 100 | g_assert_true(jsc_value_is_object(jsP.get())); |
| 101 | g_assert_true(jsc_value_get_context(jsP.get()) == jsContext.get()); |
| 102 | |
| 103 | value = adoptGRef(jsc_context_evaluate(jsContext.get(), "document.getElementById('paragraph')" , -1)); |
| 104 | g_assert_true(value.get() == jsP.get()); |
| 105 | #if PLATFORM(GTK) |
| 106 | value = adoptGRef(jsc_value_object_invoke_method(jsDocument.get(), "getElementById" , G_TYPE_STRING, "paragraph" , G_TYPE_NONE)); |
| 107 | g_assert_true(value.get() == jsP.get()); |
| 108 | #endif |
| 109 | |
| 110 | value = adoptGRef(jsc_value_object_get_property(jsP.get(), "innerText" )); |
| 111 | g_assert_true(JSC_IS_VALUE(value.get())); |
| 112 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(value.get())); |
| 113 | g_assert_true(jsc_value_is_string(value.get())); |
| 114 | GUniquePtr<char> strValue(jsc_value_to_string(value.get())); |
| 115 | g_assert_cmpstr(strValue.get(), ==, "This is a test" ); |
| 116 | |
| 117 | GRefPtr<JSCValue> jsImage = adoptGRef(jsc_value_object_invoke_method(jsDocument.get(), "getElementById" , G_TYPE_STRING, "image" , G_TYPE_NONE)); |
| 118 | g_assert_true(JSC_IS_VALUE(jsImage.get())); |
| 119 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsImage.get())); |
| 120 | g_assert_true(jsc_value_is_object(jsImage.get())); |
| 121 | |
| 122 | WebKitDOMNode* image = webkit_dom_node_for_js_value(jsImage.get()); |
| 123 | g_assert_true(WEBKIT_DOM_IS_ELEMENT(image)); |
| 124 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(image)); |
| 125 | #if PLATFORM(GTK) |
| 126 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
| 127 | g_assert_true(webkit_dom_document_get_element_by_id(document, "image" ) == WEBKIT_DOM_ELEMENT(image)); |
| 128 | G_GNUC_END_IGNORE_DEPRECATIONS; |
| 129 | #endif |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | static void willSubmitFormCallback(WebKitWebPage* page, WebKitDOMElement*, WebKitFormSubmissionStep, WebKitFrame* sourceFrame, WebKitFrame*, GPtrArray*, GPtrArray*, gpointer userData) |
| 135 | { |
| 136 | // The form is submitted from a subframe. It should have a different ID than the main frame. |
| 137 | WebKitFrame* mainFrame = webkit_web_page_get_main_frame(page); |
| 138 | g_assert_cmpuint(webkit_frame_get_id(mainFrame), !=, webkit_frame_get_id(sourceFrame)); |
| 139 | |
| 140 | auto* test = static_cast<WebKitFrameTest*>(userData); |
| 141 | g_main_loop_quit(test->m_mainLoop.get()); |
| 142 | } |
| 143 | |
| 144 | bool testSubframe(WebKitWebPage* page) |
| 145 | { |
| 146 | WebKitFrame* mainFrame = webkit_web_page_get_main_frame(page); |
| 147 | g_assert_true(WEBKIT_IS_FRAME(mainFrame)); |
| 148 | |
| 149 | GRefPtr<JSCContext> jsContext = adoptGRef(webkit_frame_get_js_context(mainFrame)); |
| 150 | g_assert_true(JSC_IS_CONTEXT(jsContext.get())); |
| 151 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsContext.get())); |
| 152 | |
| 153 | GRefPtr<JSCValue> jsParentDocument = adoptGRef(jsc_context_get_value(jsContext.get(), "document" )); |
| 154 | g_assert_true(JSC_IS_VALUE(jsParentDocument.get())); |
| 155 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(jsParentDocument.get())); |
| 156 | |
| 157 | GRefPtr<JSCValue> subframe = adoptGRef(jsc_value_object_invoke_method(jsParentDocument.get(), "getElementById" , G_TYPE_STRING, "frame" , G_TYPE_NONE)); |
| 158 | g_assert_true(JSC_IS_VALUE(subframe.get())); |
| 159 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(subframe.get())); |
| 160 | |
| 161 | GRefPtr<JSCValue> contentWindow = adoptGRef(jsc_value_object_get_property(subframe.get(), "contentWindow" )); |
| 162 | g_assert_true(JSC_IS_VALUE(contentWindow.get())); |
| 163 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(contentWindow.get())); |
| 164 | |
| 165 | GRefPtr<JSCValue> undefined = adoptGRef(jsc_value_object_invoke_method(contentWindow.get(), "postMessage" , G_TYPE_STRING, "submit the form!" , G_TYPE_STRING, "*" , G_TYPE_NONE)); |
| 166 | g_assert_true(JSC_IS_VALUE(undefined.get())); |
| 167 | g_assert_true(jsc_value_is_undefined(undefined.get())); |
| 168 | |
| 169 | g_signal_connect(page, "will-submit-form" , reinterpret_cast<GCallback>(willSubmitFormCallback), this); |
| 170 | |
| 171 | m_mainLoop = adoptGRef(g_main_loop_new(nullptr, FALSE)); |
| 172 | g_main_loop_run(m_mainLoop.get()); |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | bool runTest(const char* testName, WebKitWebPage* page) override |
| 178 | { |
| 179 | if (!strcmp(testName, "main-frame" )) |
| 180 | return testMainFrame(page); |
| 181 | if (!strcmp(testName, "uri" )) |
| 182 | return testURI(page); |
| 183 | if (!strcmp(testName, "javascript-context" )) |
| 184 | return testJavaScriptContext(page); |
| 185 | if (!strcmp(testName, "javascript-values" )) |
| 186 | return testJavaScriptValues(page); |
| 187 | if (!strcmp(testName, "subframe" )) |
| 188 | return testSubframe(page); |
| 189 | |
| 190 | g_assert_not_reached(); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | GRefPtr<GMainLoop> m_mainLoop; |
| 195 | }; |
| 196 | |
| 197 | static void __attribute__((constructor)) registerTests() |
| 198 | { |
| 199 | REGISTER_TEST(WebKitFrameTest, "WebKitFrame/main-frame" ); |
| 200 | REGISTER_TEST(WebKitFrameTest, "WebKitFrame/uri" ); |
| 201 | REGISTER_TEST(WebKitFrameTest, "WebKitFrame/javascript-context" ); |
| 202 | REGISTER_TEST(WebKitFrameTest, "WebKitFrame/javascript-values" ); |
| 203 | REGISTER_TEST(WebKitFrameTest, "WebKitFrame/subframe" ); |
| 204 | } |
| 205 | |