browser_toolbox_sidebar.js 5.7 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. function test() {
  5. const Cu = Components.utils;
  6. let {ToolSidebar} = require("devtools/client/framework/sidebar");
  7. const toolURL = "data:text/xml;charset=utf8,<?xml version='1.0'?>" +
  8. "<window xmlns='http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'>" +
  9. "<hbox flex='1'><description flex='1'>foo</description><splitter class='devtools-side-splitter'/>" +
  10. "<tabbox flex='1' id='sidebar' class='devtools-sidebar-tabs'><tabs/><tabpanels flex='1'/></tabbox>" +
  11. "</hbox>" +
  12. "</window>";
  13. const tab1URL = "data:text/html;charset=utf8,<title>1</title><p>1</p>";
  14. const tab2URL = "data:text/html;charset=utf8,<title>2</title><p>2</p>";
  15. const tab3URL = "data:text/html;charset=utf8,<title>3</title><p>3</p>";
  16. let panelDoc;
  17. let tab1Selected = false;
  18. let registeredTabs = {};
  19. let readyTabs = {};
  20. let toolDefinition = {
  21. id: "fakeTool4242",
  22. visibilityswitch: "devtools.fakeTool4242.enabled",
  23. url: toolURL,
  24. label: "FAKE TOOL!!!",
  25. isTargetSupported: () => true,
  26. build: function (iframeWindow, toolbox) {
  27. let deferred = defer();
  28. executeSoon(() => {
  29. deferred.resolve({
  30. target: toolbox.target,
  31. toolbox: toolbox,
  32. isReady: true,
  33. destroy: function () {},
  34. panelDoc: iframeWindow.document,
  35. });
  36. });
  37. return deferred.promise;
  38. },
  39. };
  40. gDevTools.registerTool(toolDefinition);
  41. addTab("about:blank").then(function (aTab) {
  42. let target = TargetFactory.forTab(aTab);
  43. gDevTools.showToolbox(target, toolDefinition.id).then(function (toolbox) {
  44. let panel = toolbox.getPanel(toolDefinition.id);
  45. panel.toolbox = toolbox;
  46. ok(true, "Tool open");
  47. let tabbox = panel.panelDoc.getElementById("sidebar");
  48. panel.sidebar = new ToolSidebar(tabbox, panel, "testbug865688", true);
  49. panel.sidebar.on("new-tab-registered", function (event, id) {
  50. registeredTabs[id] = true;
  51. });
  52. panel.sidebar.once("tab1-ready", function (event) {
  53. info(event);
  54. readyTabs.tab1 = true;
  55. allTabsReady(panel);
  56. });
  57. panel.sidebar.once("tab2-ready", function (event) {
  58. info(event);
  59. readyTabs.tab2 = true;
  60. allTabsReady(panel);
  61. });
  62. panel.sidebar.once("tab3-ready", function (event) {
  63. info(event);
  64. readyTabs.tab3 = true;
  65. allTabsReady(panel);
  66. });
  67. panel.sidebar.once("tab1-selected", function (event) {
  68. info(event);
  69. tab1Selected = true;
  70. allTabsReady(panel);
  71. });
  72. panel.sidebar.addTab("tab1", tab1URL, {selected: true});
  73. panel.sidebar.addTab("tab2", tab2URL);
  74. panel.sidebar.addTab("tab3", tab3URL);
  75. panel.sidebar.show();
  76. }).then(null, console.error);
  77. });
  78. function allTabsReady(panel) {
  79. if (!tab1Selected || !readyTabs.tab1 || !readyTabs.tab2 || !readyTabs.tab3) {
  80. return;
  81. }
  82. ok(registeredTabs.tab1, "tab1 registered");
  83. ok(registeredTabs.tab2, "tab2 registered");
  84. ok(registeredTabs.tab3, "tab3 registered");
  85. ok(readyTabs.tab1, "tab1 ready");
  86. ok(readyTabs.tab2, "tab2 ready");
  87. ok(readyTabs.tab3, "tab3 ready");
  88. let tabs = panel.sidebar._tabbox.querySelectorAll("tab");
  89. let panels = panel.sidebar._tabbox.querySelectorAll("tabpanel");
  90. let label = 1;
  91. for (let tab of tabs) {
  92. is(tab.getAttribute("label"), label++, "Tab has the right title");
  93. }
  94. is(label, 4, "Found the right amount of tabs.");
  95. is(panel.sidebar._tabbox.selectedPanel, panels[0], "First tab is selected");
  96. is(panel.sidebar.getCurrentTabID(), "tab1", "getCurrentTabID() is correct");
  97. panel.sidebar.once("tab1-unselected", function () {
  98. ok(true, "received 'unselected' event");
  99. panel.sidebar.once("tab2-selected", function () {
  100. ok(true, "received 'selected' event");
  101. tabs[1].focus();
  102. is(panel.sidebar._panelDoc.activeElement, tabs[1],
  103. "Focus is set to second tab");
  104. panel.sidebar.hide();
  105. isnot(panel.sidebar._panelDoc.activeElement, tabs[1],
  106. "Focus is reset for sidebar");
  107. is(panel.sidebar._tabbox.getAttribute("hidden"), "true", "Sidebar hidden");
  108. is(panel.sidebar.getWindowForTab("tab1").location.href, tab1URL, "Window is accessible");
  109. testRemoval(panel);
  110. });
  111. });
  112. panel.sidebar.select("tab2");
  113. }
  114. function testRemoval(panel) {
  115. panel.sidebar.once("tab-unregistered", function (event, id) {
  116. info(event);
  117. registeredTabs[id] = false;
  118. is(id, "tab3", "The right tab must be removed");
  119. let tabs = panel.sidebar._tabbox.querySelectorAll("tab");
  120. let panels = panel.sidebar._tabbox.querySelectorAll("tabpanel");
  121. is(tabs.length, 2, "There is the right number of tabs");
  122. is(panels.length, 2, "There is the right number of panels");
  123. testWidth(panel);
  124. });
  125. panel.sidebar.removeTab("tab3");
  126. }
  127. function testWidth(panel) {
  128. let tabbox = panel.panelDoc.getElementById("sidebar");
  129. tabbox.width = 420;
  130. panel.sidebar.destroy().then(function () {
  131. tabbox.width = 0;
  132. panel.sidebar = new ToolSidebar(tabbox, panel, "testbug865688", true);
  133. panel.sidebar.show();
  134. is(panel.panelDoc.getElementById("sidebar").width, 420, "Width restored");
  135. finishUp(panel);
  136. });
  137. }
  138. function finishUp(panel) {
  139. panel.sidebar.destroy();
  140. panel.toolbox.destroy().then(function () {
  141. gDevTools.unregisterTool(toolDefinition.id);
  142. gBrowser.removeCurrentTab();
  143. executeSoon(function () {
  144. finish();
  145. });
  146. });
  147. }
  148. }