1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Include test fixture.
6 GEN_INCLUDE(['../../testing/chromevox_next_e2e_test_base.js']);
9 * Test fixture for cvox2.cursors.
11 * @extends {ChromeVoxNextE2ETest}
13 function CursorsTest() {
14 ChromeVoxNextE2ETest.call(this);
17 CursorsTest.prototype = {
18 __proto__: ChromeVoxNextE2ETest.prototype,
20 /** Test cursors.Cursor. @const {string} */
23 /** Test cursors.Range. @const {string} */
29 window.BACKWARD = AutomationUtil.Dir.BACKWARD;
30 window.FORWARD = AutomationUtil.Dir.FORWARD;
31 window.CHARACTER = cursors.Unit.CHARACTER;
32 window.WORD = cursors.Unit.WORD;
33 window.LINE = cursors.Unit.LINE;
34 window.DOM_NODE = cursors.Unit.DOM_NODE;
35 window.BOUND = cursors.Movement.BOUND;
36 window.DIRECTIONAL = cursors.Movement.DIRECTIONAL;
40 * Performs a series of operations on a cursor and asserts the result.
41 * @param {cursors.Cursor} cursor The starting cursor.
42 * @param {!Array<Array<
47 * moves An array of arrays. Each inner array contains 4 items: unit,
48 * movement, direction, and assertions object. See example below.
50 cursorMoveAndAssert: function(cursor, moves) {
52 while (move = moves.shift()) {
53 cursor = cursor.move(move[0], move[1], move[2]);
54 var expected = move[3];
55 this.makeCursorAssertion(expected, cursor);
60 * Performs a series of operations on a range and asserts the result.
61 * @param {cursors.Range} range The starting range.
62 * @param {!Array<Array<
67 * moves An array of arrays. Each inner array contains 4 items: unit,
68 * direction, start and end assertions objects. See example below.
70 rangeMoveAndAssert: function(range, moves) {
72 while (move = moves.shift()) {
73 range = range.move(move[0], move[1]);
74 var expectedStart = move[2];
75 var expectedEnd = move[3];
77 this.makeCursorAssertion(expectedStart, range.start);
78 this.makeCursorAssertion(expectedEnd, range.end);
83 * Makes assertions about the given |cursor|.
84 * @param {Object} expected
85 * @param {Cursor} cursor
87 makeCursorAssertion: function(expected, cursor) {
88 if (goog.isDef(expected.index))
89 assertEquals(expected.index, cursor.getIndex());
90 if (goog.isDef(expected.value))
91 assertEquals(expected.value, cursor.node.value);
95 * Runs the specified moves on the |doc| and asserts expectations.
96 * @param {function} doc
97 * @param {string=} opt_testType Either CURSOR or RANGE.
99 runCursorMovesOnDocument: function(doc, moves, opt_testType) {
100 this.runWithLoadedTree(doc,
104 // This occurs as a result of a load complete.
105 var start = AutomationUtil.findNodePost(root,
107 AutomationPredicate.leaf);
109 var cursor = new cursors.Cursor(start, 0);
110 if (!opt_testType || opt_testType == this.CURSOR) {
111 var cursor = new cursors.Cursor(start, 0);
112 this.cursorMoveAndAssert(cursor, moves);
113 } else if (opt_testType == this.RANGE) {
114 var range = new cursors.Range(cursor, cursor);
115 this.rangeMoveAndAssert(range, moves);
120 simpleDoc: function() {/*!
121 <p>start <span>same line</span>
125 multiInlineDoc: function() {/*!
126 <p style='max-width: 5px'>start diff line</p>
131 TEST_F('CursorsTest', 'CharacterCursor', function() {
132 this.runCursorMovesOnDocument(this.simpleDoc, [
133 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'start '}],
134 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}],
135 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}],
137 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'start '}],
138 [CHARACTER, DIRECTIONAL, FORWARD, {index: 2, value: 'start '}],
139 [CHARACTER, DIRECTIONAL, FORWARD, {index: 3, value: 'start '}],
140 [CHARACTER, DIRECTIONAL, FORWARD, {index: 4, value: 'start '}],
141 [CHARACTER, DIRECTIONAL, FORWARD, {index: 5, value: 'start '}],
143 [CHARACTER, DIRECTIONAL, FORWARD, {index: 0, value: 'same line'}],
144 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'same line'}],
145 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 0, value: 'same line'}],
147 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 5, value: 'start '}],]);
150 TEST_F('CursorsTest', 'WordCursor', function() {
151 this.runCursorMovesOnDocument(this.simpleDoc, [
153 [WORD, BOUND, BACKWARD, {index: 0, value: 'start '}],
154 [WORD, BOUND, BACKWARD, {index: 0, value: 'start '}],
155 [WORD, BOUND, FORWARD, {index: 5, value: 'start '}],
156 [WORD, BOUND, FORWARD, {index: 5, value: 'start '}],
158 // Word (DIRECTIONAL).
159 [WORD, DIRECTIONAL, FORWARD, {index: 0, value: 'same line'}],
160 [WORD, DIRECTIONAL, FORWARD, {index: 5, value: 'same line'}],
162 [WORD, DIRECTIONAL, FORWARD, {index: 0, value: 'end'}],
163 [WORD, DIRECTIONAL, FORWARD, {index: 0, value: 'end'}],
165 [WORD, DIRECTIONAL, BACKWARD, {index: 5, value: 'same line'}],
166 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'same line'}],
168 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}],
169 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}]]);
172 TEST_F('CursorsTest', 'CharacterWordCursor', function() {
173 this.runCursorMovesOnDocument(this.simpleDoc, [
174 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'start '}],
176 [WORD, DIRECTIONAL, FORWARD, {index: 0, value: 'same line'}],
177 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'same line'}],
178 [WORD, DIRECTIONAL, FORWARD, {index: 5, value: 'same line'}],
179 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 4, value: 'same line'}],
180 [WORD, DIRECTIONAL, FORWARD, {index: 5, value: 'same line'}],
181 [CHARACTER, DIRECTIONAL, FORWARD, {index: 6, value: 'same line'}],
182 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'same line'}],
183 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 5, value: 'start '}],
184 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 4, value: 'start '}],
185 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}]]);
188 TEST_F('CursorsTest', 'LineCursor', function() {
189 this.runCursorMovesOnDocument(this.simpleDoc, [
191 [LINE, BOUND, FORWARD, {value: 'same line'}],
192 [LINE, BOUND, FORWARD, {value: 'same line'}],
193 [LINE, BOUND, BACKWARD, {value: 'start '}],
194 [LINE, BOUND, BACKWARD, {value: 'start '}],
196 // Line (DIRECTIONAL).
197 [LINE, DIRECTIONAL, FORWARD, {value: 'end'}],
198 [LINE, DIRECTIONAL, FORWARD, {value: 'end'}],
199 [LINE, DIRECTIONAL, BACKWARD, {value: 'same line'}],
200 [LINE, DIRECTIONAL, BACKWARD, {value: 'same line'}],
201 [LINE, BOUND, BACKWARD, {value: 'start '}],
202 [LINE, DIRECTIONAL, FORWARD, {value: 'end'}]]);
205 TEST_F('CursorsTest', 'CharacterRange', function() {
206 this.runCursorMovesOnDocument(this.simpleDoc, [
208 {value: 'start ', index: 1}, {value: 'start ', index: 2}],
210 {value: 'start ', index: 2}, {value: 'start ', index: 3}],
212 {value: 'start ', index: 3}, {value: 'start ', index: 4}],
214 {value: 'start ', index: 4}, {value: 'start ', index: 5}],
216 {value: 'start ', index: 5}, {value: 'start ', index: 5}],
219 {value: 'same line', index: 0}, {value: 'same line', index: 1}],
221 [CHARACTER, BACKWARD,
222 {value: 'start ', index: 5}, {value: 'start ', index: 5}],
223 [CHARACTER, BACKWARD,
224 {value: 'start ', index: 4}, {value: 'start ', index: 5}],
225 [CHARACTER, BACKWARD,
226 {value: 'start ', index: 3}, {value: 'start ', index: 4}],
227 [CHARACTER, BACKWARD,
228 {value: 'start ', index: 2}, {value: 'start ', index: 3}],
229 [CHARACTER, BACKWARD,
230 {value: 'start ', index: 1}, {value: 'start ', index: 2}],
231 [CHARACTER, BACKWARD,
232 {value: 'start ', index: 0}, {value: 'start ', index: 1}],
233 [CHARACTER, BACKWARD,
234 {value: 'start ', index: 0}, {value: 'start ', index: 1}],
238 TEST_F('CursorsTest', 'WordRange', function() {
239 this.runCursorMovesOnDocument(this.simpleDoc, [
241 {value: 'same line', index: 0}, {value: 'same line', index: 4}],
243 {value: 'same line', index: 5}, {value: 'same line', index: 9}],
246 {value: 'end', index: 0}, {value: 'end', index: 3}],
248 {value: 'end', index: 0}, {value: 'end', index: 3}],
251 {value: 'same line', index: 5}, {value: 'same line', index: 9}],
253 {value: 'same line', index: 0}, {value: 'same line', index: 4}],
256 {value: 'start ', index: 0}, {value: 'start ', index: 5}],
258 {value: 'start ', index: 0}, {value: 'start ', index: 5}],
263 TEST_F('CursorsTest', 'LineRange', function() {
264 this.runCursorMovesOnDocument(this.simpleDoc, [
265 [LINE, FORWARD, {value: 'end', index: 0}, {value: 'end', index: 3}],
266 [LINE, FORWARD, {value: 'end', index: 0}, {value: 'end', index: 3}],
269 {value: 'start ', index: 0}, {value: 'same line', index: 9}],
272 {value: 'start ', index: 0}, {value: 'same line', index: 9}],
276 TEST_F('CursorsTest', 'DontSplitOnNodeNavigation', function() {
277 this.runWithLoadedTree(this.multiInlineDoc, function(root) {
278 var para = root.firstChild;
279 assertEquals('paragraph', para.role);
280 var cursor = new cursors.Cursor(para, 0);
281 cursor = cursor.move(DOM_NODE, DIRECTIONAL, FORWARD);
282 assertEquals('staticText', cursor.node.role);
283 assertEquals('end', cursor.node.value);
285 cursor = cursor.move(DOM_NODE, DIRECTIONAL, BACKWARD);
286 assertEquals('staticText', cursor.node.role);
287 assertEquals('start diff line', cursor.node.value);
289 assertEquals('inlineTextBox', cursor.node.firstChild.role);
290 assertEquals('start ', cursor.node.firstChild.value);
291 assertEquals('diff ', cursor.node.firstChild.nextSibling.value);
292 assertEquals('line', cursor.node.lastChild.value);