AX: Implement bounds/position and index related text marker functions using TextIterator
[WebKit-https.git] / LayoutTests / accessibility / mac / text-marker-word-nav.html
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2 <html>
3 <head>
4 <meta charset="utf-8"> 
5 <script src="../../resources/js-test-pre.js"></script>
6 </head>
7 <style>
8 .userselect { user-select: none; -webkit-user-select: none; }
9 </style>
10 <body id="body">
11
12 <div id="text" tabindex="0">word1 test</div>
13 <span id="span">Thisis</span>longword I<span>'ll try.</span>
14 Test Content<span id="target" contenteditable="true">editable is working.</span>
15
16 <div id="text2">
17 c <img src="#" aria-label="blah" style="background-color: #aaaaaa; width: 100px; height: 100px;">d
18 </div>
19
20 <div class="userselect" id="text3">can't select</div>
21
22 <div id="text4">
23 巧克力是食物吗?
24 </div>
25 <div id="text4a">
26 كيف حالك؟
27 </div>
28
29 <pre id="text5">
30 both   spaces
31 line breaks
32 </pre>
33
34 <div id="text6">
35 some<br>text
36 </div>
37
38 <div id="text7">
39 test audio <audio controls><source src="test.mp3" type="audio/mpeg"></audio>file
40 </div>
41
42 <p id="text8">
43 <strong>Edit</strong>
44 text
45 </p>
46
47 <p id="description"></p>
48 <div id="console"></div>
49
50 <script>
51
52     description("This tests that word navigation is working correctly.");
53     
54     if (window.accessibilityController) {
55         
56         var text = accessibilityController.accessibleElementById("text");
57         // Get the actual text node.
58         text = text.childAtIndex(0);
59         
60         // Check that we can get the word range. Land at "w" in "word1 test".
61         var textMarkerRange = text.textMarkerRangeForElement(text);
62         var startMarker = text.startTextMarkerForTextMarkerRange(textMarkerRange);
63         var currentMarker = advanceAndVerify(startMarker, 1, text);
64         
65         // Check that we are at the end of paragraph, so right word should be empty
66         currentMarker = advanceAndVerify(currentMarker, 9, text);
67         
68         // Check the case with span
69         // At "T" in "Thisis", should return the word as "Thisislongword".
70         currentMarker = advanceAndVerify(currentMarker, 2, text);
71         // At " " before "I", the word should be "I'll".
72         currentMarker = advanceAndVerify(currentMarker, 14, text);
73         // At " " before "try", the word should excludes "."
74         currentMarker = advanceAndVerify(currentMarker, 5, text);
75         
76         // Check the case with contenteditable
77         // At "e" in "editable", the word should NOT include "Content" before it.
78         currentMarker = advanceAndVerify(currentMarker, 18, text);
79         
80         // Check the case with replaced node, the replaced node should be considered a word.
81         var text2 = accessibilityController.accessibleElementById("text2");
82         textMarkerRange = text2.textMarkerRangeForElement(text2);
83         currentMarker = text2.startTextMarkerForTextMarkerRange(textMarkerRange);
84         currentMarker = advanceAndVerify(currentMarker, 2, text2);
85         currentMarker = advanceAndVerify(currentMarker, 1, text2);
86         
87         // Check user-select:none is also working.
88         var text3 = accessibilityController.accessibleElementById("text3");
89         textMarkerRange = text3.textMarkerRangeForElement(text3);
90         currentMarker = text3.startTextMarkerForTextMarkerRange(textMarkerRange);
91         currentMarker = advanceAndVerify(currentMarker, 1, text3);
92         
93         // Check multi-language, Chinese here.
94         var text4 = accessibilityController.accessibleElementById("text4");
95         textMarkerRange = text4.textMarkerRangeForElement(text4);
96         currentMarker = text4.startTextMarkerForTextMarkerRange(textMarkerRange);
97         // Make sure when we are at the beginning of line, it won't go to previous node.
98         currentMarker = advanceAndVerify(currentMarker, 0, text4);
99         currentMarker = advanceAndVerify(currentMarker, 2, text4);
100         currentMarker = advanceAndVerify(currentMarker, 1, text4);
101         currentMarker = advanceAndVerify(currentMarker, 1, text4);
102         // And Arabic
103         var text4a = accessibilityController.accessibleElementById("text4a");
104         textMarkerRange = text4a.textMarkerRangeForElement(text4a);
105         currentMarker = text4a.startTextMarkerForTextMarkerRange(textMarkerRange);
106         currentMarker = advanceAndVerify(currentMarker, 3, text4a);
107         currentMarker = advanceAndVerify(currentMarker, 1, text4a);
108         
109         // Check text in pre tag with line breaks.
110         var text5 = accessibilityController.accessibleElementById("text5");
111         textMarkerRange = text5.textMarkerRangeForElement(text5);
112         currentMarker = text5.startTextMarkerForTextMarkerRange(textMarkerRange);
113         // At "h" in "both", right word should be "   ".
114         currentMarker = advanceAndVerify(currentMarker, 4, text5);
115         // At the end of first line, right word should be new line.
116         currentMarker = advanceAndVerify(currentMarker, 9, text5);
117         
118         // Check text with br tag in it.
119         var text6 = accessibilityController.accessibleElementById("text6");
120         textMarkerRange = text6.textMarkerRangeForElement(text6);
121         currentMarker = text6.startTextMarkerForTextMarkerRange(textMarkerRange);
122         currentMarker = advanceAndVerify(currentMarker, 4, text6);
123         currentMarker = advanceAndVerify(currentMarker, 1, text6);
124         
125         // Check <audio> element.
126         var text7 = accessibilityController.accessibleElementById("text7");
127         textMarkerRange = text7.textMarkerRangeForElement(text7);
128         currentMarker = text7.startTextMarkerForTextMarkerRange(textMarkerRange);
129         currentMarker = advanceAndVerify(currentMarker, 11, text7);
130         currentMarker = advanceAndVerify(currentMarker, 1, text7);
131         currentMarker = advanceAndVerify(currentMarker, 1, text7);
132         
133         // For node with text node children, we should treat the visual space as word boundary.
134         var text8 = accessibilityController.accessibleElementById("text8");
135         textMarkerRange = text8.textMarkerRangeForElement(text8);
136         currentMarker = text8.startTextMarkerForTextMarkerRange(textMarkerRange);
137         currentMarker = advanceAndVerify(currentMarker, 4, text8);
138         
139         // Check the word marker runs from start to end, and backwards.
140         // Make sure it won't hang.
141         verifyDocument(text);
142         
143         function advanceAndVerify(currentMarker, offset, obj) {
144             var previousMarker = currentMarker;
145             for (var i = 0; i < offset; i++) {
146                 previousMarker = currentMarker;
147                 currentMarker = obj.nextTextMarker(previousMarker);
148             }
149             verifyWordRangeForTextMarker(previousMarker, currentMarker, obj);
150             return currentMarker;
151         }
152         
153         function replaceAttachmentInString(str) {
154             var newline = '\n';
155             str =  str.replace(String.fromCharCode(65532), "[ATTACHMENT]");
156             str = str.replace(newline, "'line break'");
157             return str;
158         }
159         
160         function verifyWordRangeForTextMarker(preMarker, textMarker, obj) {
161             var markerRange = obj.textMarkerRangeForMarkers(preMarker, textMarker);
162             var currentCharacter = replaceAttachmentInString(obj.stringForTextMarkerRange(markerRange));
163             debug("Current character is: " + currentCharacter);
164             
165             var previousWordRange = obj.leftWordTextMarkerRangeForTextMarker(textMarker);
166             var nextWordRange = obj.rightWordTextMarkerRangeForTextMarker(textMarker);
167             var preWord = replaceAttachmentInString(obj.stringForTextMarkerRange(previousWordRange));
168             var nextWord = replaceAttachmentInString(obj.stringForTextMarkerRange(nextWordRange));
169             debug("Left word is: " + preWord);
170             debug("Right word is: " + nextWord);
171             
172             var preWordStart = obj.previousWordStartTextMarkerForTextMarker(textMarker);
173             var nextWordEnd = obj.nextWordEndTextMarkerForTextMarker(textMarker);
174             var preAndNextWordRange = obj.textMarkerRangeForMarkers(preWordStart, nextWordEnd);
175             var preAndNextWord = replaceAttachmentInString(obj.stringForTextMarkerRange(preAndNextWordRange));
176             debug("Pre word start to next word end: " + preAndNextWord + "\n");
177         }
178         
179         function verifyDocument(obj) {
180             var start = obj.startTextMarker;
181             
182             // Going forward.
183             debug("Test going forward.");
184             var current = start;
185             var endWord = "file";
186             var currWord = "";
187             while(currWord != endWord) {
188                 var nextWordRange = obj.rightWordTextMarkerRangeForTextMarker(current);
189                 currWord = obj.stringForTextMarkerRange(nextWordRange);
190                 current = obj.nextWordEndTextMarkerForTextMarker(current);
191             }
192             debug("End word: " + replaceAttachmentInString(currWord));
193             
194             // Going backwards.
195             debug("\nTest going backwards.");
196             var startWord = "word1";
197             currWord = ""; 
198             while(currWord != startWord) {
199                 var previousWordRange = obj.leftWordTextMarkerRangeForTextMarker(current);
200                 currWord = obj.stringForTextMarkerRange(previousWordRange);
201                 current = obj.previousWordStartTextMarkerForTextMarker(current);
202             }
203             debug("Start word: " + replaceAttachmentInString(currWord));
204         }
205     }
206
207 </script>
208
209 <script src="../../resources/js-test-post.js"></script>
210 </body>
211 </html>