Commit 9ed5385a authored by Emma Zuehlcke's avatar Emma Zuehlcke
Browse files

Bug 1787911 - Update and add tests for cookiebanners.service.mode.privateBrowsing. r=timhuang

parent 112f6ef0
Loading
Loading
Loading
Loading
+118 −2
Original line number Diff line number Diff line
@@ -220,8 +220,11 @@ add_setup(async function() {

  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("cookiebanners.service.mode");
    Services.prefs.clearUserPref("cookiebanners.service.mode.privateBrowsing");
    if (
      Services.prefs.getIntPref("cookiebanners.service.mode") !=
        Ci.nsICookieBannerService.MODE_DISABLED ||
      Services.prefs.getIntPref("cookiebanners.service.mode.privateBrowsing") !=
        Ci.nsICookieBannerService.MODE_DISABLED
    ) {
      // Restore original rules.
@@ -237,6 +240,10 @@ add_task(async function test_cookie_banner_service_disabled() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["cookiebanners.service.mode", Ci.nsICookieBannerService.MODE_DISABLED],
      [
        "cookiebanners.service.mode.privateBrowsing",
        Ci.nsICookieBannerService.MODE_DISABLED,
      ],
    ],
  });

@@ -384,6 +391,41 @@ add_task(async function test_embedded_iframe() {
add_task(async function test_pbm() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "cookiebanners.service.mode.privateBrowsing",
        Ci.nsICookieBannerService.MODE_REJECT,
      ],
    ],
  });

  insertTestRules();

  let pbmWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });

  await openPageAndVerify({
    win: pbmWindow,
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: false,
    expected: "OptOut",
  });

  await BrowserTestUtils.closeWindow(pbmWindow);
});

/**
 * Tests service mode pref combinations for normal and private browsing.
 */
add_task(async function test_pref_pbm_pref() {
  info("Enable in normal browsing but disable in private browsing.");
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "cookiebanners.service.mode.privateBrowsing",
        Ci.nsICookieBannerService.MODE_DISABLED,
      ],
      ["cookiebanners.service.mode", Ci.nsICookieBannerService.MODE_REJECT],
    ],
  });
@@ -394,6 +436,39 @@ add_task(async function test_pbm() {
    private: true,
  });

  await openPageAndVerify({
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: false,
    expected: "OptOut",
  });

  await openPageAndVerify({
    win: pbmWindow,
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: true,
    expected: "NoClick",
  });

  info("Disable in normal browsing but enable in private browsing.");
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "cookiebanners.service.mode.privateBrowsing",
        Ci.nsICookieBannerService.MODE_REJECT,
      ],
      ["cookiebanners.service.mode", Ci.nsICookieBannerService.MODE_DISABLED],
    ],
  });

  await openPageAndVerify({
    domain: TEST_DOMAIN_A,
    testURL: TEST_PAGE_A,
    visible: true,
    expected: "NoClick",
  });

  await openPageAndVerify({
    win: pbmWindow,
    domain: TEST_DOMAIN_A,
@@ -402,6 +477,44 @@ add_task(async function test_pbm() {
    expected: "OptOut",
  });

  info(
    "Set normal browsing to REJECT_OR_ACCEPT and private browsing to REJECT."
  );
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "cookiebanners.service.mode.privateBrowsing",
        Ci.nsICookieBannerService.MODE_REJECT,
      ],
      [
        "cookiebanners.service.mode",
        Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT,
      ],
    ],
  });

  info(
    "The normal browsing window accepts the banner according to the opt-in rule."
  );
  await openPageAndVerify({
    win: window,
    domain: TEST_DOMAIN_B,
    testURL: TEST_PAGE_B,
    visible: false,
    expected: "OptIn",
  });

  info(
    "The private browsing window should not perform any click, because there is only an opt-in rule."
  );
  await openPageAndVerify({
    win: pbmWindow,
    domain: TEST_DOMAIN_B,
    testURL: TEST_PAGE_B,
    visible: true,
    expected: "NoClick",
  });

  await BrowserTestUtils.closeWindow(pbmWindow);
});

@@ -411,7 +524,10 @@ add_task(async function test_pbm() {
add_task(async function test_embedded_iframe_pbm() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["cookiebanners.service.mode", Ci.nsICookieBannerService.MODE_REJECT],
      [
        "cookiebanners.service.mode.privateBrowsing",
        Ci.nsICookieBannerService.MODE_REJECT,
      ],
    ],
  });

