Bug 1274106 - Ensure opening tabs in background from about:debugging test. r=jdescottes draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Wed, 14 Sep 2016 07:41:08 -0700
changeset 413927 b1a934ae91d15718ec9e9d2b9893263193fd48b1
parent 413770 1b65a62685ca092cf6ce799e7cd39a2058bd9065
child 531340 86395dbf0e0fcf6f7cb0654f2c3476ba9bb1dec1
push id29555
push userbmo:poirot.alex@gmail.com
push dateThu, 15 Sep 2016 08:13:45 +0000
reviewersjdescottes
bugs1274106
milestone51.0a1
Bug 1274106 - Ensure opening tabs in background from about:debugging test. r=jdescottes MozReview-Commit-ID: kSeAw9Xz1A
devtools/client/aboutdebugging/test/browser_service_workers_not_compatible.js
devtools/client/aboutdebugging/test/browser_tabs.js
devtools/client/aboutdebugging/test/head.js
devtools/client/framework/test/shared-head.js
--- a/devtools/client/aboutdebugging/test/browser_service_workers_not_compatible.js
+++ b/devtools/client/aboutdebugging/test/browser_service_workers_not_compatible.js
@@ -50,11 +50,11 @@ add_task(function* () {
   let win = OpenBrowserWindow({private: true});
   yield waitForDelayedStartupFinished(win);
 
   let { tab, document } = yield openAboutDebugging("workers", win);
   // Check that the warning img appears in the UI
   let img = document.querySelector(imgClass);
   ok(img, "warning message is rendered");
 
-  yield closeAboutDebugging(tab, win);
+  yield closeAboutDebugging(tab);
   win.close();
 });
--- a/devtools/client/aboutdebugging/test/browser_tabs.js
+++ b/devtools/client/aboutdebugging/test/browser_tabs.js
@@ -16,17 +16,17 @@ add_task(function* () {
   // Refresh tabsElement to get the .target-list element
   tabsElement = getTabList(document);
 
   let names = [...tabsElement.querySelectorAll(".target-name")];
   let initialTabCount = names.length;
 
   // Open a new tab in background and wait for its addition in the UI
   let onNewTab = waitForMutation(tabsElement, { childList: true });
-  let newTab = yield addTab(TAB_URL, null, true);
+  let newTab = yield addTab(TAB_URL, { background: true });
   yield onNewTab;
 
   // Check that the new tab appears in the UI, but with an empty name
   let newNames = [...tabsElement.querySelectorAll(".target-name")];
   newNames = newNames.filter(node => !names.includes(node));
   is(newNames.length, 1, "A new tab appeared in the list");
   let newTabTarget = newNames[0];
 
--- a/devtools/client/aboutdebugging/test/head.js
+++ b/devtools/client/aboutdebugging/test/head.js
@@ -26,17 +26,17 @@ registerCleanupFunction(() => {
 
 function* openAboutDebugging(page, win) {
   info("opening about:debugging");
   let url = "about:debugging";
   if (page) {
     url += "#" + page;
   }
 
-  let tab = yield addTab(url, win);
+  let tab = yield addTab(url, { window: win });
   let browser = tab.linkedBrowser;
   let document = browser.contentDocument;
 
   if (!document.querySelector(".app")) {
     yield waitForMutation(document.body, { childList: true });
   }
 
   return { tab, document };
@@ -58,59 +58,19 @@ function changeAboutDebuggingHash(docume
 
 function openPanel(document, panelId) {
   info(`Opening ${panelId} panel`);
   document.querySelector(`[aria-controls="${panelId}"]`).click();
   return waitForMutation(
     document.querySelector(".main-content"), {childList: true});
 }
 
-function closeAboutDebugging(tab, win) {
+function closeAboutDebugging(tab) {
   info("Closing about:debugging");
-  return removeTab(tab, win);
-}
-
-function addTab(url, win, backgroundTab = false) {
-  info("Adding tab: " + url);
-
-  return new Promise(done => {
-    let targetWindow = win || window;
-    let targetBrowser = targetWindow.gBrowser;
-
-    targetWindow.focus();
-    let tab = targetBrowser.addTab(url);
-    if (!backgroundTab) {
-      targetBrowser.selectedTab = tab;
-    }
-    let linkedBrowser = tab.linkedBrowser;
-
-    BrowserTestUtils.browserLoaded(linkedBrowser)
-      .then(function () {
-        info("Tab added and finished loading: " + url);
-        done(tab);
-      });
-  });
-}
-
-function removeTab(tab, win) {
-  info("Removing tab.");
-
-  return new Promise(done => {
-    let targetWindow = win || window;
-    let targetBrowser = targetWindow.gBrowser;
-    let tabContainer = targetBrowser.tabContainer;
-
-    tabContainer.addEventListener("TabClose", function onClose() {
-      tabContainer.removeEventListener("TabClose", onClose, false);
-      info("Tab removed and finished closing.");
-      done();
-    }, false);
-
-    targetBrowser.removeTab(tab);
-  });
+  return removeTab(tab);
 }
 
 function getSupportsFile(path) {
   let cr = Cc["@mozilla.org/chrome/chrome-registry;1"]
     .getService(Ci.nsIChromeRegistry);
   let uri = Services.io.newURI(CHROME_URL_ROOT + path, null, null);
   let fileurl = cr.convertChromeURL(uri);
   return fileurl.QueryInterface(Ci.nsIFileURL);
--- a/devtools/client/framework/test/shared-head.js
+++ b/devtools/client/framework/test/shared-head.js
@@ -102,37 +102,47 @@ registerCleanupFunction(function* cleanu
   while (gBrowser.tabs.length > 1) {
     yield closeTabAndToolbox(gBrowser.selectedTab);
   }
 });
 
 /**
  * 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
+ * @param {Object} options Object with various optional fields:
+ *   - {Boolean} background If true, open the tab in background
+ *   - {ChromeWindow} window Firefox top level window we should use to open the tab
  * @return a promise that resolves to the tab object when the url is loaded
  */
-var addTab = Task.async(function* (url) {
+var addTab = Task.async(function* (url, options = { background: false, window: window }) {
   info("Adding a new tab with URL: " + url);
 
-  let tab = gBrowser.selectedTab = gBrowser.addTab(url);
-  yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
+  let { background } = options;
+  let { gBrowser } = options.window ? options.window : window;
+
+  let tab = gBrowser.addTab(url);
+  if (!background) {
+    gBrowser.selectedTab = tab;
+  }
+  yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);
 
   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.
  */
 var removeTab = Task.async(function* (tab) {
   info("Removing tab.");
 
+  let { gBrowser } = tab.ownerDocument.defaultView;
   let onClose = once(gBrowser.tabContainer, "TabClose");
   gBrowser.removeTab(tab);
   yield onClose;
 
   info("Tab removed and finished closing");
 });
 
 /**