Debian Codegraph @

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

raw
1 // Copyright 2011 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.AnimatedZippyTest');
16 goog.setTestOnly('goog.ui.AnimatedZippyTest');
17
18 goog.require('goog.a11y.aria');
19 goog.require('goog.a11y.aria.Role');
20 goog.require('goog.a11y.aria.State');
21 goog.require('goog.asserts');
22 goog.require('goog.dom');
23 goog.require('goog.events');
24 goog.require('goog.functions');
25 goog.require('goog.fx.Animation');
26 goog.require('goog.fx.Transition');
27 goog.require('goog.testing.PropertyReplacer');
28 goog.require('goog.testing.asserts');
29 goog.require('goog.testing.jsunit');
30 goog.require('goog.ui.AnimatedZippy');
31 goog.require('goog.ui.Zippy');
32
33 var animatedZippy;
34 var animatedZippyHeaderEl;
35 var propertyReplacer;
36
37 function setUp() {
38 animatedZippyHeaderEl = goog.dom.getElement('t1');
39 goog.asserts.assert(animatedZippyHeaderEl);
40 animatedZippy = new goog.ui.AnimatedZippy(
41 animatedZippyHeaderEl, goog.dom.getElement('c1'));
42
43 propertyReplacer = new goog.testing.PropertyReplacer();
44 }
45
46 function tearDown() {
47 propertyReplacer.reset();
48 animatedZippy.dispose();
49 }
50
51 function testConstructor() {
52 assertNotNull('must not be null', animatedZippy);
53 assertEquals(
54 goog.a11y.aria.getRole(animatedZippyHeaderEl), goog.a11y.aria.Role.TAB);
55 }
56
57 function testConstructorAriaRoleOverride() {
58 animatedZippy = new goog.ui.AnimatedZippy(
59 animatedZippyHeaderEl, goog.dom.getElement('c1'), null, null,
60 goog.a11y.aria.Role.BUTTON);
61 assertEquals(
62 goog.a11y.aria.getRole(animatedZippyHeaderEl),
63 goog.a11y.aria.Role.BUTTON);
64 }
65
66 function testExpandCollapse() {
67 var animationsPlayed = 0;
68 var toggleEventsFired = 0;
69
70 propertyReplacer.replace(goog.fx.Animation.prototype, 'play', function() {
71 animationsPlayed++;
72 this.dispatchAnimationEvent(goog.fx.Transition.EventType.END);
73 });
74 propertyReplacer.replace(
75 goog.ui.AnimatedZippy.prototype, 'onAnimate_', goog.functions.NULL);
76
77 goog.events.listenOnce(
78 animatedZippy, goog.ui.Zippy.Events.TOGGLE, function(e) {
79 toggleEventsFired++;
80 assertTrue('TOGGLE event must be for expansion', e.expanded);
81 assertEquals('expanded must be true', true, animatedZippy.isExpanded());
82 assertEquals(
83 'aria-expanded must be true', 'true',
84 goog.a11y.aria.getState(
85 animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
86 });
87
88 animatedZippy.expand();
89
90 goog.events.listenOnce(
91 animatedZippy, goog.ui.Zippy.Events.TOGGLE, function(e) {
92 toggleEventsFired++;
93 assertFalse('TOGGLE event must be for collapse', e.expanded);
94 assertEquals(
95 'expanded must be false', false, animatedZippy.isExpanded());
96 assertEquals(
97 'aria-expanded must be false', 'false',
98 goog.a11y.aria.getState(
99 animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
100 });
101
102 animatedZippy.collapse();
103
104 assertEquals('animations must play', 2, animationsPlayed);
105 assertEquals('TOGGLE events must fire', 2, toggleEventsFired);
106 }
107
108
109 /** Tests the TOGGLE_ANIMATION_BEGIN event. */
110 function testToggleBegin() {
111 var animationsPlayed = 0;
112 var toggleEventsFired = 0;
113
114 propertyReplacer.replace(goog.fx.Animation.prototype, 'play', function() {
115 animationsPlayed++;
116 this.dispatchAnimationEvent(goog.fx.Transition.EventType.BEGIN);
117 this.dispatchAnimationEvent(goog.fx.Transition.EventType.END);
118 });
119 propertyReplacer.replace(
120 goog.ui.AnimatedZippy.prototype, 'onAnimate_', goog.functions.NULL);
121
122 goog.events.listenOnce(
123 animatedZippy, goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_BEGIN,
124 function(e) {
125 toggleEventsFired++;
126 assertTrue(
127 'TOGGLE_ANIMATION_BEGIN event must be for expansion', e.expanded);
128 assertEquals(
129 'expanded must be false', false, animatedZippy.isExpanded());
130 assertEquals(
131 'aria-expanded must be true', 'true',
132 goog.a11y.aria.getState(
133 animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
134 });
135
136 animatedZippy.expand();
137
138 goog.events.listenOnce(
139 animatedZippy, goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_BEGIN,
140 function(e) {
141 toggleEventsFired++;
142 assertFalse(
143 'TOGGLE_ANIMATION_BEGIN event must be for collapse', e.expanded);
144 assertEquals('expanded must be true', true, animatedZippy.isExpanded());
145 assertEquals(
146 'aria-expanded must be false', 'false',
147 goog.a11y.aria.getState(
148 animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
149 });
150
151 animatedZippy.collapse();
152
153 assertEquals('animations must play', 2, animationsPlayed);
154 assertEquals('TOGGLE_ANIMATION_BEGIN events must fire', 2, toggleEventsFired);
155 }
156
157
158 /** Tests the TOGGLE_ANIMATION_END event. */
159 function testToggleEnd() {
160 var animationsPlayed = 0;
161 var toggleEventsFired = 0;
162
163 propertyReplacer.replace(goog.fx.Animation.prototype, 'play', function() {
164 animationsPlayed++;
165 this.dispatchAnimationEvent(goog.fx.Transition.EventType.END);
166 });
167 propertyReplacer.replace(
168 goog.ui.AnimatedZippy.prototype, 'onAnimate_', goog.functions.NULL);
169
170 goog.events.listenOnce(
171 animatedZippy, goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_END,
172 function(e) {
173 toggleEventsFired++;
174 assertTrue(
175 'TOGGLE_ANIMATION_END event must be for expansion', e.expanded);
176 assertEquals('expanded must be true', true, animatedZippy.isExpanded());
177 assertEquals(
178 'aria-expanded must be true', 'true',
179 goog.a11y.aria.getState(
180 animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
181 });
182
183 animatedZippy.expand();
184
185 goog.events.listenOnce(
186 animatedZippy, goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_END,
187 function(e) {
188 toggleEventsFired++;
189 assertFalse(
190 'TOGGLE_ANIMATION_END event must be for collapse', e.expanded);
191 assertEquals(
192 'expanded must be false', false, animatedZippy.isExpanded());
193 assertEquals(
194 'aria-expanded must be false', 'false',
195 goog.a11y.aria.getState(
196 animatedZippyHeaderEl, goog.a11y.aria.State.EXPANDED));
197 });
198
199 animatedZippy.collapse();
200
201 assertEquals('animations must play', 2, animationsPlayed);
202 assertEquals('TOGGLE_ANIMATION_END events must fire', 2, toggleEventsFired);
203 }