+72 −9
Original line number Diff line number Diff line
@@ -10,8 +10,11 @@ function genUUID() {
add_setup(async function() {
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("cookiebanners.service.mode");
    Services.prefs.clearUserPref("cookiebanners.service.mode.privateBrowsing");
    if (
      Services.prefs.getIntPref("cookiebanners.service.mode") !=
        Ci.nsICookieBannerService.MODE_DISABLED ||
      Services.prefs.getIntPref("cookiebanners.service.mode.privateBrowsing") !=
        Ci.nsICookieBannerService.MODE_DISABLED
    ) {
      // Restore original rules.
@@ -26,6 +29,10 @@ add_task(async function test_enabled_pref() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["cookiebanners.service.mode", Ci.nsICookieBannerService.MODE_DISABLED],
      [
        "cookiebanners.service.mode.privateBrowsing",
        Ci.nsICookieBannerService.MODE_DISABLED,
      ],
    ],
  });

@@ -73,7 +80,8 @@ add_task(async function test_enabled_pref() {
  Assert.throws(
    () => {
      Services.cookieBanners.getCookiesForURI(
        Services.io.newURI("https://example.com")
        Services.io.newURI("https://example.com"),
        false
      );
    },
    /NS_ERROR_NOT_AVAILABLE/,
@@ -117,6 +125,54 @@ add_task(async function test_enabled_pref() {
  );
});

/**
 * Test both service mode pref combinations to ensure the cookie banner service
 * is (un-)initialized correctly.
 */
add_task(async function test_enabled_pref_pbm_combinations() {
  const MODES = [
    Ci.nsICookieBannerService.MODE_DISABLED,
    Ci.nsICookieBannerService.MODE_REJECT,
    Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT,
  ];

  // Test all pref combinations
  MODES.forEach(modeNormal => {
    MODES.forEach(modePrivate => {
      info(
        `cookiebanners.service.mode=${modeNormal}; cookiebanners.service.mode.privateBrowsing=${modePrivate}`
      );
      Services.prefs.setIntPref("cookiebanners.service.mode", modeNormal);
      Services.prefs.setIntPref(
        "cookiebanners.service.mode.privateBrowsing",
        modePrivate
      );

      if (
        modeNormal == Ci.nsICookieBannerService.MODE_DISABLED &&
        modePrivate == Ci.nsICookieBannerService.MODE_DISABLED
      ) {
        Assert.throws(
          () => {
            Services.cookieBanners.rules;
          },
          /NS_ERROR_NOT_AVAILABLE/,
          "Cookie banner service should be disabled. Should throw NS_ERROR_NOT_AVAILABLE for rules getter."
        );
      } else {
        ok(
          Services.cookieBanners.rules,
          "Cookie banner service should be enabled, rules getter should not throw."
        );
      }
    });
  });

  // Cleanup.
  Services.prefs.clearUserPref("cookiebanners.service.mode");
  Services.prefs.clearUserPref("cookiebanners.service.mode.privateBrowsing");
});

add_task(async function test_insertAndGetRule() {
  info("Enabling cookie banner service with MODE_REJECT");
  await SpecialPowers.pushPrefEnv({
@@ -259,7 +315,8 @@ add_task(async function test_insertAndGetRule() {

  info("Getting cookies by URI for example.com.");
  let ruleArray = Services.cookieBanners.getCookiesForURI(
    Services.io.newURI("http://example.com")
    Services.io.newURI("http://example.com"),
    false
  );
  ok(
    ruleArray && Array.isArray(ruleArray),
@@ -300,7 +357,8 @@ add_task(async function test_insertAndGetRule() {

  info("Getting cookies by URI for example.org.");
  let ruleArray2 = Services.cookieBanners.getCookiesForURI(
    Services.io.newURI("http://example.org")
    Services.io.newURI("http://example.org"),
    false
  );
  ok(
    ruleArray2 && Array.isArray(ruleArray2),
@@ -342,7 +400,8 @@ add_task(async function test_insertAndGetRule() {
  });

  ruleArray2 = Services.cookieBanners.getCookiesForURI(
    Services.io.newURI("http://example.org")
    Services.io.newURI("http://example.org"),
    false
  );
  ok(
    ruleArray2 && Array.isArray(ruleArray2),
@@ -356,7 +415,8 @@ add_task(async function test_insertAndGetRule() {

  info("Calling getCookiesForURI for unknown domain.");
  let ruleArrayUnknown = Services.cookieBanners.getCookiesForURI(
    Services.io.newURI("http://example.net")
    Services.io.newURI("http://example.net"),
    false
  );
  ok(
    ruleArrayUnknown && Array.isArray(ruleArrayUnknown),
@@ -622,7 +682,8 @@ add_task(async function test_globalRules() {

  is(
    Services.cookieBanners.getCookiesForURI(
      Services.io.newURI("http://example.com")
      Services.io.newURI("http://example.com"),
      false
    ).length,
    1,
    "There should be a cookie rule for example.com"
@@ -636,7 +697,8 @@ add_task(async function test_globalRules() {

  is(
    Services.cookieBanners.getCookiesForURI(
      Services.io.newURI("http://thishasnorule.com")
      Services.io.newURI("http://thishasnorule.com"),
      false
    ).length,
    0,
    "There should be no cookie rule for thishasnorule.com"
@@ -674,7 +736,8 @@ add_task(async function test_globalRules() {

  is(
    Services.cookieBanners.getCookiesForURI(
      Services.io.newURI("http://thishasnorule.com")
      Services.io.newURI("http://thishasnorule.com"),
      false
    ).length,
    0,
    "There should be no cookie rule for thishasnorule.com"
+8 −2
Original line number Diff line number Diff line
@@ -118,8 +118,11 @@ async function visitTestSites(urls = [ORIGIN_A, ORIGIN_B]) {
add_setup(async function() {
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("cookiebanners.service.mode");
    Services.prefs.clearUserPref("cookiebanners.service.mode.privateBrowsing");
    if (
      Services.prefs.getIntPref("cookiebanners.service.mode") !=
        Ci.nsICookieBannerService.MODE_DISABLED ||
      Services.prefs.getIntPref("cookiebanners.service.mode.privateBrowsing") !=
        Ci.nsICookieBannerService.MODE_DISABLED
    ) {
      // Restore original rules.
@@ -353,7 +356,10 @@ add_task(async function test_cookie_header_and_document() {
add_task(async function test_pbm() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["cookiebanners.service.mode", Ci.nsICookieBannerService.MODE_REJECT],
      [
        "cookiebanners.service.mode.privateBrowsing",
        Ci.nsICookieBannerService.MODE_REJECT,
      ],
      ["cookiebanners.cookieInjector.enabled", true],
    ],
  });