summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/common/braille_util_test.unitjs
blob: a1ee77ff9f44c3e59289f9cd834ed9a577a2982f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Include test fixture.
GEN_INCLUDE(['../testing/chromevox_unittest_base.js']);

/**
 * Test fixture.
 * @constructor
 * @extends {ChromeVoxUnitTestBase}
 */
function CvoxBrailleUtilUnitTest() {}

CvoxBrailleUtilUnitTest.prototype = {
  __proto__: ChromeVoxUnitTestBase.prototype,

  /** @override */
  closureModuleDeps: [
    'cvox.BrailleUtil',
    'cvox.CursorSelection',
    'cvox.NavigationShifter',
    'cvox.TestMsgs',
  ],

  /** @override */
  setUp: function() {
    cvox.ChromeVox.msgs = new cvox.TestMsgs();
  },

  /**
   * @param {!Node} expectedParent Expected parent node.
   * @param {!Node} node Node to examine.
   * @private
   */
  assertTextNodeChildOf_: function(expectedParent, node) {
    assertEquals(Node.TEXT_NODE, node.nodeType);
    assertEquals(expectedParent, node.parentNode);
  },

  /**
   * Helper to retrieve braille for testing.
   * @param {!cvox.CursorSelection} prevSel Previous selection.
   * @param {!cvox.CursorSelection} sel Current selection.
   * @return {!cvox.NavBraille} Resulting braille.
   * @private
   */
  getBraille_: function(prevSel, sel) {
    return (new cvox.NavigationShifter).getBraille(prevSel, sel);
  },

  /**
   * Asserts that two NavBraille objects are equal, ignoring spans.
   * @param {Object} expected Expected result, should have no spans.
   * @param {cvox.NavBraille} actual Actual result.
   */
  assertBrailleEquals: function(expected, actual) {
    actual = new cvox.NavBraille({
      text: actual.text.toString(),
      startIndex: actual.startIndex,
      endIndex: actual.endIndex
    });
    assertThat(actual, eqJSON(new cvox.NavBraille(expected)));
  }
};

