WPT: service worker: Refactor and format navigation-redirect.https.html.

In preparation for extending or modifying the test to test Client.url
and resultingClientId for https://github.com/whatwg/html/pull/3891.

Bug: 876223
Change-Id: I50e8b8c21c5f8639a24a9c2ec00df3ccafdc8ac8
Reviewed-on: https://chromium-review.googlesource.com/1184656
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: Makoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585738}
diff --git a/service-workers/service-worker/navigation-redirect.https.html b/service-workers/service-worker/navigation-redirect.https.html
index ed300fa..0f1d377 100644
--- a/service-workers/service-worker/navigation-redirect.https.html
+++ b/service-workers/service-worker/navigation-redirect.https.html
@@ -7,62 +7,51 @@
 <script src="resources/test-helpers.sub.js"></script>
 <body>
 <script>
-var host_info = get_host_info();
+const host_info = get_host_info();
 
 // This test registers three Service Workers at SCOPE1, SCOPE2 and
 // OTHER_ORIGIN_SCOPE. And checks the redirected page's URL and the requests
 // which are intercepted by Service Worker while loading redirect page.
-var BASE_URL = host_info['HTTPS_ORIGIN'] + base_path();
-var OTHER_BASE_URL = host_info['HTTPS_REMOTE_ORIGIN'] + base_path();
+const BASE_URL = host_info['HTTPS_ORIGIN'] + base_path();
+const OTHER_BASE_URL = host_info['HTTPS_REMOTE_ORIGIN'] + base_path();
 
-var SCOPE1 = BASE_URL + 'resources/navigation-redirect-scope1.py?';
-var SCOPE2 = BASE_URL + 'resources/navigation-redirect-scope2.py?';
-var OUT_SCOPE = BASE_URL + 'resources/navigation-redirect-out-scope.py?';
-var SCRIPT = 'resources/redirect-worker.js';
+const SCOPE1 = BASE_URL + 'resources/navigation-redirect-scope1.py?';
+const SCOPE2 = BASE_URL + 'resources/navigation-redirect-scope2.py?';
+const OUT_SCOPE = BASE_URL + 'resources/navigation-redirect-out-scope.py?';
+const SCRIPT = 'resources/redirect-worker.js';
 
-var OTHER_ORIGIN_IFRAME_URL =
-    OTHER_BASE_URL + 'resources/navigation-redirect-other-origin.html';
-var OTHER_ORIGIN_SCOPE =
-    OTHER_BASE_URL + 'resources/navigation-redirect-scope1.py?';
-var OTHER_ORIGIN_OUT_SCOPE =
-    OTHER_BASE_URL + 'resources/navigation-redirect-out-scope.py?';
+const OTHER_ORIGIN_IFRAME_URL =
+      OTHER_BASE_URL + 'resources/navigation-redirect-other-origin.html';
+const OTHER_ORIGIN_SCOPE =
+      OTHER_BASE_URL + 'resources/navigation-redirect-scope1.py?';
+const OTHER_ORIGIN_OUT_SCOPE =
+      OTHER_BASE_URL + 'resources/navigation-redirect-out-scope.py?';
 
-var workers;
-var other_origin_frame;
-var setup_environment_promise;
-var message_resolvers = {};
-var next_message_id = 0;
+let registrations;
+let workers;
+let other_origin_frame;
+let message_resolvers = {};
+let next_message_id = 0;
 
