Bug 1181837 - 1 - Minor shared-head.js cleanup and code duplication removal draft
authorPatrick Brosset <pbrosset@mozilla.com>
Fri, 08 Jan 2016 09:07:24 -0800
changeset 321234 24c283c9f058ac11ddf800cf20f2a8328effc556
parent 321233 7737b44d63eea024ded2751ae8d92cb72ac741f9
child 321235 c1d7ede7b3df630b230b574d51d6b47054cc5dab
child 321240 577fa2af9f44c1532ae2a084bce9424eca7f2e40
push id9354
push userpbrosset@mozilla.com
push dateWed, 13 Jan 2016 09:58:41 +0000
bugs1181837
milestone46.0a1
Bug 1181837 - 1 - Minor shared-head.js cleanup and code duplication removal
devtools/client/framework/test/shared-head.js
--- a/devtools/client/framework/test/shared-head.js
+++ b/devtools/client/framework/test/shared-head.js
@@ -1,13 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-// This shared-head.js file is used for multiple directories in devtools.
+"use strict";
+
+// This shared-head.js file is used for multiple mochitest test directories in
+// devtools.
+// It contains various common helper functions.
 
 var {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 function scopedCuImport(path) {
   const scope = {};
   Cu.import(path, scope);
   return scope;
 }
@@ -59,60 +63,58 @@ registerCleanupFunction(function* cleanu
   }
 });
 
 /**
  * Add a new test tab in the browser and load the given url.
  * @param {String} url The url to be loaded in the new tab
  * @return a promise that resolves to the tab object when the url is loaded
  */
-function addTab(url) {
+var addTab = Task.async(function*(url) {
   info("Adding a new tab with URL: '" + url + "'");
-  let def = promise.defer();
 
   let tab = gBrowser.selectedTab = gBrowser.addTab(url);
-  gBrowser.selectedBrowser.addEventListener("load", function onload() {
-    gBrowser.selectedBrowser.removeEventListener("load", onload, true);
-    info("URL '" + url + "' loading complete");
-    def.resolve(tab);
-  }, true);
+  yield once(gBrowser.selectedBrowser, "load", true);
 
-  return def.promise;
-}
+  info("Tab added and finished loading");
+
+  return tab;
+});
 
 /**
  * Remove the given tab.
  * @param {Object} tab The tab to be removed.
  * @return Promise<undefined> resolved when the tab is successfully removed.
  */
-function removeTab(tab) {
+var removeTab = Task.async(function*(tab) {
   info("Removing tab.");
-  return new Promise(resolve => {
-    let tabContainer = gBrowser.tabContainer;
-    tabContainer.addEventListener("TabClose", function onClose(aEvent) {
-      tabContainer.removeEventListener("TabClose", onClose, false);
-      info("Tab removed and finished closing.");
-      resolve();
-    }, false);
+
+  let onClose = once(gBrowser.tabContainer, "TabClose");
+  gBrowser.removeTab(tab);
+  yield onClose;
 
-    gBrowser.removeTab(tab);
-  });
-}
+  info("Tab removed and finished closing");
+});
 
+/**
+ * Simulate a key event from a <key> element.
+ * @param {DOMNode} key
+ */
 function synthesizeKeyFromKeyTag(key) {
   is(key && key.tagName, "key", "Successfully retrieved the <key> node");
 
   let modifiersAttr = key.getAttribute("modifiers");
 
   let name = null;
 
-  if (key.getAttribute("keycode"))
+  if (key.getAttribute("keycode")) {
     name = key.getAttribute("keycode");
-  else if (key.getAttribute("key"))
+  } else if (key.getAttribute("key")) {
     name = key.getAttribute("key");
+  }
 
   isnot(name, null, "Successfully retrieved keycode/key");
 
   let modifiers = {
     shiftKey: !!modifiersAttr.match("shift"),
     ctrlKey: !!modifiersAttr.match("control"),
     altKey: !!modifiersAttr.match("alt"),
     metaKey: !!modifiersAttr.match("meta"),
@@ -126,17 +128,17 @@ function synthesizeKeyFromKeyTag(key) {
 /**
  * Wait for eventName on target.
  * @param {Object} target An observable object that either supports on/off or
  * addEventListener/removeEventListener
  * @param {String} eventName
  * @param {Boolean} useCapture Optional, for addEventListener/removeEventListener
  * @return A promise that resolves when the event has been handled
  */
-function once(target, eventName, useCapture=false) {
+function once(target, eventName, useCapture = false) {
   info("Waiting for event: '" + eventName + "' on " + target + ".");
 
   let deferred = promise.defer();
 
   for (let [add, remove] of [
     ["addEventListener", "removeEventListener"],
     ["addListener", "removeListener"],
     ["on", "off"]
@@ -165,33 +167,41 @@ function once(target, eventName, useCapt
  *                 - "helper_attributes_test_runner.js"
  *                 - "../../../commandline/test/helpers.js"
  */
 function loadHelperScript(filePath) {
   let testDir = gTestPath.substr(0, gTestPath.lastIndexOf("/"));
   Services.scriptloader.loadSubScript(testDir + "/" + filePath, this);
 }
 
+/**
+ * Wait for a tick.
+ * @return {Promise}
+ */
 function waitForTick() {
   let deferred = promise.defer();
   executeSoon(deferred.resolve);
   return deferred.promise;
 }
 
-function loadToolbox (url) {
-  let { promise: p, resolve } = promise.defer();
-  gBrowser.selectedTab = gBrowser.addTab();
-  let target = TargetFactory.forTab(gBrowser.selectedTab);
+/**
+ * Add a new tab and open the toolbox in it.
+ * @param {String} url The URL for the tab to be opened.
+ * @return {Promise} Resolves when the tab has been added, loaded and the
+ * toolbox has been opened. Resolves to the toolbox.
+ */
+var loadToolbox = Task.async(function*(url) {
+  let tab = yield addTab(url);
+  let toolbox = yield gDevTools.showToolbox(TargetFactory.forTab(tab));
+  return toolbox;
+});
 
-  gBrowser.selectedBrowser.addEventListener("load", function onLoad(evt) {
-    gBrowser.selectedBrowser.removeEventListener(evt.type, onLoad, true);
-    gDevTools.showToolbox(target).then(resolve);
-  }, true);
-
-  content.location = url;
-  return p;
-}
-
-function unloadToolbox (toolbox) {
+/**
+ * Close a toolbox and the current tab.
+ * @param {Toolbox} toolbox The toolbox to close.
+ * @return {Promise} Resolves when the toolbox and tab have been destroyed and
+ * closed.
+ */
+function unloadToolbox(toolbox) {
   return toolbox.destroy().then(function() {
     gBrowser.removeCurrentTab();
   });
 }