Bug 1405243 - Extract consoleAPICall helper function in common.js; r=bgrins. draft
authorNicolas Chevobbe <nchevobbe@mozilla.com>
Mon, 19 Mar 2018 18:09:41 +0100
changeset 769473 61fa671d0f6c31b68d9b94a388454d39446a12bf
parent 769468 beeec437f12592b2229c89a5ab9125cc66d1971e
push id103139
push userbmo:nchevobbe@mozilla.com
push dateMon, 19 Mar 2018 17:25:00 +0000
reviewersbgrins
bugs1405243
milestone61.0a1
Bug 1405243 - Extract consoleAPICall helper function in common.js; r=bgrins. The function was used through multiple tests so it makes sense to have it in common.js. We take this as an opportunity to unify the style of the tests, and to make closeDebugger return a Promise. MozReview-Commit-ID: 7y8dMNErYZp
devtools/shared/webconsole/test/common.js
devtools/shared/webconsole/test/test_console_assert.html
devtools/shared/webconsole/test/test_console_group_styling.html
devtools/shared/webconsole/test/test_console_timestamp.html
--- a/devtools/shared/webconsole/test/common.js
+++ b/devtools/shared/webconsole/test/common.js
@@ -3,17 +3,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/. */
 
 "use strict";
 
 /* exported ObjectClient, attachConsole, attachConsoleToTab, attachConsoleToWorker,
    closeDebugger, checkConsoleAPICalls, checkRawHeaders, runTests, nextTest, Ci, Cc,
-   withActiveServiceWorker, Services */
+   withActiveServiceWorker, Services, consoleAPICall */
 
 const {require} = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {});
 const {DebuggerServer} = require("devtools/server/main");
 const {DebuggerClient} = require("devtools/shared/client/debugger-client");
 const ObjectClient = require("devtools/shared/client/object-client");
 const Services = require("Services");
 
 function initCommon() {
@@ -124,19 +124,25 @@ var _attachConsole = async function(
   } else {
     state.actor = tab.consoleActor;
     state.dbgClient.attachConsole(tab.consoleActor, listeners,
                                    _onAttachConsole.bind(null, state));
   }
 };
 
 function closeDebugger(state, callback) {
-  state.dbgClient.close().then(callback);
+  const onClose = state.dbgClient.close();
+
   state.dbgClient = null;
   state.client = null;
+
+  if (typeof callback === "function") {
+    onClose.then(callback);
+  }
+  return onClose;
 }
 
 function checkConsoleAPICalls(consoleCalls, expectedConsoleCalls) {
   is(consoleCalls.length, expectedConsoleCalls.length,
     "received correct number of console calls");
   expectedConsoleCalls.forEach(function(message, index) {
     info("checking received console call #" + index);
     checkConsoleAPICall(consoleCalls[index], expectedConsoleCalls[index]);
@@ -259,8 +265,22 @@ function withActiveServiceWorker(win, ur
         if (sw.state === "activated") {
           sw.removeEventListener("statechange", stateHandler);
           resolve(swr);
         }
       });
     });
   });
 }
+
+/**
+ *
+ * @param {DebuggerClient} debuggerClient
+ * @param {Function} consoleCall: A function which calls the consoleAPI, e.g. :
+ *                         `() => top.console.log("test")`.
+ * @returns {Promise} A promise that will be resolved with the packet sent by the server
+ *                    in response to the consoleAPI call.
+ */
+function consoleAPICall(debuggerClient, consoleCall) {
+  const onConsoleAPICall = debuggerClient.addOneTimeListener("consoleAPICall");
+  consoleCall();
+  return onConsoleAPICall;
+}
--- a/devtools/shared/webconsole/test/test_console_assert.html
+++ b/devtools/shared/webconsole/test/test_console_assert.html
@@ -14,79 +14,84 @@ window.onload = async function () {
   SimpleTest.waitForExplicitFinish();
   let state;
   try {
     state = await new Promise(resolve =>
       attachConsole(["ConsoleAPI"], resolve)
     );
     const {dbgClient} = state;
 
-    const consoleAPICall = consoleCall => {
-      const onConsoleAPICall = dbgClient.addOneTimeListener("consoleAPICall");
-      consoleCall();
-      return onConsoleAPICall;
-    };
-
-    await testFalseAssert(consoleAPICall);
-    await testFalsyAssert(consoleAPICall);
-    await testUndefinedAssert(consoleAPICall);
-    await testNullAssert(consoleAPICall);
-    await testTrueAssert(consoleAPICall);
+    await testFalseAssert(dbgClient);
+    await testFalsyAssert(dbgClient);
+    await testUndefinedAssert(dbgClient);
+    await testNullAssert(dbgClient);
+    await testTrueAssert(dbgClient);
 
   } catch (e) {
     ok(false, `Error thrown: ${e.message}`);
   }
 
-  closeDebugger(state, () => SimpleTest.finish());
+  await closeDebugger(state);
+  SimpleTest.finish();
 };
 
