Bug 1480450 - Move TP tests to using async/await and promises. r=dimi!
authorFrancois Marier <francois@mozilla.com>
Mon, 13 Aug 2018 14:41:18 +0000
changeset 486388 cf5e3a3105dc6006d90642d03d1c2e5b2abee8de
parent 486343 bb02d7d281cb13805772a9875c4f31074c152c7b
child 486389 34d3b10c54ebd19d277f23684fb651688c0ef68c
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdimi
bugs1480450
milestone63.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 1480450 - Move TP tests to using async/await and promises. r=dimi! There are no actual changes/additions to these tests. Depends on D3130 Differential Revision: https://phabricator.services.mozilla.com/D3131
toolkit/components/url-classifier/tests/mochitest/test_privatebrowsing_trackingprotection.html
toolkit/components/url-classifier/tests/mochitest/test_trackingprotection_whitelist.html
--- a/toolkit/components/url-classifier/tests/mochitest/test_privatebrowsing_trackingprotection.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_privatebrowsing_trackingprotection.html
@@ -18,36 +18,42 @@
 
 var mainWindow = window.docShell.rootTreeItem.domWindow;
 var contentPage = "http://www.itisatrap.org/chrome/toolkit/components/url-classifier/tests/mochitest/classifiedAnnotatedPBFrame.html";
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://testing-common/UrlClassifierTestUtils.jsm");
 ChromeUtils.import("resource://testing-common/TestUtils.jsm");
 
