Debian Codegraph @

closure-library/closure/goog/ui/popupmenu_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.PopupMenuTest');
16 goog.setTestOnly('goog.ui.PopupMenuTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.events.EventHandler');
20 goog.require('goog.events.EventType');
21 goog.require('goog.events.KeyCodes');
22 goog.require('goog.math.Box');
23 goog.require('goog.math.Coordinate');
24 goog.require('goog.positioning.Corner');
25 goog.require('goog.style');
26 goog.require('goog.testing.events');
27 goog.require('goog.testing.jsunit');
28 goog.require('goog.ui.Menu');
29 goog.require('goog.ui.MenuItem');
30 goog.require('goog.ui.PopupMenu');
31
32 var anchor;
33 var menu;
34 var menuitem;
35
36 // Event handler
37 var handler;
38 var showPopup;
39 var beforeShowPopupCalled;
40 var popup;
41
42 function setUp() {
43 anchor = goog.dom.getElement('popup-anchor');
44 menu = goog.dom.getElement('menu');
45 menuitem1 = goog.dom.getElement('menuitem_1');
46 menuitem3 = goog.dom.getElement('menuitem_3');
47 handler = new goog.events.EventHandler();
48 popup = new goog.ui.PopupMenu();
49 }
50
51 function tearDown() {
52 handler.dispose();
53 popup.dispose();
54 }
55
56
57 /**
58 * Asserts properties of {@code target} matches the expected value.
59 *
60 * @param {Object} target The target specifying how the popup menu should be
61 * attached to an anchor.
62 * @param {Element} expectedElement The expected anchoring element.
63 * @param {goog.positioning.Corner} expectedTargetCorner The expected value of
64 * the {@code target.targetCorner_} property.
65 * @param {goog.positioning.Corner} expectedMenuCorner The expected value of
66 * the {@code target.menuCorner_} property.
67 * @param {goog.events.EventType} expectedEventType The expected value of the
68 * {@code target.eventType_} property.
69 * @param {goog.math.Box} expectedMargin The expected value of the
70 * {@code target.margin_} property.
71 */
72 function assertTarget(
73 target, expectedElement, expectedTargetCorner, expectedMenuCorner,
74 expectedEventType, expectedMargin) {
75 var expectedTarget = {
76 element_: expectedElement,
77 targetCorner_: expectedTargetCorner,
78 menuCorner_: expectedMenuCorner,
79 eventType_: expectedEventType,
80 margin_: expectedMargin
81 };
82
83 assertObjectEquals('Target does not match.', expectedTarget, target);
84 }
85
86
87 /**
88 * Test menu receives BEFORE_SHOW event before it's displayed.
89 */
90 function testBeforeShowEvent() {
91 popup.render();
92 var target = popup.createAttachTarget(anchor);
93 popup.attach(anchor);
94
95 function beforeShowPopup(e) {
96 // Ensure that the element is not yet visible.
97 assertFalse(
98 'The element should not be shown when BEFORE_SHOW event is ' +
99 'being handled',
100 goog.style.isElementShown(popup.getElement()));
101 // Verify that current anchor is set before dispatching BEFORE_SHOW.
102 assertNotNullNorUndefined(popup.getAttachedElement());
103 assertEquals(
104 'The attached anchor element is incorrect', target.element_,
105 popup.getAttachedElement());
106 beforeShowPopupCalled = true;
107 return showPopup;
108 };
109 function onShowPopup(e) {
110 assertEquals(
111 'The attached anchor element is incorrect', target.element_,
112 popup.getAttachedElement());
113 };
114
115 handler.listen(popup, goog.ui.Menu.EventType.BEFORE_SHOW, beforeShowPopup);
116 handler.listen(popup, goog.ui.Menu.EventType.SHOW, onShowPopup);
117
118 beforeShowPopupCalled = false;
119 showPopup = false;
120 popup.showMenu(target, 0, 0);
121 assertTrue(
122 'BEFORE_SHOW event handler should be called on #showMenu',
123 beforeShowPopupCalled);
124 assertFalse(
125 'The element should not be shown when BEFORE_SHOW handler ' +
126 'returned false',
127 goog.style.isElementShown(popup.getElement()));
128
129 beforeShowPopupCalled = false;
130 showPopup = true;
131 popup.showMenu(target, 0, 0);
132 assertTrue(
133 'The element should be shown when BEFORE_SHOW handler ' +
134 'returned true',
135 goog.style.isElementShown(popup.getElement()));
136 }
137
138
139 /**
140 * Test the behavior of {@link PopupMenu.isAttachTarget}.
141 */
142 function testIsAttachTarget() {
143 popup.render();
144 // Before 'attach' is called.
145 assertFalse(
146 'Menu should not be attached to the element',
147 popup.isAttachTarget(anchor));
148
149 popup.attach(anchor);
150 assertTrue(
151 'Menu should be attached to the anchor', popup.isAttachTarget(anchor));
152
153 popup.detach(anchor);
154 assertFalse(
155 'Menu is expected to be detached from the element',
156 popup.isAttachTarget(anchor));
157 }
158
159
160 /**
161 * Tests the behavior of {@link PopupMenu.createAttachTarget}.
162 */
163 function testCreateAttachTarget() {
164 // Randomly picking parameters.
165 var targetCorner = goog.positioning.Corner.TOP_END;
166 var menuCorner = goog.positioning.Corner.BOTTOM_LEFT;
167 var contextMenu = false; // Show menu on mouse down event.
168 var margin = new goog.math.Box(0, 10, 5, 25);
169
170 // Simply setting the required parameters.
171 var target = popup.createAttachTarget(anchor);
172 assertTrue(popup.isAttachTarget(anchor));
173 assertTarget(
174 target, anchor, undefined, undefined, goog.events.EventType.MOUSEDOWN,
175 undefined);
176
177 // Creating another target with all the parameters.
178 target = popup.createAttachTarget(
179 anchor, targetCorner, menuCorner, contextMenu, margin);
180 assertTrue(popup.isAttachTarget(anchor));
181 assertTarget(
182 target, anchor, targetCorner, menuCorner, goog.events.EventType.MOUSEDOWN,
183 margin);
184
185 // Finally, switch up the 'contextMenu'
186 target = popup.createAttachTarget(
187 anchor, undefined, undefined, true /*opt_contextMenu*/, undefined);
188 assertTarget(
189 target, anchor, undefined, undefined, goog.events.EventType.CONTEXTMENU,
190 undefined);
191 }
192
193
194 /**
195 * Tests the behavior of {@link PopupMenu.getAttachTarget}.
196 */
197 function testGetAttachTarget() {
198 popup.render();
199 // Before the menu is attached to the anchor.
200 var target = popup.getAttachTarget(anchor);
201 assertTrue(
202 'Not expecting a target before the element is attach to the menu',
203 target == null);
204
205 // Randomly picking parameters.
206 var targetCorner = goog.positioning.Corner.TOP_END;
207 var menuCorner = goog.positioning.Corner.BOTTOM_LEFT;
208 var contextMenu = false; // Show menu on mouse down event.
209 var margin = new goog.math.Box(0, 10, 5, 25);
210
211 popup.attach(anchor, targetCorner, menuCorner, contextMenu, margin);
212 target = popup.getAttachTarget(anchor);
213 assertTrue(
214 'Failed to get target after attaching element to menu', target != null);
215
216 // Make sure we got the right target back.
217 assertTarget(
218 target, anchor, targetCorner, menuCorner, goog.events.EventType.MOUSEDOWN,
219 margin);
220 }
221
222 function testSmallViewportSliding() {
223 popup.render();
224 popup.getElement().style.position = 'absolute';
225 popup.getElement().style.outline = '1px solid blue';
226 var item = new goog.ui.MenuItem('Test Item');
227 popup.addChild(item, true);
228 item.getElement().style.overflow = 'hidden';
229
230 var viewport = goog.style.getClientViewportElement();
231 var viewportRect = goog.style.getVisibleRectForElement(viewport);
232
233 var middlePos = Math.floor((viewportRect.right - viewportRect.left) / 2);
234 var leftwardPos = Math.floor((viewportRect.right - viewportRect.left) / 3);
235 var rightwardPos =
236 Math.floor((viewportRect.right - viewportRect.left) / 3 * 2);
237
238 // Can interpret these positions as widths relative to the viewport as well.
239 var smallWidth = leftwardPos;
240 var mediumWidth = middlePos;
241 var largeWidth = rightwardPos;
242
243 // Test small menu first. This should be small enough that it will display
244 // its upper left corner where we tell it to in all three positions.
245 popup.getElement().style.width = smallWidth + 'px';
246
247 var target = popup.createAttachTarget(anchor);
248 popup.attach(anchor);
249
250 popup.showMenu(target, leftwardPos, 0);
251 assertObjectEquals(
252 'Popup in wrong position: small size, leftward pos',
253 new goog.math.Coordinate(leftwardPos, 0),
254 goog.style.getPosition(popup.getElement()));
255
256 popup.showMenu(target, middlePos, 0);
257 assertObjectEquals(
258 'Popup in wrong position: small size, middle pos',
259 new goog.math.Coordinate(middlePos, 0),
260 goog.style.getPosition(popup.getElement()));
261
262 popup.showMenu(target, rightwardPos, 0);
263 assertObjectEquals(
264 'Popup in wrong position: small size, rightward pos',
265 new goog.math.Coordinate(rightwardPos, 0),
266 goog.style.getPosition(popup.getElement()));
267
268 // Test medium menu next. This should display with its upper left corner
269 // at the target when leftward and middle, but on the right it should
270 // position its upper right corner at the target instead.
271 popup.getElement().style.width = mediumWidth + 'px';
272
273 popup.showMenu(target, leftwardPos, 0);
274 assertObjectEquals(
275 'Popup in wrong position: medium size, leftward pos',
276 new goog.math.Coordinate(leftwardPos, 0),
277 goog.style.getPosition(popup.getElement()));
278
279 popup.showMenu(target, middlePos, 0);
280 assertObjectEquals(
281 'Popup in wrong position: medium size, middle pos',
282 new goog.math.Coordinate(middlePos, 0),
283 goog.style.getPosition(popup.getElement()));
284
285 popup.showMenu(target, rightwardPos, 0);
286 assertObjectEquals(
287 'Popup in wrong position: medium size, rightward pos',
288 new goog.math.Coordinate(rightwardPos - mediumWidth, 0),
289 goog.style.getPosition(popup.getElement()));
290
291 // Test large menu next. This should display with its upper left corner at
292 // the target when leftward, and its upper right corner at the target when
293 // rightward, but right in the middle neither corner can be at the target and
294 // keep the entire menu onscreen, so it should place its upper right corner
295 // at the very right edge of the viewport.
296 popup.getElement().style.width = largeWidth + 'px';
297 popup.showMenu(target, leftwardPos, 0);
298 assertObjectEquals(
299 'Popup in wrong position: large size, leftward pos',
300 new goog.math.Coordinate(leftwardPos, 0),
301 goog.style.getPosition(popup.getElement()));
302
303 popup.showMenu(target, middlePos, 0);
304 assertObjectEquals(
305 'Popup in wrong position: large size, middle pos',
306 new goog.math.Coordinate(
307 viewportRect.right - viewportRect.left - largeWidth, 0),
308 goog.style.getPosition(popup.getElement()));
309
310 popup.showMenu(target, rightwardPos, 0);
311 assertObjectEquals(
312 'Popup in wrong position: large size, rightward pos',
313 new goog.math.Coordinate(rightwardPos - largeWidth, 0),
314 goog.style.getPosition(popup.getElement()));
315
316 // Make sure that the menu still displays correctly if we give the target
317 // a target corner. We can't set the overflow policy in that case, but it
318 // should still display.
319 popup.detach(anchor);
320 anchor.style.position = 'absolute';
321 anchor.style.left = '24px';
322 anchor.style.top = '24px';
323 var targetCorner = goog.positioning.Corner.TOP_END;
324 target = popup.createAttachTarget(anchor, targetCorner);
325 popup.attach(anchor, targetCorner);
326 popup.getElement().style.width = smallWidth + 'px';
327 popup.showMenu(target, leftwardPos, 0);
328 assertObjectEquals(
329 'Popup in wrong position: small size, leftward pos, with target corner',
330 new goog.math.Coordinate(24, 24),
331 goog.style.getPosition(popup.getElement()));
332 }
333
334
335 /**
336 * Tests that the menu is shown if the SPACE or ENTER keys are pressed, and that
337 * none of the menu items are highlighted (PopupMenu.highlightedIndex == -1).
338 */
339 function testKeyboardEventsShowMenu() {
340 popup.decorate(menu);
341 popup.attach(anchor);
342 popup.hide();
343 assertFalse(popup.isVisible());
344 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.SPACE);
345 assertTrue(popup.isVisible());
346 assertEquals(-1, popup.getHighlightedIndex());
347 popup.hide();
348 assertFalse(popup.isVisible());
349 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.ENTER);
350 assertTrue(popup.isVisible());
351 assertEquals(-1, popup.getHighlightedIndex());
352 }
353
354
355 /**
356 * Tests that the menu is shown and the first item is highlighted if the DOWN
357 * key is pressed.
358 */
359 function testDownKey() {
360 popup.decorate(menu);
361 popup.attach(anchor);
362 popup.hide();
363 assertFalse(popup.isVisible());
364 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.DOWN);
365 assertTrue(popup.isVisible());
366 assertEquals(0, popup.getHighlightedIndex());
367 }
368
369
370 /**
371 * Tests activation of menu items by keyboard.
372 */
373 function testMenuItemKeyboardActivation() {
374 popup.decorate(menu);
375 popup.attach(anchor);
376 // Check that if the ESC key is pressed the focus is on
377 // the anchor element.
378 goog.testing.events.fireKeySequence(menu, goog.events.KeyCodes.ESC);
379 assertEquals(anchor, document.activeElement);
380
381 var menuitemListenerFired = false;
382 function onMenuitemAction(event) {
383 if (event.keyCode == goog.events.KeyCodes.SPACE ||
384 event.keyCode == goog.events.KeyCodes.ENTER) {
385 menuitemListenerFired = true;
386 }
387 };
388 handler.listen(menuitem1, goog.events.EventType.KEYDOWN, onMenuitemAction);
389 // Simulate opening a menu using the DOWN key, and pressing the SPACE/ENTER
390 // key in order to activate the first menuitem.
391 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.DOWN);
392 goog.testing.events.fireKeySequence(menu, goog.events.KeyCodes.SPACE);
393 assertTrue(menuitemListenerFired);
394 menuitemListenerFired = false;
395 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.DOWN);
396 goog.testing.events.fireKeySequence(menu, goog.events.KeyCodes.ENTER);
397 assertTrue(menuitemListenerFired);
398 // Make sure the menu item's listener doesn't fire for any key.
399 menuitemListenerFired = false;
400 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.DOWN);
401 goog.testing.events.fireKeySequence(menu, goog.events.KeyCodes.SHIFT);
402 assertFalse(menuitemListenerFired);
403
404 // Simulate opening menu and moving down to the third menu item using the
405 // DOWN key, and then activating it using the SPACE key.
406 menuitemListenerFired = false;
407 handler.listen(menuitem3, goog.events.EventType.KEYDOWN, onMenuitemAction);
408 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.DOWN);
409 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.DOWN);
410 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.DOWN);
411 goog.testing.events.fireKeySequence(menu, goog.events.KeyCodes.SPACE);
412 assertTrue(menuitemListenerFired);
413 }
414
415
416 /**
417 * Tests that a context menu isn't shown if the SPACE or ENTER keys are pressed.
418 */
419 function testContextMenuKeyboard() {
420 popup.attach(anchor, null, null, true);
421 popup.hide();
422 assertFalse(popup.isVisible());
423 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.SPACE);
424 assertFalse(popup.isVisible());
425 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.ENTER);
426 assertFalse(popup.isVisible());
427 }
428
429
430 /**
431 * Tests that there is no crash when hitting a key when no menu item is
432 * highlighted.
433 */
434 function testKeyPressWithNoHighlightedItem() {
435 popup.decorate(menu);
436 popup.attach(anchor);
437 goog.testing.events.fireKeySequence(anchor, goog.events.KeyCodes.SPACE);
438 assertTrue(popup.isVisible());
439 try {
440 goog.testing.events.fireKeySequence(menu, goog.events.KeyCodes.SPACE);
441 } catch (e) {
442 fail(
443 'Crash attempting to reference null selected menu item after ' +
444 'keyboard event.');
445 }
446 }