toolkit/content/tests/chrome/test_menulist_keynav.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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"?>
     5 <window title="Menulist Key Navigation Tests"
     6         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
     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>      
    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"/>
    23 <script class="testbody" type="application/javascript">
    24 <![CDATA[
    26 SimpleTest.waitForExplicitFinish();
    28 var gShowPopup = false;
    29 var gModifiers = 0;
    31 var iswin = (navigator.platform.indexOf("Win") == 0);
    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   }
    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");
    54   synthesizeKey("G", { });
    55   is(list.selectedItem, $("i1"), "letter pressed not found selectedItem");
    57   keyCheck(list, "T", 2, "letter pressed");
    58   keyCheck(list, "T", 2, "letter pressed");
    59   setTimeout(pressedAgain, 1200); 
    60 }
    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 }
    70 function differentPressed()
    71 {
    72   var list = $("list");
    73   keyCheck(list, "O", 1, "different letter pressed");
    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   }
    82   // now make sure that using a key scrolls the menu correctly
    83   gShowPopup = true;
    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;
    96   list.menupopup.maxHeight = 100;
    97   list.open = true;
    99   var rowdiff = list.getItemAtIndex(1).getBoundingClientRect().top -
   100                 list.getItemAtIndex(0).getBoundingClientRect().top;
   102   var item = list.getItemAtIndex(10);
   103   var originalPosition = item.getBoundingClientRect().top;
   105   list.menuBoxObject.activeChild = item;
   106   ok(item.getBoundingClientRect().top < originalPosition,
   107     "position of item 1: " + item.getBoundingClientRect().top + " -> " + originalPosition);
   109   originalPosition = item.getBoundingClientRect().top;
   111   synthesizeKey("VK_DOWN", { });
   112   is(item.getBoundingClientRect().top, originalPosition - rowdiff, "position of item 10");
   114   list.open = false;
   116   checkEnter();
   117 }
   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 }
   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 }
   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 }
   145 function checkEnterWithModifiers()
   146 {
   147   is(gModifiers, 1, "modifiers checked when not set");
   149   var list = $("list");
   150   ok(!list.open, "list closed on enter press");
   151   list.removeEventListener("popuphidden", checkEnterWithModifiers, false);
   153   list.addEventListener("popuphidden", done, false);
   154   list.open = true;
   156   synthesizeKey("VK_RETURN", { shiftKey: true, ctrlKey: true, altKey: true, metaKey: true });
   157 }
   159 function done()
   160 {
   161   is(gModifiers, 2, "modifiers checked when set");
   163   var list = $("list");
   164   ok(!list.open, "list closed on enter press with modifiers");
   165   list.removeEventListener("popuphidden", done, false);
   167   SimpleTest.finish();
   168 }
   170 SimpleTest.waitForFocus(runTests);
   172 ]]>
   173 </script>
   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>
   184 </window>

mercurial