Bug 1368648: Remove importScript functionality from Marionette r?ato draft
authorDavid Burns <dburns@mozilla.com>
Tue, 30 May 2017 11:25:14 +0100
changeset 586509 8e5bcdd09762ae2029f43160375c0f4b9851cb90
parent 586508 30daca3da53c64db14c83967fcbded02eb6d7fc4
child 631016 d8d8ebce649e5ab491a80b915405a1556bc3bb96
push id61437
push userbmo:dburns@mozilla.com
push dateTue, 30 May 2017 15:43:05 +0000
reviewersato
bugs1368648
milestone55.0a1
Bug 1368648: Remove importScript functionality from Marionette r?ato This removes importScript functionality that was used extensively for B2G testing but is no longer used in tree. MozReview-Commit-ID: 1O7GWCZPWRZ
testing/marionette/driver.js
testing/marionette/evaluate.js
testing/marionette/frame.js
testing/marionette/harness/marionette_harness/tests/unit/importanotherscript.js
testing/marionette/harness/marionette_harness/tests/unit/importscript.js
testing/marionette/harness/marionette_harness/tests/unit/test_import_script.py
testing/marionette/harness/marionette_harness/tests/unit/unit-tests.ini
testing/marionette/listener.js
--- a/testing/marionette/driver.js
+++ b/testing/marionette/driver.js
@@ -116,18 +116,16 @@ this.GeckoDriver = function (appName, se
   this.oopFrameId = null;
   this.observing = null;
   this._browserIds = new WeakMap();
 
   // The curent context decides if commands should affect chrome- or
   // content space.
   this.context = Context.CONTENT;
 
-  this.importedScripts = new evaluate.ScriptStorageService(
-      [Context.CHROME, Context.CONTENT]);
   this.sandboxes = new Sandboxes(() => this.getCurrentWindow());
   this.legacyactions = new legacyaction.Chain();
 
   this.timer = null;
   this.inactivityTimer = null;
 
   this.marionetteLog = new logging.ContentLogger();
   this.testName = null;
@@ -884,17 +882,16 @@ GeckoDriver.prototype.execute_ = functio
     case Context.CHROME:
       let sb = this.sandboxes.get(opts.sandboxName, opts.newSandbox);
       if (opts.sandboxName) {
         sb = sandbox.augment(sb, new logging.Adapter(this.marionetteLog));
         sb = sandbox.augment(sb, {global: sb});
       }
 
       opts.timeout = timeout;
-      script = this.importedScripts.for(Context.CHROME).concat(script);
       let wargs = evaluate.fromJSON(args, this.curBrowser.seenEls, sb.window);
       let evaluatePromise = evaluate.sandbox(sb, script, wargs, opts);
       return evaluatePromise.then(res => evaluate.toJSON(res, this.curBrowser.seenEls));
   }
 };
 
 /**
  * Execute pure JavaScript.  Used to execute simpletest harness tests,
@@ -2723,43 +2720,16 @@ GeckoDriver.prototype.getAppCacheStatus 
 
     case Context.CONTENT:
       resp.body.value = yield this.listener.getAppCacheStatus();
       break;
   }
 };
 
 /**
- * Import script to the JS evaluation runtime.
- *
- * Imported scripts are exposed in the contexts of all subsequent
- * calls to {@code executeScript}, {@code executeAsyncScript}, and
- * {@code executeJSScript} by prepending them to the evaluated script.
- *
- * Scripts can be cleared with the {@code clearImportedScripts} command.
- *
- * @param {string} script
- *     Script to include.  If the script is byte-by-byte equal to an
- *     existing imported script, it is not imported.
- */
-GeckoDriver.prototype.importScript = function*(cmd, resp) {
-  let script = cmd.parameters.script;
-  this.importedScripts.for(this.context).add(script);
-};
-
-/**
- * Clear all scripts that are imported into the JS evaluation runtime.
- *
- * Scripts can be imported using the {@code importScript} command.
- */
-GeckoDriver.prototype.clearImportedScripts = function*(cmd, resp) {
-  this.importedScripts.for(this.context).clear();
-};
-
-/**
  * Takes a screenshot of a web element, current frame, or viewport.
  *
  * The screen capture is returned as a lossless PNG image encoded as
  * a base 64 string.
  *
  * If called in the content context, the <code>id</code> argument is not null
  * and refers to a present and visible web element's ID, the capture area
  * will be limited to the bounding box of that element. Otherwise, the
@@ -3371,18 +3341,16 @@ GeckoDriver.prototype.commands = {
   "setWindowRect": GeckoDriver.prototype.setWindowRect,
   "getWindowRect": GeckoDriver.prototype.getWindowRect,
   "getActiveFrame": GeckoDriver.prototype.getActiveFrame,
   "switchToFrame": GeckoDriver.prototype.switchToFrame,
   "switchToParentFrame": GeckoDriver.prototype.switchToParentFrame,
   "switchToWindow": GeckoDriver.prototype.switchToWindow,
   "switchToShadowRoot": GeckoDriver.prototype.switchToShadowRoot,
   "deleteSession": GeckoDriver.prototype.deleteSession,
-  "importScript": GeckoDriver.prototype.importScript,
-  "clearImportedScripts": GeckoDriver.prototype.clearImportedScripts,
   "getAppCacheStatus": GeckoDriver.prototype.getAppCacheStatus,
   "close": GeckoDriver.prototype.close,
   "closeChromeWindow": GeckoDriver.prototype.closeChromeWindow,
   "setTestName": GeckoDriver.prototype.setTestName,
   "takeScreenshot": GeckoDriver.prototype.takeScreenshot,
   "addCookie": GeckoDriver.prototype.addCookie,
   "getCookies": GeckoDriver.prototype.getCookies,
   "deleteAllCookies": GeckoDriver.prototype.deleteAllCookies,
--- a/testing/marionette/evaluate.js
+++ b/testing/marionette/evaluate.js
@@ -442,171 +442,8 @@ this.Sandboxes = class {
 
   /**
    * Clears cache of sandboxes.
    */
   clear() {
     this.boxes_.clear();
   }
 };
