Commit 8ffdb93d authored by Francois Marier's avatar Francois Marier
Browse files

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

--HG--
extra : moz-landing-system : lando
parent b5aa77a9
Loading
Loading
Loading
Loading
+46 −40
Original line number Diff line number Diff line
@@ -23,8 +23,9 @@ 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});
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(() => {
@@ -37,12 +38,17 @@ function testOnWindow(aPrivate, aCallback) {

          win.content.addEventListener("load", function innerLoad2() {
            win.content.removeEventListener("load", innerLoad2);
          SimpleTest.executeSoon(function() { aCallback(win); });
            SimpleTest.executeSoon(function() {
              resolve(win);
            });
          }, false, true);
        }, true);
      SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
        SimpleTest.executeSoon(function() {
          win.gBrowser.loadURI(contentPage);
        });
      });
    }, {capture: true, once: true});
  });
}

var badids = [
@@ -102,29 +108,29 @@ SpecialPowers.pushPrefEnv(
           ["privacy.trackingprotection.pbmode.enabled", true]]},
  test);

function test() {
async function test() {
  SimpleTest.registerCleanupFunction(UrlClassifierTestUtils.cleanupTestTrackers);
  UrlClassifierTestUtils.addTestTrackers().then(() => {
  await UrlClassifierTestUtils.addTestTrackers();

  // Normal mode, with the pref (trackers should be loaded)
    testOnWindow(false, function(aWindow) {
  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();
  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();
        });
      });
    });
  await SpecialPowers.setBoolPref("privacy.trackingprotection.pbmode.enabled", false);
  await testOnWindow(true).then(function(aWindow) {
    checkLoads(aWindow, false);
    aWindow.close();
  });
  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
+47 −42
Original line number Diff line number Diff line
@@ -24,7 +24,8 @@ 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) {
function testOnWindow(contentPage) {
  return new Promise((resolve, reject) => {
    var win = mainWindow.OpenBrowserWindow();
    win.addEventListener("load", function() {
      TestUtils.topicObserved("browser-delayed-startup-finished",
@@ -38,12 +39,17 @@ function testOnWindow(contentPage, aCallback) {

          win.content.addEventListener("load", function innerLoad2() {
            win.content.removeEventListener("load", innerLoad2);
          SimpleTest.executeSoon(function() { aCallback(win); });
            SimpleTest.executeSoon(function() {
              resolve(win);
            });
          }, false, true);
        }, true);
      SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
        SimpleTest.executeSoon(function() {
          win.gBrowser.loadURI(contentPage);
        });
      });
    }, {capture: true, once: true});
  });
}

var alwaysbadids = [
@@ -99,32 +105,31 @@ SpecialPowers.pushPrefEnv(
           ["channelclassifier.allowlist_example", true]]},
  test);

function test() {
async function test() {
  SimpleTest.registerCleanupFunction(UrlClassifierTestUtils.cleanupTestTrackers);
  UrlClassifierTestUtils.addTestTrackers().then(() => {
  await UrlClassifierTestUtils.addTestTrackers();

  // Load the test from a URL on the whitelist
    testOnWindow(contentPage1, function(aWindow) {
  await testOnWindow(contentPage1).then(function(aWindow) {
    checkLoads(aWindow, true);
    aWindow.close();
  });

  // Load the test from a URL that's NOT on the whitelist
      testOnWindow(contentPage2, function(aWindow1) {
        checkLoads(aWindow1, false);
        aWindow1.close();
  await testOnWindow(contentPage2).then(function(aWindow) {
    checkLoads(aWindow, false);
    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();
            });
  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();