| 1 | /* |
| 2 | * Copyright (C) 2012, 2019 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 Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2,1 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 "TestMain.h" |
| 23 | #include "WebKitTestBus.h" |
| 24 | |
| 25 | // The libatspi headers don't use G_BEGIN_DECLS |
| 26 | extern "C" { |
| 27 | #include <atspi/atspi.h> |
| 28 | } |
| 29 | |
| 30 | static WebKitTestBus* bus; |
| 31 | |
| 32 | class AccessibilityTest : public Test { |
| 33 | public: |
| 34 | MAKE_GLIB_TEST_FIXTURE(AccessibilityTest); |
| 35 | |
| 36 | AccessibilityTest() |
| 37 | { |
| 38 | GUniquePtr<char> testServerPath(g_build_filename(WEBKIT_EXEC_PATH, "TestWebKitAPI" , "WebKit2Gtk" , "AccessibilityTestServer" , nullptr)); |
| 39 | char* args[2]; |
| 40 | args[0] = testServerPath.get(); |
| 41 | args[1] = nullptr; |
| 42 | |
| 43 | g_assert_true(g_spawn_async(nullptr, args, nullptr, G_SPAWN_DEFAULT, nullptr, nullptr, &m_childProcessID, nullptr)); |
| 44 | } |
| 45 | |
| 46 | ~AccessibilityTest() |
| 47 | { |
| 48 | if (m_childProcessID) { |
| 49 | g_spawn_close_pid(m_childProcessID); |
| 50 | kill(m_childProcessID, SIGTERM); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void loadHTMLAndWaitUntilFinished(const char* html, const char* baseURI) |
| 55 | { |
| 56 | ensureProxy(); |
| 57 | |
| 58 | GUniqueOutPtr<GError> error; |
| 59 | GRefPtr<GVariant> result = adoptGRef(g_dbus_proxy_call_sync(m_proxy.get(), "LoadHTML" , |
| 60 | g_variant_new("(ss)" , html, baseURI ? baseURI : "" ), G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error.outPtr())); |
| 61 | g_assert_no_error(error.get()); |
| 62 | } |
| 63 | |
| 64 | GRefPtr<AtspiAccessible> findTestServerApplication() |
| 65 | { |
| 66 | // Only one desktop is supported by ATSPI at the moment. |
| 67 | GRefPtr<AtspiAccessible> desktop = adoptGRef(atspi_get_desktop(0)); |
| 68 | |
| 69 | int childCount = atspi_accessible_get_child_count(desktop.get(), nullptr); |
| 70 | for (int i = 0; i < childCount; ++i) { |
| 71 | GRefPtr<AtspiAccessible> current = adoptGRef(atspi_accessible_get_child_at_index(desktop.get(), i, nullptr)); |
| 72 | if (!g_strcmp0(atspi_accessible_get_name(current.get(), nullptr), "AccessibilityTestServer" )) |
| 73 | return current; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | GRefPtr<AtspiAccessible> findDocumentWeb(AtspiAccessible* accessible) |
| 80 | { |
| 81 | int childCount = atspi_accessible_get_child_count(accessible, nullptr); |
| 82 | for (int i = 0; i < childCount; ++i) { |
| 83 | GRefPtr<AtspiAccessible> child = adoptGRef(atspi_accessible_get_child_at_index(accessible, i, nullptr)); |
| 84 | if (atspi_accessible_get_role(child.get(), nullptr) == ATSPI_ROLE_DOCUMENT_WEB) |
| 85 | return child; |
| 86 | |
| 87 | if (auto documentWeb = findDocumentWeb(child.get())) |
| 88 | return documentWeb; |
| 89 | } |
| 90 | return nullptr; |
| 91 | } |
| 92 | |
| 93 | GRefPtr<AtspiAccessible> findRootObject(AtspiAccessible* application) |
| 94 | { |
| 95 | // Find the document web, its parent is the scroll view (WebCore root object) and its parent is |
| 96 | // the GtkPlug (WebProcess root element). |
| 97 | auto documentWeb = findDocumentWeb(application); |
| 98 | if (!documentWeb) |
| 99 | return nullptr; |
| 100 | |
| 101 | auto parent = adoptGRef(atspi_accessible_get_parent(documentWeb.get(), nullptr)); |
| 102 | return parent ? adoptGRef(atspi_accessible_get_parent(parent.get(), nullptr)) : nullptr; |
| 103 | } |
| 104 | |
| 105 | void waitUntilChildrenRemoved(AtspiAccessible* accessible) |
| 106 | { |
| 107 | m_eventSource = accessible; |
| 108 | GRefPtr<AtspiEventListener> listener = adoptGRef(atspi_event_listener_new( |
| 109 | [](AtspiEvent* event, gpointer userData) { |
| 110 | auto* test = static_cast<AccessibilityTest*>(userData); |
| 111 | if (event->source == test->m_eventSource) |
| 112 | g_main_loop_quit(test->m_mainLoop.get()); |
| 113 | }, this, nullptr)); |
| 114 | atspi_event_listener_register(listener.get(), "object:children-changed:remove" , nullptr); |
| 115 | g_main_loop_run(m_mainLoop.get()); |
| 116 | m_eventSource = nullptr; |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | void ensureProxy() |
| 121 | { |
| 122 | if (m_proxy) |
| 123 | return; |
| 124 | |
| 125 | m_mainLoop = adoptGRef(g_main_loop_new(nullptr, FALSE)); |
| 126 | m_proxy = adoptGRef(bus->createProxy("org.webkit.gtk.AccessibilityTest" , "/org/webkit/gtk/AccessibilityTest" , "org.webkit.gtk.AccessibilityTest" , m_mainLoop.get())); |
| 127 | } |
| 128 | |
| 129 | GPid m_childProcessID { 0 }; |
| 130 | GRefPtr<GDBusProxy> m_proxy; |
| 131 | GRefPtr<GMainLoop> m_mainLoop; |
| 132 | AtspiAccessible* m_eventSource { nullptr }; |
| 133 | }; |
| 134 | |
| 135 | static void testAtspiBasicHierarchy(AccessibilityTest* test, gconstpointer) |
| 136 | { |
| 137 | test->loadHTMLAndWaitUntilFinished( |
| 138 | "<html>" |
| 139 | " <body>" |
| 140 | " <h1>This is a test</h1>" |
| 141 | " <p>This is a paragraph with some plain text.</p>" |
| 142 | " <p>This paragraph contains <a href=\"http://www.webkitgtk.org\">a link</a> in the middle.</p>" |
| 143 | " </body>" |
| 144 | "</html>" , |
| 145 | nullptr); |
| 146 | |
| 147 | auto testServerApp = test->findTestServerApplication(); |
| 148 | g_assert_true(ATSPI_IS_ACCESSIBLE(testServerApp.get())); |
| 149 | GUniquePtr<char> name(atspi_accessible_get_name(testServerApp.get(), nullptr)); |
| 150 | g_assert_cmpstr(name.get(), ==, "AccessibilityTestServer" ); |
| 151 | g_assert_cmpint(atspi_accessible_get_role(testServerApp.get(), nullptr), ==, ATSPI_ROLE_APPLICATION); |
| 152 | |
| 153 | auto rootObject = test->findRootObject(testServerApp.get()); |
| 154 | g_assert_true(ATSPI_IS_ACCESSIBLE(rootObject.get())); |
| 155 | g_assert_cmpint(atspi_accessible_get_role(rootObject.get(), nullptr), ==, ATSPI_ROLE_FILLER); |
| 156 | |
| 157 | auto scrollView = adoptGRef(atspi_accessible_get_child_at_index(rootObject.get(), 0, nullptr)); |
| 158 | g_assert_true(ATSPI_IS_ACCESSIBLE(scrollView.get())); |
| 159 | g_assert_cmpint(atspi_accessible_get_role(scrollView.get(), nullptr), ==, ATSPI_ROLE_SCROLL_PANE); |
| 160 | |
| 161 | auto documentWeb = adoptGRef(atspi_accessible_get_child_at_index(scrollView.get(), 0, nullptr)); |
| 162 | g_assert_true(ATSPI_IS_ACCESSIBLE(documentWeb.get())); |
| 163 | g_assert_cmpint(atspi_accessible_get_role(documentWeb.get(), nullptr), ==, ATSPI_ROLE_DOCUMENT_WEB); |
| 164 | |
| 165 | auto h1 = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 0, nullptr)); |
| 166 | g_assert_true(ATSPI_IS_ACCESSIBLE(h1.get())); |
| 167 | name.reset(atspi_accessible_get_name(h1.get(), nullptr)); |
| 168 | g_assert_cmpstr(name.get(), ==, "This is a test" ); |
| 169 | g_assert_cmpint(atspi_accessible_get_role(h1.get(), nullptr), ==, ATSPI_ROLE_HEADING); |
| 170 | |
| 171 | auto p1 = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 1, nullptr)); |
| 172 | g_assert_true(ATSPI_IS_ACCESSIBLE(p1.get())); |
| 173 | g_assert_cmpint(atspi_accessible_get_role(p1.get(), nullptr), ==, ATSPI_ROLE_PARAGRAPH); |
| 174 | |
| 175 | auto p2 = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 2, nullptr)); |
| 176 | g_assert_true(ATSPI_IS_ACCESSIBLE(p2.get())); |
| 177 | g_assert_cmpint(atspi_accessible_get_role(p2.get(), nullptr), ==, ATSPI_ROLE_PARAGRAPH); |
| 178 | |
| 179 | auto link = adoptGRef(atspi_accessible_get_child_at_index(p2.get(), 0, nullptr)); |
| 180 | g_assert_true(ATSPI_IS_ACCESSIBLE(link.get())); |
| 181 | name.reset(atspi_accessible_get_name(link.get(), nullptr)); |
| 182 | g_assert_cmpstr(name.get(), ==, "a link" ); |
| 183 | g_assert_cmpint(atspi_accessible_get_role(link.get(), nullptr), ==, ATSPI_ROLE_LINK); |
| 184 | |
| 185 | test->loadHTMLAndWaitUntilFinished( |
| 186 | "<html>" |
| 187 | " <body>" |
| 188 | " <h1>This is another test</h1>" |
| 189 | " <img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3AYWDTMVwnSZnwAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFklEQVQI12P8z8DAwMDAxMDAwMDAAAANHQEDK+mmyAAAAABJRU5ErkJggg=='/>" |
| 190 | " </body>" |
| 191 | "</html>" , |
| 192 | nullptr); |
| 193 | |
| 194 | // Check that children-changed::remove is emitted on the root object on navigation, |
| 195 | // and the a11y hierarchy is updated. |
| 196 | test->waitUntilChildrenRemoved(rootObject.get()); |
| 197 | |
| 198 | documentWeb = test->findDocumentWeb(testServerApp.get()); |
| 199 | g_assert_true(ATSPI_IS_ACCESSIBLE(documentWeb.get())); |
| 200 | g_assert_cmpint(atspi_accessible_get_role(documentWeb.get(), nullptr), ==, ATSPI_ROLE_DOCUMENT_WEB); |
| 201 | |
| 202 | h1 = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 0, nullptr)); |
| 203 | g_assert_true(ATSPI_IS_ACCESSIBLE(h1.get())); |
| 204 | name.reset(atspi_accessible_get_name(h1.get(), nullptr)); |
| 205 | g_assert_cmpstr(name.get(), ==, "This is another test" ); |
| 206 | g_assert_cmpint(atspi_accessible_get_role(h1.get(), nullptr), ==, ATSPI_ROLE_HEADING); |
| 207 | |
| 208 | auto section = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 1, nullptr)); |
| 209 | g_assert_true(ATSPI_IS_ACCESSIBLE(section.get())); |
| 210 | g_assert_cmpint(atspi_accessible_get_role(section.get(), nullptr), ==, ATSPI_ROLE_SECTION); |
| 211 | |
| 212 | auto img = adoptGRef(atspi_accessible_get_child_at_index(section.get(), 0, nullptr)); |
| 213 | g_assert_true(ATSPI_IS_ACCESSIBLE(img.get())); |
| 214 | g_assert_cmpint(atspi_accessible_get_role(img.get(), nullptr), ==, ATSPI_ROLE_IMAGE); |
| 215 | } |
| 216 | |
| 217 | void beforeAll() |
| 218 | { |
| 219 | bus = new WebKitTestBus(); |
| 220 | if (!bus->run()) |
| 221 | return; |
| 222 | |
| 223 | AccessibilityTest::add("WebKitAccessibility" , "atspi-basic-hierarchy" , testAtspiBasicHierarchy); |
| 224 | } |
| 225 | |
| 226 | void afterAll() |
| 227 | { |
| 228 | delete bus; |
| 229 | } |
| 230 | |