-
-/**
- * Stores scripts imported from the local end through the
- * {@code GeckoDriver#importScript} command.
- *
- * Imported scripts are prepended to the script that is evaluated
- * on each call to {@code GeckoDriver#executeScript},
- * {@code GeckoDriver#executeAsyncScript}, and
- * {@code GeckoDriver#executeJSScript}.
- *
- * Usage:
- *
- *     let importedScripts = new evaluate.ScriptStorage();
- *     importedScripts.add(firstScript);
- *     importedScripts.add(secondScript);
- *
- *     let scriptToEval = importedScripts.concat(script);
- *     // firstScript and secondScript are prepended to script
- *
- */
-evaluate.ScriptStorage = class extends Set {
-
-  /**
-   * Produce a string of all stored scripts.
-   *
-   * The stored scripts are concatenated into a string, with optional
-   * additional scripts then appended.
-   *
-   * @param {...string} addional
-   *     Optional scripts to include.
-   *
-   * @return {string}
-   *     Concatenated string consisting of stored scripts and additional
-   *     scripts, in that order.
-   */
-  concat(...additional) {
-    let rv = "";
-    for (let s of this) {
-      rv = s + rv;
-    }
-    for (let s of additional) {
-      rv = rv + s;
-    }
-    return rv;
-  }
-
-  toJson() {
-    return Array.from(this);
-  }
-
-};
-
-/**
- * Service that enables the script storage service to be queried from
- * content space.
- *
- * The storage can back multiple |ScriptStorage|, each typically belonging
- * to a |Context|.  Since imported scripts' scope are global and not
- * scoped to the current browsing context, all imported scripts are stored
- * in chrome space and fetched by content space as needed.
- *
- * Usage in chrome space:
- *
- *     let service = new evaluate.ScriptStorageService(
- *         [Context.CHROME, Context.CONTENT]);
- *     let storage = service.for(Context.CHROME);
- *     let scriptToEval = storage.concat(script);
- *
- */
-evaluate.ScriptStorageService = class extends Map {
-
-  /**
-   * Create the service.
-   *
-   * An optional array of names for script storages to initially create
-   * can be provided.
-   *
-   * @param {Array.<string>=} initialStorages
-   *     List of names of the script storages to create initially.
-   */
-  constructor(initialStorages = []) {
-    super(initialStorages.map(name => [name, new evaluate.ScriptStorage()]));
-  }
-
-  /**
-   * Retrieve the scripts associated with the given context.
-   *
-   * @param {Context} context
-   *     Context to retrieve the scripts from.
-   *
-   * @return {ScriptStorage}
-   *     Scrips associated with given |context|.
-   */
-  for(context) {
-    return this.get(context);
-  }
-
-  processMessage(msg) {
-    switch (msg.name) {
-      case "Marionette:getImportedScripts":
-        let storage = this.for.apply(this, msg.json);
-        return storage.toJson();
-
-      default:
-        throw new TypeError("Unknown message: " + msg.name);
-    }
-  }
-
-  // TODO(ato): The idea of services in chrome space
-  // can be generalised at some later time (see cookies.js:38).
-  receiveMessage(msg) {
-    try {
-      return this.processMessage(msg);
-    } catch (e) {
-      logger.error(e);
-    }
-  }
-
-};
-
-evaluate.ScriptStorageService.prototype.QueryInterface =
-    XPCOMUtils.generateQI([
-      Ci.nsIMessageListener,
-      Ci.nsISupportsWeakReference,
-    ]);
-
-/**
- * Bridges the script storage in chrome space, to make it possible to
- * retrieve a {@code ScriptStorage} associated with a given
- * {@code Context} from content space.
- *
- * Usage in content space:
- *
- *     let client = new evaluate.ScriptStorageServiceClient(chromeProxy);
- *     let storage = client.for(Context.CONTENT);
- *     let scriptToEval = storage.concat(script);
- *
- */
-evaluate.ScriptStorageServiceClient = class {
-
-  /**
-   * @param {proxy.SyncChromeSender} chromeProxy
-   *     Proxy for communicating with chrome space.
-   */
-  constructor(chromeProxy) {
-    this.chrome = chromeProxy;
-  }
-
-  /**
-   * Retrieve scripts associated with the given context.
-   *
-   * @param {Context} context
-   *     Context to retrieve scripts from.
-   *
-   * @return {ScriptStorage}
-   *     Scripts associated with given |context|.
-   */
-  for(context) {
-    let scripts = this.chrome.getImportedScripts(context)[0];
-    return new evaluate.ScriptStorage(scripts);
-  }
-
-};
--- a/testing/marionette/frame.js
+++ b/testing/marionette/frame.js
@@ -215,17 +215,16 @@ frame.Manager = class {
     mm.addWeakMessageListener("Marionette:done", this.driver);
     mm.addWeakMessageListener("Marionette:error", this.driver);
     mm.addWeakMessageListener("Marionette:emitTouchEvent", this.driver);
     mm.addWeakMessageListener("Marionette:log", this.driver);
     mm.addWeakMessageListener("Marionette:shareData", this.driver);
     mm.addWeakMessageListener("Marionette:switchToModalOrigin", this.driver);
     mm.addWeakMessageListener("Marionette:switchedToFrame", this.driver);
     mm.addWeakMessageListener("Marionette:getVisibleCookies", this.driver);
-    mm.addWeakMessageListener("Marionette:getImportedScripts", this.driver.importedScripts);
     mm.addWeakMessageListener("Marionette:register", this.driver);
     mm.addWeakMessageListener("Marionette:listenersAttached", this.driver);
     mm.addWeakMessageListener("MarionetteFrame:handleModal", this);
     mm.addWeakMessageListener("MarionetteFrame:getCurrentFrameId", this);
     mm.addWeakMessageListener("MarionetteFrame:getInterruptedState", this);
   }
 
   /**
deleted file mode 100644
--- a/testing/marionette/harness/marionette_harness/tests/unit/importanotherscript.js
+++ /dev/null
@@ -1,1 +0,0 @@
-var testAnotherFunc = function() { return "i'm yet another test function!";};
deleted file mode 100644
--- a/testing/marionette/harness/marionette_harness/tests/unit/importscript.js
+++ /dev/null
@@ -1,1 +0,0 @@
-var testFunc = function() { return "i'm a test function!";};
deleted file mode 100644
--- a/testing/marionette/harness/marionette_harness/tests/unit/test_import_script.py
+++ /dev/null
@@ -1,138 +0,0 @@
-# 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/.
-
-import os
-
-from marionette_driver.by import By
-from marionette_driver.errors import JavascriptException
-
-from marionette_harness import (
-    MarionetteTestCase,
-    skip_if_chrome,
-    skip_if_mobile,
-    WindowManagerMixin,
-)
-
-
-class TestImportScriptContent(WindowManagerMixin, MarionetteTestCase):
-    contexts = set(["chrome", "content"])
-
-    script_file = os.path.abspath(
-        os.path.join(__file__, os.path.pardir, "importscript.js"))
-    another_script_file = os.path.abspath(
-        os.path.join(__file__, os.path.pardir, "importanotherscript.js"))
-
-    def setUp(self):
-        super(TestImportScriptContent, self).setUp()
-
-        for context in self.contexts:
-            with self.marionette.using_context(context):
-                self.marionette.clear_imported_scripts()
-        self.reset_context()
-
-    def tearDown(self):
-        self.close_all_windows()
-
-        super(TestImportScriptContent, self).tearDown()
-
-    def reset_context(self):
-        self.marionette.set_context("content")
-
-    @property
-    def current_context(self):
-        return self.marionette._send_message("getContext", key="value")
-
-    @property
-    def other_context(self):
-        return self.contexts.copy().difference([self.current_context]).pop()
-
-    def is_defined(self, symbol):
-        return self.marionette.execute_script(
-            "return typeof {} != 'undefined'".format(symbol))
-
-    def assert_defined(self, symbol, msg=None):
-        if msg is None:
-            msg = "Expected symbol {} to be defined".format(symbol)
-        self.assertTrue(self.is_defined(symbol), msg)
-
-    def assert_undefined(self, symbol, msg=None):
-        if msg is None:
-            msg = "Expected symbol {} to be undefined".format(symbol)
-        self.assertFalse(self.is_defined(symbol), msg)
-
-    def assert_scripts_cleared(self):
-        self.marionette.import_script(self.script_file)
-        self.assert_defined("testFunc")
-        self.marionette.clear_imported_scripts()
-        self.assert_undefined("testFunc")
-
-    def test_import_script(self):
-        self.marionette.import_script(self.script_file)
-        self.assertEqual(
-            "i'm a test function!", self.marionette.execute_script("return testFunc();"))
-        self.assertEqual("i'm a test function!", self.marionette.execute_async_script(
-            "marionetteScriptFinished(testFunc());"))
-
-    def test_import_script_twice(self):
-        self.marionette.import_script(self.script_file)
-        self.assert_defined("testFunc")
-
-        # TODO(ato): Note that the WebDriver command primitives
-        # does not allow us to check what scripts have been imported.
-        # I suspect we must to do this through an xpcshell test.
-
-        self.marionette.import_script(self.script_file)
-        self.assert_defined("testFunc")
-
-    def test_import_script_and_clear(self):
-        self.marionette.import_script(self.script_file)
-        self.assert_defined("testFunc")
-        self.marionette.clear_imported_scripts()
-        self.assert_scripts_cleared()
-        self.assert_undefined("testFunc")
-        with self.assertRaises(JavascriptException):
-            self.marionette.execute_script("return testFunc()")
-        with self.assertRaises(JavascriptException):
-            self.marionette.execute_async_script(
-                "marionetteScriptFinished(testFunc())")
-
-    def test_clear_scripts_in_other_context(self):
-        self.marionette.import_script(self.script_file)
-        self.assert_defined("testFunc")
-
-        # clearing other context's script file should not affect ours
-        with self.marionette.using_context(self.other_context):
-            self.marionette.clear_imported_scripts()
-            self.assert_undefined("testFunc")
-
-        self.assert_defined("testFunc")
-
-    def test_multiple_imports(self):
-        self.marionette.import_script(self.script_file)
-        self.marionette.import_script(self.another_script_file)
-        self.assert_defined("testFunc")
-        self.assert_defined("testAnotherFunc")
-
-    @skip_if_chrome("Needs content scope")
-    @skip_if_mobile("New windows not supported in Fennec")
-    def test_imports_apply_globally(self):
-        self.marionette.navigate(
-            self.marionette.absolute_url("test_windows.html"))
-
-        def open_window_with_link():
-            self.marionette.find_element(By.LINK_TEXT, "Open new window").click()
-
-        new_window = self.open_window(trigger=open_window_with_link)
-        self.marionette.switch_to_window(new_window)
-
-        self.marionette.import_script(self.script_file)
-        self.marionette.close_chrome_window()
-
-        self.marionette.switch_to_window(self.start_window)
-        self.assert_defined("testFunc")
-
-
-class TestImportScriptChrome(TestImportScriptContent):
-    def reset_context(self):
-        self.marionette.set_context("chrome")
--- a/testing/marionette/harness/marionette_harness/tests/unit/unit-tests.ini
+++ b/testing/marionette/harness/marionette_harness/tests/unit/unit-tests.ini
@@ -1,17 +1,16 @@
 [test_marionette.py]
 [test_geckoinstance.py]
 [test_data_driven.py]
 [test_session.py]
 [test_capabilities.py]
 [test_accessibility.py]
 [test_expectedfail.py]
 expected = fail
-[test_import_script.py]
 [test_click.py]
 [test_click_chrome.py]
 skip-if = appname == 'fennec'
 [test_checkbox.py]
 [test_checkbox_chrome.py]
 skip-if = appname == 'fennec'
 [test_elementsize.py]
 [test_elementsize_chrome.py]
--- a/testing/marionette/listener.js
+++ b/testing/marionette/listener.js
@@ -78,17 +78,16 @@ var EVENT_INTERVAL = 30; // milliseconds
 var multiLast = {};
 var asyncChrome = proxy.toChromeAsync({
   addMessageListener: addMessageListenerId.bind(this),
   removeMessageListener: removeMessageListenerId.bind(this),
   sendAsyncMessage: sendAsyncMessage.bind(this),
 });
 var syncChrome = proxy.toChrome(sendSyncMessage.bind(this));
 var cookies = new Cookies(() => curContainer.frame.document, syncChrome);
-var importedScripts = new evaluate.ScriptStorageServiceClient(syncChrome);
 
 Cu.import("resource://gre/modules/Log.jsm");
 var logger = Log.repository.getLogger("Marionette");
 // Append only once to avoid duplicated output after listener.js gets reloaded
 if (logger.ownAppenders.length == 0) {
   logger.addAppender(new Log.DumpAppender());
 }
 logger.debug("loaded listener.js");
@@ -727,29 +726,27 @@ function checkForInterrupted() {
         sendSyncMessage("Marionette:switchToModalOrigin");
       }
       sendSyncMessage("Marionette:switchedToFrame", { restorePrevious: true });
     }
 }
 
 function* execute(script, args, timeout, opts) {
   opts.timeout = timeout;
-  script = importedScripts.for("content").concat(script);
 
   let sb = sandbox.createMutable(curContainer.frame);
   let wargs = evaluate.fromJSON(
       args, seenEls, curContainer.frame, curContainer.shadowRoot);
   let res = yield evaluate.sandbox(sb, script, wargs, opts);
 
   return evaluate.toJSON(res, seenEls);
 }
 
 function* executeInSandbox(script, args, timeout, opts) {
   opts.timeout = timeout;
-  script = importedScripts.for("content").concat(script);
 
   let sb = sandboxes.get(opts.sandboxName, opts.newSandbox);
   if (opts.sandboxName) {
     sb = sandbox.augment(sb, {global: sb});
     sb = sandbox.augment(sb, new logging.Adapter(contentLog));
   }
 
   let wargs = evaluate.fromJSON(
@@ -761,17 +758,16 @@ function* executeInSandbox(script, args,
       "Marionette:shareData",
       {log: evaluate.toJSON(contentLog.get(), seenEls)});
   return evaluate.toJSON(res, seenEls);
 }
 
 function* executeSimpleTest(script, args, timeout, opts) {
   opts.timeout = timeout;
   let win = curContainer.frame;
-  script = importedScripts.for("content").concat(script);
 
   let harness = new simpletest.Harness(
       win,
       "content",
       contentLog,
       timeout,
       marionetteTestName);
   let sb = sandbox.createSimpleTest(curContainer.frame, harness);