blob: a5cb497a4dcb96360f2a2fbeafe61a46b7e90a04 [file] [log] [blame]
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001// Copyright 2018 The Chromium OS 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.
4
5'use strict';
6
7/**
8 * @fileoverview hterm.AccessibilityReader unit tests.
9 */
10hterm.AccessibilityReader.Tests = new lib.TestManager.Suite(
11 'hterm.AccessibilityReader.Tests');
12
13/**
14 * Clear out the current document and create a new hterm.AccessibilityReader
15 * object for testing.
16 *
17 * Called before each test case in this suite.
18 */
19hterm.AccessibilityReader.Tests.prototype.preamble = function(result, cx) {
20 const document = cx.window.document;
21
22 document.body.innerHTML = '';
23
24 const div = this.div = document.createElement('div');
25 div.style.position = 'absolute';
26 div.style.height = '100%';
27 div.style.width = '100%';
28
29 this.accessibilityReader = new hterm.AccessibilityReader(div);
Raymes Khouryf1c61ba2018-05-28 14:05:38 +100030 this.liveElement = div.firstChild.firstChild;
Raymes Khoury3e44bc92018-05-17 10:54:23 +100031
32 document.body.appendChild(div);
33};
34
35/**
36 * Test that printing text to the terminal will cause nodes to be added to the
37 * live region for accessibility purposes. This shouldn't happen until after a
38 * small delay has passed.
39 */
40hterm.AccessibilityReader.Tests.addTest(
41 'a11y-live-region-single-delay', function(result, cx) {
42 this.accessibilityReader.announce('Some test output');
43 this.accessibilityReader.announce('Some other test output');
Raymes Khouryf1c61ba2018-05-28 14:05:38 +100044 this.accessibilityReader.newLine();
45 this.accessibilityReader.announce('More output');
Raymes Khoury3e44bc92018-05-17 10:54:23 +100046
Raymes Khouryf1c61ba2018-05-28 14:05:38 +100047 result.assertEQ('', this.liveElement.getAttribute('aria-label'));
48
49 const checkClear = () => {
50 result.assertEQ('',
51 this.liveElement.getAttribute('aria-label'));
52 return true;
53 };
54
55 const checkFirstAnnounce = () => {
56 result.assertEQ('Some test output Some other test output\nMore output',
57 this.liveElement.getAttribute('aria-label'));
58 return true;
59 };
60
61 const checksToComplete = [checkClear, checkFirstAnnounce];
Raymes Khoury3e44bc92018-05-17 10:54:23 +100062
63 const observer = new MutationObserver(() => {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +100064 if (checksToComplete[0]()) {
65 checksToComplete.shift();
Raymes Khoury3e44bc92018-05-17 10:54:23 +100066 }
67
Raymes Khouryf1c61ba2018-05-28 14:05:38 +100068 if (checksToComplete.length == 0) {
69 observer.disconnect();
70 result.pass();
71 }
Raymes Khoury3e44bc92018-05-17 10:54:23 +100072 });
73
Raymes Khouryf1c61ba2018-05-28 14:05:38 +100074 observer.observe(this.liveElement, {attributes: true});
75 // This should only need to be 2x the initial delay but we wait longer to
Raymes Khoury3e44bc92018-05-17 10:54:23 +100076 // avoid flakiness.
77 result.requestTime(500);
78});
79
Raymes Khouryf1c61ba2018-05-28 14:05:38 +100080
Raymes Khoury3e44bc92018-05-17 10:54:23 +100081/**
82 * Test that after text has been added to the live region, there is again a
83 * delay before adding more text.
84 */
85hterm.AccessibilityReader.Tests.addTest(
86 'a11y-live-region-double-delay', function(result, cx) {
87 this.accessibilityReader.announce('Some test output');
88 this.accessibilityReader.announce('Some other test output');
Raymes Khouryf1c61ba2018-05-28 14:05:38 +100089 this.accessibilityReader.newLine();
90 this.accessibilityReader.announce('More output');
Raymes Khoury3e44bc92018-05-17 10:54:23 +100091
Raymes Khouryf1c61ba2018-05-28 14:05:38 +100092 result.assertEQ('', this.liveElement.getAttribute('aria-label'));
93
94 const checkClear = () => {
95 result.assertEQ('', this.liveElement.getAttribute('aria-label'));
96 return true;
97 };
Raymes Khoury3e44bc92018-05-17 10:54:23 +100098
99 const checkFirstAnnounce = () => {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +1000100 result.assertEQ('Some test output Some other test output\nMore output',
101 this.liveElement.getAttribute('aria-label'));
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000102
103 this.accessibilityReader.announce('more text');
Raymes Khouryf1c61ba2018-05-28 14:05:38 +1000104 this.accessibilityReader.newLine();
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000105 this.accessibilityReader.announce('...and more');
106 return true;
107 };
108
109 const checkSecondAnnounce = () => {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +1000110 result.assertEQ('more text\n...and more',
111 this.liveElement.getAttribute('aria-label'));
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000112 return true;
113 };
114
Raymes Khouryf1c61ba2018-05-28 14:05:38 +1000115 const checksToComplete = [checkClear,
116 checkFirstAnnounce,
117 checkClear,
118 checkSecondAnnounce];
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000119
120 const observer = new MutationObserver(() => {
121 if (checksToComplete[0]()) {
122 checksToComplete.shift();
123 }
124
125 if (checksToComplete.length == 0) {
126 observer.disconnect();
127 result.pass();
128 }
129 });
130
Raymes Khouryf1c61ba2018-05-28 14:05:38 +1000131 observer.observe(this.liveElement, {attributes: true});
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000132 // This should only need to be 2x the initial delay but we wait longer to
133 // avoid flakiness.
134 result.requestTime(500);
135});