| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.a11y.aria.AnnouncerTest'); |
| 16 |
goog.setTestOnly('goog.a11y.aria.AnnouncerTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.Announcer'); |
| 20 |
goog.require('goog.a11y.aria.LivePriority'); |
| 21 |
goog.require('goog.a11y.aria.State'); |
| 22 |
goog.require('goog.array'); |
| 23 |
goog.require('goog.dom'); |
| 24 |
goog.require('goog.dom.TagName'); |
| 25 |
goog.require('goog.dom.iframe'); |
| 26 |
goog.require('goog.testing.MockClock'); |
| 27 |
goog.require('goog.testing.jsunit'); |
| 28 |
|
| 29 |
var sandbox; |
| 30 |
var someDiv; |
| 31 |
var someSpan; |
| 32 |
var mockClock; |
| 33 |
|
| 34 |
function setUp() { |
| 35 |
sandbox = goog.dom.getElement('sandbox'); |
| 36 |
someDiv = goog.dom.createDom(goog.dom.TagName.DIV, {id: 'someDiv'}, 'DIV'); |
| 37 |
someSpan = |
| 38 |
goog.dom.createDom(goog.dom.TagName.SPAN, {id: 'someSpan'}, 'SPAN'); |
| 39 |
sandbox.appendChild(someDiv); |
| 40 |
someDiv.appendChild(someSpan); |
| 41 |
|
| 42 |
mockClock = new goog.testing.MockClock(true); |
| 43 |
} |
| 44 |
|
| 45 |
function tearDown() { |
| 46 |
goog.dom.removeChildren(sandbox); |
| 47 |
someDiv = null; |
| 48 |
someSpan = null; |
| 49 |
|
| 50 |
goog.dispose(mockClock); |
| 51 |
} |
| 52 |
|
| 53 |
function testAnnouncerAndDispose() { |
| 54 |
var text = 'test content'; |
| 55 |
var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper()); |
| 56 |
announcer.say(text); |
| 57 |
checkLiveRegionContains(text, 'polite'); |
| 58 |
goog.dispose(announcer); |
| 59 |
} |
| 60 |
|
| 61 |
function testAnnouncerTwice() { |
| 62 |
var text = 'test content1'; |
| 63 |
var text2 = 'test content2'; |
| 64 |
var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper()); |
| 65 |
announcer.say(text); |
| 66 |
announcer.say(text2); |
| 67 |
checkLiveRegionContains(text2, 'polite'); |
| 68 |
goog.dispose(announcer); |
| 69 |
} |
| 70 |
|
| 71 |
function testAnnouncerTwiceSameMessage() { |
| 72 |
var text = 'test content'; |
| 73 |
var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper()); |
| 74 |
announcer.say(text); |
| 75 |
var firstLiveRegion = getLiveRegion('polite'); |
| 76 |
announcer.say(text, undefined); |
| 77 |
var secondLiveRegion = getLiveRegion('polite'); |
| 78 |
assertEquals(firstLiveRegion, secondLiveRegion); |
| 79 |
checkLiveRegionContains(text, 'polite'); |
| 80 |
goog.dispose(announcer); |
| 81 |
} |
| 82 |
|
| 83 |
function testAnnouncerAssertive() { |
| 84 |
var text = 'test content'; |
| 85 |
var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper()); |
| 86 |
announcer.say(text, goog.a11y.aria.LivePriority.ASSERTIVE); |
| 87 |
checkLiveRegionContains(text, 'assertive'); |
| 88 |
goog.dispose(announcer); |
| 89 |
} |
| 90 |
|
| 91 |
function testAnnouncerInIframe() { |
| 92 |
var text = 'test content'; |
| 93 |
var frame = goog.dom.iframe.createWithContent(sandbox); |
| 94 |
var helper = |
| 95 |
goog.dom.getDomHelper(goog.dom.getFrameContentDocument(frame).body); |
| 96 |
var announcer = new goog.a11y.aria.Announcer(helper); |
| 97 |
announcer.say(text, 'polite', helper); |
| 98 |
checkLiveRegionContains(text, 'polite', helper); |
| 99 |
goog.dispose(announcer); |
| 100 |
} |
| 101 |
|
| 102 |
function testAnnouncerWithAriaHidden() { |
| 103 |
var text = 'test content1'; |
| 104 |
var text2 = 'test content2'; |
| 105 |
var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper()); |
| 106 |
announcer.say(text); |
| 107 |
// Set aria-hidden attribute on the live region (simulates a modal dialog |
| 108 |
// being opened). |
| 109 |
var liveRegion = getLiveRegion('polite'); |
| 110 |
goog.a11y.aria.setState(liveRegion, goog.a11y.aria.State.HIDDEN, true); |
| 111 |
|
| 112 |
// Announce a new message and make sure that the aria-hidden was removed. |
| 113 |
announcer.say(text2); |
| 114 |
checkLiveRegionContains(text2, 'polite'); |
| 115 |
assertEquals( |
| 116 |
'', goog.a11y.aria.getState(liveRegion, goog.a11y.aria.State.HIDDEN)); |
| 117 |
goog.dispose(announcer); |
| 118 |
} |
| 119 |
|
| 120 |
function getLiveRegion(priority, opt_domHelper) { |
| 121 |
var dom = opt_domHelper || goog.dom.getDomHelper(); |
| 122 |
var divs = dom.getElementsByTagNameAndClass(goog.dom.TagName.DIV, null); |
| 123 |
var liveRegions = []; |
| 124 |
goog.array.forEach(divs, function(div) { |
| 125 |
if (goog.a11y.aria.getState(div, 'live') == priority) { |
| 126 |
liveRegions.push(div); |
| 127 |
} |
| 128 |
}); |
| 129 |
assertEquals(1, liveRegions.length); |
| 130 |
return liveRegions[0]; |
| 131 |
} |
| 132 |
|
| 133 |
function checkLiveRegionContains(text, priority, opt_domHelper) { |
| 134 |
var liveRegion = getLiveRegion(priority, opt_domHelper); |
| 135 |
mockClock.tick(1); |
| 136 |
assertEquals(text, goog.dom.getTextContent(liveRegion)); |
| 137 |
} |