browser_menu_api.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. "use strict";
  5. // Test that the Menu API works
  6. const URL = "data:text/html;charset=utf8,test page for menu api";
  7. const Menu = require("devtools/client/framework/menu");
  8. const MenuItem = require("devtools/client/framework/menu-item");
  9. add_task(function* () {
  10. info("Create a test tab and open the toolbox");
  11. let tab = yield addTab(URL);
  12. let target = TargetFactory.forTab(tab);
  13. let toolbox = yield gDevTools.showToolbox(target, "webconsole");
  14. yield testMenuItems();
  15. yield testMenuPopup(toolbox);
  16. yield testSubmenu(toolbox);
  17. });
  18. function* testMenuItems() {
  19. let menu = new Menu();
  20. let menuItem1 = new MenuItem();
  21. let menuItem2 = new MenuItem();
  22. menu.append(menuItem1);
  23. menu.append(menuItem2);
  24. is(menu.items.length, 2, "Correct number of 'items'");
  25. is(menu.items[0], menuItem1, "Correct reference to MenuItem");
  26. is(menu.items[1], menuItem2, "Correct reference to MenuItem");
  27. }
  28. function* testMenuPopup(toolbox) {
  29. let clickFired = false;
  30. let menu = new Menu({
  31. id: "menu-popup",
  32. });
  33. menu.append(new MenuItem({ type: "separator" }));
  34. let MENU_ITEMS = [
  35. new MenuItem({
  36. id: "menu-item-1",
  37. label: "Normal Item",
  38. click: () => {
  39. info("Click callback has fired for menu item");
  40. clickFired = true;
  41. },
  42. }),
  43. new MenuItem({
  44. label: "Checked Item",
  45. type: "checkbox",
  46. checked: true,
  47. }),
  48. new MenuItem({
  49. label: "Radio Item",
  50. type: "radio",
  51. }),
  52. new MenuItem({
  53. label: "Disabled Item",
  54. disabled: true,
  55. }),
  56. ];
  57. for (let item of MENU_ITEMS) {
  58. menu.append(item);
  59. }
  60. // Append an invisible MenuItem, which shouldn't show up in the DOM
  61. menu.append(new MenuItem({
  62. label: "Invisible",
  63. visible: false,
  64. }));
  65. menu.popup(0, 0, toolbox);
  66. ok(toolbox.doc.querySelector("#menu-popup"), "A popup is in the DOM");
  67. let menuSeparators =
  68. toolbox.doc.querySelectorAll("#menu-popup > menuseparator");
  69. is(menuSeparators.length, 1, "A separator is in the menu");
  70. let menuItems = toolbox.doc.querySelectorAll("#menu-popup > menuitem");
  71. is(menuItems.length, MENU_ITEMS.length, "Correct number of menuitems");
  72. is(menuItems[0].id, MENU_ITEMS[0].id, "Correct id for menuitem");
  73. is(menuItems[0].getAttribute("label"), MENU_ITEMS[0].label, "Correct label");
  74. is(menuItems[1].getAttribute("label"), MENU_ITEMS[1].label, "Correct label");
  75. is(menuItems[1].getAttribute("type"), "checkbox", "Correct type attr");
  76. is(menuItems[1].getAttribute("checked"), "true", "Has checked attr");
  77. is(menuItems[2].getAttribute("label"), MENU_ITEMS[2].label, "Correct label");
  78. is(menuItems[2].getAttribute("type"), "radio", "Correct type attr");
  79. ok(!menuItems[2].hasAttribute("checked"), "Doesn't have checked attr");
  80. is(menuItems[3].getAttribute("label"), MENU_ITEMS[3].label, "Correct label");
  81. is(menuItems[3].getAttribute("disabled"), "true", "disabled attr menuitem");
  82. yield once(menu, "open");
  83. let closed = once(menu, "close");
  84. EventUtils.synthesizeMouseAtCenter(menuItems[0], {}, toolbox.win);
  85. yield closed;
  86. ok(clickFired, "Click has fired");
  87. ok(!toolbox.doc.querySelector("#menu-popup"), "Popup removed from the DOM");
  88. }
  89. function* testSubmenu(toolbox) {
  90. let clickFired = false;
  91. let menu = new Menu({
  92. id: "menu-popup",
  93. });
  94. let submenu = new Menu({
  95. id: "submenu-popup",
  96. });
  97. submenu.append(new MenuItem({
  98. label: "Submenu item",
  99. click: () => {
  100. info("Click callback has fired for submenu item");
  101. clickFired = true;
  102. },
  103. }));
  104. menu.append(new MenuItem({
  105. label: "Submenu parent",
  106. submenu: submenu,
  107. }));
  108. menu.append(new MenuItem({
  109. label: "Submenu parent with attributes",
  110. id: "submenu-parent-with-attrs",
  111. submenu: submenu,
  112. accesskey: "A",
  113. disabled: true,
  114. }));
  115. menu.popup(0, 0, toolbox);
  116. ok(toolbox.doc.querySelector("#menu-popup"), "A popup is in the DOM");
  117. is(toolbox.doc.querySelectorAll("#menu-popup > menuitem").length, 0,
  118. "No menuitem children");
  119. let menus = toolbox.doc.querySelectorAll("#menu-popup > menu");
  120. is(menus.length, 2, "Correct number of menus");
  121. is(menus[0].getAttribute("label"), "Submenu parent", "Correct label");
  122. ok(!menus[0].hasAttribute("disabled"), "Correct disabled state");
  123. is(menus[1].getAttribute("accesskey"), "A", "Correct accesskey");
  124. ok(menus[1].hasAttribute("disabled"), "Correct disabled state");
  125. ok(menus[1].id, "submenu-parent-with-attrs", "Correct id");
  126. let subMenuItems = menus[0].querySelectorAll("menupopup > menuitem");
  127. is(subMenuItems.length, 1, "Correct number of submenu items");
  128. is(subMenuItems[0].getAttribute("label"), "Submenu item", "Correct label");
  129. yield once(menu, "open");
  130. let closed = once(menu, "close");
  131. info("Using keyboard navigation to open, close, and reopen the submenu");
  132. let shown = once(menus[0], "popupshown");
  133. EventUtils.synthesizeKey("VK_DOWN", {});
  134. EventUtils.synthesizeKey("VK_RIGHT", {});
  135. yield shown;
  136. let hidden = once(menus[0], "popuphidden");
  137. EventUtils.synthesizeKey("VK_LEFT", {});
  138. yield hidden;
  139. shown = once(menus[0], "popupshown");
  140. EventUtils.synthesizeKey("VK_RIGHT", {});
  141. yield shown;
  142. info("Clicking the submenu item");
  143. EventUtils.synthesizeMouseAtCenter(subMenuItems[0], {}, toolbox.win);
  144. yield closed;
  145. ok(clickFired, "Click has fired");
  146. }