-async function testFalseAssert(consoleAPICall) {
+async function testFalseAssert(debuggerClient) {
   info(`Testing console.assert(false)`);
-  const packet = await consoleAPICall(() =>
-    top.console.assert(false, "assertion is false"));
+  const packet = await consoleAPICall(
+    debuggerClient,
+    () => top.console.assert(false, "assertion is false")
+  );
 
   checkConsoleAPICall(packet.message, {
     arguments: ["assertion is false"]
   });
 }
 
-async function testFalsyAssert(consoleAPICall) {
+async function testFalsyAssert(debuggerClient) {
   info(`Testing console.assert(0")`);
-  const packet = await consoleAPICall(() =>
-    top.console.assert(0, "assertion is false"));
+  const packet = await consoleAPICall(
+    debuggerClient,
+    () => top.console.assert(0, "assertion is false")
+  );
 
   checkConsoleAPICall(packet.message, {
     arguments: ["assertion is false"]
   });
 }
 
-async function testUndefinedAssert(consoleAPICall) {
+async function testUndefinedAssert(debuggerClient) {
   info(`Testing console.assert(undefined)`);
-  const packet = await consoleAPICall(() =>
-    top.console.assert(undefined, "assertion is false"));
+  const packet = await consoleAPICall(
+    debuggerClient,
+    () => top.console.assert(undefined, "assertion is false")
+  );
 
   checkConsoleAPICall(packet.message, {
     arguments: ["assertion is false"]
   });
 }
 
-async function testNullAssert(consoleAPICall) {
+async function testNullAssert(debuggerClient) {
   info(`Testing console.assert(null)`);
-  const packet = await consoleAPICall(() =>
-    top.console.assert(null, "assertion is false"));
+  const packet = await consoleAPICall(
+    debuggerClient,
+    () => top.console.assert(null, "assertion is false")
+  );
 
   checkConsoleAPICall(packet.message, {
     arguments: ["assertion is false"]
   });
 }
 
-async function testTrueAssert(consoleAPICall) {
+async function testTrueAssert(debuggerClient) {
   info(`Testing console.assert(true)`);
-  const onConsoleApiCall = consoleAPICall(() =>
-    top.console.assert(true, "assertion is false"));
+  const onConsoleApiCall = consoleAPICall(
+    debuggerClient,
+    () => top.console.assert(true, "assertion is false")
+  );
 
   const TIMEOUT = Symbol();
   const onTimeout = new Promise(resolve => setTimeout(() => resolve(TIMEOUT), 1000));
 
   const res = await Promise.race([onConsoleApiCall, onTimeout]);
   is(res, TIMEOUT,
     "There was no consoleAPICall event in response to a truthy console.assert");
 }
--- a/devtools/shared/webconsole/test/test_console_group_styling.html
+++ b/devtools/shared/webconsole/test/test_console_group_styling.html
@@ -28,17 +28,18 @@ window.onload = async function () {
     await testMultipleCustomStyleGroup(debuggerClient);
     await testMultipleCustomStyleGroupCollapsed(debuggerClient);
     await testFormatterWithNoStyleGroup(debuggerClient);
     await testFormatterWithNoStyleGroupCollapsed(debuggerClient);
   } catch (e) {
     ok(false, `Error thrown: ${e.message}`);
   }
 
-  closeDebugger(state, () => SimpleTest.finish());
+  await closeDebugger(state);
+  SimpleTest.finish();
 };
 
 async function testSingleCustomStyleGroup(debuggerClient) {
   info("Testing console.group with a custom style");
   let packet = await consoleAPICall(
     debuggerClient,
     () => top.console.group("%cfoobar", "color:red")
   );
@@ -109,22 +110,16 @@ async function testFormatterWithNoStyleG
   );
 
   checkConsoleAPICall(packet.message, {
     arguments: ["%cfoobaz"],
     styles: []
   });
 }
 
-function consoleAPICall(debuggerClient, consoleCall) {
-  const onConsoleAPICall = debuggerClient.addOneTimeListener("consoleAPICall");
-  consoleCall();
-  return onConsoleAPICall;
-}
-
   </script>
 </head>
 <body>
   <p id="display"></p>
   <div id="content" style="display: none">
   </div>
   <pre id="test">
   </pre>
--- a/devtools/shared/webconsole/test/test_console_timestamp.html
+++ b/devtools/shared/webconsole/test/test_console_timestamp.html
@@ -37,23 +37,16 @@ window.onload = async function () {
   } catch (e) {
     ok(false, `Error thrown: ${e.message}`);
   }
 
   await closeDebugger(state);
   SimpleTest.finish();
 };
 
-
-function consoleAPICall(debuggerClient, consoleCall) {
-  const onConsoleAPICall = debuggerClient.addOneTimeListener("consoleAPICall");
-  consoleCall();
-  return onConsoleAPICall;
-}
-
   </script>
 </head>
 <body>
   <p id="display"></p>
   <div id="content" style="display: none">
   </div>
   <pre id="test">
   </pre>