announcer_test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. goog.provide('goog.a11y.aria.AnnouncerTest');
  15. goog.setTestOnly('goog.a11y.aria.AnnouncerTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.Announcer');
  18. goog.require('goog.a11y.aria.LivePriority');
  19. goog.require('goog.a11y.aria.State');
  20. goog.require('goog.array');
  21. goog.require('goog.dom');
  22. goog.require('goog.dom.TagName');
  23. goog.require('goog.dom.iframe');
  24. goog.require('goog.testing.MockClock');
  25. goog.require('goog.testing.jsunit');
  26. var sandbox;
  27. var someDiv;
  28. var someSpan;
  29. var mockClock;
  30. function setUp() {
  31. sandbox = goog.dom.getElement('sandbox');
  32. someDiv = goog.dom.createDom(goog.dom.TagName.DIV, {id: 'someDiv'}, 'DIV');
  33. someSpan =
  34. goog.dom.createDom(goog.dom.TagName.SPAN, {id: 'someSpan'}, 'SPAN');
  35. sandbox.appendChild(someDiv);
  36. someDiv.appendChild(someSpan);
  37. mockClock = new goog.testing.MockClock(true);
  38. }
  39. function tearDown() {
  40. goog.dom.removeChildren(sandbox);
  41. someDiv = null;
  42. someSpan = null;
  43. goog.dispose(mockClock);
  44. }
  45. function testAnnouncerAndDispose() {
  46. var text = 'test content';
  47. var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper());
  48. announcer.say(text);
  49. checkLiveRegionContains(text, 'polite');
  50. goog.dispose(announcer);
  51. }
  52. function testAnnouncerTwice() {
  53. var text = 'test content1';
  54. var text2 = 'test content2';
  55. var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper());
  56. announcer.say(text);
  57. announcer.say(text2);
  58. checkLiveRegionContains(text2, 'polite');
  59. goog.dispose(announcer);
  60. }
  61. function testAnnouncerTwiceSameMessage() {
  62. var text = 'test content';
  63. var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper());
  64. announcer.say(text);
  65. var firstLiveRegion = getLiveRegion('polite');
  66. announcer.say(text, undefined);
  67. var secondLiveRegion = getLiveRegion('polite');
  68. assertEquals(firstLiveRegion, secondLiveRegion);
  69. checkLiveRegionContains(text, 'polite');
  70. goog.dispose(announcer);
  71. }
  72. function testAnnouncerAssertive() {
  73. var text = 'test content';
  74. var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper());
  75. announcer.say(text, goog.a11y.aria.LivePriority.ASSERTIVE);
  76. checkLiveRegionContains(text, 'assertive');
  77. goog.dispose(announcer);
  78. }
  79. function testAnnouncerInIframe() {
  80. var text = 'test content';
  81. var frame = goog.dom.iframe.createWithContent(sandbox);
  82. var helper =
  83. goog.dom.getDomHelper(goog.dom.getFrameContentDocument(frame).body);
  84. var announcer = new goog.a11y.aria.Announcer(helper);
  85. announcer.say(text, 'polite', helper);
  86. checkLiveRegionContains(text, 'polite', helper);
  87. goog.dispose(announcer);
  88. }
  89. function testAnnouncerWithAriaHidden() {
  90. var text = 'test content1';
  91. var text2 = 'test content2';
  92. var announcer = new goog.a11y.aria.Announcer(goog.dom.getDomHelper());
  93. announcer.say(text);
  94. // Set aria-hidden attribute on the live region (simulates a modal dialog
  95. // being opened).
  96. var liveRegion = getLiveRegion('polite');
  97. goog.a11y.aria.setState(liveRegion, goog.a11y.aria.State.HIDDEN, true);
  98. // Announce a new message and make sure that the aria-hidden was removed.
  99. announcer.say(text2);
  100. checkLiveRegionContains(text2, 'polite');
  101. assertEquals(
  102. '', goog.a11y.aria.getState(liveRegion, goog.a11y.aria.State.HIDDEN));
  103. goog.dispose(announcer);
  104. }
  105. function getLiveRegion(priority, opt_domHelper) {
  106. var dom = opt_domHelper || goog.dom.getDomHelper();
  107. var divs = dom.getElementsByTagNameAndClass(goog.dom.TagName.DIV, null);
  108. var liveRegions = [];
  109. goog.array.forEach(divs, function(div) {
  110. if (goog.a11y.aria.getState(div, 'live') == priority) {
  111. liveRegions.push(div);
  112. }
  113. });
  114. assertEquals(1, liveRegions.length);
  115. return liveRegions[0];
  116. }
  117. function checkLiveRegionContains(text, priority, opt_domHelper) {
  118. var liveRegion = getLiveRegion(priority, opt_domHelper);
  119. mockClock.tick(1);
  120. assertEquals(text, goog.dom.getTextContent(liveRegion));
  121. }