Debian Codegraph @

closure-library/closure/goog/ui/menu_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.MenuTest');
16 goog.setTestOnly('goog.ui.MenuTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.events');
20 goog.require('goog.math.Coordinate');
21 goog.require('goog.testing.events');
22 goog.require('goog.testing.jsunit');
23 goog.require('goog.ui.Component');
24 goog.require('goog.ui.Menu');
25
26 var menu;
27 var clonedMenuDom;
28
29 function setUp() {
30 clonedMenuDom = goog.dom.getElement('demoMenu').cloneNode(true);
31
32 menu = new goog.ui.Menu();
33 }
34
35 function tearDown() {
36 menu.dispose();
37
38 var element = goog.dom.getElement('demoMenu');
39 element.parentNode.replaceChild(clonedMenuDom, element);
40 }
41
42
43 /** @bug 1463524 */
44 function testMouseupDoesntActivateMenuItemImmediately() {
45 menu.decorate(goog.dom.getElement('demoMenu'));
46
47 var fakeEvent = {clientX: 42, clientY: 42};
48 var itemElem = goog.dom.getElement('menuItem2');
49 var coords = new goog.math.Coordinate(42, 42);
50
51 var menuItem = menu.getChildAt(1);
52 var actionDispatched = false;
53 goog.events.listen(menuItem, goog.ui.Component.EventType.ACTION, function(e) {
54 actionDispatched = true;
55 });
56
57 menu.setVisible(true, false, fakeEvent);
58 // Makes the menu item active so it can be selected on mouseup.
59 menuItem.setActive(true);
60
61 goog.testing.events.fireMouseUpEvent(itemElem, undefined, coords);
62 assertFalse(
63 'ACTION should not be dispatched after the initial mouseup',
64 actionDispatched);
65
66 goog.testing.events.fireMouseUpEvent(itemElem, undefined, coords);
67 assertTrue(
68 'ACTION should be dispatched after the second mouseup', actionDispatched);
69 }
70
71 function testHoverBehavior() {
72 menu.decorate(goog.dom.getElement('demoMenu'));
73
74 goog.testing.events.fireMouseOverEvent(
75 goog.dom.getElement('menuItem2'), document.body);
76 assertEquals(1, menu.getHighlightedIndex());
77
78 menu.exitDocument();
79 assertEquals(-1, menu.getHighlightedIndex());
80 }
81
82 function testIndirectionDecoration() {
83 menu.decorate(goog.dom.getElement('complexMenu'));
84
85 goog.testing.events.fireMouseOverEvent(
86 goog.dom.getElement('complexItem3'), document.body);
87 assertEquals(2, menu.getHighlightedIndex());
88
89 menu.exitDocument();
90 assertEquals(-1, menu.getHighlightedIndex());
91 }
92
93 function testSetHighlightedIndex() {
94 menu.decorate(goog.dom.getElement('scrollableMenu'));
95 assertEquals(0, menu.getElement().scrollTop);
96
97 // Scroll down
98 var element = goog.dom.getElement('scrollableMenuItem4');
99 menu.setHighlightedIndex(3);
100 assertTrue(element.offsetTop >= menu.getElement().scrollTop);
101 assertTrue(
102 element.offsetTop <=
103 menu.getElement().scrollTop + menu.getElement().offsetHeight);
104
105 // Scroll up
106 element = goog.dom.getElement('scrollableMenuItem3');
107 menu.setHighlightedIndex(2);
108 assertTrue(element.offsetTop >= menu.getElement().scrollTop);
109 assertTrue(
110 element.offsetTop <=
111 menu.getElement().scrollTop + menu.getElement().offsetHeight);
112
113 menu.exitDocument();
114 assertEquals(-1, menu.getHighlightedIndex());
115 }