Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 1 | // 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 | */ |
| 10 | hterm.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 | */ |
| 19 | hterm.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 Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 30 | this.liveElement = div.firstChild.firstChild; |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 31 | |
| 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 | */ |
| 40 | hterm.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 Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 44 | this.accessibilityReader.newLine(); |
| 45 | this.accessibilityReader.announce('More output'); |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 46 | |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 47 | 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 Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 62 | |
| 63 | const observer = new MutationObserver(() => { |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 64 | if (checksToComplete[0]()) { |
| 65 | checksToComplete.shift(); |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 66 | } |
| 67 | |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 68 | if (checksToComplete.length == 0) { |
| 69 | observer.disconnect(); |
| 70 | result.pass(); |
| 71 | } |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 72 | }); |
| 73 | |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 74 | observer.observe(this.liveElement, {attributes: true}); |
| 75 | // This should only need to be 2x the initial delay but we wait longer to |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 76 | // avoid flakiness. |
| 77 | result.requestTime(500); |
| 78 | }); |
| 79 | |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 80 | |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 81 | /** |
| 82 | * Test that after text has been added to the live region, there is again a |
| 83 | * delay before adding more text. |
| 84 | */ |
| 85 | hterm.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 Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 89 | this.accessibilityReader.newLine(); |
| 90 | this.accessibilityReader.announce('More output'); |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 91 | |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 92 | result.assertEQ('', this.liveElement.getAttribute('aria-label')); |
| 93 | |
| 94 | const checkClear = () => { |
| 95 | result.assertEQ('', this.liveElement.getAttribute('aria-label')); |
| 96 | return true; |
| 97 | }; |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 98 | |
| 99 | const checkFirstAnnounce = () => { |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 100 | result.assertEQ('Some test output Some other test output\nMore output', |
| 101 | this.liveElement.getAttribute('aria-label')); |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 102 | |
| 103 | this.accessibilityReader.announce('more text'); |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 104 | this.accessibilityReader.newLine(); |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 105 | this.accessibilityReader.announce('...and more'); |
| 106 | return true; |
| 107 | }; |
| 108 | |
| 109 | const checkSecondAnnounce = () => { |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 110 | result.assertEQ('more text\n...and more', |
| 111 | this.liveElement.getAttribute('aria-label')); |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 112 | return true; |
| 113 | }; |
| 114 | |
Raymes Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 115 | const checksToComplete = [checkClear, |
| 116 | checkFirstAnnounce, |
| 117 | checkClear, |
| 118 | checkSecondAnnounce]; |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 119 | |
| 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 Khoury | f1c61ba | 2018-05-28 14:05:38 +1000 | [diff] [blame^] | 131 | observer.observe(this.liveElement, {attributes: true}); |
Raymes Khoury | 3e44bc9 | 2018-05-17 10:54:23 +1000 | [diff] [blame] | 132 | // This should only need to be 2x the initial delay but we wait longer to |
| 133 | // avoid flakiness. |
| 134 | result.requestTime(500); |
| 135 | }); |