Skip to content

Commit 3ab72c0

Browse files
AX: Add waitForNotifications and enhance waitForNotification in accessibility-helper.js
https://bugs.webkit.org/show_bug.cgi?id=306500 <rdar://problem/169155012> Reviewed by Tyler Wilcock. Enhance waitForNotification to support global notification listeners when no specific AccessibilityUIElement is provided. This allows tests to listen for notifications without knowing which element will emit them. Add new waitForNotifications function that waits for a specified count of notifications and invokes a handler callback for each one. This is useful for tests that need to verify multiple sequential notifications are fired, such as text selection change tests. * LayoutTests/resources/accessibility-helper.js: Canonical link: https://commits.webkit.org/306418@main
1 parent e2602ac commit 3ab72c0

1 file changed

Lines changed: 63 additions & 11 deletions

File tree

LayoutTests/resources/accessibility-helper.js

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -270,21 +270,73 @@ async function waitForElementById(id) {
270270
}
271271

272272
// Executes the operation and waits until an accessibility notification of the provided
273-
// `notificationName` is received. A notification listener is added to the AccessibilityUIElement
274-
// passed in; before the operation is executed. The `operation` is expected to be a function,
273+
// `notificationName` is received. A notification listener is added to the passed
274+
// AccessibilityUIElement if not null, or a global listener is installed, before
275+
// the operation is executed. The `operation` is expected to be a function,
275276
// which can optionally be async.
276-
async function waitForNotification(accessibilityElement, notificationName, operation) {
277-
var reached = false;
278-
function listener(receivedNotification) {
279-
if (receivedNotification == notificationName) {
280-
reached = true;
281-
accessibilityElement.removeNotificationListener(listener);
282-
}
277+
async function waitForNotification(axElement, notificationName, operation) {
278+
var received = false;
279+
280+
function elementListener(notification) {
281+
if (notification != notificationName)
282+
return;
283+
received = true;
284+
axElement.removeNotificationListener(elementListener);
285+
}
286+
287+
function globalListener(element, notification) {
288+
if (notification != notificationName)
289+
return;
290+
received = true;
291+
accessibilityController.removeNotificationListener(globalListener);
283292
}
284293

285-
accessibilityElement.addNotificationListener(listener);
294+
if (axElement)
295+
axElement.addNotificationListener(elementListener);
296+
else
297+
accessibilityController.addNotificationListener(globalListener);
298+
299+
await operation();
300+
await waitFor(() => { return received; });
301+
}
302+
303+
// Executes the passed operation function and waits for expectedCount number of
304+
// notifications of the given name. It takes a notificationHandler function to
305+
// be executed when the proper notifications are received. Similarly to
306+
// waitForNotification, the listener can be added to the given AccessibilityUIElement
307+
// or globally.
308+
async function waitForNotifications(axElement, notificationName, expectedCount, operation, notificationHandler) {
309+
var receivedCount = 0;
310+
311+
function elementListener(notification) {
312+
if (notification != notificationName)
313+
return;
314+
++receivedCount;
315+
316+
notificationHandler(axElement, notification);
317+
318+
if (receivedCount == expectedCount)
319+
axElement.removeNotificationListener(elementListener);
320+
}
321+
322+
function globalListener(element, notification) {
323+
if (notification != notificationName)
324+
return;
325+
++receivedCount;
326+
327+
notificationHandler(element, notification);
328+
329+
if (receivedCount == expectedCount)
330+
accessibilityController.removeNotificationListener(globalListener);
331+
}
332+
333+
if (axElement)
334+
axElement.addNotificationListener(elementListener);
335+
else
336+
accessibilityController.addNotificationListener(globalListener);
337+
286338
await operation();
287-
await waitFor(() => { return reached; });
339+
await waitFor(() => { return receivedCount == expectedCount; });
288340
}
289341

290342
// Expect an expression to equal a value and return the result as a string.

0 commit comments

Comments
 (0)