toolkit/content/tests/chrome/test_menulist_keynav.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/tests/chrome/test_menulist_keynav.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
     1.7 +
     1.8 +<window title="Menulist Key Navigation Tests"
     1.9 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.10 +
    1.11 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>      
    1.12 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>      
    1.13 +
    1.14 +<button id="button1" label="One"/>
    1.15 +<menulist id="list">
    1.16 +  <menupopup id="popup" onpopupshowing="return gShowPopup;">
    1.17 +    <menuitem id="i1" label="One"/>
    1.18 +    <menuitem id="i2" label="Two"/>
    1.19 +    <menuitem id="i2b" disabled="true" label="Two and a Half"/>
    1.20 +    <menuitem id="i3" label="Three"/>
    1.21 +    <menuitem id="i4" label="Four"/>
    1.22 +  </menupopup>
    1.23 +</menulist>
    1.24 +<button id="button2" label="Two"/>
    1.25 +
    1.26 +<script class="testbody" type="application/javascript">
    1.27 +<![CDATA[
    1.28 +
    1.29 +SimpleTest.waitForExplicitFinish();
    1.30 +
    1.31 +var gShowPopup = false;
    1.32 +var gModifiers = 0;
    1.33 +
    1.34 +var iswin = (navigator.platform.indexOf("Win") == 0);
    1.35 +
    1.36 +function runTests()
    1.37 +{
    1.38 +  var list = $("list");
    1.39 +  list.focus();
    1.40 +  // on Mac, up and cursor keys open the menu, but on other platforms, the
    1.41 +  // cursor keys navigate between items without opening the menu
    1.42 +  if (navigator.platform.indexOf("Mac") == -1) {
    1.43 +    keyCheck(list, "VK_DOWN", 2, "cursor down");
    1.44 +    keyCheck(list, "VK_DOWN", iswin ? "2b" : 3, "cursor down skip disabled");
    1.45 +    keyCheck(list, "VK_UP", 2, "cursor up skip disabled");
    1.46 +    keyCheck(list, "VK_UP", 1, "cursor up");
    1.47 +    keyCheck(list, "VK_UP", 4, "cursor up wrap");
    1.48 +    keyCheck(list, "VK_DOWN", 1, "cursor down wrap");
    1.49 +  }
    1.50 +
    1.51 +  // check that attempting to open the menulist does not change the selection
    1.52 +  synthesizeKey("VK_DOWN", { altKey: navigator.platform.indexOf("Mac") == -1 });
    1.53 +  is(list.selectedItem, $("i1"), "open menulist down selectedItem");
    1.54 +  synthesizeKey("VK_UP", { altKey: navigator.platform.indexOf("Mac") == -1 });
    1.55 +  is(list.selectedItem, $("i1"), "open menulist up selectedItem");
    1.56 +
    1.57 +  synthesizeKey("G", { });
    1.58 +  is(list.selectedItem, $("i1"), "letter pressed not found selectedItem");
    1.59 +
    1.60 +  keyCheck(list, "T", 2, "letter pressed");
    1.61 +  keyCheck(list, "T", 2, "letter pressed");
    1.62 +  setTimeout(pressedAgain, 1200); 
    1.63 +}
    1.64 +
    1.65 +function pressedAgain()
    1.66 +{
    1.67 +  var list = $("list");
    1.68 +  keyCheck(list, "T", iswin ? "2b" : 3, "letter pressed again");
    1.69 +  keyCheck(list, "W", 2, "second letter pressed");
    1.70 +  setTimeout(differentPressed, 1200); 
    1.71 +}
    1.72 +
    1.73 +function differentPressed()
    1.74 +{
    1.75 +  var list = $("list");
    1.76 +  keyCheck(list, "O", 1, "different letter pressed");
    1.77 +
    1.78 +  if (navigator.platform.indexOf("Mac") == -1) {
    1.79 +    $("button1").focus();
    1.80 +    synthesizeKeyExpectEvent("VK_TAB", { }, list, "focus", "focus to menulist");
    1.81 +    synthesizeKeyExpectEvent("VK_TAB", { }, $("button2"), "focus", "focus to button");
    1.82 +    is(document.activeElement, $("button2"), "tab from menulist focused button");
    1.83 +  }
    1.84 +
    1.85 +  // now make sure that using a key scrolls the menu correctly
    1.86 +  gShowPopup = true;
    1.87 +
    1.88 +  for (let i = 0; i < 65; i++) {
    1.89 +    list.appendItem("Item" + i, "item" + i);
    1.90 +  }
    1.91 +  list.open = true;
    1.92 +  is(list.getBoundingClientRect().width, list.firstChild.getBoundingClientRect().width,
    1.93 +     "menu and popup width match");
    1.94 +  var minScrollbarWidth = window.matchMedia("(-moz-overlay-scrollbars)").matches ? 0 : 3;
    1.95 +  ok(list.getBoundingClientRect().width >= list.getItemAtIndex(0).getBoundingClientRect().width + minScrollbarWidth,
    1.96 +     "menuitem width accounts for scrollbar");
    1.97 +  list.open = false;
    1.98 +
    1.99 +  list.menupopup.maxHeight = 100;
   1.100 +  list.open = true;
   1.101 +
   1.102 +  var rowdiff = list.getItemAtIndex(1).getBoundingClientRect().top -
   1.103 +                list.getItemAtIndex(0).getBoundingClientRect().top;
   1.104 +
   1.105 +  var item = list.getItemAtIndex(10);
   1.106 +  var originalPosition = item.getBoundingClientRect().top;
   1.107 +
   1.108 +  list.menuBoxObject.activeChild = item;
   1.109 +  ok(item.getBoundingClientRect().top < originalPosition,
   1.110 +    "position of item 1: " + item.getBoundingClientRect().top + " -> " + originalPosition);
   1.111 +
   1.112 +  originalPosition = item.getBoundingClientRect().top;
   1.113 +
   1.114 +  synthesizeKey("VK_DOWN", { });
   1.115 +  is(item.getBoundingClientRect().top, originalPosition - rowdiff, "position of item 10");
   1.116 +
   1.117 +  list.open = false;
   1.118 +
   1.119 +  checkEnter();
   1.120 +}
   1.121 +
   1.122 +function keyCheck(list, key, index, testname)
   1.123 +{
   1.124 +  var item = $("i" + index);
   1.125 +  synthesizeKeyExpectEvent(key, { }, item, "command", testname);
   1.126 +  is(list.selectedItem, item, testname + " selectedItem");
   1.127 +}
   1.128 +
   1.129 +function checkModifiers(event)
   1.130 +{
   1.131 +  var expectedModifiers = (gModifiers == 1);
   1.132 +  is(event.shiftKey, expectedModifiers, "shift key pressed");
   1.133 +  is(event.ctrlKey, expectedModifiers, "ctrl key pressed");
   1.134 +  is(event.altKey, expectedModifiers, "alt key pressed");
   1.135 +  is(event.metaKey, expectedModifiers, "meta key pressed");
   1.136 +  gModifiers++;
   1.137 +}
   1.138 +
   1.139 +function checkEnter()
   1.140 +{
   1.141 +  var list = $("list");
   1.142 +  list.addEventListener("popuphidden", checkEnterWithModifiers, false);
   1.143 +  list.addEventListener("command", checkModifiers, false);
   1.144 +  list.open = true;
   1.145 +  synthesizeKey("VK_RETURN", { });
   1.146 +}
   1.147 +
   1.148 +function checkEnterWithModifiers()
   1.149 +{
   1.150 +  is(gModifiers, 1, "modifiers checked when not set");
   1.151 +
   1.152 +  var list = $("list");
   1.153 +  ok(!list.open, "list closed on enter press");
   1.154 +  list.removeEventListener("popuphidden", checkEnterWithModifiers, false);
   1.155 +
   1.156 +  list.addEventListener("popuphidden", done, false);
   1.157 +  list.open = true;
   1.158 +
   1.159 +  synthesizeKey("VK_RETURN", { shiftKey: true, ctrlKey: true, altKey: true, metaKey: true });
   1.160 +}
   1.161 +
   1.162 +function done()
   1.163 +{
   1.164 +  is(gModifiers, 2, "modifiers checked when set");
   1.165 +
   1.166 +  var list = $("list");
   1.167 +  ok(!list.open, "list closed on enter press with modifiers");
   1.168 +  list.removeEventListener("popuphidden", done, false);
   1.169 +
   1.170 +  SimpleTest.finish();
   1.171 +}
   1.172 +
   1.173 +SimpleTest.waitForFocus(runTests);
   1.174 +
   1.175 +]]>
   1.176 +</script>
   1.177 +
   1.178 +<body xmlns="http://www.w3.org/1999/xhtml">
   1.179 +<p id="display">
   1.180 +</p>
   1.181 +<div id="content" style="display: none">
   1.182 +</div>
   1.183 +<pre id="test">
   1.184 +</pre>
   1.185 +</body>
   1.186 +
   1.187 +</window>

mercurial