Debian Codegraph @

closure-library/closure/goog/ui/menubutton_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.MenuButtonTest');
16 goog.setTestOnly('goog.ui.MenuButtonTest');
17
18 goog.require('goog.Timer');
19 goog.require('goog.a11y.aria');
20 goog.require('goog.a11y.aria.State');
21 goog.require('goog.dom');
22 goog.require('goog.dom.TagName');
23 goog.require('goog.events');
24 goog.require('goog.events.Event');
25 goog.require('goog.events.EventType');
26 goog.require('goog.events.KeyCodes');
27 goog.require('goog.events.KeyHandler');
28 goog.require('goog.positioning');
29 goog.require('goog.positioning.Corner');
30 goog.require('goog.positioning.MenuAnchoredPosition');
31 goog.require('goog.positioning.Overflow');
32 goog.require('goog.style');
33 goog.require('goog.testing.ExpectedFailures');
34 goog.require('goog.testing.PropertyReplacer');
35 goog.require('goog.testing.events');
36 goog.require('goog.testing.jsunit');
37 goog.require('goog.testing.recordFunction');
38 goog.require('goog.ui.Component');
39 goog.require('goog.ui.Menu');
40 goog.require('goog.ui.MenuButton');
41 goog.require('goog.ui.MenuItem');
42 goog.require('goog.ui.SubMenu');
43 goog.require('goog.userAgent');
44 goog.require('goog.userAgent.product');
45 goog.require('goog.userAgent.product.isVersion');
46
47 var menuButton;
48 var clonedMenuButtonDom;
49 var expectedFailures;
50
51 function setUpPage() {
52 expectedFailures = new goog.testing.ExpectedFailures();
53 }
54
55 // Mock out goog.positioning.positionAtCoordinate to always ignore failure when
56 // the window is too small, since we don't care about the viewport size on
57 // the selenium farm.
58 // TODO(nicksantos): Move this into a common location if we ever have enough
59 // code for a general goog.testing.ui library.
60 var originalPositionAtCoordinate = goog.positioning.positionAtCoordinate;
61 goog.positioning.positionAtCoordinate = function(
62 absolutePos, movableElement, movableElementCorner, opt_margin, opt_viewport,
63 opt_overflow, opt_preferredSize) {
64 return originalPositionAtCoordinate.call(
65 this, absolutePos, movableElement, movableElementCorner, opt_margin,
66 opt_viewport, goog.positioning.Overflow.IGNORE, opt_preferredSize);
67 };
68
69 function MyFakeEvent(keyCode, opt_eventType) {
70 this.type = opt_eventType || goog.events.KeyHandler.EventType.KEY;
71 this.keyCode = keyCode;
72 this.propagationStopped = false;
73 this.preventDefault = goog.nullFunction;
74 this.stopPropagation = function() { this.propagationStopped = true; };
75 }
76
77 function setUp() {
78 window.scrollTo(0, 0);
79
80 var viewportSize = goog.dom.getViewportSize();
81 // Some tests need enough size viewport.
82 if (viewportSize.width < 600 || viewportSize.height < 600) {
83 window.moveTo(0, 0);
84 window.resizeTo(640, 640);
85 }
86
87 clonedMenuButtonDom = goog.dom.getElement('demoMenuButton').cloneNode(true);
88
89 menuButton = new goog.ui.MenuButton();
90 }
91
92 function tearDown() {
93 expectedFailures.handleTearDown();
94 menuButton.dispose();
95
96 var element = goog.dom.getElement('demoMenuButton');
97 element.parentNode.replaceChild(clonedMenuButtonDom, element);
98 }
99
100
101 /**
102 * Check if the aria-haspopup property is set correctly.
103 */
104 function checkHasPopUp() {
105 menuButton.enterDocument();
106 assertFalse(
107 'Menu button must have aria-haspopup attribute set to false',
108 goog.a11y.aria.getState(
109 menuButton.getElement(), goog.a11y.aria.State.HASPOPUP));
110 var menu = new goog.ui.Menu();
111 menu.createDom();
112 menuButton.setMenu(menu);
113 assertTrue(
114 'Menu button must have aria-haspopup attribute set to true',
115 goog.a11y.aria.getState(
116 menuButton.getElement(), goog.a11y.aria.State.HASPOPUP));
117 menuButton.setMenu(null);
118 assertFale(
119 'Menu button must have aria-haspopup attribute set to false',
120 goog.a11y.aria.getState(
121 menuButton.getElement(), goog.a11y.aria.State.HASPOPUP));
122 }
123
124
125 /**
126 * Open the menu and click on the menu item inside.
127 * Check if the aria-haspopup property is set correctly.
128 */
129 function testBasicButtonBehavior() {
130 var node = goog.dom.getElement('demoMenuButton');
131 menuButton.decorate(node);
132
133 assertEquals(
134 'Menu button must have aria-haspopup attribute set to true', 'true',
135 goog.a11y.aria.getState(
136 menuButton.getElement(), goog.a11y.aria.State.HASPOPUP));
137
138 goog.testing.events.fireClickSequence(node);
139
140 assertTrue('Menu must open after click', menuButton.isOpen());
141
142 var menuItemClicked = 0;
143 var lastMenuItemClicked = null;
144 goog.events.listen(
145 menuButton.getMenu(), goog.ui.Component.EventType.ACTION, function(e) {
146 menuItemClicked++;
147 lastMenuItemClicked = e.target;
148 });
149
150 var menuItem2 = goog.dom.getElement('menuItem2');
151 goog.testing.events.fireClickSequence(menuItem2);
152 assertFalse('Menu must close on clicking when open', menuButton.isOpen());
153 assertEquals('Number of menu items clicked should be 1', 1, menuItemClicked);
154 assertEquals(
155 'menuItem2 should be the last menuitem clicked', menuItem2,
156 lastMenuItemClicked.getElement());
157 }
158
159
160 /**
161 * Open the menu, highlight first menuitem and then the second.
162 * Check if the aria-activedescendant property is set correctly.
163 */
164 function testHighlightItemBehavior() {
165 var node = goog.dom.getElement('demoMenuButton');
166 menuButton.decorate(node);
167 goog.testing.events.fireClickSequence(node);
168
169 assertTrue('Menu must open after click', menuButton.isOpen());
170
171 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.DOWN));
172 assertNotNull(menuButton.getElement());
173 assertEquals(
174 'First menuitem must be the aria-activedescendant', 'menuItem1',
175 goog.a11y.aria.getState(
176 menuButton.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT));
177
178 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.DOWN));
179 assertEquals(
180 'Second menuitem must be the aria-activedescendant', 'menuItem2',
181 goog.a11y.aria.getState(
182 menuButton.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT));
183 }
184
185
186 /**
187 * Check that the appropriate items are selected when menus are opened with the
188 * keyboard and setSelectFirstOnEnterOrSpace is not set.
189 */
190 function testHighlightFirstOnOpen() {
191 var node = goog.dom.getElement('demoMenuButton');
192 menuButton.decorate(node);
193 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.ENTER));
194 assertEquals(
195 'By default no items should be highlighted when opened with enter.', null,
196 menuButton.getMenu().getHighlighted());
197
198 menuButton.setOpen(false);
199 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.DOWN));
200 assertTrue('Menu must open after down key', menuButton.isOpen());
201 assertEquals(
202 'First menuitem must be highlighted', 'menuItem1',
203 menuButton.getMenu().getHighlighted().getElement().id);
204 }
205
206
207 /**
208 * Check that the appropriate items are selected when menus are opened with the
209 * keyboard, setSelectFirstOnEnterOrSpace is not set, and the first menu item is
210 * disabled.
211 */
212 function testHighlightFirstOnOpen_withFirstDisabled() {
213 var node = goog.dom.getElement('demoMenuButton');
214 menuButton.decorate(node);
215 var menu = menuButton.getMenu();
216 menu.getItemAt(0).setEnabled(false);
217
218 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.ENTER));
219 assertEquals(
220 'By default no items should be highlighted when opened with enter.', null,
221 menuButton.getMenu().getHighlighted());
222
223 menuButton.setOpen(false);
224 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.DOWN));
225 assertTrue('Menu must open after down key', menuButton.isOpen());
226 assertEquals(
227 'First enabled menuitem must be highlighted', 'menuItem2',
228 menuButton.getMenu().getHighlighted().getElement().id);
229 }
230
231
232 /**
233 * Check that the appropriate items are selected when menus are opened with the
234 * keyboard and setSelectFirstOnEnterOrSpace is set.
235 */
236 function testHighlightFirstOnOpen_withEnterOrSpaceSet() {
237 var node = goog.dom.getElement('demoMenuButton');
238 menuButton.decorate(node);
239 menuButton.setSelectFirstOnEnterOrSpace(true);
240 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.ENTER));
241 assertEquals(
242 'The first item should be highlighted when opened with enter ' +
243 'after setting selectFirstOnEnterOrSpace',
244 'menuItem1', menuButton.getMenu().getHighlighted().getElement().id);
245 }
246
247
248 /**
249 * Check that the appropriate item is selected when a menu is opened with the
250 * keyboard, setSelectFirstOnEnterOrSpace is true, and the first menu item is
251 * disabled.
252 */
253 function testHighlightFirstOnOpen_withEnterOrSpaceSetAndFirstDisabled() {
254 var node = goog.dom.getElement('demoMenuButton');
255 menuButton.decorate(node);
256 menuButton.setSelectFirstOnEnterOrSpace(true);
257 var menu = menuButton.getMenu();
258 menu.getItemAt(0).setEnabled(false);
259
260 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.ENTER));
261 assertEquals(
262 'The first enabled item should be highlighted when opened ' +
263 'with enter after setting selectFirstOnEnterOrSpace',
264 'menuItem2', menuButton.getMenu().getHighlighted().getElement().id);
265 }
266
267
268 /**
269 * Open the menu, enter a submenu and then back out of it.
270 * Check if the aria-activedescendant property is set correctly.
271 */
272 function testCloseSubMenuBehavior() {
273 var node = goog.dom.getElement('demoMenuButton');
274 menuButton.decorate(node);
275 var menu = menuButton.getMenu();
276 var subMenu = new goog.ui.SubMenu('Submenu');
277 menu.addItem(subMenu);
278 subMenu.getElement().id = 'subMenu';
279 var subMenuMenu = new goog.ui.Menu();
280 subMenu.setMenu(subMenuMenu);
281 var subMenuItem = new goog.ui.MenuItem('Submenu item 1');
282 subMenuMenu.addItem(subMenuItem);
283 subMenuItem.getElement().id = 'subMenuItem1';
284 menuButton.setOpen(true);
285
286 for (var i = 0; i < 4; i++) {
287 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.DOWN));
288 }
289 assertEquals(
290 'Submenu must be the aria-activedescendant', 'subMenu',
291 goog.a11y.aria.getState(
292 menuButton.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT));
293
294 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.RIGHT));
295 assertEquals(
296 'Submenu item 1 must be the aria-activedescendant', 'subMenuItem1',
297 goog.a11y.aria.getState(
298 menuButton.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT));
299
300 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.LEFT));
301 assertEquals(
302 'Submenu must be the aria-activedescendant', 'subMenu',
303 goog.a11y.aria.getState(
304 menuButton.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT));
305 }
306
307
308 /**
309 * Make sure the menu opens when enter is pressed.
310 */
311 function testEnterOpensMenu() {
312 var node = goog.dom.getElement('demoMenuButton');
313 menuButton.decorate(node);
314 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.ENTER));
315 assertTrue('Menu must open after enter', menuButton.isOpen());
316 }
317
318
319 /**
320 * Tests the behavior of the enter and space keys when the menu is open.
321 */
322 function testSpaceOrEnterClosesMenu() {
323 var node = goog.dom.getElement('demoMenuButton');
324 menuButton.decorate(node);
325
326 menuButton.setOpen(true);
327 menuButton.handleKeyEvent(new MyFakeEvent(goog.events.KeyCodes.ENTER));
328 assertFalse('Menu should close after pressing Enter', menuButton.isOpen());
329
330 menuButton.setOpen(true);
331 menuButton.handleKeyEvent(
332 new MyFakeEvent(goog.events.KeyCodes.SPACE, goog.events.EventType.KEYUP));
333 assertFalse('Menu should close after pressing Space', menuButton.isOpen());
334 }
335
336
337 /**
338 * Tests that a keydown event of the escape key propagates normally when the
339 * menu is closed.
340 */
341 function testStopEscapePropagationMenuClosed() {
342 var node = goog.dom.getElement('demoMenuButton');
343 var fakeEvent = new MyFakeEvent(
344 goog.events.KeyCodes.ESCAPE, goog.events.EventType.KEYDOWN);
345 menuButton.decorate(node);
346 menuButton.setOpen(false);
347
348 menuButton.handleKeyDownEvent_(fakeEvent);
349 assertFalse(
350 'Event propagation was erroneously stopped.',
351 fakeEvent.propagationStopped);
352 }
353
354
355 /**
356 * Tests that a keydown event of the escape key is prevented from propagating
357 * when the menu is open.
358 */
359 function testStopEscapePropagationMenuOpen() {
360 var node = goog.dom.getElement('demoMenuButton');
361 var fakeEvent = new MyFakeEvent(
362 goog.events.KeyCodes.ESCAPE, goog.events.EventType.KEYDOWN);
363 menuButton.decorate(node);
364 menuButton.setOpen(true);
365
366 menuButton.handleKeyDownEvent_(fakeEvent);
367 assertTrue(
368 'Event propagation was not stopped.', fakeEvent.propagationStopped);
369 }
370
371
372 /**
373 * Open the menu and click on the menu item inside after exiting and entering
374 * the document once, to test proper setup/teardown behavior of MenuButton.
375 */
376 function testButtonAfterEnterDocument() {
377 var node = goog.dom.getElement('demoMenuButton');
378 menuButton.decorate(node);
379
380 menuButton.exitDocument();
381 menuButton.enterDocument();
382
383 goog.testing.events.fireClickSequence(node);
384 assertTrue('Menu must open after click', menuButton.isOpen());
385
386 var menuItem2 = goog.dom.getElement('menuItem2');
387 goog.testing.events.fireClickSequence(menuItem2);
388 assertFalse('Menu must close on clicking when open', menuButton.isOpen());
389 }
390
391
392 /**
393 * Renders the menu button, moves its menu and then repositions to make sure the
394 * position is more or less ok.
395 */
396 function testPositionMenu() {
397 var node = goog.dom.getElement('demoMenuButton');
398 menuButton.decorate(node);
399 var menu = menuButton.getMenu();
400 menu.setVisible(true, true);
401
402 // Move to 500, 500
403 menu.setPosition(500, 500);
404
405 // Now reposition and make sure position is more or less ok.
406 menuButton.positionMenu();
407 var menuNode = goog.dom.getElement('demoMenu');
408 assertRoughlyEquals(
409 menuNode.offsetTop, node.offsetTop + node.offsetHeight, 20);
410 assertRoughlyEquals(menuNode.offsetLeft, node.offsetLeft, 20);
411 }
412
413
414 /**
415 * Tests that calling positionMenu when the menu is not in the document does not
416 * throw an exception.
417 */
418 function testPositionMenuNotInDocument() {
419 var menu = new goog.ui.Menu();
420 menu.createDom();
421 menuButton.setMenu(menu);
422 menuButton.positionMenu();
423 }
424
425
426 /**
427 * Shows the menu and moves the menu button, a timer correct the menu position.
428 */
429 function testOpenedMenuPositionCorrection() {
430 var iframe = goog.dom.getElement('iframe1');
431 var iframeDoc = goog.dom.getFrameContentDocument(iframe);
432 var iframeDom = goog.dom.getDomHelper(iframeDoc);
433 var iframeWindow = goog.dom.getWindow(iframeDoc);
434
435 var button = new goog.ui.MenuButton();
436 iframeWindow.scrollTo(0, 0);
437 var node = iframeDom.getElement('demoMenuButton');
438 button.decorate(node);
439 var mockTimer = new goog.Timer();
440 // Don't start the timer. We manually dispatch the Tick event.
441 mockTimer.start = goog.nullFunction;
442 button.timer_ = mockTimer;
443
444 var replacer = new goog.testing.PropertyReplacer();
445 var positionMenuCalled;
446 var origPositionMenu = goog.bind(button.positionMenu, button);
447 replacer.set(button, 'positionMenu', function() {
448 positionMenuCalled = true;
449 origPositionMenu();
450 });
451
452 // Show the menu.
453 button.setOpen(true);
454
455 // Confirm the menu position
456 var menuNode = iframeDom.getElement('demoMenu');
457 assertRoughlyEquals(
458 menuNode.offsetTop, node.offsetTop + node.offsetHeight, 20);
459 assertRoughlyEquals(menuNode.offsetLeft, node.offsetLeft, 20);
460
461 positionMenuCalled = false;
462 // A Tick event is dispatched.
463 mockTimer.dispatchEvent(goog.Timer.TICK);
464 assertFalse('positionMenu() shouldn\'t be called.', positionMenuCalled);
465
466 // Move the menu button by DOM structure change
467 var p1 = iframeDom.createDom(
468 goog.dom.TagName.P, null, iframeDom.createTextNode('foo'));
469 var p2 = iframeDom.createDom(
470 goog.dom.TagName.P, null, iframeDom.createTextNode('foo'));
471 var p3 = iframeDom.createDom(
472 goog.dom.TagName.P, null, iframeDom.createTextNode('foo'));
473 iframeDom.insertSiblingBefore(p1, node);
474 iframeDom.insertSiblingBefore(p2, node);
475 iframeDom.insertSiblingBefore(p3, node);
476
477 // Confirm the menu is detached from the button.
478 assertTrue(
479 Math.abs(node.offsetTop + node.offsetHeight - menuNode.offsetTop) > 20);
480
481 positionMenuCalled = false;
482 // A Tick event is dispatched.
483 mockTimer.dispatchEvent(goog.Timer.TICK);
484 assertTrue('positionMenu() should be called.', positionMenuCalled);
485
486 // The menu is moved to appropriate position again.
487 assertRoughlyEquals(
488 menuNode.offsetTop, node.offsetTop + node.offsetHeight, 20);
489
490 // Make the frame page scrollable.
491 var viewportHeight = iframeDom.getViewportSize().height;
492 var footer = iframeDom.getElement('footer');
493 goog.style.setSize(footer, 1, viewportHeight * 2);
494 // Change the viewport offset.
495 iframeWindow.scrollTo(0, viewportHeight);
496 // A Tick event is dispatched and positionMenu() should be called.
497 positionMenuCalled = false;
498 mockTimer.dispatchEvent(goog.Timer.TICK);
499 assertTrue('positionMenu() should be called.', positionMenuCalled);
500 goog.style.setSize(footer, 1, 1);
501
502 // Tear down.
503 iframeDom.removeNode(p1);
504 iframeDom.removeNode(p2);
505 iframeDom.removeNode(p3);
506 replacer.reset();
507 button.dispose();
508 }
509
510
511 /**
512 * Use a different button to position the menu and make sure it does so
513 * correctly.
514 */
515 function testAlternatePositioningElement() {
516 var node = goog.dom.getElement('demoMenuButton');
517 menuButton.decorate(node);
518
519 var posElement = goog.dom.getElement('positionElement');
520 menuButton.setPositionElement(posElement);
521
522 // Show the menu.
523 menuButton.setOpen(true);
524
525 // Confirm the menu position
526 var menuNode = menuButton.getMenu().getElement();
527 assertRoughlyEquals(
528 menuNode.offsetTop, posElement.offsetTop + posElement.offsetHeight, 20);
529 assertRoughlyEquals(menuNode.offsetLeft, posElement.offsetLeft, 20);
530 }
531
532
533 /**
534 * Test forced positioning above the button.
535 */
536 function testPositioningAboveAnchor() {
537 var node = goog.dom.getElement('demoMenuButton');
538 menuButton.decorate(node);
539
540 // Show the menu.
541 menuButton.setAlignMenuToStart(true); // Should get overridden below
542 menuButton.setScrollOnOverflow(true); // Should get overridden below
543
544 var position = new goog.positioning.MenuAnchoredPosition(
545 menuButton.getElement(), goog.positioning.Corner.TOP_START,
546 /* opt_adjust */ false, /* opt_resize */ false);
547 menuButton.setMenuPosition(position);
548 menuButton.setOpen(true);
549
550 // Confirm the menu position
551 var buttonBounds = goog.style.getBounds(node);
552 var menuNode = menuButton.getMenu().getElement();
553 var menuBounds = goog.style.getBounds(menuNode);
554
555 assertRoughlyEquals(menuBounds.top + menuBounds.height, buttonBounds.top, 3);
556 assertRoughlyEquals(menuBounds.left, buttonBounds.left, 3);
557 // For this test to be valid, the node must have non-trival height.
558 assertRoughlyEquals(node.offsetHeight, 19, 3);
559 }
560
561
562 /**
563 * Test forced positioning below the button.
564 */
565 function testPositioningBelowAnchor() {
566 var node = goog.dom.getElement('demoMenuButton');
567 menuButton.decorate(node);
568
569 // Show the menu.
570 menuButton.setAlignMenuToStart(true); // Should get overridden below
571 menuButton.setScrollOnOverflow(true); // Should get overridden below
572
573 var position = new goog.positioning.MenuAnchoredPosition(
574 menuButton.getElement(), goog.positioning.Corner.BOTTOM_START,
575 /* opt_adjust */ false, /* opt_resize */ false);
576 menuButton.setMenuPosition(position);
577 menuButton.setOpen(true);
578
579 // Confirm the menu position
580 var buttonBounds = goog.style.getBounds(node);
581 var menuNode = menuButton.getMenu().getElement();
582 var menuBounds = goog.style.getBounds(menuNode);
583
584 expectedFailures.expectFailureFor(isWinSafariBefore5());
585 try {
586 assertRoughlyEquals(
587 menuBounds.top, buttonBounds.top + buttonBounds.height, 3);
588 assertRoughlyEquals(menuBounds.left, buttonBounds.left, 3);
589 } catch (e) {
590 expectedFailures.handleException(e);
591 }
592 // For this test to be valid, the node must have non-trival height.
593 assertRoughlyEquals(node.offsetHeight, 19, 3);
594 }
595
596 function isWinSafariBefore5() {
597 return goog.userAgent.WINDOWS && goog.userAgent.product.SAFARI &&
598 goog.userAgent.product.isVersion(4) &&
599 !goog.userAgent.product.isVersion(5);
600 }
601
602
603 /**
604 * Tests that space, and only space, fire on key up.
605 */
606 function testSpaceFireOnKeyUp() {
607 var node = goog.dom.getElement('demoMenuButton');
608 menuButton.decorate(node);
609
610 e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, menuButton);
611 e.preventDefault = goog.testing.recordFunction();
612 e.keyCode = goog.events.KeyCodes.SPACE;
613 menuButton.handleKeyEvent(e);
614 assertFalse(
615 'Menu must not have been triggered by Space keypress',
616 menuButton.isOpen());
617 assertNotNull('Page scrolling is prevented', e.preventDefault.getLastCall());
618
619 e = new goog.events.Event(goog.events.EventType.KEYUP, menuButton);
620 e.keyCode = goog.events.KeyCodes.SPACE;
621 menuButton.handleKeyEvent(e);
622 assertTrue(
623 'Menu must have been triggered by Space keyup', menuButton.isOpen());
624 menuButton.getMenu().setHighlightedIndex(0);
625 e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, menuButton);
626 e.keyCode = goog.events.KeyCodes.DOWN;
627 menuButton.handleKeyEvent(e);
628 assertEquals(
629 'Highlighted menu item must have hanged by Down keypress', 1,
630 menuButton.getMenu().getHighlightedIndex());
631
632 menuButton.getMenu().setHighlightedIndex(0);
633 e = new goog.events.Event(goog.events.EventType.KEYUP, menuButton);
634 e.keyCode = goog.events.KeyCodes.DOWN;
635 menuButton.handleKeyEvent(e);
636 assertEquals(
637 'Highlighted menu item must not have changed by Down keyup', 0,
638 menuButton.getMenu().getHighlightedIndex());
639 }
640
641
642 /**
643 * Tests that preventing the button from closing also prevents the menu from
644 * being hidden.
645 */
646 function testPreventHide() {
647 var node = goog.dom.getElement('demoMenuButton');
648 menuButton.decorate(node);
649 menuButton.setDispatchTransitionEvents(goog.ui.Component.State.OPENED, true);
650
651 // Show the menu.
652 menuButton.setOpen(true);
653 assertTrue('Menu button should be open.', menuButton.isOpen());
654 assertTrue('Menu should be visible.', menuButton.getMenu().isVisible());
655
656 var key = goog.events.listen(
657 menuButton, goog.ui.Component.EventType.CLOSE,
658 function(event) { event.preventDefault(); });
659
660 // Try to hide the menu.
661 menuButton.setOpen(false);
662 assertTrue('Menu button should still be open.', menuButton.isOpen());
663 assertTrue('Menu should still be visible.', menuButton.getMenu().isVisible());
664
665 // Remove listener and try again.
666 goog.events.unlistenByKey(key);
667 menuButton.setOpen(false);
668 assertFalse('Menu button should not be open.', menuButton.isOpen());
669 assertFalse('Menu should not be visible.', menuButton.getMenu().isVisible());
670 }
671
672
673 /**
674 * Tests that opening and closing the menu does not affect how adding or
675 * removing menu items changes the size of the menu.
676 */
677 function testResizeOnItemAddOrRemove() {
678 var node = goog.dom.getElement('demoMenuButton');
679 menuButton.decorate(node);
680 var menu = menuButton.getMenu();
681
682 // Show the menu.
683 menuButton.setOpen(true);
684 var originalSize = goog.style.getSize(menu.getElement());
685
686 // Check that removing an item while the menu is left open correctly changes
687 // the size of the menu.
688 // Remove an item using a method on Menu.
689 var item = menu.removeChildAt(0, true);
690 // Confirm size of menu changed.
691 var afterRemoveSize = goog.style.getSize(menu.getElement());
692 assertTrue(
693 'Height of menu must decrease after removing a menu item.',
694 afterRemoveSize.height < originalSize.height);
695
696 // Check that removing an item while the menu is closed, then opened
697 // (so that reposition is called) correctly changes the size of the menu.
698 // Hide menu.
699 menuButton.setOpen(false);
700 var item2 = menu.removeChildAt(0, true);
701 menuButton.setOpen(true);
702 // Confirm size of menu changed.
703 var afterRemoveAgainSize = goog.style.getSize(menu.getElement());
704 assertTrue(
705 'Height of menu must decrease after removing a second menu item.',
706 afterRemoveAgainSize.height < afterRemoveSize.height);
707
708 // Check that adding an item while the menu is opened, then closed, then
709 // opened, correctly changes the size of the menu.
710 // Add an item, this time using a MenuButton method.
711 menuButton.setOpen(true);
712 menuButton.addItem(item2);
713 menuButton.setOpen(false);
714 menuButton.setOpen(true);
715 // Confirm size of menu changed.
716 var afterAddSize = goog.style.getSize(menu.getElement());
717 assertTrue(
718 'Height of menu must increase after adding a menu item.',
719 afterRemoveAgainSize.height < afterAddSize.height);
720 assertEquals(
721 'Removing and adding back items must not change the height of a menu.',
722 afterRemoveSize.height, afterAddSize.height);
723
724 // Add back the last item to keep state consistent.
725 menuButton.addItem(item);
726 }
727
728
729 /**
730 * Tests that adding and removing items from a menu with scrollOnOverflow is on
731 * correctly resizes the menu.
732 */
733 function testResizeOnItemAddOrRemoveWithScrollOnOverflow() {
734 var node = goog.dom.getElement('demoMenuButton');
735 menuButton.decorate(node);
736 var menu = menuButton.getMenu();
737
738 // Show the menu.
739 menuButton.setScrollOnOverflow(true);
740 menuButton.setOpen(true);
741 var originalSize = goog.style.getSize(menu.getElement());
742
743 // Check that removing an item while the menu is left open correctly changes
744 // the size of the menu.
745 // Remove an item using a method on Menu.
746 var item = menu.removeChildAt(0, true);
747 menuButton.invalidateMenuSize();
748 menuButton.positionMenu();
749
750 // Confirm size of menu changed.
751 var afterRemoveSize = goog.style.getSize(menu.getElement());
752 assertTrue(
753 'Height of menu must decrease after removing a menu item.',
754 afterRemoveSize.height < originalSize.height);
755
756 var item2 = menu.removeChildAt(0, true);
757 menuButton.invalidateMenuSize();
758 menuButton.positionMenu();
759
760 // Confirm size of menu changed.
761 var afterRemoveAgainSize = goog.style.getSize(menu.getElement());
762 assertTrue(
763 'Height of menu must decrease after removing a second menu item.',
764 afterRemoveAgainSize.height < afterRemoveSize.height);
765
766 // Check that adding an item while the menu is opened correctly changes the
767 // size of the menu.
768 menuButton.addItem(item2);
769 menuButton.invalidateMenuSize();
770 menuButton.positionMenu();
771
772 // Confirm size of menu changed.
773 var afterAddSize = goog.style.getSize(menu.getElement());
774 assertTrue(
775 'Height of menu must increase after adding a menu item.',
776 afterRemoveAgainSize.height < afterAddSize.height);
777 assertEquals(
778 'Removing and adding back items must not change the height of a menu.',
779 afterRemoveSize.height, afterAddSize.height);
780 }
781
782
783 /**
784 * Try rendering the menu as a sibling rather than as a child of the dom. This
785 * tests the case when the button is rendered, rather than decorated.
786 */
787 function testRenderMenuAsSibling() {
788 menuButton.setRenderMenuAsSibling(true);
789 menuButton.addItem(new goog.ui.MenuItem('Menu item 1'));
790 menuButton.addItem(new goog.ui.MenuItem('Menu item 2'));
791 // By default the menu is rendered into the top level dom and the button
792 // is rendered into whatever parent we provide. If we don't provide a
793 // parent then we aren't really testing anything, since both would be, by
794 // default, rendered into the top level dom, and therefore siblings.
795 menuButton.render(goog.dom.getElement('siblingTest'));
796 menuButton.setOpen(true);
797 assertEquals(
798 menuButton.getElement().parentNode,
799 menuButton.getMenu().getElement().parentNode);
800 }
801
802
803 /**
804 * Check that we render the menu as a sibling of the menu button, immediately
805 * after the menu button.
806 */
807 function testRenderMenuAsSiblingForDecoratedButton() {
808 var menu = new goog.ui.Menu();
809 menu.addChild(new goog.ui.MenuItem('Menu item 1'), true /* render */);
810 menu.addChild(new goog.ui.MenuItem('Menu item 2'), true /* render */);
811 menu.addChild(new goog.ui.MenuItem('Menu item 3'), true /* render */);
812
813 var menuButton = new goog.ui.MenuButton();
814 menuButton.setMenu(menu);
815 menuButton.setRenderMenuAsSibling(true);
816 var node = goog.dom.getElement('button1');
817 menuButton.decorate(node);
818
819 menuButton.setOpen(true);
820
821 assertEquals(
822 'The menu should be rendered immediately after the menu button',
823 goog.dom.getNextElementSibling(menuButton.getElement()),
824 menu.getElement());
825
826 assertEquals(
827 'The menu should be rendered immediately before the next button',
828 goog.dom.getNextElementSibling(menu.getElement()),
829 goog.dom.getElement('button2'));
830 }
831
832 function testAlignToStartSetter() {
833 assertTrue(menuButton.isAlignMenuToStart());
834
835 menuButton.setAlignMenuToStart(false);
836 assertFalse(menuButton.isAlignMenuToStart());
837
838 menuButton.setAlignMenuToStart(true);
839 assertTrue(menuButton.isAlignMenuToStart());
840 }
841
842 function testScrollOnOverflowSetter() {
843 assertFalse(menuButton.isScrollOnOverflow());
844
845 menuButton.setScrollOnOverflow(true);
846 assertTrue(menuButton.isScrollOnOverflow());
847
848 menuButton.setScrollOnOverflow(false);
849 assertFalse(menuButton.isScrollOnOverflow());
850 }
851
852
853 /**
854 * Tests that the attached menu has been set to aria-hidden=false explicitly
855 * when the menu is opened.
856 */
857 function testSetOpenUnsetsAriaHidden() {
858 var node = goog.dom.getElement('demoMenuButton');
859 menuButton.decorate(node);
860 var menuElem = menuButton.getMenu().getElementStrict();
861 goog.a11y.aria.setState(menuElem, goog.a11y.aria.State.HIDDEN, true);
862 menuButton.setOpen(true);
863 assertEquals(
864 '', goog.a11y.aria.getState(menuElem, goog.a11y.aria.State.HIDDEN));
865 }