1 | /* |
2 | * Copyright (C) 2017 Aidan Holm <aidanholm@gmail.com> |
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 | |
26 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
27 | |
28 | class WebKitDOMClientRectTest : public WebProcessTest { |
29 | public: |
30 | static std::unique_ptr<WebProcessTest> create() { return std::unique_ptr<WebKitDOMClientRectTest>(new WebKitDOMClientRectTest()); } |
31 | |
32 | private: |
33 | static void checkClientRectPosition(WebKitDOMClientRect* clientRect) |
34 | { |
35 | // Expected values correspond to CSS in TestDOMClientRect.cpp |
36 | g_assert_cmpfloat(webkit_dom_client_rect_get_top(clientRect), ==, -25); |
37 | g_assert_cmpfloat(webkit_dom_client_rect_get_right(clientRect), ==, 50); |
38 | g_assert_cmpfloat(webkit_dom_client_rect_get_bottom(clientRect), ==, 175); |
39 | g_assert_cmpfloat(webkit_dom_client_rect_get_left(clientRect), ==, -50); |
40 | g_assert_cmpfloat(webkit_dom_client_rect_get_width(clientRect), ==, 100); |
41 | g_assert_cmpfloat(webkit_dom_client_rect_get_height(clientRect), ==, 200); |
42 | } |
43 | |
44 | |
45 | bool testDivBoundingClientRectPosition(WebKitWebPage* page) |
46 | { |
47 | WebKitDOMDocument* document = webkit_web_page_get_dom_document(page); |
48 | g_assert_true(WEBKIT_DOM_IS_DOCUMENT(document)); |
49 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(document)); |
50 | |
51 | WebKitDOMElement* div = webkit_dom_document_get_element_by_id(document, "rect" ); |
52 | g_assert_true(WEBKIT_DOM_IS_HTML_ELEMENT(div)); |
53 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(div)); |
54 | |
55 | GRefPtr<WebKitDOMClientRect> clientRect = adoptGRef(webkit_dom_element_get_bounding_client_rect(div)); |
56 | g_assert_true(WEBKIT_DOM_IS_CLIENT_RECT(clientRect.get())); |
57 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(clientRect.get())); |
58 | checkClientRectPosition(clientRect.get()); |
59 | |
60 | return true; |
61 | } |
62 | |
63 | bool testDivClientRectsPositionAndLength(WebKitWebPage* page) |
64 | { |
65 | WebKitDOMDocument* document = webkit_web_page_get_dom_document(page); |
66 | g_assert_true(WEBKIT_DOM_IS_DOCUMENT(document)); |
67 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(document)); |
68 | |
69 | WebKitDOMElement* div = webkit_dom_document_get_element_by_id(document, "rect" ); |
70 | g_assert_true(WEBKIT_DOM_IS_HTML_ELEMENT(div)); |
71 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(div)); |
72 | |
73 | GRefPtr<WebKitDOMClientRectList> clientRectList = adoptGRef(webkit_dom_element_get_client_rects(div)); |
74 | g_assert_true(WEBKIT_DOM_IS_CLIENT_RECT_LIST(clientRectList.get())); |
75 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(clientRectList.get())); |
76 | |
77 | g_assert_cmpuint(webkit_dom_client_rect_list_get_length(clientRectList.get()), ==, 1); |
78 | |
79 | GRefPtr<WebKitDOMClientRect> clientRect = adoptGRef(webkit_dom_client_rect_list_item(clientRectList.get(), 0)); |
80 | g_assert_true(WEBKIT_DOM_IS_CLIENT_RECT(clientRect.get())); |
81 | assertObjectIsDeletedWhenTestFinishes(G_OBJECT(clientRect.get())); |
82 | checkClientRectPosition(clientRect.get()); |
83 | |
84 | // Getting the clientRect twice should return the same pointer. |
85 | GRefPtr<WebKitDOMClientRect> clientRect2 = adoptGRef(webkit_dom_client_rect_list_item(clientRectList.get(), 0)); |
86 | g_assert_true(clientRect.get() == clientRect2.get()); |
87 | |
88 | return true; |
89 | } |
90 | |
91 | bool runTest(const char* testName, WebKitWebPage* page) override |
92 | { |
93 | if (!strcmp(testName, "div-bounding-client-rect-position" )) |
94 | return testDivBoundingClientRectPosition(page); |
95 | if (!strcmp(testName, "div-client-rects-position-and-length" )) |
96 | return testDivClientRectsPositionAndLength(page); |
97 | |
98 | g_assert_not_reached(); |
99 | return false; |
100 | } |
101 | }; |
102 | |
103 | static void __attribute__((constructor)) registerTests() |
104 | { |
105 | REGISTER_TEST(WebKitDOMClientRectTest, "WebKitDOMClientRect/div-bounding-client-rect-position" ); |
106 | REGISTER_TEST(WebKitDOMClientRectTest, "WebKitDOMClientRect/div-client-rects-position-and-length" ); |
107 | } |
108 | |
109 | G_GNUC_END_IGNORE_DEPRECATIONS; |
110 | |