TEST_F('CvoxBrailleUtilUnitTest', 'BrailleName', function() {
  this.loadHtml(
      '<div id="navbar">' +
      '<a id="1" href="one.com">one</a>' +
      '<a id="2" href="two.com">two</a>' +
      '<a id="3" href="three.com">three</a>' +
      '</div>');
  var navbar = cvox.CursorSelection.fromNode($('navbar'));
  var braille = this.getBraille_(navbar, navbar);
  this.assertBrailleEquals(
      {text: 'one lnk two lnk three lnk',
       startIndex: 0,
       endIndex: 1
      }, braille);

  var one =
      cvox.CursorSelection.fromNode($('1').firstChild);
  braille = this.getBraille_(one, one);
  this.assertBrailleEquals(
      {text: 'one lnk two lnk three lnk',
       startIndex: 0,
       endIndex: 1
      }, braille);

  var two =
      cvox.CursorSelection.fromNode($('2').firstChild);
  braille = this.getBraille_(one, two);
  this.assertBrailleEquals(
      {text: 'one lnk two lnk three lnk',
       startIndex: 8,
       endIndex: 9
      }, braille);

  var three =
      cvox.CursorSelection.fromNode($('3').firstChild);
  braille = this.getBraille_(two, three);
  this.assertBrailleEquals(
      {text: 'one lnk two lnk three lnk',
       startIndex: 16,
       endIndex: 17
      }, braille);
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'NameTemplate', function() {
  this.loadHtml(
      '<button id="1">Submit</button>' +
      '<input id="2" type="text" aria-label="Search">'
      );

  var button = cvox.CursorSelection.fromNode($('1'));

  this.assertBrailleEquals(
      {text: 'Submit btn',
       startIndex: 0,
       endIndex: 1
      }, this.getBraille_(button, button));

  var inputElement = $('2');
  var input = cvox.CursorSelection.fromNode(inputElement);

  // Note: the cursor appears where text would be typed.
  this.assertBrailleEquals(
      {text: 'Search: ed',
       startIndex: 0,
       endIndex: 1
      }, this.getBraille_(input, input));
  inputElement.focus();
  this.assertBrailleEquals(
      {text: 'Search:  ed',
       startIndex: 8,
       endIndex: 8
      }, this.getBraille_(input, input));
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'TextField', function() {
  this.loadHtml(
      '<input id="1" type="text" aria-label="Search" value="larry">'
      );

  var inputElement = $('1');
  var input = cvox.CursorSelection.fromNode(inputElement);

  // Note: the cursor appears where text would be typed.
  // The cursor is at the beginning by default.
  this.assertBrailleEquals(
      {text: 'Search: larry ed',
       startIndex: 0,
       endIndex: 1
      }, this.getBraille_(input, input));
  inputElement.focus();
  inputElement.selectionStart = 0;
  inputElement.selectionEnd = 5;
  this.assertBrailleEquals(
      {text: 'Search: larry ed',
       startIndex: 8,
       endIndex: 13
      }, this.getBraille_(input, input));
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'TextFieldEmpty', function() {
  this.loadHtml(
      '<input id="1" type="text">'
      );

  var inputElement = $('1');
  var input = cvox.CursorSelection.fromNode($('1'));

  this.assertBrailleEquals(
      {text: ': ed',
       startIndex: 0,
       endIndex: 1
      }, this.getBraille_(input, input));
  inputElement.focus();
  this.assertBrailleEquals(
      {text: ':  ed',
       startIndex: 2,
       endIndex: 2
      }, this.getBraille_(input, input));
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'TextFieldSelection', function() {
  this.loadHtml(
      '<input id="1" type="text" value="strawberry">'
      );

  var inputElem = $('1');
  inputElem.focus();
  var input = cvox.CursorSelection.fromNode(inputElem);

  // Selection.
  inputElem.selectionStart = 2;
  inputElem.selectionEnd = 5;
  this.assertBrailleEquals(
      {text: ': strawberry ed',
       startIndex: 4,
       endIndex: 7
      }, this.getBraille_(input, input));

  // Cursor at end.
  inputElem.selectionStart = 10;
  inputElem.selectionEnd = 10;
  this.assertBrailleEquals(
      {text: ': strawberry ed',
       startIndex: 12,
       endIndex: 12
      }, this.getBraille_(input, input));
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'StateTemplate', function() {
  this.loadHtml(
      '<input id="1" type="checkbox" aria-label="Save">');

  var checkbox = cvox.CursorSelection.fromNode($('1'));

  this.assertBrailleEquals(
      {text: 'Save chk ( )',
       startIndex: 0,
       endIndex: 1
      }, this.getBraille_(checkbox, checkbox));

  $('1').checked = true;

  this.assertBrailleEquals(
      {text: 'Save chk (x)',
       startIndex: 0,
       endIndex: 1
      }, this.getBraille_(checkbox, checkbox));
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'AccessKey', function() {
  this.loadHtml(
      '<a href="http://www.google.com" id="1" accesskey="g">Google</a>');

  var link = cvox.CursorSelection.fromNode($('1'));

  this.assertBrailleEquals(
      {text: 'Google lnk access key:g',
       startIndex: 0,
       endIndex: 1
      }, this.getBraille_(link, link));
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'ContainerTemplate', function() {
  this.loadHtml(
      '<h1>' +
      '<a id="1" href="#menu">Skip To Menu</a>' +
      '</h1>'
      );

  var link = cvox.CursorSelection.fromNode($('1'));

  var navBraille = this.getBraille_(
      cvox.CursorSelection.fromBody(), link);
  this.assertBrailleEquals(
      {text: 'h1 Skip To Menu intlnk',
       startIndex: 0,
       endIndex: 1
      }, navBraille);
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'LinkSpans', function() {
  this.loadHtml('<p><a id="1" href="#1">Hello</a> from' +
      ' <a id="2" href="//www.google.com/">ChromeVox</a>');
  var link1 = $('1');
  var link2 = $('2');
  var navBraille = this.getBraille_(
      cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(link1));
  assertEquals('Hello intlnk from ChromeVox lnk',
      navBraille.text.toString());
  assertEquals(link1, navBraille.text.getSpan(0));
  assertEquals(link1, navBraille.text.getSpan(11));
  assertEquals('undefined', typeof navBraille.text.getSpan(12));
  assertEquals('undefined', typeof navBraille.text.getSpan(17));
  assertEquals(link2, navBraille.text.getSpan(18));
  assertEquals(link2, navBraille.text.getSpan(30));
});


TEST_F('CvoxBrailleUtilUnitTest', 'VisitedLink', function() {
  this.loadHtml('<p><a id="1" href="http://visited.link">Hello</a> there.');
  var link = $('1');
  var navBraille = this.getBraille_(
      cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(link));
  this.assertBrailleEquals({text: 'Hello lnk there.',
                            startIndex: 0,
                            endIndex: 1},
                           navBraille);
  cvox.ChromeVox.visitedUrls[link.href] = true;
  navBraille = this.getBraille_(
      cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(link));
  this.assertBrailleEquals({text: 'Hello vlnk there.',
                            startIndex: 0,
                            endIndex: 1},
                           navBraille);
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'NestedElements', function() {
  this.loadHtml('<h1 id="test-h1">Larry, ' +
      '<a href="#batman" id="batman-link">Sergey</a> and Eric</h1>');
  var h1 = $('test-h1');
  var link = $('batman-link');
  var navBraille = this.getBraille_(
      cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(h1));
  assertEquals('h1 Larry, Sergey intlnk and Eric',
      navBraille.text.toString());
  this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(0));
  this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(5));
  assertEquals(link, navBraille.text.getSpan(15));
  this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(30));
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'GetTemplatedOverride', function() {
  assertEquals('Menu mnu',
               cvox.BrailleUtil.getTemplated(null, null,
                                             { 'name': 'Menu',
                                              'roleMsg': 'aria_role_menu' }).
                                                  toString());
  assertEquals('alrt: Watch out!',
              cvox.BrailleUtil.getTemplated(null, null,
                                            { 'name': 'Watch out!',
                                              'roleMsg': 'aria_role_alert' }).
                                                  toString());
  // Test all properties.  role, if present, overrides roleMsg.
  assertEquals('Name Value Role State',
              cvox.BrailleUtil.getTemplated(null, null,
                                            { 'name': 'Name',
                                              'role': 'Role',
                                              'roleMsg': 'excluded',
                                              'value': 'Value',
                                              'state': 'State'
                                            }).toString());
});


/**
 * @export
 */
TEST_F('CvoxBrailleUtilUnitTest', 'CreateValue', function() {
  var s;
  var valueSpan;
  var selectiponSpan;

  // Value without a selection.
  s = cvox.BrailleUtil.createValue('value');
  assertEquals('value', s.toString());
  assertUndefined(s.getSpanInstanceOf(cvox.ValueSelectionSpan));
  valueSpan = s.getSpanInstanceOf(cvox.ValueSpan);
  assertEquals(0, s.getSpanStart(valueSpan));
  assertEquals(s.getLength(), s.getSpanEnd(valueSpan));

  // Value with a carret at the start of the text.
  s = cvox.BrailleUtil.createValue('value', 0);
  selectionSpan = s.getSpanInstanceOf(cvox.ValueSelectionSpan);
  assertEquals(0, s.getSpanStart(selectionSpan));
  assertEquals(0, s.getSpanEnd(selectionSpan));

  // Value with a carret inside the text.
  s = cvox.BrailleUtil.createValue('value', 1);
  selectionSpan = s.getSpanInstanceOf(cvox.ValueSelectionSpan);
  assertEquals(1, s.getSpanStart(selectionSpan));
  assertEquals(1, s.getSpanEnd(selectionSpan));

  // Value with a carret at the end of the text.
  s = cvox.BrailleUtil.createValue('value', 5);
  selectionSpan = s.getSpanInstanceOf(cvox.ValueSelectionSpan);
  assertEquals(5, s.getSpanStart(selectionSpan));
  assertEquals(5, s.getSpanEnd(selectionSpan));

  // All of the value selected selected with reversed start and end.
  s = cvox.BrailleUtil.createValue('value', 5, 0);
  selectionSpan = s.getSpanInstanceOf(cvox.ValueSelectionSpan);
  assertEquals(0, s.getSpanStart(selectionSpan));
  assertEquals(5, s.getSpanEnd(selectionSpan));
});