Bug 1245383 - Use ContentTask.spawn to access the content window in 2 scratchpad tests; r=ochameau draft
authorPatrick Brosset <pbrosset@mozilla.com>
Fri, 04 Mar 2016 15:06:37 +0100
changeset 336943 6eb8ed5ff1a98c6dfb6d8a4fa387e09489cc0b45
parent 336929 34c795c24d3178e5492e3f0989c13149e5c09e41
child 515527 00aeb2e5893df0e270c0ade2e008b7fa64911d77
push id12214
push userpbrosset@mozilla.com
push dateFri, 04 Mar 2016 14:09:28 +0000
reviewersochameau
bugs1245383
milestone47.0a1
Bug 1245383 - Use ContentTask.spawn to access the content window in 2 scratchpad tests; r=ochameau browser_scratchpad_contexts.js and browser_scratchpad_execute_print.js used to access javascript properties on the content window object by using 'content.' directly. Now these tests use 'ContentTask.spawn' instead, which required the runAsyncCallbackTests helper to be changed so it would use a Task and yield on the promises returned by prepare and then. MozReview-Commit-ID: Cgw2mRxPrP4
devtools/client/scratchpad/test/browser_scratchpad_contexts.js
devtools/client/scratchpad/test/browser_scratchpad_execute_print.js
devtools/client/scratchpad/test/head.js
--- a/devtools/client/scratchpad/test/browser_scratchpad_contexts.js
+++ b/devtools/client/scratchpad/test/browser_scratchpad_contexts.js
@@ -1,67 +1,66 @@
 /* vim: set ts=2 et sw=2 tw=80: */
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
 
-function test()
-{
+function test() {
   waitForExplicitFinish();
 
   gBrowser.selectedTab = gBrowser.addTab();
   gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
     gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
     openScratchpad(runTests);
   }, true);
 
   content.location = "data:text/html,test context switch in Scratchpad";
 }
 
