Debian Codegraph @

closure-library/closure/goog/ui/modalariavisibilityhelper_test.js

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