-function setup_environment(t) {
-  if (setup_environment_promise)
-    return setup_environment_promise;
-  setup_environment_promise =
-      with_iframe(OTHER_ORIGIN_IFRAME_URL)
-        .then(function(f) {
-            // In this frame we register a Service Worker at OTHER_ORIGIN_SCOPE.
-            // And will use this frame to communicate with the worker.
-            other_origin_frame = f;
-            return Promise.all(
-                [service_worker_unregister_and_register(t, SCRIPT, SCOPE1),
-                 service_worker_unregister_and_register(t, SCRIPT, SCOPE2)]);
-          })
-        .then(function(registrations) {
-            add_completion_callback(function() {
-                registrations[0].unregister();
-                registrations[1].unregister();
-                send_to_iframe(other_origin_frame, 'unregister')
-                  .then(function() { other_origin_frame.remove(); });
-              });
-            workers = registrations.map(get_effective_worker);
-            return Promise.all([
-                wait_for_state(t, workers[0], 'activated'),
-                wait_for_state(t, workers[1], 'activated'),
-                // This promise will resolve when |wait_for_worker_promise|
-                // in OTHER_ORIGIN_IFRAME_URL resolves.
-                send_to_iframe(other_origin_frame, 'wait_for_worker')]);
-          });
-  return setup_environment_promise;
-}
+promise_test(async t  => {
+  // In this frame we register a service worker at OTHER_ORIGIN_SCOPE.
+  // And will use this frame to communicate with the worker.
+  other_origin_frame = await with_iframe(OTHER_ORIGIN_IFRAME_URL);
+
+  // Register same-origin service workers.
+  registrations = await Promise.all([
+      service_worker_unregister_and_register(t, SCRIPT, SCOPE1),
+      service_worker_unregister_and_register(t, SCRIPT, SCOPE2)]);
+
+  // Wait for all workers to activate.
+  workers = registrations.map(get_effective_worker);
+  return Promise.all([
+      wait_for_state(t, workers[0], 'activated'),
+      wait_for_state(t, workers[1], 'activated'),
+      // This promise will resolve when |wait_for_worker_promise|
+      // in OTHER_ORIGIN_IFRAME_URL resolves.
+      send_to_iframe(other_origin_frame, 'wait_for_worker')]);
+}, 'initialize global state');
 
 function get_effective_worker(registration) {
   if (registration.active)
@@ -73,45 +62,43 @@
     return registration.installing;
 }
 
