modalariavisibilityhelper_test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015 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.ui.ModalAriaVisibilityHelperTest');
  15. goog.setTestOnly('goog.ui.ModalAriaVisibilityHelperTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.State');
  18. goog.require('goog.dom');
  19. goog.require('goog.string');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.ui.ModalAriaVisibilityHelper');
  22. function tearDown() {
  23. clearAriaState('div-1');
  24. clearAriaState('div-2');
  25. clearAriaState('div-4');
  26. }
  27. function testHide() {
  28. var helper = createHelper('div-1');
  29. helper.setBackgroundVisibility(true /* hide */);
  30. assertUnalteredElements();
  31. assertEmptyAriaHiddenState('div-1');
  32. assertAriaHiddenState('div-2', 'true');
  33. assertAriaHiddenState('div-4', 'true');
  34. }
  35. function testUnhide() {
  36. var helper = createHelper('div-1');
  37. helper.setBackgroundVisibility(false /* hide */);
  38. assertUnalteredElements();
  39. assertEmptyAriaHiddenState('div-1');
  40. assertEmptyAriaHiddenState('div-2');
  41. assertEmptyAriaHiddenState('div-4');
  42. }
  43. function testMultipleCalls() {
  44. var helper = createHelper('div-2');
  45. helper.setBackgroundVisibility(true /* hide */);
  46. assertUnalteredElements();
  47. assertAriaHiddenState('div-1', 'true');
  48. assertEmptyAriaHiddenState('div-2');
  49. assertAriaHiddenState('div-4', 'true');
  50. helper.setBackgroundVisibility(false /* hide */);
  51. assertUnalteredElements();
  52. assertEmptyAriaHiddenState('div-1');
  53. assertEmptyAriaHiddenState('div-2');
  54. assertEmptyAriaHiddenState('div-4');
  55. helper.setBackgroundVisibility(true /* hide */);
  56. assertUnalteredElements();
  57. assertAriaHiddenState('div-1', 'true');
  58. assertEmptyAriaHiddenState('div-2');
  59. assertAriaHiddenState('div-4', 'true');
  60. }
  61. function assertUnalteredElements() {
  62. assertEmptyAriaHiddenState('div-2-1');
  63. assertAriaHiddenState('div-3', 'false');
  64. assertAriaHiddenState('div-5', 'true');
  65. }
  66. /**
  67. * @param {string} id Id of the element.
  68. * @return {!goog.ui.ModalAriaVisibilityHelper}
  69. */
  70. function createHelper(id) {
  71. return new goog.ui.ModalAriaVisibilityHelper(
  72. goog.dom.getElement(id), goog.dom.getDomHelper());
  73. }
  74. function clearAriaState(id) {
  75. goog.a11y.aria.removeState(
  76. goog.dom.getElement(id), goog.a11y.aria.State.HIDDEN);
  77. }
  78. /**
  79. * @param {string} id Id of the element.
  80. */
  81. function assertEmptyAriaHiddenState(id) {
  82. var element = goog.dom.getElement(id);
  83. assertTrue(
  84. goog.string.isEmptyOrWhitespace(
  85. goog.string.makeSafe(
  86. goog.a11y.aria.getState(element, goog.a11y.aria.State.HIDDEN))));
  87. }
  88. /**
  89. * @param {string} id Id of the element.
  90. * @param {string} expectedState
  91. */
  92. function assertAriaHiddenState(id, expectedState) {
  93. var element = goog.dom.getElement(id);
  94. assertEquals(
  95. expectedState,
  96. goog.a11y.aria.getState(element, goog.a11y.aria.State.HIDDEN));
  97. }