<!doctype html>
<meta charset=utf-8>
<title>Use Clear-Site-Data to immediately unregister service workers</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script src="resources/unregister-immediately-helpers.js"></script>
<body>
<script>
'use strict';

// These tests use the Clear-Site-Data network response header to immediately
// unregister a service worker registration with a worker whose state is
// 'installing' or 'parsed'.  Clear-Site-Data must delete the registration,
// abort the installation and then clear the registration by setting the
// worker's state to 'redundant'.

promise_test(async test => {
  // This test keeps the the service worker in the 'parsed' state by using a
  // script with an infinite loop.
  const script_url = 'resources/onparse-infiniteloop-worker.js';
  const scope_url =
    'resources/scope-for-unregister-immediately-with-parsed-worker';

  await service_worker_unregister(test, /*scope=*/script_url);

  // Clear-Site-Data must cause register() to fail.
  const register_promise = promise_rejects_dom(test, 'AbortError',
    navigator.serviceWorker.register(script_url, { scope: scope_url}));;

  await Promise.all([clear_site_data(), register_promise]);

  await assert_no_registrations_exist();
 }, 'Clear-Site-Data must abort service worker registration.');

promise_test(async test => {
  // This test keeps the the service worker in the 'installing' state by using a
  // script with an install event waitUntil() promise that never resolves.
  const script_url = 'resources/oninstall-waituntil-forever.js';
  const scope_url =
    'resources/scope-for-unregister-immediately-with-installing-worker';

  const registration = await service_worker_unregister_and_register(
    test, script_url, scope_url);
  const service_worker = registration.installing;

  // Clear-Site-Data must cause install to fail.
  await Promise.all([
    clear_site_data(),
    wait_for_state(test, service_worker, 'redundant')]);

  await assert_no_registrations_exist();
 }, 'Clear-Site-Data must unregister a registration with a worker '
     + 'in the "installing" state.');

</script>
</body>
