<!DOCTYPE html>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title>Clear-Site-Data: cache for bfcache</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="support/clear-cache-helper.sub.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="/common/utils.js"></script>
<script src="/html/browsers/browsing-the-web/back-forward-cache/resources/helper.sub.js"></script>
<script type="module">

const sameOrigin =
  'https://{{host}}:{{ports[https][0]}}';
const subdomainOrigin =
  'https://{{hosts[][www2]}}:{{ports[https][0]}}';
const crossSiteOrigin =
  'https://{{hosts[alt][]}}:{{ports[https][0]}}';

// The tests are built on top of the back-forward-cache test harness.
// Here is the steps for the tests:
// 1. Open a new window and navigate to a test URL.
// 2. Navigate the window to a second page.
// 3. Trigger the clear-site-data header either by window.open() or loading an
//    iframe from the second page.
// 4. Navigate back to the first page.
// 5. Assert that the first page is or is not cached.

function runBfCacheClearTest(params, description) {
  runBfcacheTest(
    {
      targetOrigin: sameOrigin,
      scripts: ["/clear-site-data/support/clear-cache-helper.sub.js"],
      funcBeforeBackNavigation: async (getUrlParams, mode) => {

        const cacheHelper = self.crypto.randomUUID();
        const testUrl = getUrl(cacheHelper, getUrlParams);

        let clearingPromise;
        if (mode === "window") {
          clearingPromise = new Promise(resolve => {
            window.addEventListener("message", resolve, {once: true});
            window.open(testUrl);
          });
        } else if (mode === "iframe") {
          clearingPromise = new Promise(resolve => {
            const iframe = document.createElement("iframe");
            iframe.src = testUrl;
            document.body.appendChild(iframe);
            iframe.onload = resolve;
          });
        } else {
          throw new Error("Unsupported mode");
        }

        await clearingPromise;
      },
      argsBeforeBackNavigation: [params.getUrlParams, params.mode],
      ...params,
    },
    description
  );
}

runBfCacheClearTest(
  {
    getUrlParams: {
      clear: "cache",
    },
    mode: "iframe",
    shouldBeCached: false,
  },
  "BfCached document shouldn't be cached after receiving clear-cache header from the same origin."
);

runBfCacheClearTest(
  {
    targetOrigin: subdomainOrigin,
    getUrlParams: {
      subdomain: true,
      clear: "cache",
    },
    mode: "iframe",
    shouldBeCached: true,
  },
  "BfCached document should be cached after receiving clear-cache header from a subdomain."
);

runBfCacheClearTest(
  {
    targetOrigin: crossSiteOrigin,
    getUrlParams: {
      secondOrigin: true,
      clear: "cache",
    },
    mode: "iframe",
    shouldBeCached: true,
  },
  "BfCached document should be cached after receiving clear-cache header from another site."
);

runBfCacheClearTest(
  {
    getUrlParams: {
      clear: "cache",
    },
    mode: "window",
    shouldBeCached: false,
  },
  "BfCached document shouldn't be cached after receiving clear-cache header from another window."
);

</script>
