|
1 <?xml version="1.0"?> |
|
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> |
|
3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> |
|
4 |
|
5 <window title="Menulist Key Navigation Tests" |
|
6 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> |
|
7 |
|
8 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script> |
|
10 |
|
11 <button id="button1" label="One"/> |
|
12 <menulist id="list"> |
|
13 <menupopup id="popup" onpopupshowing="return gShowPopup;"> |
|
14 <menuitem id="i1" label="One"/> |
|
15 <menuitem id="i2" label="Two"/> |
|
16 <menuitem id="i2b" disabled="true" label="Two and a Half"/> |
|
17 <menuitem id="i3" label="Three"/> |
|
18 <menuitem id="i4" label="Four"/> |
|
19 </menupopup> |
|
20 </menulist> |
|
21 <button id="button2" label="Two"/> |
|
22 |
|
23 <script class="testbody" type="application/javascript"> |
|
24 <![CDATA[ |
|
25 |
|
26 SimpleTest.waitForExplicitFinish(); |
|
27 |
|
28 var gShowPopup = false; |
|
29 var gModifiers = 0; |
|
30 |
|
31 var iswin = (navigator.platform.indexOf("Win") == 0); |
|
32 |
|
33 function runTests() |
|
34 { |
|
35 var list = $("list"); |
|
36 list.focus(); |
|
37 // on Mac, up and cursor keys open the menu, but on other platforms, the |
|
38 // cursor keys navigate between items without opening the menu |
|
39 if (navigator.platform.indexOf("Mac") == -1) { |
|
40 keyCheck(list, "VK_DOWN", 2, "cursor down"); |
|
41 keyCheck(list, "VK_DOWN", iswin ? "2b" : 3, "cursor down skip disabled"); |
|
42 keyCheck(list, "VK_UP", 2, "cursor up skip disabled"); |
|
43 keyCheck(list, "VK_UP", 1, "cursor up"); |
|
44 keyCheck(list, "VK_UP", 4, "cursor up wrap"); |
|
45 keyCheck(list, "VK_DOWN", 1, "cursor down wrap"); |
|
46 } |
|
47 |
|
48 // check that attempting to open the menulist does not change the selection |
|
49 synthesizeKey("VK_DOWN", { altKey: navigator.platform.indexOf("Mac") == -1 }); |
|
50 is(list.selectedItem, $("i1"), "open menulist down selectedItem"); |
|
51 synthesizeKey("VK_UP", { altKey: navigator.platform.indexOf("Mac") == -1 }); |
|
52 is(list.selectedItem, $("i1"), "open menulist up selectedItem"); |
|
53 |
|
54 synthesizeKey("G", { }); |
|
55 is(list.selectedItem, $("i1"), "letter pressed not found selectedItem"); |
|
56 |
|
57 keyCheck(list, "T", 2, "letter pressed"); |
|
58 keyCheck(list, "T", 2, "letter pressed"); |
|
59 setTimeout(pressedAgain, 1200); |
|
60 } |
|
61 |
|
62 function pressedAgain() |
|
63 { |
|
64 var list = $("list"); |
|
65 keyCheck(list, "T", iswin ? "2b" : 3, "letter pressed again"); |
|
66 keyCheck(list, "W", 2, "second letter pressed"); |
|
67 setTimeout(differentPressed, 1200); |
|
68 } |
|
69 |
|
70 function differentPressed() |
|
71 { |
|
72 var list = $("list"); |
|
73 keyCheck(list, "O", 1, "different letter pressed"); |
|
74 |
|
75 if (navigator.platform.indexOf("Mac") == -1) { |
|
76 $("button1").focus(); |
|
77 synthesizeKeyExpectEvent("VK_TAB", { }, list, "focus", "focus to menulist"); |
|
78 synthesizeKeyExpectEvent("VK_TAB", { }, $("button2"), "focus", "focus to button"); |
|
79 is(document.activeElement, $("button2"), "tab from menulist focused button"); |
|
80 } |
|
81 |
|
82 // now make sure that using a key scrolls the menu correctly |
|
83 gShowPopup = true; |
|
84 |
|
85 for (let i = 0; i < 65; i++) { |
|
86 list.appendItem("Item" + i, "item" + i); |
|
87 } |
|
88 list.open = true; |
|
89 is(list.getBoundingClientRect().width, list.firstChild.getBoundingClientRect().width, |
|
90 "menu and popup width match"); |
|
91 var minScrollbarWidth = window.matchMedia("(-moz-overlay-scrollbars)").matches ? 0 : 3; |
|
92 ok(list.getBoundingClientRect().width >= list.getItemAtIndex(0).getBoundingClientRect().width + minScrollbarWidth, |
|
93 "menuitem width accounts for scrollbar"); |
|
94 list.open = false; |
|
95 |
|
96 list.menupopup.maxHeight = 100; |
|
97 list.open = true; |
|
98 |
|
99 var rowdiff = list.getItemAtIndex(1).getBoundingClientRect().top - |
|
100 list.getItemAtIndex(0).getBoundingClientRect().top; |
|
101 |
|
102 var item = list.getItemAtIndex(10); |
|
103 var originalPosition = item.getBoundingClientRect().top; |
|
104 |
|
105 list.menuBoxObject.activeChild = item; |
|
106 ok(item.getBoundingClientRect().top < originalPosition, |
|
107 "position of item 1: " + item.getBoundingClientRect().top + " -> " + originalPosition); |
|
108 |
|
109 originalPosition = item.getBoundingClientRect().top; |
|
110 |
|
111 synthesizeKey("VK_DOWN", { }); |
|
112 is(item.getBoundingClientRect().top, originalPosition - rowdiff, "position of item 10"); |
|
113 |
|
114 list.open = false; |
|
115 |
|
116 checkEnter(); |
|
117 } |
|
118 |
|
119 function keyCheck(list, key, index, testname) |
|
120 { |
|
121 var item = $("i" + index); |
|
122 synthesizeKeyExpectEvent(key, { }, item, "command", testname); |
|
123 is(list.selectedItem, item, testname + " selectedItem"); |
|
124 } |
|
125 |
|
126 function checkModifiers(event) |
|
127 { |
|
128 var expectedModifiers = (gModifiers == 1); |
|
129 is(event.shiftKey, expectedModifiers, "shift key pressed"); |
|
130 is(event.ctrlKey, expectedModifiers, "ctrl key pressed"); |
|
131 is(event.altKey, expectedModifiers, "alt key pressed"); |
|
132 is(event.metaKey, expectedModifiers, "meta key pressed"); |
|
133 gModifiers++; |
|
134 } |
|
135 |
|
136 function checkEnter() |
|
137 { |
|
138 var list = $("list"); |
|
139 list.addEventListener("popuphidden", checkEnterWithModifiers, false); |
|
140 list.addEventListener("command", checkModifiers, false); |
|
141 list.open = true; |
|
142 synthesizeKey("VK_RETURN", { }); |
|
143 } |
|
144 |
|
145 function checkEnterWithModifiers() |
|
146 { |
|
147 is(gModifiers, 1, "modifiers checked when not set"); |
|
148 |
|
149 var list = $("list"); |
|
150 ok(!list.open, "list closed on enter press"); |
|
151 list.removeEventListener("popuphidden", checkEnterWithModifiers, false); |
|
152 |
|
153 list.addEventListener("popuphidden", done, false); |
|
154 list.open = true; |
|
155 |
|
156 synthesizeKey("VK_RETURN", { shiftKey: true, ctrlKey: true, altKey: true, metaKey: true }); |
|
157 } |
|
158 |
|
159 function done() |
|
160 { |
|
161 is(gModifiers, 2, "modifiers checked when set"); |
|
162 |
|
163 var list = $("list"); |
|
164 ok(!list.open, "list closed on enter press with modifiers"); |
|
165 list.removeEventListener("popuphidden", done, false); |
|
166 |
|
167 SimpleTest.finish(); |
|
168 } |
|
169 |
|
170 SimpleTest.waitForFocus(runTests); |
|
171 |
|
172 ]]> |
|
173 </script> |
|
174 |
|
175 <body xmlns="http://www.w3.org/1999/xhtml"> |
|
176 <p id="display"> |
|
177 </p> |
|
178 <div id="content" style="display: none"> |
|
179 </div> |
|
180 <pre id="test"> |
|
181 </pre> |
|
182 </body> |
|
183 |
|
184 </window> |