-function check_all_intercepted_urls(expected_urls) {
-  var urls = [];
-  return get_intercepted_urls(workers[0])
-    .then(function(url) {
-      urls.push(url);
-      return get_intercepted_urls(workers[1]);
-    }).then(function(url) {
-      urls.push(url);
-      // Gets the request URLs which are intercepted by OTHER_ORIGIN_SCOPE's
-      // SW. This promise will resolve when get_intercepted_urls() in
-      // OTHER_ORIGIN_IFRAME_URL resolves.
-      return send_to_iframe(other_origin_frame, 'get_intercepted_urls');
-    }).then(function(url) {
-      urls.push(url);
-      return urls;
-    }).then(function(urls) {
-        assert_object_equals(
-            urls, expected_urls,
-            'Intercepted URLs should match.');
-      });
+async function check_all_intercepted_urls(expected_urls) {
+  const urls = [];
+  urls.push(await get_intercepted_urls(workers[0]));
+  urls.push(await get_intercepted_urls(workers[1]));
+  // Gets the request URLs which are intercepted by OTHER_ORIGIN_SCOPE's
+  // SW. This promise will resolve when get_intercepted_urls() in
+  // OTHER_ORIGIN_IFRAME_URL resolves.
+  urls.push(await send_to_iframe(other_origin_frame, 'get_intercepted_urls'));
+  assert_object_equals(urls, expected_urls, 'Intercepted URLs should match.');
 }
 
-function test_redirect(url, expected_last_url,
-                       expected_intercepted_urls) {
-  var message_promise = new Promise(function(resolve) {
-      // A message which ID is 'last_url' will be sent from the iframe.
-      message_resolvers['last_url'] = resolve;
+// Creates an iframe and navigates to |url|, which is expected to start a chain
+// of redirects.
+// - |expected_last_url| is the expected window.location after the
+//   navigation.
+// - |expected_intercepted_urls| is the expected URLs that the service
+//   workers were dispatched fetch events for. The format is:
+//   [
+//     [urls from workers[0]],
+//     [urls from workers[1]],
+//     [urls from cross-origin worker]
+//   ]
+function redirect_test(url,
+                       expected_last_url,
+                       expected_intercepted_urls,
+                       test_name) {
+  promise_test(async t => {
+    const message_promise = new Promise(resolve => {
+        // A message with ID 'last_url' will be sent from the iframe.
+        message_resolvers['last_url'] = resolve;
     });
-  return with_iframe(url)
-    .then(function(f) {
-        f.remove();
-        return check_all_intercepted_urls(expected_intercepted_urls);
-      })
-    .then(function() { return message_promise; })
-    .then(function(last_url) {
-        assert_equals(
-            last_url, expected_last_url,
-            'Last URL should match.');
-      });
+    const frame = await with_iframe(url);
+    frame.remove();
+    await check_all_intercepted_urls(expected_intercepted_urls);
+    const last_url = await message_promise;
+    assert_equals(last_url, expected_last_url, 'Last URL should match.');
+  }, test_name);
 }
 
 window.addEventListener('message', on_message, false);
@@ -129,400 +116,310 @@
 
 function send_to_iframe(frame, message) {
   var message_id = next_message_id++;
-  return new Promise(function(resolve) {
-      message_resolvers[message_id] = resolve;
-      frame.contentWindow.postMessage(
-          {id: message_id, message: message},
-          host_info['HTTPS_REMOTE_ORIGIN']);
-    });
+  return new Promise(resolve => {
+    message_resolvers[message_id] = resolve;
+    frame.contentWindow.postMessage(
+        {id: message_id, message: message},
+        host_info['HTTPS_REMOTE_ORIGIN']);
+  });
 }
 
 function get_intercepted_urls(worker) {
-  return new Promise(function(resolve) {
-      var channel = new MessageChannel();
-      channel.port1.onmessage = function(msg) { resolve(msg.data.urls); };
-      worker.postMessage({port: channel.port2}, [channel.port2]);
-    });
+  return new Promise(resolve => {
+    var channel = new MessageChannel();
+    channel.port1.onmessage = function(msg) { resolve(msg.data.urls); };
+    worker.postMessage({port: channel.port2}, [channel.port2]);
+  });
 }
 
-// Normal redirect.
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            OUT_SCOPE + 'url=' + encodeURIComponent(SCOPE1),
-            SCOPE1,
-            [[SCOPE1], [], []]);
-      });
-  }, 'Normal redirect to same-origin scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            OUT_SCOPE + 'url=' + encodeURIComponent(SCOPE1) + '#ref',
-            SCOPE1 + '#ref',
-            [[SCOPE1 + '#ref'], [], []]);
-      });
-  }, 'Normal redirect to same-origin scope with a hash fragment.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            OUT_SCOPE + 'url=' + encodeURIComponent(SCOPE1 + '#ref2') + '#ref',
-            SCOPE1 + '#ref2',
-            [[SCOPE1 + '#ref2'], [], []]);
-      });
-  }, 'Normal redirect to same-origin scope with different hash fragments.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            OUT_SCOPE + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
-            OTHER_ORIGIN_SCOPE,
-            [[], [], [OTHER_ORIGIN_SCOPE]]);
-      });
-  }, 'Normal redirect to other-origin scope.');
+// Normal redirect (from out-scope to in-scope).
+redirect_test(
+    OUT_SCOPE + 'url=' + encodeURIComponent(SCOPE1),
+    SCOPE1,
+    [[SCOPE1], [], []],
+    'Normal redirect to same-origin scope.');
+redirect_test(
+    OUT_SCOPE + 'url=' + encodeURIComponent(SCOPE1) + '#ref',
+    SCOPE1 + '#ref',
+    [[SCOPE1 + '#ref'], [], []],
+    'Normal redirect to same-origin scope with a hash fragment.');
+redirect_test(
+    OUT_SCOPE + 'url=' + encodeURIComponent(SCOPE1 + '#ref2') + '#ref',
+    SCOPE1 + '#ref2',
+    [[SCOPE1 + '#ref2'], [], []],
+    'Normal redirect to same-origin scope with different hash fragments.');
+redirect_test(
+    OUT_SCOPE + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
+    OTHER_ORIGIN_SCOPE,
+    [[], [], [OTHER_ORIGIN_SCOPE]],
+    'Normal redirect to other-origin scope.');
 
 // SW fallbacked redirect. SW doesn't handle the fetch request.
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'url=' + encodeURIComponent(OUT_SCOPE),
-            OUT_SCOPE,
-            [[SCOPE1 + 'url=' + encodeURIComponent(OUT_SCOPE)], [], []]);
-      });
-  }, 'SW-fallbacked redirect to same-origin out-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'url=' + encodeURIComponent(SCOPE1),
-            SCOPE1,
-            [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE1), SCOPE1], [], []]);
-      });
-  }, 'SW-fallbacked redirect to same-origin same-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'url=' + encodeURIComponent(SCOPE1) + '#ref',
-            SCOPE1 + '#ref',
-            [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE1) + '#ref',
-              SCOPE1 + '#ref'],
-             [], []]);
-      });
-  }, 'SW-fallbacked redirect to same-origin same-scope with a hash fragment.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'url=' + encodeURIComponent(SCOPE1 + '#ref2') + '#ref',
-            SCOPE1 + '#ref2',
-            [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE1 + '#ref2') + '#ref',
-              SCOPE1 + '#ref2'],
-             [], []]);
-      });
-  }, 'SW-fallbacked redirect to same-origin same-scope with different hash ' +
-     'fragments.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'url=' + encodeURIComponent(SCOPE2),
-            SCOPE2,
-            [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE2)], [SCOPE2], []]);
-      });
-  }, 'SW-fallbacked redirect to same-origin other-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
-            OTHER_ORIGIN_OUT_SCOPE,
-            [[SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)],
-             [],
-             []]);
-      });
-  }, 'SW-fallbacked redirect to other-origin out-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
-            OTHER_ORIGIN_SCOPE,
-            [[SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE)],
-             [],
-             [OTHER_ORIGIN_SCOPE]]);
-      });
-  }, 'SW-fallbacked redirect to other-origin in-scope.');
+redirect_test(
+    SCOPE1 + 'url=' + encodeURIComponent(OUT_SCOPE),
+    OUT_SCOPE,
+    [[SCOPE1 + 'url=' + encodeURIComponent(OUT_SCOPE)], [], []],
+    'SW-fallbacked redirect to same-origin out-scope.');
+redirect_test(
+    SCOPE1 + 'url=' + encodeURIComponent(SCOPE1),
+    SCOPE1,
+    [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE1), SCOPE1], [], []],
+    'SW-fallbacked redirect to same-origin same-scope.');
+redirect_test(
+    SCOPE1 + 'url=' + encodeURIComponent(SCOPE1) + '#ref',
+    SCOPE1 + '#ref',
+    [
+      [SCOPE1 + 'url=' + encodeURIComponent(SCOPE1) + '#ref', SCOPE1 + '#ref'],
+      [],
+      []
+    ],
+    'SW-fallbacked redirect to same-origin same-scope with a hash fragment.');
+redirect_test(
+    SCOPE1 + 'url=' + encodeURIComponent(SCOPE1 + '#ref2') + '#ref',
+    SCOPE1 + '#ref2',
+    [
+      [
+        SCOPE1 + 'url=' + encodeURIComponent(SCOPE1 + '#ref2') + '#ref',
+        SCOPE1 + '#ref2'
+      ],
+      [],
+      []
+    ],
+    'SW-fallbacked redirect to same-origin same-scope with different hash ' +
+    'fragments.');
+redirect_test(
+    SCOPE1 + 'url=' + encodeURIComponent(SCOPE2),
+    SCOPE2,
+    [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE2)], [SCOPE2], []],
+    'SW-fallbacked redirect to same-origin other-scope.');
+redirect_test(
+    SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
+    OTHER_ORIGIN_OUT_SCOPE,
+    [[SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)], [], []],
+    'SW-fallbacked redirect to other-origin out-scope.');
+redirect_test(
+    SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
+    OTHER_ORIGIN_SCOPE,
+    [
+      [SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE)],
+      [],
+      [OTHER_ORIGIN_SCOPE]
+    ],
+    'SW-fallbacked redirect to other-origin in-scope.');
 
 // SW generated redirect.
 // SW: event.respondWith(Response.redirect(params['url']));
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE),
-            OUT_SCOPE,
-            [[SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE)], [], []]);
-      });
-  }, 'SW-generated redirect to same-origin out-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE) + '#ref',
-            OUT_SCOPE + '#ref',
-            [[SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE) + '#ref'],
-              [], []]);
-      });
-  }, 'SW-generated redirect to same-origin out-scope with a hash fragment.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE + '#ref2') +
-            '#ref',
-            OUT_SCOPE + '#ref2',
-            [[SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE + '#ref2') +
-              '#ref'],
-             [], []]);
-      });
-  }, 'SW-generated redirect to same-origin out-scope with different hash' +
-     'fragments.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE1),
-            SCOPE1,
-            [[SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE1), SCOPE1],
-             [],
-             []]);
-      });
-  }, 'SW-generated redirect to same-origin same-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE2),
-            SCOPE2,
-            [[SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE2)],
-             [SCOPE2],
-             []]);
-      });
-  }, 'SW-generated redirect to same-origin other-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
-            OTHER_ORIGIN_OUT_SCOPE,
-            [[SCOPE1 + 'sw=gen&url=' +
-              encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)],
-             [],
-             []]);
-      });
-  }, 'SW-generated redirect to other-origin out-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
-            OTHER_ORIGIN_SCOPE,
-            [[SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE)],
-             [],
-             [OTHER_ORIGIN_SCOPE]]);
-      });
-  }, 'SW-generated redirect to other-origin in-scope.');
+redirect_test(
+    SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE),
+    OUT_SCOPE,
+    [[SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE)], [], []],
+    'SW-generated redirect to same-origin out-scope.');
+redirect_test(
+    SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE) + '#ref',
+    OUT_SCOPE + '#ref',
+    [
+      [SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE) + '#ref'],
+      [],
+      []
+    ],
+    'SW-generated redirect to same-origin out-scope with a hash fragment.');
+redirect_test(
+    SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE + '#ref2') + '#ref',
+    OUT_SCOPE + '#ref2',
+    [
+      [SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE + '#ref2') + '#ref'],
+      [],
+      []
+    ],
+    'SW-generated redirect to same-origin out-scope with different hash ' +
+    'fragments.');
+redirect_test(
+    SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE1),
+    SCOPE1,
+    [[SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE1), SCOPE1], [], []],
+    'SW-generated redirect to same-origin same-scope.');
+redirect_test(
+    SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE2),
+    SCOPE2,
+    [[SCOPE1 + 'sw=gen&url=' + encodeURIComponent(SCOPE2)], [SCOPE2], []],
+    'SW-generated redirect to same-origin other-scope.');
+redirect_test(
+    SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
+    OTHER_ORIGIN_OUT_SCOPE,
+    [
+      [SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)],
+      [],
+      []
+    ],
+    'SW-generated redirect to other-origin out-scope.');
+redirect_test(
+    SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
+    OTHER_ORIGIN_SCOPE,
+    [
+      [SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE)],
+      [],
+      [OTHER_ORIGIN_SCOPE]
+    ],
+    'SW-generated redirect to other-origin in-scope.');
 
 // SW fetched redirect.
 // SW: event.respondWith(fetch(event.request));
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OUT_SCOPE),
-            OUT_SCOPE,
-            [[SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OUT_SCOPE)],
-             [],
-             []]);
-      });
-  }, 'SW-fetched redirect to same-origin out-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE1),
-            SCOPE1,
-            [[SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE1), SCOPE1],
-             [],
-             []]);
-      });
-  }, 'SW-fetched redirect to same-origin same-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE2),
-            SCOPE2,
-            [[SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE2)],
-             [SCOPE2],
-             []]);
-      });
-  }, 'SW-fetched redirect to same-origin other-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=fetch&url=' +
-            encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
-            OTHER_ORIGIN_OUT_SCOPE,
-            [[SCOPE1 + 'sw=fetch&url=' +
-              encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)],
-             [],
-             []]);
-      });
-  }, 'SW-fetched redirect to other-origin out-scope.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
-            OTHER_ORIGIN_SCOPE,
-            [[SCOPE1 + 'sw=fetch&url=' +
-              encodeURIComponent(OTHER_ORIGIN_SCOPE)],
-             [],
-             [OTHER_ORIGIN_SCOPE]]);
-      });
-  }, 'SW-fetched redirect to other-origin in-scope.');
+redirect_test(
+    SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OUT_SCOPE),
+    OUT_SCOPE,
+    [[SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OUT_SCOPE)],
+    [],
+    []],
+    'SW-fetched redirect to same-origin out-scope.');
+redirect_test(
+    SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE1),
+    SCOPE1,
+    [[SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE1), SCOPE1], [], []],
+    'SW-fetched redirect to same-origin same-scope.');
+redirect_test(
+    SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE2),
+    SCOPE2,
+    [
+      [SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(SCOPE2)],
+      [SCOPE2],
+      []
+    ],
+    'SW-fetched redirect to same-origin other-scope.');
+redirect_test(
+    SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
+    OTHER_ORIGIN_OUT_SCOPE,
+    [
+      [SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)],
+      [],
+      []
+    ], 'SW-fetched redirect to other-origin out-scope.');
+redirect_test(
+    SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
+    OTHER_ORIGIN_SCOPE,
+    [
+      [SCOPE1 + 'sw=fetch&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE)],
+      [],
+      [OTHER_ORIGIN_SCOPE]
+    ],
+    'SW-fetched redirect to other-origin in-scope.');
 
 // Opaque redirect.
 // SW: event.respondWith(fetch(
 //         new Request(event.request.url, {redirect: 'manual'})));
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OUT_SCOPE),
-            OUT_SCOPE,
-            [[SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OUT_SCOPE)],
-             [],
-             []]);
-      });
-  }, 'Redirect to same-origin out-scope with opaque redirect response.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE1),
-            SCOPE1,
-            [[SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE1), SCOPE1],
-             [],
-             []]);
-      });
-  }, 'Redirect to same-origin same-scope with opaque redirect response.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE2),
-            SCOPE2,
-            [[SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE2)],
-             [SCOPE2],
-             []]);
-      });
-  }, 'Redirect to same-origin other-scope with opaque redirect response.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manual&url=' +
-            encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
-            OTHER_ORIGIN_OUT_SCOPE,
-            [[SCOPE1 + 'sw=manual&url=' +
-              encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)],
-             [],
-             []]);
-      });
-  }, 'Redirect to other-origin out-scope with opaque redirect response.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
-            OTHER_ORIGIN_SCOPE,
-            [[SCOPE1 + 'sw=manual&url=' +
-              encodeURIComponent(OTHER_ORIGIN_SCOPE)],
-             [],
-             [OTHER_ORIGIN_SCOPE]]);
-      });
-  }, 'Redirect to other-origin in-scope with opaque redirect response.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manual&noLocationRedirect',
-            SCOPE1 + 'sw=manual&noLocationRedirect',
-            [[SCOPE1 + 'sw=manual&noLocationRedirect'],
-             [],
-             []]);
-      });
-  }, 'No location redirect response.');
+redirect_test(
+    SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OUT_SCOPE),
+    OUT_SCOPE,
+    [[SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OUT_SCOPE)], [], []],
+    'Redirect to same-origin out-scope with opaque redirect response.');
+redirect_test(
+    SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE1),
+    SCOPE1,
+    [[SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE1), SCOPE1], [], []],
+    'Redirect to same-origin same-scope with opaque redirect response.');
+redirect_test(
+    SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE2),
+    SCOPE2,
+    [[SCOPE1 + 'sw=manual&url=' + encodeURIComponent(SCOPE2)], [SCOPE2], []],
+    'Redirect to same-origin other-scope with opaque redirect response.');
+redirect_test(
+    SCOPE1 + 'sw=manual&url=' +
+    encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
+    OTHER_ORIGIN_OUT_SCOPE,
+    [
+      [SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)],
+      [],
+      []
+    ],
+    'Redirect to other-origin out-scope with opaque redirect response.');
+redirect_test(
+    SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE),
+    OTHER_ORIGIN_SCOPE,
+    [
+      [SCOPE1 + 'sw=manual&url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE)],
+      [],
+      [OTHER_ORIGIN_SCOPE]
+    ],
+    'Redirect to other-origin in-scope with opaque redirect response.');
+redirect_test(
+    SCOPE1 + 'sw=manual&noLocationRedirect',
+    SCOPE1 + 'sw=manual&noLocationRedirect',
+    [[SCOPE1 + 'sw=manual&noLocationRedirect'], [], []],
+    'No location redirect response.');
 
 // Opaque redirect passed through Cache.
 // SW responds with an opaque redirectresponse from the Cache API.
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manualThroughCache&url=' +
-            encodeURIComponent(OUT_SCOPE),
-            OUT_SCOPE,
-            [[SCOPE1 + 'sw=manualThroughCache&url=' +
-              encodeURIComponent(OUT_SCOPE)],
-             [],
-             []]);
-      });
-  },
-  'Redirect to same-origin out-scope with opaque redirect response which ' +
-  'is passed through Cache.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manualThroughCache&url=' +
-            encodeURIComponent(SCOPE1),
-            SCOPE1,
-            [[SCOPE1 + 'sw=manualThroughCache&url=' +
-              encodeURIComponent(SCOPE1), SCOPE1],
-             [],
-             []]);
-      });
-  },
-  'Redirect to same-origin same-scope with opaque redirect response which ' +
-  'is passed through Cache.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manualThroughCache&url=' +
-            encodeURIComponent(SCOPE2),
-            SCOPE2,
-            [[SCOPE1 + 'sw=manualThroughCache&url=' +
-              encodeURIComponent(SCOPE2)],
-             [SCOPE2],
-             []]);
-      });
-  },
-  'Redirect to same-origin other-scope with opaque redirect response which ' +
-  'is passed through Cache.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manualThroughCache&url=' +
-            encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
-            OTHER_ORIGIN_OUT_SCOPE,
-            [[SCOPE1 + 'sw=manualThroughCache&url=' +
-              encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)],
-             [],
-             []]);
-      });
-  },
-  'Redirect to other-origin out-scope with opaque redirect response which ' +
-  'is passed through Cache.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manualThroughCache&url=' +
-            encodeURIComponent(OTHER_ORIGIN_SCOPE),
-            OTHER_ORIGIN_SCOPE,
-            [[SCOPE1 + 'sw=manualThroughCache&url=' +
-              encodeURIComponent(OTHER_ORIGIN_SCOPE)],
-             [],
-             [OTHER_ORIGIN_SCOPE]]);
-      });
-  },
-  'Redirect to other-origin in-scope with opaque redirect response which ' +
-  'is passed through Cache.');
-promise_test(function(t) {
-    return setup_environment(t).then(function() {
-        return test_redirect(
-            SCOPE1 + 'sw=manualThroughCache&noLocationRedirect',
-            SCOPE1 + 'sw=manualThroughCache&noLocationRedirect',
-            [[SCOPE1 + 'sw=manualThroughCache&noLocationRedirect'],
-             [],
-             []]);
-      });
-  }, 'No location redirect response via Cache.');
+redirect_test(
+    SCOPE1 + 'sw=manualThroughCache&url=' + encodeURIComponent(OUT_SCOPE),
+    OUT_SCOPE,
+    [
+      [SCOPE1 + 'sw=manualThroughCache&url=' + encodeURIComponent(OUT_SCOPE)],
+      [],
+      []
+    ],
+    'Redirect to same-origin out-scope with opaque redirect response which ' +
+    'is passed through Cache.');
+redirect_test(
+    SCOPE1 + 'sw=manualThroughCache&url=' + encodeURIComponent(SCOPE1),
+    SCOPE1,
+    [
+      [
+        SCOPE1 + 'sw=manualThroughCache&url=' + encodeURIComponent(SCOPE1),
+        SCOPE1
+      ],
+      [],
+      []
+    ],
+    'Redirect to same-origin same-scope with opaque redirect response which ' +
+    'is passed through Cache.');
+redirect_test(
+    SCOPE1 + 'sw=manualThroughCache&url=' + encodeURIComponent(SCOPE2),
+    SCOPE2,
+    [
+      [SCOPE1 + 'sw=manualThroughCache&url=' + encodeURIComponent(SCOPE2)],
+      [SCOPE2],
+      []
+    ],
+    'Redirect to same-origin other-scope with opaque redirect response which ' +
+    'is passed through Cache.');
+redirect_test(
+    SCOPE1 + 'sw=manualThroughCache&url=' +
+        encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE),
+    OTHER_ORIGIN_OUT_SCOPE,
+    [
+      [SCOPE1 + 'sw=manualThroughCache&url=' +
+          encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)
+      ],
+      [],
+      []
+    ],
+    'Redirect to other-origin out-scope with opaque redirect response which ' +
+    'is passed through Cache.');
+redirect_test(
+    SCOPE1 + 'sw=manualThroughCache&url=' +
+        encodeURIComponent(OTHER_ORIGIN_SCOPE),
+    OTHER_ORIGIN_SCOPE,
+    [
+      [SCOPE1 + 'sw=manualThroughCache&url=' +
+          encodeURIComponent(OTHER_ORIGIN_SCOPE)],
+       [],
+       [OTHER_ORIGIN_SCOPE],
+    ],
+    'Redirect to other-origin in-scope with opaque redirect response which ' +
+    'is passed through Cache.');
+redirect_test(
+    SCOPE1 + 'sw=manualThroughCache&noLocationRedirect',
+    SCOPE1 + 'sw=manualThroughCache&noLocationRedirect',
+    [[SCOPE1 + 'sw=manualThroughCache&noLocationRedirect'], [], []],
+    'No location redirect response via Cache.');
+
+// Clean up the test environment. This promise_test() needs to be the last one.
+promise_test(async t => {
+  registrations.forEach(async registration => {
+    if (registration)
+      await registration.unregister();
+  });
+  await send_to_iframe(other_origin_frame, 'unregister');
+  other_origin_frame.remove();
+}, 'clean up global state');
 </script>
 </body>