Bug 1338493 - Part 1, make webReqeust test to fiddle the main event loop after loading CSS/JS. r?kmag draft
authorShih-Chiang Chien <schien@mozilla.com>
Mon, 22 May 2017 11:41:08 +0800
changeset 602121 923eab1f89db95bbc25288b4ebe7e187575b7561
parent 601236 306d2070e105b75f7076d30cc0288171f1435c07
child 602122 5962ce1243e3e2d34dcf54f53d50c5c6c9060de9
push id66286
push userschien@mozilla.com
push dateThu, 29 Jun 2017 17:52:09 +0000
reviewerskmag
bugs1338493
milestone56.0a1
Bug 1338493 - Part 1, make webReqeust test to fiddle the main event loop after loading CSS/JS. r?kmag Making ODA completely off-main-thread will make the order webRequest.onCompleted and CSS/JS execution unpredictable. |window.requestIdleCallback| is used to fiddle the main thread event queue. MozReview-Commit-ID: LIk0T96K6p1
toolkit/components/extensions/test/mochitest/test_ext_webrequest_basic.html
--- a/toolkit/components/extensions/test/mochitest/test_ext_webrequest_basic.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_webrequest_basic.html
@@ -7,16 +7,24 @@
   <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
   <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
   <script type="text/javascript" src="head_webrequest.js"></script>
   <script type="text/javascript" src="head.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 <script>
 "use strict";
 
+function spinEventLoop() {
+  return new Promise((resolve, reject) => {
+    window.requestIdleCallback(() => {
+      resolve();
+    });
+  });
+}
+
 if (AppConstants.platform === "android") {
   SimpleTest.requestLongerTimeout(3);
 }
 
 let extension;
 add_task(async function setup() {
   // Clear the image cache, since it gets in the way otherwise.
   let imgTools = SpecialPowers.Cc["@mozilla.org/image/tools;1"].getService(SpecialPowers.Ci.imgITools);
@@ -59,16 +67,18 @@ add_task(async function test_webRequest_
   extension.sendMessage("set-expected", {expect, origin: location.href});
   await extension.awaitMessage("continue");
   addStylesheet("file_style_bad.css");
   await extension.awaitMessage("cancelled");
   // we redirect to style_good which completes the test
   addStylesheet("file_style_redirect.css");
   await extension.awaitMessage("done");
 
+  // waiting for CSS applied.
+  await spinEventLoop();
   let style = window.getComputedStyle(document.getElementById("test"));
   is(style.getPropertyValue("color"), "rgb(255, 0, 0)", "Good CSS loaded");
 });
 
 add_task(async function test_webRequest_images() {
   let expect = {
     "file_image_bad.png": {
       type: "image",
@@ -114,16 +124,18 @@ add_task(async function test_webRequest_
   extension.sendMessage("set-expected", {expect, origin: location.href});
   await extension.awaitMessage("continue");
   addScript("file_script_bad.js");
   await extension.awaitMessage("cancelled");
   // we redirect to script_good which completes the test
   addScript("file_script_redirect.js");
   await extension.awaitMessage("done");
 
+  // wait for JS executed
+  await spinEventLoop();
   is(window.success, 1, "Good script ran");
   is(window.failure, undefined, "Failure script didn't run");
 });
 
 add_task(async function test_webRequest_xhr_get() {
   let expect = {
     "file_script_xhr.js": {
       type: "script",
@@ -175,16 +187,18 @@ add_task(async function test_webRequest_
   extension.sendMessage("set-expected", {expect, origin: location.href});
   await extension.awaitMessage("continue");
   addImage("file_image_good.png");
   addScript("file_script_good.js");
   addStylesheet("file_style_good.css");
   addScript("nonexistent_script_url.js");
   await extension.awaitMessage("done");
 
+  // wait for JS executed
+  await spinEventLoop();
   is(window.success, 2, "Good script ran");
   is(window.failure, undefined, "Failure script didn't run");
 });
 
 add_task(async function test_webRequest_headers() {
   let expect = {
     "file_script_nonexistent.js": {
       type: "script",