Bug 875902 - [toolbox] Simplify selectTool and test for panel readiness, r=paul
authorDavid Creswick <dcrewi@gyrae.net>
Sat, 22 Jun 2013 20:00:51 -0700
changeset 147663 1d0d9f1d632179355a516c4bf3cb0eb5758cc443
parent 147662 7c1c4bb1263a14a9cbbe8022dbfeb3e0837fa06c
child 147664 8d8330b65f2684005a263eb5ea709b45c493763b
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaul
bugs875902
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 875902 - [toolbox] Simplify selectTool and test for panel readiness, r=paul
browser/devtools/framework/test/browser_toolbox_tool_ready.js
browser/devtools/framework/toolbox-options.js
browser/devtools/framework/toolbox.js
--- a/browser/devtools/framework/test/browser_toolbox_tool_ready.js
+++ b/browser/devtools/framework/test/browser_toolbox_tool_ready.js
@@ -1,23 +1,26 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 function test() {
   addTab().then(function(data) {
-    let toolIds = [ "jsdebugger", "styleeditor", "webconsole", "inspector" ];
+    let toolIds = gDevTools.getToolDefinitionArray().map((def) => def.id);
 
     let open = function(index) {
       let toolId = toolIds[index];
 
       info("About to open " + index + "/" + toolId);
       gDevTools.showToolbox(data.target, toolId).then(function(toolbox) {
         ok(toolbox, "toolbox exists for " + toolId);
         is(toolbox.currentToolId, toolId, "currentToolId should be " + toolId);
 
+        let panel = toolbox.getCurrentPanel();
+        ok(panel.isReady, toolId + " panel should be ready");
+
         let nextIndex = index + 1;
         if (nextIndex >= toolIds.length) {
           toolbox.destroy();
           finish();
         }
         else {
           open(nextIndex);
         }
--- a/browser/devtools/framework/toolbox-options.js
+++ b/browser/devtools/framework/toolbox-options.js
@@ -33,16 +33,17 @@ XPCOMUtils.defineLazyGetter(this, "l10n"
 
 /**
  * Represents the Options Panel in the Toolbox.
  */
 function OptionsPanel(iframeWindow, toolbox) {
   this.panelDoc = iframeWindow.document;
   this.panelWin = iframeWindow;
   this.toolbox = toolbox;
+  this.isReady = false;
 
   // Make restart method available from xul
   this.panelWin.restart = this.restart;
 
   EventEmitter.decorate(this);
 };
 
 OptionsPanel.prototype = {
@@ -58,16 +59,17 @@ OptionsPanel.prototype = {
     this.populatePreferences();
     this.prepareRestartPreferences();
 
     this._disableJSClicked = this._disableJSClicked.bind(this);
 
     let disableJSNode = this.panelDoc.getElementById("devtools-disable-javascript");
     disableJSNode.addEventListener("click", this._disableJSClicked, false);
 
+    this.isReady = true;
     this.emit("ready");
     deferred.resolve(this);
     return deferred.promise;
   },
 
   setupToolsList: function() {
     let defaultToolsBox = this.panelDoc.getElementById("default-tools-box");
     let additionalToolsBox = this.panelDoc.getElementById("additional-tools-box");
--- a/browser/devtools/framework/toolbox.js
+++ b/browser/devtools/framework/toolbox.js
@@ -405,31 +405,42 @@ Toolbox.prototype = {
         }
       });
     }
 
     this._addKeysToWindow();
   },
 
   /**
-   * Load a tool with a given id.
+   * Ensure the tool with the given id is loaded.
    *
    * @param {string} id
    *        The id of the tool to load.
    */
   loadTool: function TBOX_loadTool(id) {
     let deferred = Promise.defer();
     let iframe = this.doc.getElementById("toolbox-panel-iframe-" + id);
 
     if (iframe) {
-      this.once(id + "-ready", () => { deferred.resolve() });
+      let panel = this._toolPanels.get(id);
+      if (panel) {
+        deferred.resolve(panel);
+      } else {
+        this.once(id + "-ready", (panel) => {
+          deferred.resolve(panel);
+        });
+      }
       return deferred.promise;
     }
 
     let definition = gDevTools.getToolDefinitionMap().get(id);
+    if (!definition) {
+      deferred.reject(new Error("no such tool id "+id));
+      return deferred.promise;
+    }
     iframe = this.doc.createElement("iframe");
     iframe.className = "toolbox-panel-iframe";
     iframe.id = "toolbox-panel-iframe-" + id;
     iframe.setAttribute("flex", 1);
     iframe.setAttribute("forceOwnRefreshDriver", "");
     iframe.tooltip = "aHTMLTooltip";
 
     let vbox = this.doc.getElementById("toolbox-panel-" + id);
@@ -454,18 +465,16 @@ Toolbox.prototype = {
 
   /**
    * Switch to the tool with the given id
    *
    * @param {string} id
    *        The id of the tool to switch to
    */
   selectTool: function TBOX_selectTool(id) {
-    let deferred = Promise.defer();
-
     let selected = this.doc.querySelector(".devtools-tab[selected]");
     if (selected) {
       selected.removeAttribute("selected");
     }
     let tab = this.doc.getElementById("toolbox-tab-" + id);
     tab.setAttribute("selected", "true");
 
     let prevToolId = this._currentToolId;
@@ -503,51 +512,25 @@ Toolbox.prototype = {
     }
     tabstrip.selectedItem = tab;
 
     // and select the right iframe
     let deck = this.doc.getElementById("toolbox-deck");
     deck.selectedIndex = index;
 
     this._currentToolId = id;
-
-    let resolveSelected = panel => {
-      this.emit("select", id);
-      this.emit(id + "-selected", panel);
-      deferred.resolve(panel);
-    };
-
-    let iframe = this.doc.getElementById("toolbox-panel-iframe-" + id);
-    if (!iframe) {
-      this.loadTool(id).then((panel) => {
-        this.emit("select", id);
-        this.emit(id + "-selected", panel);
-        deferred.resolve(panel);
-      });
-    } else {
-      let panel = this._toolPanels.get(id);
-      // only emit 'select' event if the iframe has been loaded
-      if (panel && (!panel.contentDocument ||
-                    panel.contentDocument.readyState == "complete")) {
-        resolveSelected(panel);
-      }
-      else if (panel) {
-        let boundLoad = function() {
-          panel.removeEventListener("DOMContentLoaded", boundLoad, true);
-          resolveSelected(panel);
-        };
-        panel.addEventListener("DOMContentLoaded", boundLoad, true);
-      }
-    }
-
     if (id != "options") {
       Services.prefs.setCharPref(this._prefs.LAST_TOOL, id);
     }
 
-    return deferred.promise;
+    return this.loadTool(id).then((panel) => {
+      this.emit("select", id);
+      this.emit(id + "-selected", panel);
+      return panel;
+    });
   },
 
   /**
    * Raise the toolbox host.
    */
   raise: function TBOX_raise() {
     this._host.raise();
   },