Bug 1465635 - Convert browser_two_tabs.js to async. r=yulia
authorAlexandre Poirot <poirot.alex@gmail.com>
Sat, 02 Feb 2019 11:24:56 +0000
changeset 456546 2fe3a0ae216beb2ef4ebbb5e02019d41845c4f4a
parent 456545 f7b6f8b332f117054071e8e796b30a96370eabeb
child 456547 9c7fe2ba8434594aa5d49149e74ffba6d21303e2
push id35489
push userrmaries@mozilla.com
push dateSat, 02 Feb 2019 21:36:03 +0000
treeherdermozilla-central@63348118ef1d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersyulia
bugs1465635
milestone67.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 1465635 - Convert browser_two_tabs.js to async. r=yulia Differential Revision: https://phabricator.services.mozilla.com/D15825
devtools/client/framework/test/browser_two_tabs.js
--- a/devtools/client/framework/test/browser_two_tabs.js
+++ b/devtools/client/framework/test/browser_two_tabs.js
@@ -8,129 +8,90 @@
  */
 
 var { DebuggerServer } = require("devtools/server/main");
 var { DebuggerClient } = require("devtools/shared/client/debugger-client");
 
 const TAB_URL_1 = "data:text/html;charset=utf-8,foo";
 const TAB_URL_2 = "data:text/html;charset=utf-8,bar";
 
-var gClient;
-var gTab1, gTab2;
-var gTargetFront1, gTargetFront2;
-
-function test() {
-  waitForExplicitFinish();
-
+add_task(async () => {
   DebuggerServer.init();
   DebuggerServer.registerAllActors();
 
-  openTabs();
-}
+  const tab1 = await addTab(TAB_URL_1);
+  const tab2 = await addTab(TAB_URL_2);
+
+  // Connect to debugger server to fetch the two target actors for each tab
+  const client = new DebuggerClient(DebuggerServer.connectPipe());
+  await client.connect();
 
-function openTabs() {
-  // Open two tabs, select the second
-  addTab(TAB_URL_1).then(tab1 => {
-    gTab1 = tab1;
-    addTab(TAB_URL_2).then(tab2 => {
-      gTab2 = tab2;
+  const tabs = await client.mainRoot.listTabs();
+  const targetFront1 = tabs.find(a => a.url === TAB_URL_1);
+  const targetFront2 = tabs.find(a => a.url === TAB_URL_2);
+
+  await checkGetTab(client, tab1, tab2, targetFront1, targetFront2);
+  await checkGetTabFailures(client);
+  await checkSelectedTargetActor(client, targetFront2);
+
+  await removeTab(tab2);
+  await checkFirstTargetActor(client, targetFront1);
 
-      connect();
-    });
-  });
-}
+  await removeTab(tab1);
+  await client.close();
+});
 
