/toolkit/content/tests/chrome/test_menulist_keynav.xul

http://github.com/zpao/v8monkey · Unknown · 183 lines · 145 code · 38 blank · 0 comment · 0 complexity · f6589b91446d474c4ce676b4513cb79e MD5 · raw file

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