Clean up asynchronous behavior in some accessibility tests
[WebKit-https.git] / LayoutTests / accessibility / mac / selection-notification-focus-change.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <script src="../../resources/js-test-pre.js"></script>
5     </head>
6     <body id="body" onload="runTest()">
7         <fieldset>
8             <div>
9                 <input id="1">
10                     <label for="1">1</label>
11                     </div>
12             <div>
13                 <input id="2">
14                     <label for="2">2</label>
15                     </div>
16         </fieldset>
17         <script>
18             description("This tests that selection changes as a result of advancing focus include AXTextSelectionChangedFocus flag.");
19             jsTestIsAsync = true;
20
21             var webArea = 0;
22             var axTextFocusChangeOne = 0;
23             var axTextFocusChangeTwo = 0;
24             var axTextFocusChangeThree = 0;
25             var axTextFocusChangeFour = 0;
26             var selectCount = 0;
27             var focusCount = 0;
28             // Notification ordering:
29             // The first tab will result in 2 selection change notifications and a focus notification          selectCount=2, focusCount=1
30             // takeFocus() will result in a focus notification                                                 selectCount=2, focusCount=2
31             // The second tab will result in another 2 selection change notifications and a focus notification selectCount=1, focusCount=3
32             // (The 2 selection change notifications per tab is caused by caret browsing being enabled)
33             function notificationCallback(notification, userInfo) {
34                 if (notification == "AXSelectedTextChanged") {
35                     selectCount++;
36                     if (selectCount == 1) {
37                         axTextFocusChangeOne = userInfo["AXTextSelectionChangedFocus"];
38                         shouldBeTrue("axTextFocusChangeOne");
39                     } else if (selectCount == 2) {
40                         axTextFocusChangeTwo = userInfo["AXTextSelectionChangedFocus"];
41                         shouldBeTrue("axTextFocusChangeTwo");
42
43                         shouldBeTrue("accessibilityController.accessibleElementById(\"1\").isFocusable");
44                         accessibilityController.accessibleElementById("1").takeFocus();
45                     } else if (selectCount == 3) {
46                         axTextFocusChangeThree = userInfo["AXTextSelectionChangedFocus"];
47                         shouldBeTrue("axTextFocusChangeThree");
48                     } else if (selectCount == 4) {
49                         axTextFocusChangeFour = userInfo["AXTextSelectionChangedFocus"];
50                         shouldBeTrue("axTextFocusChangeFour");
51                     }
52                 } else if (notification == "AXFocusChanged") {
53                     focusCount++;
54                     if (focusCount == 2)
55                         eventSender.scheduleAsynchronousKeyDown("\t");
56                 }
57                 if (selectCount == 4 && focusCount == 3) {
58                     webArea.removeNotificationListener();
59                     finishJSTest();
60                 }
61             }
62
63             function runTest() {
64                 if (window.accessibilityController) {
65                     testRunner.overridePreference("WebKitTabToLinksPreferenceKey", 1);
66
67                     accessibilityController.enableEnhancedAccessibility(true);
68                     webArea = accessibilityController.rootElement.childAtIndex(0);
69                     webArea.setBoolAttributeValue("AXCaretBrowsingEnabled", true)
70
71                     addedNotification = webArea.addNotificationListener(notificationCallback);
72                     shouldBeTrue("addedNotification");
73
74                     eventSender.keyDown("\t");
75                 } else {
76                     debug("window.accessibilityController is not present");
77                     finishJSTest();
78                 }
79             }
80             </script>
81         <script src="../../resources/js-test-post.js"></script>
82     </body>
83 </html>