Bug 1093730 - Add DevTools toolbox API for getPanelWhenReady. r=jryans
authorBrian Grinstead <bgrinstead@mozilla.com>
Mon, 08 Dec 2014 16:19:00 +0100
changeset 218886 210fcdc74cde38e9293a04583c6764e54fb2e565
parent 218832 b46dc702c59c020bd5e1d2ed238e726affa4f053
child 218887 3368df73756bfa3fc745d2ab3798486c924e8357
push id27948
push userryanvm@gmail.com
push dateTue, 09 Dec 2014 19:16:10 +0000
treeherdermozilla-central@d7c76fe69e9a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1093730
milestone37.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 1093730 - Add DevTools toolbox API for getPanelWhenReady. r=jryans
browser/devtools/framework/test/browser.ini
browser/devtools/framework/test/browser_toolbox_getpanelwhenready.js
browser/devtools/framework/toolbox.js
--- a/browser/devtools/framework/test/browser.ini
+++ b/browser/devtools/framework/test/browser.ini
@@ -14,16 +14,17 @@ skip-if = e10s # Bug 1070837 - devtools/
 [browser_dynamic_tool_enabling.js]
 [browser_keybindings.js]
 [browser_new_activation_workflow.js]
 [browser_target_events.js]
 [browser_target_remote.js]
 [browser_target_support.js]
 [browser_two_tabs.js]
 [browser_toolbox_dynamic_registration.js]
+[browser_toolbox_getpanelwhenready.js]
 [browser_toolbox_highlight.js]
 [browser_toolbox_hosts.js]
 [browser_toolbox_options.js]
 [browser_toolbox_options_devedition.js]
 [browser_toolbox_options_disable_buttons.js]
 [browser_toolbox_options_disable_cache.js]
 skip-if = e10s # Bug 1030318
 [browser_toolbox_options_disable_js.js]
new file mode 100644
--- /dev/null
+++ b/browser/devtools/framework/test/browser_toolbox_getpanelwhenready.js
@@ -0,0 +1,34 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Tests that getPanelWhenReady returns the correct panel in promise
+// resolutions regardless of whether it has opened first.
+
+let toolbox = null;
+
+const URL = "data:text/html;charset=utf8,test for getPanelWhenReady";
+
+let test = asyncTest(function*() {
+  let tab = yield addTab(URL);
+  let target = TargetFactory.forTab(tab);
+  toolbox = yield gDevTools.showToolbox(target);
+
+  let debuggerPanelPromise = toolbox.getPanelWhenReady("jsdebugger");
+  yield toolbox.selectTool("jsdebugger");
+  let debuggerPanel = yield debuggerPanelPromise;
+
+  is (debuggerPanel, toolbox.getPanel("jsdebugger"),
+      "The debugger panel from getPanelWhenReady before loading is the actual panel");
+
+  let debuggerPanel2 = yield toolbox.getPanelWhenReady("jsdebugger");
+  is (debuggerPanel2, toolbox.getPanel("jsdebugger"),
+      "The debugger panel from getPanelWhenReady after loading is the actual panel");
+
+  yield cleanup();
+});
+
+function* cleanup() {
+  yield toolbox.destroy();
+  gBrowser.removeCurrentTab();
+  toolbox = null;
+}
--- a/browser/devtools/framework/toolbox.js
+++ b/browser/devtools/framework/toolbox.js
@@ -170,16 +170,43 @@ Toolbox.prototype = {
   /**
    * Access the panel for a given tool
    */
   getPanel: function(id) {
     return this._toolPanels.get(id);
   },
 
   /**
+   * Get the panel instance for a given tool once it is ready.
+   * If the tool is already opened, the promise will resolve immediately,
+   * otherwise it will wait until the tool has been opened before resolving.
+   *
+   * Note that this does not open the tool, use selectTool if you'd
+   * like to select the tool right away.
+   *
+   * @param  {String} id
+   *         The id of the panel, for example "jsdebugger".
+   * @returns Promise
+   *          A promise that resolves once the panel is ready.
+   */
+  getPanelWhenReady: function(id) {
+    let deferred = promise.defer();
+    let panel = this.getPanel(id);
+    if (panel) {
+      deferred.resolve(panel);
+    } else {
+      this.on(id + "-ready", (e, panel) => {
+        deferred.resolve(panel);
+      });
+    }
+
+    return deferred.promise;
+  },
+
+  /**
    * This is a shortcut for getPanel(currentToolId) because it is much more
    * likely that we're going to want to get the panel that we've just made
    * visible
    */
   getCurrentPanel: function() {
     return this._toolPanels.get(this.currentToolId);
   },