Debian Codegraph @

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

raw
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.ui.PopupBaseTest');
16 goog.setTestOnly('goog.ui.PopupBaseTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.events');
21 goog.require('goog.events.EventTarget');
22 goog.require('goog.events.EventType');
23 goog.require('goog.events.KeyCodes');
24 goog.require('goog.fx.Transition');
25 goog.require('goog.fx.css3');
26 goog.require('goog.testing.MockClock');
27 goog.require('goog.testing.events');
28 goog.require('goog.testing.events.Event');
29 goog.require('goog.testing.jsunit');
30 goog.require('goog.ui.PopupBase');
31
32 var targetDiv;
33 var popupDiv;
34 var partnerDiv;
35 var clock;
36 var popup;
37
38 function setUpPage() {
39 targetDiv = goog.dom.getElement('targetDiv');
40 popupDiv = goog.dom.getElement('popupDiv');
41 partnerDiv = goog.dom.getElement('partnerDiv');
42 }
43
44 function setUp() {
45 popup = new goog.ui.PopupBase(popupDiv);
46 clock = new goog.testing.MockClock(true);
47 }
48
49 function tearDown() {
50 popup.dispose();
51 clock.uninstall();
52 document.body.setAttribute('dir', 'ltr');
53 }
54
55 function testSetVisible() {
56 popup.setVisible(true);
57 assertEquals('visible', popupDiv.style.visibility);
58 assertEquals('', popupDiv.style.display);
59 popup.setVisible(false);
60 assertEquals('hidden', popupDiv.style.visibility);
61 assertEquals('none', popupDiv.style.display);
62 }
63
64 function testEscapeDismissal() {
65 popup.setHideOnEscape(true);
66 assertTrue(
67 'Sanity check that getHideOnEscape is true when set to true.',
68 popup.getHideOnEscape());
69 popup.setVisible(true);
70 assertFalse(
71 'Escape key should be cancelled',
72 goog.testing.events.fireKeySequence(targetDiv, goog.events.KeyCodes.ESC));
73 assertFalse(popup.isVisible());
74 }
75
76 function testEscapeDismissalCanBeDisabled() {
77 popup.setHideOnEscape(false);
78 popup.setVisible(true);
79 assertTrue(
80 'Escape key should be cancelled',
81 goog.testing.events.fireKeySequence(targetDiv, goog.events.KeyCodes.ESC));
82 assertTrue(popup.isVisible());
83 }
84
85 function testEscapeDismissalIsDisabledByDefault() {
86 assertFalse(popup.getHideOnEscape());
87 }
88
89 function testEscapeDismissalDoesNotRecognizeOtherKeys() {
90 popup.setHideOnEscape(true);
91 popup.setVisible(true);
92 var eventsPropagated = 0;
93 goog.events.listenOnce(
94 goog.dom.getElement('commonAncestor'),
95 [
96 goog.events.EventType.KEYDOWN, goog.events.EventType.KEYUP,
97 goog.events.EventType.KEYPRESS
98 ],
99 function() { ++eventsPropagated; });
100 assertTrue('Popup should remain visible', popup.isVisible());
101 assertTrue(
102 'The key event default action should not be prevented',
103 goog.testing.events.fireKeySequence(targetDiv, goog.events.KeyCodes.A));
104 assertEquals(
105 'Keydown, keyup, and keypress should have all propagated', 3,
106 eventsPropagated);
107 }
108
109 function testEscapeDismissalCanBeCancelledByBeforeHideEvent() {
110 popup.setHideOnEscape(true);
111 popup.setVisible(true);
112 var eventsPropagated = 0;
113 goog.events.listenOnce(
114 goog.dom.getElement('commonAncestor'), goog.events.EventType.KEYDOWN,
115 function() { ++eventsPropagated; });
116 // Make a listener so that we stop hiding with an event handler.
117 goog.events.listenOnce(
118 popup, goog.ui.PopupBase.EventType.BEFORE_HIDE,
119 function(e) { e.preventDefault(); });
120 assertEquals('The hide should have been cancelled', true, popup.isVisible());
121 assertTrue(
122 'The key event default action should not be prevented',
123 goog.testing.events.fireKeySequence(targetDiv, goog.events.KeyCodes.ESC));
124 assertEquals('Keydown should have all propagated', 1, eventsPropagated);
125 }
126
127 function testEscapeDismissalProvidesKeyTargetAsTargetForHideEvents() {
128 popup.setHideOnEscape(true);
129 popup.setVisible(true);
130 var calls = 0;
131 goog.events.listenOnce(
132 popup,
133 [
134 goog.ui.PopupBase.EventType.BEFORE_HIDE,
135 goog.ui.PopupBase.EventType.HIDE
136 ],
137 function(e) {
138 calls++;
139 assertEquals(
140 'The key target should be the hide event target', 'targetDiv',
141 e.target.id);
142 });
143 goog.testing.events.fireKeySequence(targetDiv, goog.events.KeyCodes.ESC);
144 }
145
146 function testAutoHide() {
147 popup.setAutoHide(true);
148 popup.setVisible(true);
149 clock.tick(1000); // avoid bouncing
150 goog.testing.events.fireClickSequence(targetDiv);
151 assertFalse(popup.isVisible());
152 }
153
154 function testAutoHideCanBeDisabled() {
155 popup.setAutoHide(false);
156 popup.setVisible(true);
157 clock.tick(1000); // avoid bouncing
158 goog.testing.events.fireClickSequence(targetDiv);
159 assertTrue(
160 'Should not be hidden if auto hide is disabled', popup.isVisible());
161 }
162
163 function testAutoHideEnabledByDefault() {
164 assertTrue(popup.getAutoHide());
165 }
166
167 function testAutoHideWithPartners() {
168 popup.setAutoHide(true);
169 popup.setVisible(true);
170 popup.addAutoHidePartner(targetDiv);
171 popup.addAutoHidePartner(partnerDiv);
172 clock.tick(1000); // avoid bouncing
173
174 goog.testing.events.fireClickSequence(targetDiv);
175 assertTrue(popup.isVisible());
176 goog.testing.events.fireClickSequence(partnerDiv);
177 assertTrue(popup.isVisible());
178
179 popup.removeAutoHidePartner(partnerDiv);
180 goog.testing.events.fireClickSequence(partnerDiv);
181 assertFalse(popup.isVisible());
182 }
183
184 function testCanAddElementDuringBeforeShow() {
185 popup.setElement(null);
186 goog.events.listenOnce(
187 popup, goog.ui.PopupBase.EventType.BEFORE_SHOW,
188 function() { popup.setElement(popupDiv); });
189 popup.setVisible(true);
190 assertTrue('Popup should be shown', popup.isVisible());
191 }
192
193 function testShowWithNoElementThrowsException() {
194 popup.setElement(null);
195 var e = assertThrows(function() { popup.setVisible(true); });
196 assertEquals(
197 'Caller must call setElement before trying to show the popup', e.message);
198 }
199
200 function testShowEventFiredWithNoTransition() {
201 var showHandlerCalled = false;
202 goog.events.listen(popup, goog.ui.PopupBase.EventType.SHOW, function() {
203 showHandlerCalled = true;
204 });
205
206 popup.setVisible(true);
207 assertTrue(showHandlerCalled);
208 }
209
210 function testHideEventFiredWithNoTransition() {
211 var hideHandlerCalled = false;
212 goog.events.listen(popup, goog.ui.PopupBase.EventType.HIDE, function() {
213 hideHandlerCalled = true;
214 });
215
216 popup.setVisible(true);
217 popup.setVisible(false);
218 assertTrue(hideHandlerCalled);
219 }
220
221 function testOnShowTransition() {
222 var mockTransition = new MockTransition();
223
224 var showHandlerCalled = false;
225 goog.events.listen(popup, goog.ui.PopupBase.EventType.SHOW, function() {
226 showHandlerCalled = true;
227 });
228
229 popup.setTransition(mockTransition);
230 popup.setVisible(true);
231 assertTrue(mockTransition.wasPlayed);
232
233 assertFalse(showHandlerCalled);
234 mockTransition.dispatchEvent(goog.fx.Transition.EventType.END);
235 assertTrue(showHandlerCalled);
236 }
237
238 function testOnHideTransition() {
239 var mockTransition = new MockTransition();
240
241 var hideHandlerCalled = false;
242 goog.events.listen(popup, goog.ui.PopupBase.EventType.HIDE, function() {
243 hideHandlerCalled = true;
244 });
245
246 popup.setTransition(undefined, mockTransition);
247 popup.setVisible(true);
248 assertFalse(mockTransition.wasPlayed);
249
250 popup.setVisible(false);
251 assertTrue(mockTransition.wasPlayed);
252
253 assertFalse(hideHandlerCalled);
254 mockTransition.dispatchEvent(goog.fx.Transition.EventType.END);
255 assertTrue(hideHandlerCalled);
256 }
257
258 function testSetVisibleWorksCorrectlyWithTransitions() {
259 popup.setTransition(
260 goog.fx.css3.fadeIn(popup.getElement(), 1),
261 goog.fx.css3.fadeOut(popup.getElement(), 1));
262
263 // Consecutive calls to setVisible works without needing to wait for
264 // transition to finish.
265 popup.setVisible(true);
266 assertTrue(popup.isVisible());
267 popup.setVisible(false);
268 assertFalse(popup.isVisible());
269 clock.tick(1100);
270
271 // Calling setVisible(true) immediately changed the state to visible.
272 popup.setVisible(true);
273 assertTrue(popup.isVisible());
274 clock.tick(1100);
275
276 // Consecutive calls to setVisible, in opposite order.
277 popup.setVisible(false);
278 popup.setVisible(true);
279 assertTrue(popup.isVisible());
280 clock.tick(1100);
281
282 // Calling setVisible(false) immediately changed the state to not visible.
283 popup.setVisible(false);
284 assertFalse(popup.isVisible());
285 clock.tick(1100);
286 }
287
288 function testWasRecentlyVisibleWorksCorrectlyWithTransitions() {
289 popup.setTransition(
290 goog.fx.css3.fadeIn(popup.getElement(), 1),
291 goog.fx.css3.fadeOut(popup.getElement(), 1));
292
293 popup.setVisible(true);
294 clock.tick(1100);
295 popup.setVisible(false);
296 assertTrue(popup.isOrWasRecentlyVisible());
297 clock.tick(goog.ui.PopupBase.DEBOUNCE_DELAY_MS);
298 assertFalse(popup.isOrWasRecentlyVisible());
299 }
300
301 function testMoveOffscreenRTL() {
302 document.body.setAttribute('dir', 'rtl');
303 popup.reposition = function() {
304 this.element_.style.left = '100px';
305 this.element_.style.top = '100px';
306 };
307 popup.setType(goog.ui.PopupBase.Type.MOVE_OFFSCREEN);
308 popup.setElement(goog.dom.getElement('moveOffscreenPopupDiv'));
309 originalScrollWidth = goog.dom.getDocumentScrollElement().scrollWidth;
310 popup.setVisible(true);
311 popup.setVisible(false);
312 assertFalse(
313 'Moving a popup offscreen should not cause scrollbars',
314 goog.dom.getDocumentScrollElement().scrollWidth != originalScrollWidth);
315 }
316
317 function testOnDocumentBlurDisabledCrossIframeDismissalWithoutDelay() {
318 popup.setEnableCrossIframeDismissal(false);
319 popup.setVisible(true);
320 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
321 goog.testing.events.fireBrowserEvent(e);
322 assertTrue('Popup should remain visible', popup.isVisible());
323 }
324
325 function testOnDocumentBlurDisabledCrossIframeDismissalWithDelay() {
326 popup.setEnableCrossIframeDismissal(false);
327 popup.setVisible(true);
328 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
329 clock.tick(goog.ui.PopupBase.DEBOUNCE_DELAY_MS);
330 goog.testing.events.fireBrowserEvent(e);
331 assertTrue('Popup should remain visible', popup.isVisible());
332 }
333
334 function testOnDocumentBlurActiveElementInsidePopupWithoutDelay() {
335 popup.setVisible(true);
336 var elementInsidePopup = goog.dom.createDom(goog.dom.TagName.DIV);
337 goog.dom.append(popupDiv, elementInsidePopup);
338 elementInsidePopup.setAttribute('tabIndex', 0);
339 elementInsidePopup.focus();
340 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
341 goog.testing.events.fireBrowserEvent(e);
342 assertTrue('Popup should remain visible', popup.isVisible());
343 }
344
345 function testOnDocumentBlurActiveElementInsidePopupWithDelay() {
346 popup.setVisible(true);
347 var elementInsidePopup = goog.dom.createDom(goog.dom.TagName.DIV);
348 goog.dom.append(popupDiv, elementInsidePopup);
349 elementInsidePopup.setAttribute('tabIndex', 0);
350 elementInsidePopup.focus();
351 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
352 clock.tick(goog.ui.PopupBase.DEBOUNCE_DELAY_MS);
353 goog.testing.events.fireBrowserEvent(e);
354 assertTrue('Popup should remain visible', popup.isVisible());
355 }
356
357 function testOnDocumentBlurActiveElementIsBodyWithoutDelay() {
358 popup.setVisible(true);
359 var bodyElement =
360 goog.dom.getDomHelper().getElementsByTagNameAndClass('body')[0];
361 bodyElement.setAttribute('tabIndex', 0);
362 bodyElement.focus();
363 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
364 goog.testing.events.fireBrowserEvent(e);
365 assertTrue('Popup should remain visible', popup.isVisible());
366 }
367
368 function testOnDocumentBlurActiveElementIsBodyWithDelay() {
369 popup.setVisible(true);
370 var bodyElement =
371 goog.dom.getDomHelper().getElementsByTagNameAndClass('body')[0];
372 bodyElement.setAttribute('tabIndex', 0);
373 bodyElement.focus();
374 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
375 clock.tick(goog.ui.PopupBase.DEBOUNCE_DELAY_MS);
376 goog.testing.events.fireBrowserEvent(e);
377 assertTrue('Popup should remain visible', popup.isVisible());
378 }
379
380 function testOnDocumentBlurEventTargetNotDocumentWithoutDelay() {
381 popup.setVisible(true);
382 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, targetDiv);
383 goog.testing.events.fireBrowserEvent(e);
384 assertTrue('Popup should remain visible', popup.isVisible());
385 }
386
387 function testOnDocumentBlurEventTargetNotDocumentWithDelay() {
388 popup.setVisible(true);
389 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, targetDiv);
390 clock.tick(goog.ui.PopupBase.DEBOUNCE_DELAY_MS);
391 goog.testing.events.fireBrowserEvent(e);
392 assertTrue('Popup should remain visible', popup.isVisible());
393 }
394
395 function testOnDocumentBlurShouldDebounceWithoutDelay() {
396 popup.setVisible(true);
397 var commonAncestor = goog.dom.getElement('commonAncestor');
398 var focusDiv = goog.dom.createDom(goog.dom.TagName.DIV, 'tabIndex');
399 focusDiv.setAttribute('tabIndex', 0);
400 goog.dom.appendChild(commonAncestor, focusDiv);
401 focusDiv.focus();
402 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
403 goog.testing.events.fireBrowserEvent(e);
404 assertTrue('Popup should be visible', popup.isVisible());
405 goog.dom.removeNode(focusDiv);
406 }
407
408 function testOnDocumentBlurShouldNotDebounceWithDelay() {
409 popup.setVisible(true);
410 clock.tick(goog.ui.PopupBase.DEBOUNCE_DELAY_MS);
411 var commonAncestor = goog.dom.getElement('commonAncestor');
412 var focusDiv = goog.dom.createDom(goog.dom.TagName.DIV, 'tabIndex');
413 focusDiv.setAttribute('tabIndex', 0);
414 goog.dom.appendChild(commonAncestor, focusDiv);
415 focusDiv.focus();
416 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
417 goog.testing.events.fireBrowserEvent(e);
418 assertFalse('Popup should be invisible', popup.isVisible());
419 goog.dom.removeNode(focusDiv);
420 }
421
422
423 function testOnDocumentBlurShouldNotHideBubbleWithoutDelay() {
424 popup.setVisible(true);
425 var commonAncestor = goog.dom.getElement('commonAncestor');
426 var focusDiv = goog.dom.createDom(goog.dom.TagName.DIV, 'tabIndex');
427 focusDiv.setAttribute('tabIndex', 0);
428 goog.dom.appendChild(commonAncestor, focusDiv);
429 focusDiv.focus();
430 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
431 goog.testing.events.fireBrowserEvent(e);
432 assertTrue('Popup should be visible', popup.isVisible());
433 goog.dom.removeNode(focusDiv);
434 }
435
436 function testOnDocumentBlurShouldHideBubbleWithDelay() {
437 popup.setVisible(true);
438 clock.tick(goog.ui.PopupBase.DEBOUNCE_DELAY_MS);
439 var commonAncestor = goog.dom.getElement('commonAncestor');
440 var focusDiv = goog.dom.createDom(goog.dom.TagName.DIV, 'tabIndex');
441 focusDiv.setAttribute('tabIndex', 0);
442 goog.dom.appendChild(commonAncestor, focusDiv);
443 focusDiv.focus();
444 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, document);
445 goog.testing.events.fireBrowserEvent(e);
446 assertFalse('Popup should be invisible', popup.isVisible());
447 goog.dom.removeNode(focusDiv);
448 }
449
450
451
452 /**
453 * @implements {goog.fx.Transition}
454 * @extends {goog.events.EventTarget}
455 * @constructor
456 */
457 var MockTransition = function() {
458 MockTransition.base(this, 'constructor');
459 this.wasPlayed = false;
460 };
461 goog.inherits(MockTransition, goog.events.EventTarget);
462
463
464 MockTransition.prototype.play = function() {
465 this.wasPlayed = true;
466 };
467
468
469 MockTransition.prototype.stop = goog.nullFunction;
470
471
472 // TODO(gboyer): Write better unit tests for click and cross-iframe dismissal.