-
-function runTests()
-{
+function runTests() {
   let sp = gScratchpadWindow.Scratchpad;
   let contentMenu = gScratchpadWindow.document.getElementById("sp-menu-content");
   let chromeMenu = gScratchpadWindow.document.getElementById("sp-menu-browser");
   let notificationBox = sp.notificationBox;
 
   ok(contentMenu, "found #sp-menu-content");
   ok(chromeMenu, "found #sp-menu-browser");
   ok(notificationBox, "found Scratchpad.notificationBox");
 
   let tests = [{
     method: "run",
-    prepare: function() {
+    prepare: function*() {
       sp.setContentContext();
 
       is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT,
          "executionContext is content");
 
       is(contentMenu.getAttribute("checked"), "true",
          "content menuitem is checked");
 
       isnot(chromeMenu.getAttribute("checked"), "true",
          "chrome menuitem is not checked");
 
       ok(!notificationBox.currentNotification,
          "there is no notification in content context");
 
       sp.editor.setText("window.foobarBug636725 = 'aloha';");
 
-      ok(!content.wrappedJSObject.foobarBug636725,
-         "no content.foobarBug636725");
+      let pageResult = yield inContent(function*() {
+        return content.wrappedJSObject.foobarBug636725;
+      });
+      ok(!pageResult, "no content.foobarBug636725");
     },
-    then: function() {
+    then: function*() {
       is(content.wrappedJSObject.foobarBug636725, "aloha",
          "content.foobarBug636725 has been set");
     }
-  },
-  {
+  }, {
     method: "run",
-    prepare: function() {
+    prepare: function*() {
       sp.setBrowserContext();
 
       is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_BROWSER,
          "executionContext is chrome");
 
       is(chromeMenu.getAttribute("checked"), "true",
          "chrome menuitem is checked");
 
@@ -72,80 +71,75 @@ function runTests()
          "there is a notification in browser context");
 
       let [ from, to ] = sp.editor.getPosition(31, 32);
       sp.editor.replaceText("2'", from, to);
 
       is(sp.getText(), "window.foobarBug636725 = 'aloha2';",
          "setText() worked");
     },
-    then: function() {
+    then: function*() {
       is(window.foobarBug636725, "aloha2",
          "window.foobarBug636725 has been set");
 
       delete window.foobarBug636725;
       ok(!window.foobarBug636725, "no window.foobarBug636725");
     }
-  },
-  {
+  }, {
     method: "run",
-    prepare: function() {
+    prepare: function*() {
       sp.editor.replaceText("gBrowser", sp.editor.getPosition(7));
 
       is(sp.getText(), "window.gBrowser",
          "setText() worked with no end for the replace range");
     },
-    then: function([, , result]) {
+    then: function*([, , result]) {
       is(result.class, "XULElement",
          "chrome context has access to chrome objects");
     }
-  },
-  {
+  }, {
     method: "run",
-    prepare: function() {
+    prepare: function*() {
       // Check that the sandbox is cached.
       sp.editor.setText("typeof foobarBug636725cache;");
     },
-    then: function([, , result]) {
+    then: function*([, , result]) {
       is(result, "undefined", "global variable does not exist");
     }
-  },
-  {
+  }, {
     method: "run",
-    prepare: function() {
+    prepare: function*() {
       sp.editor.setText("window.foobarBug636725cache = 'foo';" +
                  "typeof foobarBug636725cache;");
     },
-    then: function([, , result]) {
+    then: function*([, , result]) {
       is(result, "string",
          "global variable exists across two different executions");
     }
-  },
-  {
+  }, {
     method: "run",
-    prepare: function() {
+    prepare: function*() {
       sp.editor.setText("window.foobarBug636725cache2 = 'foo';" +
                  "typeof foobarBug636725cache2;");
     },
-    then: function([, , result]) {
+    then: function*([, , result]) {
       is(result, "string",
          "global variable exists across two different executions");
     }
-  },
-  {
+  }, {
     method: "run",
-    prepare: function() {
+    prepare: function*() {
       sp.setContentContext();
 
       is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT,
          "executionContext is content");
 
       sp.editor.setText("typeof foobarBug636725cache2;");
     },
-    then: function([, , result]) {
+    then: function*([, , result]) {
       is(result, "undefined",
          "global variable no longer exists after changing the context");
     }
   }];
 
   runAsyncCallbackTests(sp, tests).then(() => {
     sp.setBrowserContext();
     sp.editor.setText("delete foobarBug636725cache;" +
--- a/devtools/client/scratchpad/test/browser_scratchpad_execute_print.js
+++ b/devtools/client/scratchpad/test/browser_scratchpad_execute_print.js
@@ -1,89 +1,95 @@
 /* vim: set ts=2 et sw=2 tw=80: */
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
 
-function test()
-{
+function test() {
   waitForExplicitFinish();
 
   gBrowser.selectedTab = gBrowser.addTab();
   gBrowser.selectedBrowser.addEventListener("load", function onTabLoad() {
     gBrowser.selectedBrowser.removeEventListener("load", onTabLoad, true);
     openScratchpad(runTests);
   }, true);
 
   content.location = "data:text/html,<p>test run() and display() in Scratchpad";
 }
 
-
-function runTests()
-{
+function runTests() {
   let sp = gScratchpadWindow.Scratchpad;
   let tests = [{
     method: "run",
-    prepare: function() {
-      content.wrappedJSObject.foobarBug636725 = 1;
+    prepare: function*() {
+      yield inContent(function*() {
+        content.wrappedJSObject.foobarBug636725 = 1;
+      });
       sp.editor.setText("++window.foobarBug636725");
     },
-    then: function([code, , result]) {
+    then: function*([code, , result]) {
       is(code, sp.getText(), "code is correct");
-      is(result, content.wrappedJSObject.foobarBug636725,
+
+      let pageResult = yield inContent(function*() {
+        return content.wrappedJSObject.foobarBug636725;
+      });
+      is(result, pageResult,
          "result is correct");
 
       is(sp.getText(), "++window.foobarBug636725",
          "run() does not change the editor content");
 
-      is(content.wrappedJSObject.foobarBug636725, 2,
-         "run() updated window.foobarBug636725");
+      is(pageResult, 2, "run() updated window.foobarBug636725");
     }
-  },
-  {
+  }, {
     method: "display",
-    prepare: function() {},
-    then: function() {
-      is(content.wrappedJSObject.foobarBug636725, 3,
-         "display() updated window.foobarBug636725");
+    prepare: function*() {},
+    then: function*() {
+      let pageResult = yield inContent(function*() {
+        return content.wrappedJSObject.foobarBug636725;
+      });
+      is(pageResult, 3, "display() updated window.foobarBug636725");
 
       is(sp.getText(), "++window.foobarBug636725\n/*\n3\n*/",
          "display() shows evaluation result in the textbox");
 
       is(sp.editor.getSelection(), "\n/*\n3\n*/", "getSelection is correct");
     }
-  },
-  {
+  }, {
     method: "run",
-    prepare: function() {
+    prepare: function*() {
       sp.editor.setText("window.foobarBug636725 = 'a';\n" +
         "window.foobarBug636725 = 'b';");
       sp.editor.setSelection({ line: 0, ch: 0 }, { line: 0, ch: 29 });
     },
-    then: function([code, , result]) {
+    then: function*([code, , result]) {
       is(code, "window.foobarBug636725 = 'a';", "code is correct");
       is(result, "a", "result is correct");
 
       is(sp.getText(), "window.foobarBug636725 = 'a';\n" +
                        "window.foobarBug636725 = 'b';",
          "run() does not change the textbox value");
 
-      is(content.wrappedJSObject.foobarBug636725, "a",
-         "run() worked for the selected range");
+      let pageResult = yield inContent(function*() {
+        return content.wrappedJSObject.foobarBug636725;
+      });
+      is(pageResult, "a", "run() worked for the selected range");
     }
-  },
-  {
+  }, {
     method: "display",
-    prepare: function() {
+    prepare: function*() {
       sp.editor.setText("window.foobarBug636725 = 'c';\n" +
                  "window.foobarBug636725 = 'b';");
       sp.editor.setSelection({ line: 0, ch: 0 }, { line: 0, ch: 22 });
     },
-    then: function() {
-      is(content.wrappedJSObject.foobarBug636725, "a",
-         "display() worked for the selected range");
+    then: function*() {
+      let pageResult = yield inContent(function*() {
+        return content.wrappedJSObject.foobarBug636725;
+      });
+      is(pageResult, "a", "display() worked for the selected range");
 
       is(sp.getText(), "window.foobarBug636725" +
                        "\n/*\na\n*/" +
                        " = 'c';\n" +
                        "window.foobarBug636725 = 'b';",
          "display() shows evaluation result in the textbox");
 
       is(sp.editor.getSelection(), "\n/*\na\n*/", "getSelection is correct");
--- a/devtools/client/scratchpad/test/head.js
+++ b/devtools/client/scratchpad/test/head.js
@@ -186,31 +186,29 @@ function runAsyncTests(aScratchpad, aTes
  *          Scratchpad method to use, one of "run", "display", or "inspect".
  *        - prepare
  *          The callback to run just prior to executing the scratchpad method.
  *        - then
  *          The callback to run when the scratchpad execution promise resolves.
  * @return Promise
  *         The promise that will be resolved when all tests are finished.
  */
-function runAsyncCallbackTests(aScratchpad, aTests)
-{
-  let deferred = promise.defer();
+var runAsyncCallbackTests = Task.async(function*(aScratchpad, aTests) {
+  for (let {prepare, method, then} of aTests) {
+    yield prepare();
+    let res = yield aScratchpad[method]();
+    yield then(res);
+  }
+});
 
-  (function runTest() {
-    if (aTests.length) {
-      let test = aTests.shift();
-      test.prepare();
-      aScratchpad[test.method]().then(test.then.bind(test)).then(runTest);
-    } else {
-      deferred.resolve();
-    }
-  })();
-
-  return deferred.promise;
+/**
+ * A simple wrapper for ContentTask.spawn for more compact code.
+ */
+function inContent(generator, browser = gBrowser.selectedBrowser, args = {}) {
+  return ContentTask.spawn(browser, args, generator);
 }
 
 function cleanup()
 {
   if (gScratchpadWindow) {
     gScratchpadWindow.close();
     gScratchpadWindow = null;
   }