Bug 1157255: Use typeof to determine MarionetteObj exports
authorAndreas Tolfsen <ato@mozilla.com>
Tue, 28 Apr 2015 19:16:46 +0100
changeset 273151 84a655fda7a21ae790c5acf5842c9b8c2c497512
parent 273150 d3d970920ea930e5268e18dc1b9a76fad5bcaff8
child 273152 786dc785bae6c3888c774398c98991d3c7d8ca2c
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1157255
milestone40.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 1157255: Use typeof to determine MarionetteObj exports This makes us not rely on try…catch logic to export MarionetteObj properties and functions on the sandbox. The patch also cleans up a few more details on createExecuteContentSandbox. r=dburns
testing/marionette/listener.js
--- a/testing/marionette/listener.js
+++ b/testing/marionette/listener.js
@@ -451,53 +451,54 @@ function checkForInterrupted() {
 
 /*
  * Marionette Methods
  */
 
 /**
  * Returns a content sandbox that can be used by the execute_foo functions.
  */
-function createExecuteContentSandbox(aWindow, timeout) {
-  let sandbox = new Cu.Sandbox(aWindow, {sandboxPrototype: aWindow});
+function createExecuteContentSandbox(win, timeout) {
+  let mn = new Marionette(
+      this,
+      win,
+      "content",
+      marionetteLogObj,
+      timeout,
+      heartbeatCallback,
+      marionetteTestName);
+  mn.runEmulatorCmd = (cmd, cb) => this.runEmulatorCmd(cmd, cb);
+  mn.runEmulatorShell = (args, cb) => this.runEmulatorShell(args, cb);
+
+  let sandbox = new Cu.Sandbox(win, {sandboxPrototype: win});
   sandbox.global = sandbox;
-  sandbox.window = aWindow;
+  sandbox.window = win;
   sandbox.document = sandbox.window.document;
   sandbox.navigator = sandbox.window.navigator;
   sandbox.testUtils = utils;
   sandbox.asyncTestCommandId = asyncTestCommandId;
+  sandbox.marionette = mn;
 
-  let marionette = new Marionette(this, aWindow, "content",
-                                  marionetteLogObj, timeout,
-                                  heartbeatCallback,
-                                  marionetteTestName);
-  marionette.runEmulatorCmd = (cmd, cb) => this.runEmulatorCmd(cmd, cb);
-  marionette.runEmulatorShell = (args, cb) => this.runEmulatorShell(args, cb);
-  sandbox.marionette = marionette;
-  marionette.exports.forEach(function(fn) {
-    try {
-      sandbox[fn] = marionette[fn].bind(marionette);
-    }
-    catch(e) {
-      sandbox[fn] = marionette[fn];
+  mn.exports.forEach(fn => {
+    if (typeof mn[fn] == "function") {
+      sandbox[fn] = mn[fn].bind(mn);
+    } else {
+      sandbox[fn] = mn[fn];
     }
   });
 
-  if (aWindow.wrappedJSObject.SpecialPowers != undefined) {
-    XPCOMUtils.defineLazyGetter(sandbox, 'SpecialPowers', function() {
-      return aWindow.wrappedJSObject.SpecialPowers;
-    });
+  let specialPowersFn;
+  if (typeof win.wrappedJSObject.SpecialPowers != "undefined") {
+    specialPowersFn = () => win.wrappedJSObject.SpecialPowers;
+  } else {
+    specialPowersFn = () => new SpecialPowers(win);
   }
-  else {
-    XPCOMUtils.defineLazyGetter(sandbox, 'SpecialPowers', function() {
-      return new SpecialPowers(aWindow);
-    });
-  }
+  XPCOMUtils.defineLazyGetter(sandbox, "SpecialPowers", specialPowersFn);
 
-  sandbox.asyncComplete = function(obj, id) {
+  sandbox.asyncComplete = (obj, id) => {
     if (id == asyncTestCommandId) {
       curFrame.removeEventListener("unload", onunload, false);
       curFrame.clearTimeout(asyncTestTimeoutId);
 
       if (inactivityTimeoutId != null) {
         curFrame.clearTimeout(inactivityTimeoutId);
       }
 
@@ -520,19 +521,19 @@ function createExecuteContentSandbox(aWi
       asyncTestTimeoutId = undefined;
       asyncTestCommandId = undefined;
       inactivityTimeoutId = null;
     }
   };
 
   sandbox.finish = function() {
     if (asyncTestRunning) {
-      sandbox.asyncComplete(marionette.generate_results(), sandbox.asyncTestCommandId);
+      sandbox.asyncComplete(mn.generate_results(), sandbox.asyncTestCommandId);
     } else {
-      return marionette.generate_results();
+      return mn.generate_results();
     }
   };
   sandbox.marionetteScriptFinished = val =>
       sandbox.asyncComplete(val, sandbox.asyncTestCommandId);
 
   return sandbox;
 }