-function testOnWindow(aPrivate, aCallback) {
-  var win = mainWindow.OpenBrowserWindow({private: aPrivate});
-  win.addEventListener("load", function() {
-    TestUtils.topicObserved("browser-delayed-startup-finished",
-                                  subject => subject == win).then(() => {
-      win.addEventListener("DOMContentLoaded", function onInnerLoad() {
-        if (win.content.location.href != contentPage) {
+function testOnWindow(aPrivate) {
+  return new Promise((resolve, reject) => {
+    let win = mainWindow.OpenBrowserWindow({private: aPrivate});
+    win.addEventListener("load", function() {
+      TestUtils.topicObserved("browser-delayed-startup-finished",
+                              subject => subject == win).then(() => {
+        win.addEventListener("DOMContentLoaded", function onInnerLoad() {
+          if (win.content.location.href != contentPage) {
+            win.gBrowser.loadURI(contentPage);
+            return;
+          }
+          win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
+
+          win.content.addEventListener("load", function innerLoad2() {
+            win.content.removeEventListener("load", innerLoad2);
+            SimpleTest.executeSoon(function() {
+              resolve(win);
+            });
+          }, false, true);
+        }, true);
+        SimpleTest.executeSoon(function() {
           win.gBrowser.loadURI(contentPage);
-          return;
-        }
-        win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
-
-        win.content.addEventListener("load", function innerLoad2() {
-          win.content.removeEventListener("load", innerLoad2);
-          SimpleTest.executeSoon(function() { aCallback(win); });
-        }, false, true);
-      }, true);
-      SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
-    });
-  }, {capture: true, once: true});
+        });
+      });
+    }, {capture: true, once: true});
+  });
 }
 
 var badids = [
   "badscript",
   "badimage",
   "badcss"
 ];
 
@@ -97,39 +103,39 @@ function checkLoads(aWindow, aBlocked) {
   is(allNodeMatch, aBlocked, "All tracking nodes are expected to be annotated as such");
 }
 
 SpecialPowers.pushPrefEnv(
   {"set": [["privacy.trackingprotection.enabled", false],
            ["privacy.trackingprotection.pbmode.enabled", true]]},
   test);
 
-function test() {
+async function test() {
   SimpleTest.registerCleanupFunction(UrlClassifierTestUtils.cleanupTestTrackers);
-  UrlClassifierTestUtils.addTestTrackers().then(() => {
-    // Normal mode, with the pref (trackers should be loaded)
-    testOnWindow(false, function(aWindow) {
-      checkLoads(aWindow, false);
-      aWindow.close();
+  await UrlClassifierTestUtils.addTestTrackers();
+
+  // Normal mode, with the pref (trackers should be loaded)
+  await testOnWindow(false).then(function(aWindow) {
+    checkLoads(aWindow, false);
+    aWindow.close();
+  });
 
-      // Private Browsing, with the pref (trackers should be blocked)
-      testOnWindow(true, function(aWindow1) {
-        checkLoads(aWindow1, true);
-        aWindow1.close();
+  // Private Browsing, with the pref (trackers should be blocked)
+  await testOnWindow(true).then(function(aWindow) {
+    checkLoads(aWindow, true);
+    aWindow.close();
+  });
 
-        // Private Browsing, without the pref (trackers should be loaded)
-        SpecialPowers.setBoolPref("privacy.trackingprotection.pbmode.enabled", false);
-        testOnWindow(true, function(aWindow2) {
-          checkLoads(aWindow2, false);
-          aWindow2.close();
-          SimpleTest.finish();
-        });
-      });
-    });
+  // Private Browsing, without the pref (trackers should be loaded)
+  await SpecialPowers.setBoolPref("privacy.trackingprotection.pbmode.enabled", false);
+  await testOnWindow(true).then(function(aWindow) {
+    checkLoads(aWindow, false);
+    aWindow.close();
   });
+  SimpleTest.finish();
 }
 
 SimpleTest.waitForExplicitFinish();
 
 </script>
 
 </pre>
 <iframe id="testFrame" width="100%" height="100%" onload=""></iframe>
--- a/toolkit/components/url-classifier/tests/mochitest/test_trackingprotection_whitelist.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_trackingprotection_whitelist.html
@@ -19,36 +19,42 @@
 var mainWindow = window.docShell.rootTreeItem.domWindow;
 var contentPage1 = "http://www.itisatrap.org/tests/toolkit/components/url-classifier/tests/mochitest/whitelistFrame.html";
 var contentPage2 = "http://example.com/tests/toolkit/components/url-classifier/tests/mochitest/whitelistFrame.html";
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://testing-common/UrlClassifierTestUtils.jsm");
 ChromeUtils.import("resource://testing-common/TestUtils.jsm");
 
-function testOnWindow(contentPage, aCallback) {
-  var win = mainWindow.OpenBrowserWindow();
-  win.addEventListener("load", function() {
-    TestUtils.topicObserved("browser-delayed-startup-finished",
-                            subject => subject == win).then(() => {
-      win.addEventListener("DOMContentLoaded", function onInnerLoad() {
-        if (win.content.location.href != contentPage) {
+function testOnWindow(contentPage) {
+  return new Promise((resolve, reject) => {
+    var win = mainWindow.OpenBrowserWindow();
+    win.addEventListener("load", function() {
+      TestUtils.topicObserved("browser-delayed-startup-finished",
+                              subject => subject == win).then(() => {
+        win.addEventListener("DOMContentLoaded", function onInnerLoad() {
+          if (win.content.location.href != contentPage) {
+            win.gBrowser.loadURI(contentPage);
+            return;
+          }
+          win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
+
+          win.content.addEventListener("load", function innerLoad2() {
+            win.content.removeEventListener("load", innerLoad2);
+            SimpleTest.executeSoon(function() {
+              resolve(win);
+            });
+          }, false, true);
+        }, true);
+        SimpleTest.executeSoon(function() {
           win.gBrowser.loadURI(contentPage);
-          return;
-        }
-        win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
-
-        win.content.addEventListener("load", function innerLoad2() {
-          win.content.removeEventListener("load", innerLoad2);
-          SimpleTest.executeSoon(function() { aCallback(win); });
-        }, false, true);
-      }, true);
-      SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
-    });
-  }, {capture: true, once: true});
+        });
+      });
+    }, {capture: true, once: true});
+  });
 }
 
 var alwaysbadids = [
   "badscript",
 ];
 
 function checkLoads(aWindow, aWhitelisted) {
   var win = aWindow.content;
@@ -94,42 +100,41 @@ function checkLoads(aWindow, aWhiteliste
   is(allNodeMatch, true, "All tracking nodes are expected to be annotated as such");
 }
 
 SpecialPowers.pushPrefEnv(
   {"set": [["privacy.trackingprotection.enabled", true],
            ["channelclassifier.allowlist_example", true]]},
   test);
 
-function test() {
+async function test() {
   SimpleTest.registerCleanupFunction(UrlClassifierTestUtils.cleanupTestTrackers);
-  UrlClassifierTestUtils.addTestTrackers().then(() => {
-    // Load the test from a URL on the whitelist
-    testOnWindow(contentPage1, function(aWindow) {
-      checkLoads(aWindow, true);
-      aWindow.close();
+  await UrlClassifierTestUtils.addTestTrackers();
 
-      // Load the test from a URL that's NOT on the whitelist
-      testOnWindow(contentPage2, function(aWindow1) {
-        checkLoads(aWindow1, false);
-        aWindow1.close();
+  // Load the test from a URL on the whitelist
+  await testOnWindow(contentPage1).then(function(aWindow) {
+    checkLoads(aWindow, true);
+    aWindow.close();
+  });
 
-        // Load the test from a URL on the whitelist but without the whitelist
-        SpecialPowers.pushPrefEnv({"set": [["urlclassifier.trackingWhitelistTable", ""]]},
-          function() {
-            testOnWindow(contentPage1, function(aWindow2) {
-              checkLoads(aWindow2, false);
-              aWindow2.close();
-              SimpleTest.finish();
-            });
-          });
+  // Load the test from a URL that's NOT on the whitelist
+  await testOnWindow(contentPage2).then(function(aWindow) {
+    checkLoads(aWindow, false);
+    aWindow.close();
+  });
 
-      });
-    });
+  // Load the test from a URL on the whitelist but without the whitelist
+  await SpecialPowers.setCharPref("urlclassifier.trackingWhitelistTable", "");
+  await testOnWindow(contentPage1).then(function(aWindow) {
+    checkLoads(aWindow, false);
+    aWindow.close();
   });
+  await SpecialPowers.clearUserPref("urlclassifier.trackingWhitelistTable");
+
+  SimpleTest.finish();
 }
 
 SimpleTest.waitForExplicitFinish();
 
 </script>
 
 </pre>
 <iframe id="testFrame" width="100%" height="100%" onload=""></iframe>