summaryrefslogtreecommitdiff
path: root/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitAccessibility.cpp
blob: ea4a8125f96ae7896e04c5fc15cf7b73db1e7be4 (plain)
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
217
218
219
220
221
222
223
224
/*
 * Copyright (C) 2012 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2,1 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 "TestMain.h"
#include "WebViewTest.h"

// The libatspi headers don't use G_BEGIN_DECLS
extern "C" {
#include <atspi/atspi.h>
}

#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <signal.h>
#include <unistd.h>
#include <wtf/PassRefPtr.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GUniquePtr.h>

// Name of the test server application creating the webView object.
static const char* kTestServerAppName = "AccessibilityTestServer";

// Max seconds to wait for the test server before inspecting it.
static const int kMaxWaitForChild = 5;

// The PID for the test server running, so we can kill it if needed.
static GPid kChildProcessPid = 0;

// Whether the child has replied and it's ready.
static bool kChildIsReady = false;

static void stopTestServer()
{
    // Do nothing if there's no server running.
    if (!kChildProcessPid)
        return;

    g_spawn_close_pid(kChildProcessPid);
    kill(kChildProcessPid, SIGTERM);
    kChildProcessPid = 0;
}

static void sigAbortHandler(int sigNum)
{
    // Just stop the test server if SIGABRT was received.
    stopTestServer();
}

static gpointer testServerMonitorThreadFunc(gpointer)
{
    // Wait for the specified timeout to happen.
    g_usleep(kMaxWaitForChild * G_USEC_PER_SEC);

    // Kill the child process if not ready yet.
    if (!kChildIsReady)
        stopTestServer();

    g_thread_exit(0);
    return 0;
}

static void startTestServerMonitor()
{
    kChildIsReady = false;
    g_thread_new("TestServerMonitor", testServerMonitorThreadFunc, 0);
}

static void startTestServer()
{
    // Prepare argv[] for spawning the server process.
    GUniquePtr<char> testServerPath(g_build_filename(WEBKIT_EXEC_PATH, "TestWebKitAPI", "WebKit2Gtk", kTestServerAppName, nullptr));

    char* testServerArgv[2];
    testServerArgv[0] = testServerPath.get();
    testServerArgv[1] = 0;

    // Spawn the server, getting its stdout file descriptor to set a
    // communication channel, so we know when it's ready.
    int childStdout = 0;
    if (!g_spawn_async_with_pipes(0, testServerArgv, 0, static_cast<GSpawnFlags>(0), 0, 0, &kChildProcessPid, 0, &childStdout, 0, 0)) {
        close(childStdout);
        return;
    }

    // Start monitoring the test server (in a separate thread) to
    // ensure we don't block on the child process more than a timeout.
    startTestServerMonitor();

    char msg[2];
    GIOChannel* ioChannel = g_io_channel_unix_new(childStdout);
    if (g_io_channel_read_chars(ioChannel, msg, 2, 0, 0) == G_IO_STATUS_NORMAL) {
        // Check whether the server sent a message saying it's ready
        // and store the result globally, so the monitor can see it.
        kChildIsReady = msg[0] == 'O' && msg[1] == 'K';
    }
    g_io_channel_unref(ioChannel);
    close(childStdout);

    // The timeout was reached and the server is not ready yet, so
    // stop it inmediately, and let the unit tests fail.
    if (!kChildIsReady)
        stopTestServer();
}

static void checkAtspiAccessible(AtspiAccessible* accessible, const char* targetName, AtspiRole targetRole)
{
    g_assert(ATSPI_IS_ACCESSIBLE(accessible));

    GUniquePtr<char> name(atspi_accessible_get_name(accessible, 0));
    g_assert_cmpstr(targetName, ==, name.get());
    g_assert_cmpint(targetRole, ==, atspi_accessible_get_role(accessible, 0));
}

static GRefPtr<AtspiAccessible> findTestServerApplication()
{
    // Only one desktop is supported by ATSPI at the moment.
    GRefPtr<AtspiAccessible> desktop = adoptGRef(atspi_get_desktop(0));

    // Look for the server application in the list of apps.
    GRefPtr<AtspiAccessible> current;
    int childCount = atspi_accessible_get_child_count(desktop.get(), 0);
    for (int i = 0; i < childCount; i++) {
        current = adoptGRef(atspi_accessible_get_child_at_index(desktop.get(), i, 0));
        if (!g_strcmp0(atspi_accessible_get_name(current.get(), 0), kTestServerAppName))
            return current;
    }

    return 0;
}

static void testAtspiBasicHierarchy(WebViewTest* test, gconstpointer)
{
    // The test server's accessibility object (UI Process).
    GRefPtr<AtspiAccessible> testServerApp = findTestServerApplication();
    g_assert(ATSPI_IS_ACCESSIBLE(testServerApp.get()));
    checkAtspiAccessible(testServerApp.get(), "AccessibilityTestServer", ATSPI_ROLE_APPLICATION);

    // The main window's accessibility object (UI Process).
    GRefPtr<AtspiAccessible> currentParent = testServerApp;
    GRefPtr<AtspiAccessible> currentChild = adoptGRef(atspi_accessible_get_child_at_index(currentParent.get(), 0, 0));
    g_assert(ATSPI_IS_ACCESSIBLE(currentChild.get()));
    checkAtspiAccessible(currentChild.get(), "", ATSPI_ROLE_FRAME);

    // The WebView's accessibility object (UI Process).
    currentParent = currentChild;
    currentChild = atspi_accessible_get_child_at_index(currentParent.get(), 0, 0);
    g_assert(ATSPI_IS_ACCESSIBLE(currentChild.get()));
    checkAtspiAccessible(currentChild.get(), "", ATSPI_ROLE_FILLER);

    // The WebPage's accessibility object (Web Process).
    currentParent = currentChild;
    currentChild = atspi_accessible_get_child_at_index(currentParent.get(), 0, 0);
    g_assert(ATSPI_IS_ACCESSIBLE(currentChild.get()));
    checkAtspiAccessible(currentChild.get(), "", ATSPI_ROLE_FILLER);

    // HTML root element's accessible element (Web Process).
    currentParent = currentChild;
    currentChild = atspi_accessible_get_child_at_index(currentParent.get(), 0, 0);
    g_assert(ATSPI_IS_ACCESSIBLE(currentChild.get()));

    // HTML body's accessible element (Web Process).
    currentParent = currentChild;
    currentChild = atspi_accessible_get_child_at_index(currentParent.get(), 0, 0);
    g_assert(ATSPI_IS_ACCESSIBLE(currentChild.get()));
    checkAtspiAccessible(currentChild.get(), "", ATSPI_ROLE_DOCUMENT_WEB);

    // HTML H1's accessible element (Web Process).
    currentParent = currentChild;
    currentChild = atspi_accessible_get_child_at_index(currentParent.get(), 0, 0);
    g_assert(ATSPI_IS_ACCESSIBLE(currentChild.get()));
    checkAtspiAccessible(currentChild.get(), "This is a test", ATSPI_ROLE_HEADING);

    // HTML first paragraph's accessible element (Web Process).
    currentChild = atspi_accessible_get_child_at_index(currentParent.get(), 1, 0);
    g_assert(ATSPI_IS_ACCESSIBLE(currentChild.get()));
    checkAtspiAccessible(currentChild.get(), "", ATSPI_ROLE_PARAGRAPH);

    // HTML second paragraph's accessible element (Web Process).
    currentChild = atspi_accessible_get_child_at_index(currentParent.get(), 2, 0);
    g_assert(ATSPI_IS_ACCESSIBLE(currentChild.get()));
    checkAtspiAccessible(currentChild.get(), "", ATSPI_ROLE_PARAGRAPH);

    // HTML link's accessible element (Web Process).
    currentParent = currentChild;
    currentChild = atspi_accessible_get_child_at_index(currentParent.get(), 0, 0);
    g_assert(ATSPI_IS_ACCESSIBLE(currentChild.get()));
    checkAtspiAccessible(currentChild.get(), "a link", ATSPI_ROLE_LINK);
}

void beforeAll()
{
    // We install a handler to ensure that we kill the child process
    // if the parent dies because of whatever the reason is.
    signal(SIGABRT, sigAbortHandler);

    // Start the accessibility test server and load the tests.
    startTestServer();
    WebViewTest::add("WebKitAccessibility", "atspi-basic-hierarchy", testAtspiBasicHierarchy);
}

void afterAll()
{
    // Ensure we stop the server.
    stopTestServer();
}