Bug 1288799 - Add a hook to run a custom onload function in APZ group subtests, and a helper to inject scripts into documents. r=botond
authorKartikaya Gupta <kgupta@mozilla.com>
Fri, 29 Jul 2016 14:27:39 -0400
changeset 307360 57a85939e44a75e496fda385bd2cc24503e23102
parent 307359 41f642c6793d4bf2df1046d88be687daead65021
child 307361 8fb07560ca17cb3d4b2483d448e8fa78a9ed7c15
push id30508
push usercbook@mozilla.com
push dateSat, 30 Jul 2016 14:21:21 +0000
treeherdermozilla-central@e5859dfe0bcb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbotond
bugs1288799
milestone50.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 1288799 - Add a hook to run a custom onload function in APZ group subtests, and a helper to inject scripts into documents. r=botond MozReview-Commit-ID: CpNBgSMRIgU
gfx/layers/apz/test/mochitest/apz_test_utils.js
--- a/gfx/layers/apz/test/mochitest/apz_test_utils.js
+++ b/gfx/layers/apz/test/mochitest/apz_test_utils.js
@@ -159,20 +159,24 @@ function waitForApzFlushedRepaints(aCall
 // windows, and returns a Promise that is resolved once all the subtests are
 // done running.
 //
 // The aSubtests array is an array of objects with the following keys:
 //   file: required, the filename of the subtest.
 //   prefs: optional, an array of arrays containing key-value prefs to set.
 //   dp_suppression: optional, a boolean on whether or not to respect displayport
 //                   suppression during the test.
+//   onload: optional, a function that will be registered as a load event listener
+//           for the child window that will hold the subtest. the function will be
+//           passed exactly one argument, which will be the child window.
 // An example of an array is:
 //   aSubtests = [
 //     { 'file': 'test_file_name.html' },
 //     { 'file': 'test_file_2.html', 'prefs': [['pref.name', true], ['other.pref', 1000]], 'dp_suppression': false }
+//     { 'file': 'file_3.html', 'onload': function(w) { w.subtestDone(); } }
 //   ];
 //
 // Each subtest should call the subtestDone() function when it is done, to
 // indicate that the window should be torn down and the next text should run.
 // The subtestDone() function is injected into the subtest's window by this
 // function prior to loading the subtest. For convenience, the |is| and |ok|
 // functions provided by SimpleTest are also mapped into the subtest's window.
 // For other things from the parent, the subtest can use window.opener.<whatever>
@@ -220,16 +224,19 @@ function runSubtestsSeriallyInFreshWindo
       }
 
       function spawnTest(aFile) {
         w = window.open('', "_blank");
         w.subtestDone = advanceSubtestExecution;
         w.SimpleTest = SimpleTest;
         w.is = function(a, b, msg) { return is(a, b, aFile + " | " + msg); };
         w.ok = function(cond, name, diag) { return ok(cond, aFile + " | " + name, diag); };
+        if (test.onload) {
+          w.addEventListener('load', function(e) { test.onload(w); }, { once: true });
+        }
         w.location = location.href.substring(0, location.href.lastIndexOf('/') + 1) + aFile;
         return w;
       }
 
       if (test.prefs) {
         // Got some prefs for this subtest, push them
         SpecialPowers.pushPrefEnv({"set": test.prefs}, function() {
           w = spawnTest(test.file);
@@ -362,8 +369,35 @@ function getQueryArgs() {
     var params = location.search.substr(1).split('&');
     for (var p of params) {
       var [k, v] = p.split('=');
       args[k] = JSON.parse(v);
     }
   }
   return args;
 }
+
+// Return a function that returns a promise to create a script element with the
+// given URI and append it to the head of the document in the given window.
+// As with runContinuation(), the extra function wrapper is for convenience
+// at the call site, so that this can be chained with other promises:
+//   waitUntilApzStable().then(injectScript('foo'))
+//                       .then(injectScript('bar'));
+// If you want to do the injection right away, run the function returned by
+// this function:
+//   injectScript('foo')();
+function injectScript(aScript, aWindow = window) {
+  return function() {
+    return new Promise(function(resolve, reject) {
+      var e = aWindow.document.createElement('script');
+      e.type = 'text/javascript';
+      e.onload = function() {
+        resolve();
+      };
+      e.onerror = function() {
+        dump('Script [' + aScript + '] errored out\n');
+        reject();
+      };
+      e.src = aScript;
+      aWindow.document.getElementsByTagName('head')[0].appendChild(e);
+    });
+  };
+}