Bug 1504756 - [marionette] Add executeSoon() to run tasks on the main thread. r=ato
authorHenrik Skupin <mail@hskupin.info>
Thu, 10 Jan 2019 10:10:14 +0000
changeset 453224 1c023add0d1533e2f1866b0a6a1e8ac6c0530d75
parent 453223 f65f0c93a581bad32c5afb454f19596f701083f5
child 453225 70e87e4f91d066b77adac21b1596429dd1c2087a
push id35349
push userbtara@mozilla.com
push dateThu, 10 Jan 2019 17:19:27 +0000
treeherdermozilla-central@a51746f37520 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato
bugs1504756
milestone66.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 1504756 - [marionette] Add executeSoon() to run tasks on the main thread. r=ato To be closer to other test harnesses which are using executeSoon() to run a task on the main thread, this patch adds the same method to Marionette's sync module. Differential Revision: https://phabricator.services.mozilla.com/D13659
testing/marionette/doc/internals/sync.rst
testing/marionette/sync.js
testing/marionette/test/unit/test_sync.js
testing/marionette/transport.js
--- a/testing/marionette/doc/internals/sync.rst
+++ b/testing/marionette/doc/internals/sync.rst
@@ -1,13 +1,15 @@
 sync module
 ===========
 
 Provides an assortment of synchronisation primitives.
 
+.. js:autofunction:: executeSoon
+
 .. js:autoclass:: MessageManagerDestroyedPromise
   :members:
 
 .. js:autoclass:: PollPromise
   :members:
 
 .. js:autoclass:: Sleep
   :members:
--- a/testing/marionette/sync.js
+++ b/testing/marionette/sync.js
@@ -13,28 +13,44 @@ const {
   stack,
   TimeoutError,
 } = ChromeUtils.import("chrome://marionette/content/error.js", {});
 const {Log} = ChromeUtils.import("chrome://marionette/content/log.js", {});
 
 XPCOMUtils.defineLazyGetter(this, "log", Log.get);
 
 this.EXPORTED_SYMBOLS = [
+  "executeSoon",
   "DebounceCallback",
   "IdlePromise",
   "MessageManagerDestroyedPromise",
   "PollPromise",
   "Sleep",
   "TimedPromise",
 ];
 
 const {TYPE_ONE_SHOT, TYPE_REPEATING_SLACK} = Ci.nsITimer;
 
 const PROMISE_TIMEOUT = AppConstants.DEBUG ? 4500 : 1500;
 
+
+/**
+ * Dispatch a function to be executed on the main thread.
+ *
+ * @param {function} func
+ *     Function to be executed.
+ */
+function executeSoon(func) {
+  if (typeof func != "function") {
+    throw new TypeError();
+  }
+
+  Services.tm.dispatchToMainThread(func);
+}
+
 /**
  * @callback Condition
  *
  * @param {function(*)} resolve
  *     To be called when the condition has been met.  Will return the
  *     resolved value.
  * @param {function} reject
  *     To be called when the condition has not been met.  Will cause
--- a/testing/marionette/test/unit/test_sync.js
+++ b/testing/marionette/test/unit/test_sync.js
@@ -30,16 +30,33 @@ class MockTimer {
     }
   }
 
   cancel() {
     this.cancelled = true;
   }
 }
 
+add_test(function test_executeSoon_callback() {
+  // executeSoon() is already defined for xpcshell in head.js. As such import
+  // our implementation into a custom namespace.
+  let sync = {};
+  ChromeUtils.import("chrome://marionette/content/sync.js", sync);
+
+  for (let func of ["foo", null, true, [], {}]) {
+    Assert.throws(() => sync.executeSoon(func), /TypeError/);
+  }
+
+  let a;
+  sync.executeSoon(() => { a = 1; });
+  executeSoon(() => equal(1, a));
+
+  run_next_test();
+});
+
 add_test(function test_PollPromise_funcTypes() {
   for (let type of ["foo", 42, null, undefined, true, [], {}]) {
     Assert.throws(() => new PollPromise(type), /TypeError/);
   }
   new PollPromise(() => {});
   new PollPromise(function() {});
 
   run_next_test();
--- a/testing/marionette/transport.js
+++ b/testing/marionette/transport.js
@@ -5,24 +5,27 @@
 "use strict";
 
 /* global Pipe, ScriptableInputStream */
 
 const CC = Components.Constructor;
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/EventEmitter.jsm");
-const {StreamUtils} =
-    ChromeUtils.import("chrome://marionette/content/stream-utils.js", {});
-const {Packet, JSONPacket, BulkPacket} =
-    ChromeUtils.import("chrome://marionette/content/packets.js", {});
-
-const executeSoon = function(func) {
-  Services.tm.dispatchToMainThread(func);
-};
+const {
+  StreamUtils,
+} = ChromeUtils.import("chrome://marionette/content/stream-utils.js", {});
+const {
+  BulkPacket,
+  JSONPacket,
+  Packet,
+} = ChromeUtils.import("chrome://marionette/content/packets.js", {});
+const {
+  executeSoon,
+} = ChromeUtils.import("chrome://marionette/content/sync.js", {});
 
 const flags = {wantVerbose: false, wantLogging: false};
 
 const dumpv =
   flags.wantVerbose ?
   function(msg) { dump(msg + "\n"); } :
   function() {};