Bug 825427 - Intermittent browser_responsiveruleview.js; r=miker
authorPatrick Brosset <pbrosset@mozilla.com>
Fri, 18 Apr 2014 10:11:30 +0200
changeset 197675 6d1315e1a0268c25b67263230fec947a63052e56
parent 197674 7af4241ce919491ad55db21cfc48b3294d05d5e2
child 197676 20ec0888f6329ff634cd0ae31816ca68b0b357ca
push idunknown
push userunknown
push dateunknown
reviewersmiker
bugs825427
milestone31.0a1
Bug 825427 - Intermittent browser_responsiveruleview.js; r=miker
browser/devtools/responsivedesign/test/browser_responsivecomputedview.js
browser/devtools/responsivedesign/test/browser_responsiveruleview.js
browser/devtools/responsivedesign/test/head.js
--- a/browser/devtools/responsivedesign/test/browser_responsivecomputedview.js
+++ b/browser/devtools/responsivedesign/test/browser_responsivecomputedview.js
@@ -10,17 +10,17 @@ function test() {
   waitForExplicitFinish();
 
   gBrowser.selectedTab = gBrowser.addTab();
   gBrowser.selectedBrowser.addEventListener("load", function onload() {
     gBrowser.selectedBrowser.removeEventListener("load", onload, true);
     waitForFocus(startTest, content);
   }, true);
 
-  content.location = "data:text/html,<html><style>" +
+  content.location = "data:text/html;charset=utf-8,<html><style>" +
     "div {" +
     "  width: 500px;" +
     "  height: 10px;" +
     "  background: purple;" +
     "} " +
     "@media screen and (max-width: 200px) {" +
     "  div { " +
     "    width: 100px;" +
@@ -48,34 +48,31 @@ function test() {
 
     instance.stack.setAttribute("notransition", "true");
     registerCleanupFunction(function() {
       instance.stack.removeAttribute("notransition");
     });
 
     instance.setSize(500, 500);
 
-    openView("computedview", onInspectorUIOpen);
+    openComputedView().then(onInspectorUIOpen);
   }
 
-  function onInspectorUIOpen(aInspector, aComputedView) {
-    inspector = aInspector;
+  function onInspectorUIOpen(args) {
+    inspector = args.inspector;
+    computedView = args.view;
     ok(inspector, "Got inspector instance");
 
     let div = content.document.getElementsByTagName("div")[0];
 
     inspector.selection.setNode(div);
     inspector.once("inspector-updated", testShrink);
-
   }
 
   function testShrink() {
-    computedView = inspector.sidebar.getWindowForTab("computedview").computedview.view;
-    ok(computedView, "We have access to the Computed View object");
-
     is(computedWidth(), "500px", "Should show 500px initially.");
 
     inspector.once("computed-view-refreshed", function onShrink() {
       is(computedWidth(), "100px", "div should be 100px after shrinking.");
       testGrow();
     });
 
     instance.setSize(100, 100);
--- a/browser/devtools/responsivedesign/test/browser_responsiveruleview.js
+++ b/browser/devtools/responsivedesign/test/browser_responsiveruleview.js
@@ -11,17 +11,17 @@ function test() {
   waitForExplicitFinish();
 
   gBrowser.selectedTab = gBrowser.addTab();
   gBrowser.selectedBrowser.addEventListener("load", function onload() {
     gBrowser.selectedBrowser.removeEventListener("load", onload, true);
     waitForFocus(startTest, content);
   }, true);
 
-  content.location = "data:text/html,<html><style>" +
+  content.location = "data:text/html;charset=utf-8,<html><style>" +
     "div {" +
     "  width: 500px;" +
     "  height: 10px;" +
     "  background: purple;" +
     "} " +
     "@media screen and (max-width: 200px) {" +
     "  div { " +
     "    width: 100px;" +
@@ -44,22 +44,22 @@ function test() {
 
     instance.stack.setAttribute("notransition", "true");
     registerCleanupFunction(function() {
       instance.stack.removeAttribute("notransition");
     });
 
     instance.setSize(500, 500);
 
-    openView("ruleview", onInspectorUIOpen);
+    openRuleView().then(onInspectorUIOpen);
   }
 
-  function onInspectorUIOpen(aInspector, aRuleView) {
-    inspector = aInspector;
-    ruleView = aRuleView;
+  function onInspectorUIOpen(args) {
+    inspector = args.inspector;
+    ruleView = args.view;
     ok(inspector, "Got inspector instance");
 
     let div = content.document.getElementsByTagName("div")[0];
     inspector.selection.setNode(div);
     inspector.once("inspector-updated", testShrink);
   }
 
   function testShrink() {
--- a/browser/devtools/responsivedesign/test/head.js
+++ b/browser/devtools/responsivedesign/test/head.js
@@ -10,32 +10,114 @@ let TargetFactory = devtools.TargetFacto
 let testDir = gTestPath.substr(0, gTestPath.lastIndexOf("/"));
 Services.scriptloader.loadSubScript(testDir + "../../../commandline/test/helpers.js", this);
 
 gDevTools.testing = true;
 SimpleTest.registerCleanupFunction(() => {
   gDevTools.testing = false;
 });
 
-function openInspector(callback)
-{
+/**
+ * Open the toolbox, with the inspector tool visible.
+ * @return a promise that resolves when the inspector is ready
+ */
+let openInspector = Task.async(function*() {
+  info("Opening the inspector");
   let target = TargetFactory.forTab(gBrowser.selectedTab);
-  gDevTools.showToolbox(target, "inspector").then(function(toolbox) {
-    callback(toolbox.getCurrentPanel());
-  });
+
+  let inspector, toolbox;
+
+  // Checking if the toolbox and the inspector are already loaded
+  // The inspector-updated event should only be waited for if the inspector
+  // isn't loaded yet
+  toolbox = gDevTools.getToolbox(target);
+  if (toolbox) {
+    inspector = toolbox.getPanel("inspector");
+    if (inspector) {
+      info("Toolbox and inspector already open");
+      return {
+        toolbox: toolbox,
+        inspector: inspector
+      };
+    }
+  }
+
+  info("Opening the toolbox");
+  toolbox = yield gDevTools.showToolbox(target, "inspector");
+  yield waitForToolboxFrameFocus(toolbox);
+  inspector = toolbox.getPanel("inspector");
+
+  info("Waiting for the inspector to update");
+  yield inspector.once("inspector-updated");
+
+  return {
+    toolbox: toolbox,
+    inspector: inspector
+  };
+});
+
+/**
+ * Wait for the toolbox frame to receive focus after it loads
+ * @param {Toolbox} toolbox
+ * @return a promise that resolves when focus has been received
+ */
+function waitForToolboxFrameFocus(toolbox) {
+  info("Making sure that the toolbox's frame is focused");
+  let def = promise.defer();
+  let win = toolbox.frame.contentWindow;
+  waitForFocus(def.resolve, win);
+  return def.promise;
 }
 
-function openView(name, callback)
-{
-  openInspector(inspector => {
-    function onReady() {
-      inspector.sidebar.select(name);
-      let { view } = inspector.sidebar.getWindowForTab(name)[name];
-      callback(inspector, view);
-    }
+/**
+ * Open the toolbox, with the inspector tool visible, and the sidebar that
+ * corresponds to the given id selected
+ * @return a promise that resolves when the inspector is ready and the sidebar
+ * view is visible and ready
+ */
+let openInspectorSideBar = Task.async(function*(id) {
+  let {toolbox, inspector} = yield openInspector();
+
+  if (!hasSideBarTab(inspector, id)) {
+    info("Waiting for the " + id + " sidebar to be ready");
+    yield inspector.sidebar.once(id + "-ready");
+  }
+
+  info("Selecting the " + id + " sidebar");
+  inspector.sidebar.select(id);
+
+  return {
+    toolbox: toolbox,
+    inspector: inspector,
+    view: inspector.sidebar.getWindowForTab(id)[id].view
+  };
+});
 
-    if (inspector.sidebar.getTab(name)) {
-      onReady();
-    } else {
-      inspector.sidebar.once(name + "-ready", onReady);
-    }
-  });
+/**
+ * Checks whether the inspector's sidebar corresponding to the given id already
+ * exists
+ * @param {InspectorPanel}
+ * @param {String}
+ * @return {Boolean}
+ */
+function hasSideBarTab(inspector, id) {
+  return !!inspector.sidebar.getWindowForTab(id);
 }
+
+/**
+ * Open the toolbox, with the inspector tool visible, and the computed-view
+ * sidebar tab selected.
+ * @return a promise that resolves when the inspector is ready and the computed
+ * view is visible and ready
+ */
+function openComputedView() {
+  return openInspectorSideBar("computedview");
+}
+
+/**
+ * Open the toolbox, with the inspector tool visible, and the rule-view
+ * sidebar tab selected.
+ * @return a promise that resolves when the inspector is ready and the rule
+ * view is visible and ready
+ */
+function openRuleView() {
+  return openInspectorSideBar("ruleview");
+}