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.BOUND = cursors.Movement.BOUND;
35 window.DIRECTIONAL = cursors.Movement.DIRECTIONAL;
39 * Performs a series of operations on a cursor and asserts the result.
40 * @param {cursors.Cursor} cursor The starting cursor.
41 * @param {!Array.<Array.<
46 * moves An array of arrays. Each inner array contains 4 items: unit,
47 * movement, direction, and assertions object. See example below.
49 cursorMoveAndAssert: function(cursor, moves) {
51 while (move = moves.shift()) {
52 cursor = cursor.move(move[0], move[1], move[2]);
53 var expected = move[3];
54 this.makeCursorAssertion(expected, cursor);
59 * Performs a series of operations on a range and asserts the result.
60 * @param {cursors.Range} range The starting range.
61 * @param {!Array.<Array.<
66 * moves An array of arrays. Each inner array contains 4 items: unit,
67 * direction, start and end assertions objects. See example below.
69 rangeMoveAndAssert: function(range, moves) {
71 while (move = moves.shift()) {
72 range = range.move(move[0], move[1]);
73 var expectedStart = move[2];
74 var expectedEnd = move[3];
76 this.makeCursorAssertion(expectedStart, range.getStart());
77 this.makeCursorAssertion(expectedEnd, range.getEnd());
82 * Makes assertions about the given |cursor|.
83 * @param {Object} expected
84 * @param {Cursor} cursor
86 makeCursorAssertion: function(expected, cursor) {
87 if (goog.isDef(expected.index))
88 assertEquals(expected.index, cursor.getIndex());
89 if (goog.isDef(expected.value))
90 assertEquals(expected.value, cursor.getNode().attributes.value);
94 * Runs the specified moves on the |doc| and asserts expectations.
95 * @param {function} doc
96 * @param {string=} opt_testType Either CURSOR or RANGE.
98 runCursorMovesOnDocument: function(doc, moves, opt_testType) {
99 this.runWithLoadedTree(doc,
103 // This occurs as a result of a load complete.
104 var start = AutomationUtil.findNodePost(root,
106 AutomationPredicate.leaf);
108 var cursor = new cursors.Cursor(start, 0);
109 if (!opt_testType || opt_testType == this.CURSOR) {
110 var cursor = new cursors.Cursor(start, 0);
111 this.cursorMoveAndAssert(cursor, moves);
112 } else if (opt_testType == this.RANGE) {
113 var range = new cursors.Range(cursor, cursor);
114 this.rangeMoveAndAssert(range, moves);
119 simpleDoc: function() {/*!
120 <p>start <span>same line</span>
125 TEST_F('CursorsTest', 'CharacterCursor', function() {
126 this.runCursorMovesOnDocument(this.simpleDoc, [
127 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'start '}],
128 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}],
129 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}],
131 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'start '}],
132 [CHARACTER, DIRECTIONAL, FORWARD, {index: 2, value: 'start '}],
133 [CHARACTER, DIRECTIONAL, FORWARD, {index: 3, value: 'start '}],
134 [CHARACTER, DIRECTIONAL, FORWARD, {index: 4, value: 'start '}],
135 [CHARACTER, DIRECTIONAL, FORWARD, {index: 5, value: 'start '}],
137 [CHARACTER, DIRECTIONAL, FORWARD, {index: 0, value: 'same line'}],
138 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'same line'}],
139 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 0, value: 'same line'}],
141 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 5, value: 'start '}],]);
144 TEST_F('CursorsTest', 'WordCursor', function() {
145 this.runCursorMovesOnDocument(this.simpleDoc, [
147 [WORD, BOUND, BACKWARD, {index: 0, value: 'start '}],
148 [WORD, BOUND, BACKWARD, {index: 0, value: 'start '}],
149 [WORD, BOUND, FORWARD, {index: 5, value: 'start '}],
150 [WORD, BOUND, FORWARD, {index: 5, value: 'start '}],
152 // Word (DIRECTIONAL).
153 [WORD, DIRECTIONAL, FORWARD, {index: 0, value: 'same line'}],
154 [WORD, DIRECTIONAL, FORWARD, {index: 5, value: 'same line'}],
156 [WORD, DIRECTIONAL, FORWARD, {index: 0, value: 'end'}],
157 [WORD, DIRECTIONAL, FORWARD, {index: 0, value: 'end'}],
159 [WORD, DIRECTIONAL, BACKWARD, {index: 5, value: 'same line'}],
160 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'same line'}],
162 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}],
163 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}]]);
166 TEST_F('CursorsTest', 'CharacterWordCursor', function() {
167 this.runCursorMovesOnDocument(this.simpleDoc, [
168 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'start '}],
170 [WORD, DIRECTIONAL, FORWARD, {index: 0, value: 'same line'}],
171 [CHARACTER, DIRECTIONAL, FORWARD, {index: 1, value: 'same line'}],
172 [WORD, DIRECTIONAL, FORWARD, {index: 5, value: 'same line'}],
173 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 4, value: 'same line'}],
174 [WORD, DIRECTIONAL, FORWARD, {index: 5, value: 'same line'}],
175 [CHARACTER, DIRECTIONAL, FORWARD, {index: 6, value: 'same line'}],
176 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'same line'}],
177 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 5, value: 'start '}],
178 [CHARACTER, DIRECTIONAL, BACKWARD, {index: 4, value: 'start '}],
179 [WORD, DIRECTIONAL, BACKWARD, {index: 0, value: 'start '}]]);
182 TEST_F('CursorsTest', 'LineCursor', function() {
183 this.runCursorMovesOnDocument(this.simpleDoc, [
185 [LINE, BOUND, FORWARD, {value: 'same line'}],
186 [LINE, BOUND, FORWARD, {value: 'same line'}],
187 [LINE, BOUND, BACKWARD, {value: 'start '}],
188 [LINE, BOUND, BACKWARD, {value: 'start '}],
190 // Line (DIRECTIONAL).
191 [LINE, DIRECTIONAL, FORWARD, {value: 'end'}],
192 [LINE, DIRECTIONAL, FORWARD, {value: 'end'}],
193 [LINE, DIRECTIONAL, BACKWARD, {value: 'same line'}],
194 [LINE, DIRECTIONAL, BACKWARD, {value: 'same line'}],
195 [LINE, BOUND, BACKWARD, {value: 'start '}],
196 [LINE, DIRECTIONAL, FORWARD, {value: 'end'}]]);
199 TEST_F('CursorsTest', 'CharacterRange', function() {
200 this.runCursorMovesOnDocument(this.simpleDoc, [
202 {value: 'start ', index: 1}, {value: 'start ', index: 2}],
204 {value: 'start ', index: 2}, {value: 'start ', index: 3}],
206 {value: 'start ', index: 3}, {value: 'start ', index: 4}],
208 {value: 'start ', index: 4}, {value: 'start ', index: 5}],
210 {value: 'start ', index: 5}, {value: 'start ', index: 5}],
213 {value: 'same line', index: 0}, {value: 'same line', index: 1}],
215 [CHARACTER, BACKWARD,
216 {value: 'start ', index: 5}, {value: 'start ', index: 5}],
217 [CHARACTER, BACKWARD,
218 {value: 'start ', index: 4}, {value: 'start ', index: 5}],
219 [CHARACTER, BACKWARD,
220 {value: 'start ', index: 3}, {value: 'start ', index: 4}],
221 [CHARACTER, BACKWARD,
222 {value: 'start ', index: 2}, {value: 'start ', index: 3}],
223 [CHARACTER, BACKWARD,
224 {value: 'start ', index: 1}, {value: 'start ', index: 2}],
225 [CHARACTER, BACKWARD,
226 {value: 'start ', index: 0}, {value: 'start ', index: 1}],
227 [CHARACTER, BACKWARD,
228 {value: 'start ', index: 0}, {value: 'start ', index: 1}],
232 TEST_F('CursorsTest', 'WordRange', function() {
233 this.runCursorMovesOnDocument(this.simpleDoc, [
235 {value: 'same line', index: 0}, {value: 'same line', index: 4}],
237 {value: 'same line', index: 5}, {value: 'same line', index: 9}],
240 {value: 'end', index: 0}, {value: 'end', index: 3}],
242 {value: 'end', index: 0}, {value: 'end', index: 3}],
245 {value: 'same line', index: 5}, {value: 'same line', index: 9}],
247 {value: 'same line', index: 0}, {value: 'same line', index: 4}],
250 {value: 'start ', index: 0}, {value: 'start ', index: 5}],
252 {value: 'start ', index: 0}, {value: 'start ', index: 5}],
257 TEST_F('CursorsTest', 'LineRange', function() {
258 this.runCursorMovesOnDocument(this.simpleDoc, [
259 [LINE, FORWARD, {value: 'end', index: 0}, {value: 'end', index: 3}],
260 [LINE, FORWARD, {value: 'end', index: 0}, {value: 'end', index: 3}],
263 {value: 'start ', index: 0}, {value: 'same line', index: 9}],
266 {value: 'start ', index: 0}, {value: 'same line', index: 9}],