add_task(async function () {
  info("Starting doubly nested tracker test");

  await SpecialPowers.flushPrefEnv();
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "network.cookie.cookieBehavior",
        Ci.nsICookieService.BEHAVIOR_PARTITION_FOREIGN,
      ],
      [
        "network.cookie.cookieBehavior.pbmode",
        Ci.nsICookieService.BEHAVIOR_PARTITION_FOREIGN,
      ],
      ["privacy.trackingprotection.enabled", false],
      ["privacy.trackingprotection.pbmode.enabled", false],
      ["privacy.trackingprotection.annotate_channels", true],
      [
        "privacy.restrict3rdpartystorage.userInteractionRequiredForHosts",
        "tracking.example.com,tracking.example.org",
      ],
      // Bug 1617611: Fix all the tests broken by "cookies SameSite=lax by default"
      ["network.cookie.sameSite.laxByDefault", false],
      ["network.lna.block_trackers", false],
    ],
  });

  await UrlClassifierTestUtils.addTestTrackers();

  let tab = BrowserTestUtils.addTab(gBrowser, TEST_3RD_PARTY_PAGE);
  gBrowser.selectedTab = tab;

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  async function loadSubpage() {
    async function runChecks() {
      is(document.cookie, "", "I don't have the cookies!");
      document.cookie = "name=partitioned; Secure; Partitioned";
      is(document.cookie, "name=partitioned", "I have partitioned cookies!");
    }

    await new Promise(resolve => {
      let ifr = document.createElement("iframe");
      ifr.onload = _ => {
        info("Sending code to the 3rd party content");
        ifr.contentWindow.postMessage(runChecks.toString(), "*");
      };

      addEventListener("message", function msg(event) {
        if (event.data.type == "finish") {
          removeEventListener("message", msg);
          resolve();
          return;
        }

        if (event.data.type == "ok") {
          ok(event.data.what, event.data.msg);
          return;
        }

        if (event.data.type == "info") {
          info(event.data.msg);
          return;
        }

        ok(false, "Unknown message");
      });

      ifr.src =
        "https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/3rdParty.html";
      document.body.appendChild(ifr);
    });
  }

  // We need to use the same scheme in Fission test.
  let testAnotherThirdPartyPage = SpecialPowers.useRemoteSubframes
    ? TEST_ANOTHER_3RD_PARTY_PAGE_HTTPS
    : TEST_ANOTHER_3RD_PARTY_PAGE;

  await SpecialPowers.spawn(
    browser,
    [{ page: testAnotherThirdPartyPage, callback: loadSubpage.toString() }],
    async function (obj) {
      await new content.Promise(resolve => {
        content.document.cookie = "name=value";
        let ifr = content.document.createElement("iframe");
        ifr.onload = _ => {
          info("Sending code to the 3rd party content");
          ifr.contentWindow.postMessage(obj.callback, "*");
        };

        content.addEventListener("message", function msg(event) {
          if (event.data.type == "finish") {
            content.removeEventListener("message", msg);
            resolve();
            return;
          }

          if (event.data.type == "ok") {
            ok(event.data.what, event.data.msg);
            return;
          }

          if (event.data.type == "info") {
            info(event.data.msg);
            return;
          }

          ok(false, "Unknown message");
        });

        ifr.src = obj.page;
        content.document.body.appendChild(ifr);
      });
    }
  );

  BrowserTestUtils.removeTab(tab);

  UrlClassifierTestUtils.cleanupTestTrackers();
});

add_task(async function () {
  info("Cleaning up.");
  SpecialPowers.clearUserPref("network.cookie.sameSite.laxByDefault");
  await new Promise(resolve => {
    Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
      resolve()
    );
  });
});
