Bug 1290966 - Remove callback function argument from sync scripts; r=automatedtester a=testonly
authorAndreas Tolfsen <ato@mozilla.com>
Thu, 04 Aug 2016 18:20:18 +0100
changeset 333137 a89e3dd6ead88552072386a87660bc91184c4eee
parent 333136 86446c9b103bff79ac2cccd2d90b401deba4bf66
child 333138 4f4995c32e45184d57c6c949f71edf29fa6654a7
push id10007
push userkwierso@gmail.com
push dateMon, 12 Sep 2016 20:39:09 +0000
treeherdermozilla-aurora@cdf6988ace78 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersautomatedtester, testonly
bugs1290966
milestone50.0a2
Bug 1290966 - Remove callback function argument from sync scripts; r=automatedtester a=testonly CLOSED TREE MozReview-Commit-ID: CaDSYA5PNFp
testing/marionette/evaluate.js
testing/marionette/harness/marionette/tests/unit/test_execute_script.py
--- a/testing/marionette/evaluate.js
+++ b/testing/marionette/evaluate.js
@@ -97,31 +97,38 @@ this.evaluate = {};
  *   If an Error was thrown whilst evaluating the script.
  * @throws ScriptTimeoutError
  *   If the script was interrupted due to script timeout.
  */
 evaluate.sandbox = function(sb, script, args = [], opts = {}) {
   let timeoutId, timeoutHandler, unloadHandler;
 
   let promise = new Promise((resolve, reject) => {
+    let src = "";
     sb[COMPLETE] = resolve;
     timeoutHandler = () => reject(new ScriptTimeoutError("Timed out"));
     unloadHandler = () => reject(
         new JavaScriptError("Document was unloaded during execution"));
 
     // wrap in function
     if (!opts.directInject) {
-      sb[CALLBACK] = sb[COMPLETE];
+      if (opts.async) {
+        sb[CALLBACK] = sb[COMPLETE];
+      }
       sb[ARGUMENTS] = Cu.cloneInto(args, sb, {wrapReflectors: true});
 
       // callback function made private
       // so that introspection is possible
       // on the arguments object
-      script = `${ARGUMENTS}.push(rv => ${CALLBACK}(rv));` +
-          `(function() { ${script} }).apply(null, ${ARGUMENTS})`;
+      if (opts.async) {
+        sb[CALLBACK] = sb[COMPLETE];
+        src += `${ARGUMENTS}.push(rv => ${CALLBACK}(rv));`;
+      }
+
+      src += `(function() { ${script} }).apply(null, ${ARGUMENTS})`;
 
       // marionetteScriptFinished is not WebDriver conformant,
       // hence it is only exposed to immutable sandboxes
       if (opts.sandboxName) {
         sb[MARIONETTE_SCRIPT_FINISHED] = sb[CALLBACK];
       }
     }
 
@@ -139,17 +146,17 @@ evaluate.sandbox = function(sb, script, 
     // timeout and unload handlers
     timeoutId = setTimeout(
         timeoutHandler, opts.timeout || DEFAULT_TIMEOUT);
     sb.window.addEventListener("unload", unloadHandler);
 
     let res;
     try {
       res = Cu.evalInSandbox(
-          script, sb, "1.8", opts.filename || "dummy file", 0);
+          src, sb, "1.8", opts.filename || "dummy file", 0);
     } catch (e) {
       let err = new JavaScriptError(
           e,
           "execute_script",
           opts.filename,
           opts.line,
           script);
       reject(err);
--- a/testing/marionette/harness/marionette/tests/unit/test_execute_script.py
+++ b/testing/marionette/harness/marionette/tests/unit/test_execute_script.py
@@ -228,16 +228,20 @@ class TestExecuteContent(MarionetteTestC
         for property in globals:
             exists = send("return typeof %s != 'undefined'" % property)
             self.assertTrue(exists, "property %s is undefined" % property)
         # TODO(ato): For some reason this fails, probably Sandbox bug?
         # self.assertTrue(send("return typeof Components == 'undefined'"))
         self.assertTrue(
             send("return typeof window.wrappedJSObject == 'undefined'"))
 
+    def test_no_callback(self):
+        self.assertTrue(self.marionette.execute_script(
+            "return typeof arguments[0] == 'undefined'"))
+
 
 class TestExecuteChrome(TestExecuteContent):
     def setUp(self):
         TestExecuteContent.setUp(self)
         self.win = self.marionette.current_window_handle
         self.marionette.set_context("chrome")
         self.marionette.execute_script(
             "window.open('chrome://marionette/content/test.xul', 'xul', 'chrome')")