-function connect() {
-  // Connect to debugger server to fetch the two target actors for each tab
-  gClient = new DebuggerClient(DebuggerServer.connectPipe());
-  gClient.connect()
-    .then(() => gClient.mainRoot.listTabs())
-    .then(tabs => {
-      // Fetch the target actors for each tab
-      gTargetFront1 = tabs.find(a => a.url === TAB_URL_1);
-      gTargetFront2 = tabs.find(a => a.url === TAB_URL_2);
-
-      checkGetTab();
-    });
+async function checkGetTab(client, tab1, tab2, targetFront1, targetFront2) {
+  let front = await client.mainRoot.getTab({tab: tab1});
+  is(targetFront1, front,
+     "getTab returns the same target form for first tab");
+  const filter = {};
+  // Filter either by tabId or outerWindowID,
+  // if we are running tests OOP or not.
+  if (tab1.linkedBrowser.frameLoader.tabParent) {
+    filter.tabId = tab1.linkedBrowser.frameLoader.tabParent.tabId;
+  } else {
+    const windowUtils = tab1.linkedBrowser.contentWindow.windowUtils;
+    filter.outerWindowID = windowUtils.outerWindowID;
+  }
+  front = await client.mainRoot.getTab(filter);
+  is(targetFront1, front,
+     "getTab returns the same target form when filtering by tabId/outerWindowID");
+  front = await client.mainRoot.getTab({tab: tab2});
+  is(targetFront2, front,
+     "getTab returns the same target form for second tab");
 }
 
-function checkGetTab() {
-  gClient.mainRoot.getTab({tab: gTab1})
-         .then(front => {
-           is(gTargetFront1, front,
-              "getTab returns the same target form for first tab");
-         })
-         .then(() => {
-           const filter = {};
-           // Filter either by tabId or outerWindowID,
-           // if we are running tests OOP or not.
-           if (gTab1.linkedBrowser.frameLoader.tabParent) {
-             filter.tabId = gTab1.linkedBrowser.frameLoader.tabParent.tabId;
-           } else {
-             const windowUtils = gTab1.linkedBrowser.contentWindow.windowUtils;
-             filter.outerWindowID = windowUtils.outerWindowID;
-           }
-           return gClient.mainRoot.getTab(filter);
-         })
-         .then(front => {
-           is(gTargetFront1, front,
-              "getTab returns the same target form when filtering by tabId/outerWindowID");
-         })
-         .then(() => gClient.mainRoot.getTab({tab: gTab2}))
-         .then(front => {
-           is(gTargetFront2, front,
-              "getTab returns the same target form for second tab");
-         })
-         .then(checkGetTabFailures);
+async function checkGetTabFailures(client) {
+  try {
+    await client.mainRoot.getTab({ tabId: -999 });
+    ok(false, "getTab unexpectedly succeed with a wrong tabId");
+  } catch (error) {
+    is(error, "Protocol error (noTab): Unable to find tab with tabId '-999'");
+  }
+
+  try {
+    await client.mainRoot.getTab({ outerWindowID: -999 });
+    ok(false, "getTab unexpectedly succeed with a wrong outerWindowID");
+  } catch (error) {
+    is(error, "Protocol error (noTab): Unable to find tab with outerWindowID '-999'");
+  }
 }
 
-function checkGetTabFailures() {
-  gClient.mainRoot.getTab({ tabId: -999 })
-    .then(
-      response => ok(false, "getTab unexpectedly succeed with a wrong tabId"),
-      response => {
-        is(response, "Protocol error (noTab): Unable to find tab with tabId '-999'");
-      }
-    )
-    .then(() => gClient.mainRoot.getTab({ outerWindowID: -999 }))
-    .then(
-      response => ok(false, "getTab unexpectedly succeed with a wrong outerWindowID"),
-      response => {
-        is(response, "Protocol error (noTab): Unable to find tab with outerWindowID '-999'");
-      }
-    )
-    .then(checkSelectedTargetActor);
+async function checkSelectedTargetActor(client, targetFront2) {
+  // Send a naive request to the second target actor to check if it works
+  const response = await client.request({
+    to: targetFront2.targetForm.consoleActor,
+    type: "startListeners",
+    listeners: [],
+  });
+  ok("startedListeners" in response, "Actor from the selected tab should respond to the request.");
 }
 
-function checkSelectedTargetActor() {
-  // Send a naive request to the second target actor to check if it works
-  gClient.request({ to: gTargetFront2.targetForm.consoleActor, type: "startListeners", listeners: [] }, aResponse => {
-    ok("startedListeners" in aResponse, "Actor from the selected tab should respond to the request.");
-
-    closeSecondTab();
+async function checkFirstTargetActor(client, targetFront1) {
+  // then send a request to the first target actor to check if it still works
+  const response = await client.request({
+    to: targetFront1.targetForm.consoleActor,
+    type: "startListeners",
+    listeners: [],
   });
-}
-
-function closeSecondTab() {
-  // Close the second tab, currently selected
-  const container = gBrowser.tabContainer;
-  container.addEventListener("TabClose", function() {
-    checkFirstTargetActor();
-  }, {once: true});
-  gBrowser.removeTab(gTab2);
+  ok("startedListeners" in response, "Actor from the first tab should still respond.");
 }
-
-function checkFirstTargetActor() {
-  // then send a request to the first target actor to check if it still works
-  gClient.request({ to: gTargetFront1.targetForm.consoleActor, type: "startListeners", listeners: [] }, aResponse => {
-    ok("startedListeners" in aResponse, "Actor from the first tab should still respond.");
-
-    cleanup();
-  });
-}
-
-function cleanup() {
-  const container = gBrowser.tabContainer;
-  container.addEventListener("TabClose", function() {
-    gClient.close().then(finish);
-  }, {once: true});
-  gBrowser.removeTab(gTab1);
-}