<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
  <title>Test CacheStorage does not prevent worker GC</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="driver.js"></script>
</head>
<body>
<iframe id="frame"></iframe>
<script class="testbody" type="text/javascript">
function startWorker() {
  return new Promise(resolve => {
    var w = new Worker("idle_worker.js");
    w.addEventListener("message", function onMessage(evt) {
      if (evt.data === "LOADED") {
        w.removeEventListener("message", onMessage);
        resolve(w);
      }
    });
  });
}

function gc() {
  return new Promise(resolve => {
    SpecialPowers.exactGC(resolve);
  });
}

SimpleTest.waitForExplicitFinish();

async function test() {
  let w = await startWorker();
  let weakWorker = SpecialPowers.Cu.getWeakReference(w);
  ok(weakWorker, "worker supports weak reference");
  w = null;
  await gc();
  await gc();
  ok(!weakWorker.get(), "worker weak reference should be garbage collected");
  SimpleTest.finish();
}
// Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
// Acquire storage access permission here so that the Cache API is avaialable
SpecialPowers.wrap(document).notifyUserGestureActivation();
SpecialPowers.addPermission("storageAccessAPI", true, window.location.href).then(() => {
  SpecialPowers.wrap(document).requestStorageAccess().then(() => {
    SpecialPowers.pushPrefEnv({
      set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
    }).then(() => {
      test();
    });
  });
});
</script>
</body>
</html>
