forked from libapps/libapps-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhterm_accessibility_reader_tests.js
More file actions
556 lines (470 loc) · 20.3 KB
/
hterm_accessibility_reader_tests.js
File metadata and controls
556 lines (470 loc) · 20.3 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview hterm.AccessibilityReader unit tests.
*/
import {hterm} from '../index.js';
/**
* Set up state for all the tests in this suite.
*/
before(() => {
// Stub out the delay loops. We don't have to worry about waiting for input
// from the user to accumulate as we don't do that.
/** @suppress {constantProperty} */
hterm.AccessibilityReader.DELAY = 0;
});
/*
* Create a new hterm.AccessibilityReader object for testing.
*
* Called before each test case in this suite.
*/
beforeEach(function() {
const document = globalThis.document;
const div = this.div = document.createElement('div');
div.style.position = 'absolute';
div.style.height = '100%';
div.style.width = '100%';
this.accessibilityReader = new hterm.AccessibilityReader(div);
this.accessibilityReader.setAccessibilityEnabled(true);
this.liveElement = div.firstChild.firstChild;
this.assertiveLiveElement = this.liveElement.nextSibling;
document.body.appendChild(div);
});
/**
* Clean up the hterm.AccessibilityReader object.
*/
afterEach(function() {
globalThis.document.body.removeChild(this.div);
});
/**
* Test that printing text to the terminal will cause nodes to be added to the
* live region for accessibility purposes. This shouldn't happen until after a
* small delay has passed.
*/
it('a11y-live-region-single-delay', function(done) {
this.accessibilityReader.announce('Some test output');
this.accessibilityReader.announce('Some other test output');
this.accessibilityReader.newLine();
this.accessibilityReader.announce('More output');
assert.equal('', this.liveElement.innerText);
const observer = new MutationObserver(() => {
assert.equal('Some test output Some other test output\nMore output',
this.liveElement.innerText);
observer.disconnect();
done();
});
observer.observe(this.liveElement, {childList: true});
});
/**
* Test that after text has been added to the live region, there is again a
* delay before adding more text.
*/
it('a11y-live-region-double-delay', function(done) {
this.accessibilityReader.announce('Some test output');
this.accessibilityReader.announce('Some other test output');
this.accessibilityReader.newLine();
this.accessibilityReader.announce('More output');
assert.equal('', this.liveElement.innerText);
const checkFirstAnnounce = () => {
assert.equal('Some test output Some other test output\nMore output',
this.liveElement.innerText);
this.accessibilityReader.announce('more text');
this.accessibilityReader.newLine();
this.accessibilityReader.announce('...and more');
return true;
};
const checkSecondAnnounce = () => {
assert.equal('more text\n...and more',
this.liveElement.innerText);
return true;
};
const checksToComplete = [checkFirstAnnounce, checkSecondAnnounce];
const observer = new MutationObserver(() => {
if (checksToComplete[0]()) {
checksToComplete.shift();
}
if (checksToComplete.length == 0) {
observer.disconnect();
done();
}
});
observer.observe(this.liveElement, {childList: true});
});
/**
* Test that adding the same text twice to the live region gets slightly
* modified to trigger an attribute change.
*/
it('a11y-live-region-duplicate-text', function(done) {
this.accessibilityReader.announce('Some test output');
assert.equal('', this.liveElement.innerText);
const checkFirstAnnounce = () => {
assert.equal('Some test output',
this.liveElement.innerText);
this.accessibilityReader.announce('Some test output');
return true;
};
const checkSecondAnnounce = () => {
assert.equal('\nSome test output',
this.liveElement.innerText);
return true;
};
const checksToComplete = [checkFirstAnnounce, checkSecondAnnounce];
const observer = new MutationObserver(() => {
if (checksToComplete[0]()) {
checksToComplete.shift();
}
if (checksToComplete.length == 0) {
observer.disconnect();
done();
}
});
observer.observe(this.liveElement, {childList: true});
});
/**
* Test that adding text to the assertive live region works correctly.
*/
it('a11y-assertive-live-region', function() {
this.accessibilityReader.assertiveAnnounce('Some test output');
assert.equal(this.assertiveLiveElement.innerText,
'Some test output');
this.accessibilityReader.clear();
assert.equal(this.assertiveLiveElement.innerText, '');
});
/**
* Test that adding the same text twice to the assertive live region gets
* slightly modified to trigger an attribute change.
*/
it('a11y-assertive-live-region-duplicate-text', function() {
this.accessibilityReader.assertiveAnnounce('Some test output');
assert.equal(this.assertiveLiveElement.innerText,
'Some test output');
this.accessibilityReader.assertiveAnnounce('Some test output');
assert.equal(this.assertiveLiveElement.innerText,
'\nSome test output');
});
/**
* Test that adding text to the assertive live region interrupts polite
* announcements.
*/
it('a11y-assertive-live-region-interrupts-polite', function(done) {
this.accessibilityReader.announce('Some test output');
this.accessibilityReader.announce('Some other test output');
this.accessibilityReader.newLine();
this.accessibilityReader.announce('More output');
assert.equal(this.liveElement.innerText, '');
assert.equal(this.assertiveLiveElement.innerText, '');
// The live element should not change because we interrupt it. It should only
// announce the 'PASS' string which comes after all the output above.
const observer = new MutationObserver(() => {
if (this.liveElement.innerText == 'PASS') {
done();
} else {
assert.equal(this.liveElement.innerText, '');
}
});
observer.observe(this.liveElement, {childList: true});
this.accessibilityReader.assertiveAnnounce('Some test output');
assert.equal(this.assertiveLiveElement.innerText,
'Some test output');
this.accessibilityReader.announce('PASS');
});
/**
* Test that nothing is announced when accessibility is disabled.
*/
it('a11y-disabled-enabled', function(done) {
this.accessibilityReader.setAccessibilityEnabled(false);
this.accessibilityReader.announce('Some test output');
this.accessibilityReader.announce('Some other test output');
this.accessibilityReader.newLine();
this.accessibilityReader.announce('More output');
assert.equal(this.liveElement.innerText, '');
// Only 'Other output' should be announced now.
this.accessibilityReader.setAccessibilityEnabled(true);
this.accessibilityReader.announce('Other output');
const observer = new MutationObserver(() => {
if (this.liveElement.innerText == 'Other output') {
done();
} else {
assert.equal(this.liveElement.innerText, '');
}
});
observer.observe(this.liveElement, {childList: true});
});
/**
* Test that when accessibility is disabled, nothing else will be announced.
*/
it('a11y-enabled-disabled', function(done) {
this.accessibilityReader.announce('Some test output');
this.accessibilityReader.announce('Some other test output');
this.accessibilityReader.newLine();
this.accessibilityReader.announce('More output');
assert.equal(this.liveElement.innerText, '');
// The live element should not change because accessibility is disabled. It
// should only announce the 'PASS' string which comes after all the output
// above.
const observer = new MutationObserver(() => {
if (this.liveElement.innerText == 'PASS') {
done();
} else {
assert.equal(this.liveElement.innerText, '');
}
});
observer.observe(this.liveElement, {childList: true});
this.accessibilityReader.setAccessibilityEnabled(false);
this.accessibilityReader.setAccessibilityEnabled(true);
this.accessibilityReader.announce('PASS');
});
/**
* Test that when accessibility is disabled, assertive announcements still work.
* These are not performance sensitive so they don't need to be gated on the
* flag.
*/
it('a11y-assertive-disabled-enabled', function() {
this.accessibilityReader.setAccessibilityEnabled(false);
this.accessibilityReader.assertiveAnnounce('Some test output');
assert.equal(this.assertiveLiveElement.innerText,
'Some test output');
this.accessibilityReader.setAccessibilityEnabled(true);
this.accessibilityReader.assertiveAnnounce('More test output');
assert.equal(this.assertiveLiveElement.innerText,
'More test output');
});
/**
* Regression test for a bug that is caused by adding 2 newlines and then
* calling announce. In this case an exception was thrown.
*/
it('a11y-newlines-then-announce', function() {
this.accessibilityReader.newLine();
this.accessibilityReader.newLine();
this.accessibilityReader.announce('Some test output');
});
/**
* Test that moving the cursor left/right through output will cause the output
* to get assertively announced.
*/
it('a11y-selection-change-left-right', function() {
// Move the cursor right 1 character.
// Simulatue a user gesture.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.afterCursorChange('abc', 0, 1);
assert.equal(this.assertiveLiveElement.innerText, 'a');
// Move the cursor left 1 character.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 2);
this.accessibilityReader.afterCursorChange('abc', 0, 1);
assert.equal(this.assertiveLiveElement.innerText, 'b');
// Move the cursor right 1 character with wide chars in the string.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('匂へどabc', 0, 0);
this.accessibilityReader.afterCursorChange('匂へどabc', 0, 2);
assert.equal(this.assertiveLiveElement.innerText, '匂');
// Move the cursor left 1 character with wide chars in the string.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('匂へどabc', 0, 9);
this.accessibilityReader.afterCursorChange('匂へどabc', 0, 8);
assert.equal(this.assertiveLiveElement.innerText, 'c');
// Move the cursor to the end of the output.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.afterCursorChange('abc', 0, 3);
assert.equal(this.assertiveLiveElement.innerText, 'abc');
// Move the cursor to the start of the output.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 3);
this.accessibilityReader.afterCursorChange('abc', 0, 0);
assert.equal(this.assertiveLiveElement.innerText, 'abc');
// Don't move the cursor at all.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.afterCursorChange('abc', 0, 0);
assert.equal(this.assertiveLiveElement.innerText, '');
// Move the cursor 1 character but without a user gesture.
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.afterCursorChange('abc', 0, 1);
assert.equal(this.assertiveLiveElement.innerText, '');
// Move the cursor 1 character but have the output change at the same time.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.afterCursorChange('abcd', 0, 1);
assert.equal(this.assertiveLiveElement.innerText, '');
// Move the cursor 1 character but have the output change elsewhere on the
// screen at the same time.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.announce('foo bar');
this.accessibilityReader.afterCursorChange('abc', 0, 1);
assert.equal(this.assertiveLiveElement.innerText, '');
// Move the cursor 1 character but have the row change as well.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.afterCursorChange('abc', 1, 1);
assert.equal(this.assertiveLiveElement.innerText, '');
});
/**
* Test that other announcements are properly ignored or spoken when navigating
* left and right through output.
*/
it('a11y-selection-change-left-right-with-announce', function(done) {
// Move the cursor 1 character. In the process of doing this, a space
// character is printed somewhere in the terminal. It should get consumed and
// not announced as it may just be a side effect of the cursor change.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.announce(' ');
this.accessibilityReader.afterCursorChange('abc', 0, 1);
assert.equal(this.assertiveLiveElement.innerText, 'a');
// Do this again but 'foo bar' is announced during the cursor change.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.announce('foo bar');
this.accessibilityReader.afterCursorChange('abc', 0, 1);
assert.equal(this.assertiveLiveElement.innerText, '');
// We check that the space gets consumed and isn't announced but 'foo bar'
// still gets announced.
const observer = new MutationObserver(() => {
if (this.liveElement.innerText == 'foo bar') {
done();
}
});
observer.observe(this.liveElement, {childList: true});
});
/**
* Test that changes to the cursor due to backspace and deletion are properly
* announced.
*/
it('a11y-selection-change-backspace-delete', function() {
// Backspace a character at the start of the string.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 1);
this.accessibilityReader.afterCursorChange('bc', 0, 0);
assert.equal(this.assertiveLiveElement.innerText, 'a');
// Backspace a character at the end of the string.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 3);
this.accessibilityReader.afterCursorChange('ab', 0, 2);
assert.equal(this.assertiveLiveElement.innerText, 'c');
// Backspace a wide character.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('匂へど', 0, 6);
this.accessibilityReader.afterCursorChange('匂へ', 0, 4);
assert.equal(this.assertiveLiveElement.innerText, 'ど');
// Do this again but add an empty space in place of the deleted character. The
// terminal may do this as spaces are no different from empty space in the
// terminal.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 3);
this.accessibilityReader.afterCursorChange('ab ', 0, 2);
assert.equal(this.assertiveLiveElement.innerText, 'c');
// Do the same thing but add other text as well. The backspace shouldn't be
// announced.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 3);
this.accessibilityReader.afterCursorChange('ab e ', 0, 2);
assert.equal(this.assertiveLiveElement.innerText, '');
// Backspace a character in the middle of the string.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 2);
this.accessibilityReader.afterCursorChange('ac', 0, 1);
assert.equal(this.assertiveLiveElement.innerText, 'b');
// Delete a character at the start of the string.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 0);
this.accessibilityReader.afterCursorChange('bc', 0, 0);
assert.equal(this.assertiveLiveElement.innerText, 'a');
// Delete a character at the end of the string.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 2);
this.accessibilityReader.afterCursorChange('ab', 0, 2);
assert.equal(this.assertiveLiveElement.innerText, 'c');
// Delete the entire end of the line of text.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc: xyzabc', 0, 11);
this.accessibilityReader.afterCursorChange('abc: ', 0, 5);
assert.equal(this.assertiveLiveElement.innerText, 'xyzabc');
// Do this again but add an empty space in place of the deleted character. The
// terminal may do this as spaces are no different from empty space in the
// terminal.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 2);
this.accessibilityReader.afterCursorChange('ab ', 0, 2);
assert.equal(this.assertiveLiveElement.innerText, 'c');
// Do the same thing but add other text as well. The delete shouldn't be
// announced.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 2);
this.accessibilityReader.afterCursorChange('ab e ', 0, 2);
assert.equal(this.assertiveLiveElement.innerText, '');
// Delete a character in the middle of the string.
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 1);
this.accessibilityReader.afterCursorChange('ac', 0, 1);
assert.equal(this.assertiveLiveElement.innerText, 'b');
// Backspace a character without a user gesture.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.beforeCursorChange('abc', 0, 1);
this.accessibilityReader.afterCursorChange('bc', 0, 0);
assert.equal(this.assertiveLiveElement.innerText, '');
});
/**
* Test that other output isn't announced during a backspace/deletion selection
* change.
*/
it('a11y-selection-change-backspace-with-announce', function(done) {
// Backspace a character. If other text is announced to the terminal in the
// process, we ignore it. This is because lots of updates can happen during a
// backspace (e.g. all the characers after the deleted character need to be
// moved and reprinted).
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 1);
this.accessibilityReader.announce('bc');
this.accessibilityReader.afterCursorChange('bc', 0, 0);
assert.equal(this.assertiveLiveElement.innerText, 'a');
// Announce something afterward to ensure the mutation observer fires and
// avoid timing out the test..
this.accessibilityReader.announce('foo');
const observer = new MutationObserver(() => {
if (this.liveElement.innerText == 'foo') {
done();
}
});
observer.observe(this.liveElement, {childList: true});
});
/**
* Test that entering a space character triggers 'Space' to be spoken.
*/
it('a11y-selection-space', function() {
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 3);
this.accessibilityReader.announce(' ');
this.accessibilityReader.afterCursorChange('abc ', 0, 4);
assert.equal(this.assertiveLiveElement.innerText, 'Space');
// No space announced if the cursor doesn't move.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc', 0, 3);
this.accessibilityReader.announce(' ');
this.accessibilityReader.afterCursorChange('abc ', 0, 3);
assert.equal(this.assertiveLiveElement.innerText, '');
// No space announced if a space is not printed to the screen.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.hasUserGesture = true;
this.accessibilityReader.beforeCursorChange('abc ', 0, 4);
this.accessibilityReader.announce('d');
this.accessibilityReader.afterCursorChange('dabc ', 0, 5);
assert.equal(this.assertiveLiveElement.innerText, '');
// No space announced if there's not a user gesture.
this.assertiveLiveElement.innerText = '';
this.accessibilityReader.beforeCursorChange('abc', 0, 3);
this.accessibilityReader.announce(' ');
this.accessibilityReader.afterCursorChange('abc ', 0, 4);
assert.equal(this.assertiveLiveElement.innerText, '');
});