Merge pull request #8934 from yoavweiss/fetch_destination_even_moar_values

Fetch destination - worker and document tests
diff --git a/fetch/api/request/destination/fetch-destination-iframe.https.html b/fetch/api/request/destination/fetch-destination-iframe.https.html
new file mode 100644
index 0000000..cb1e9d8
--- /dev/null
+++ b/fetch/api/request/destination/fetch-destination-iframe.https.html
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<title>Fetch destination tests for resources with no load event</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/common/get-host-info.sub.js"></script>
+<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
+<script>
+let frame;
+const kScope = 'resources/dummy.html?dest=document';
+
+// Set up the service worker and the frame.
+promise_test(t => {
+    const kScript = 'resources/fetch-destination-worker-iframe.js';
+    return service_worker_unregister_and_register(t, kScript, kScope)
+      .then(registration => {
+          add_completion_callback(() => {
+              registration.unregister();
+            });
+
+          return wait_for_state(t, registration.installing, 'activated');
+        });
+  }, 'Initialize global state');
+
+var waitOnMessageFromSW = async t => {
+    await new Promise((resolve, reject) => {
+        navigator.serviceWorker.onmessage = t.step_func(event => {
+            if (event.data == "PASS") {
+                resolve();
+            } else {
+                reject();
+            }
+        });
+    }).catch(() => {;
+        assert_unreached("Wrong destination.");
+    });
+    t.add_cleanup(() => { frame.contentWindow.navigator.serviceWorker.onmessage = null; });
+}
+
+// Document destination
+///////////////////////
+promise_test(async t => {
+    var f = document.createElement('iframe');
+    frame = f;
+    f.className = 'test-iframe';
+    f.src = kScope;
+    document.body.appendChild(f);
+    await waitOnMessageFromSW(t);
+    add_completion_callback(() => { f.remove(); });
+}, 'iframe fetches with a "document" Request.destination');
+
+</script>
diff --git a/fetch/api/request/destination/fetch-destination-worker.https.html b/fetch/api/request/destination/fetch-destination-worker.https.html
new file mode 100644
index 0000000..8421d4a
--- /dev/null
+++ b/fetch/api/request/destination/fetch-destination-worker.https.html
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<title>Fetch destination tests for resources with no load event</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/common/get-host-info.sub.js"></script>
+<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
+<script>
+let frame;
+
+// Set up the service worker and the frame.
+promise_test(t => {
+    const kScope = 'resources/dummy.html';
+    const kScript = 'resources/fetch-destination-worker-no-load-event.js';
+    return service_worker_unregister_and_register(t, kScript, kScope)
+      .then(registration => {
+          add_completion_callback(() => {
+              registration.unregister();
+            });
+
+          return wait_for_state(t, registration.installing, 'activated');
+        })
+      .then(() => {
+          return with_iframe(kScope);
+        })
+      .then(f => {
+          frame = f;
+          add_completion_callback(() => { f.remove(); });
+        });
+  }, 'Initialize global state');
+
+var waitOnMessageFromSW = async t => {
+    await new Promise((resolve, reject) => {
+        frame.contentWindow.navigator.serviceWorker.onmessage = t.step_func(event => {
+            if (event.data == "PASS") {
+                resolve();
+            } else {
+                reject();
+            }
+        });
+    }).catch(() => {;
+        assert_unreached("Wrong destination.");
+    });
+    t.add_cleanup(() => { frame.contentWindow.navigator.serviceWorker.onmessage = null; });
+}
+
+// worker destination
+/////////////////////
+promise_test(async t => {
+    // We can use an html file as we don't really care about the worker successfully loading.
+    let worker = new frame.contentWindow.Worker("dummy.html?t=worker&dest=worker");
+    await waitOnMessageFromSW(t);
+}, 'Worker fetches with a "worker" Request.destination');
+
+</script>
diff --git a/fetch/api/request/destination/resources/fetch-destination-worker-iframe.js b/fetch/api/request/destination/resources/fetch-destination-worker-iframe.js
new file mode 100644
index 0000000..7634583
--- /dev/null
+++ b/fetch/api/request/destination/resources/fetch-destination-worker-iframe.js
@@ -0,0 +1,20 @@
+self.addEventListener('fetch', function(event) {
+    if (event.request.url.includes('dummy')) {
+        event.waitUntil(async function() {
+            let destination = new URL(event.request.url).searchParams.get("dest");
+            let clients = await self.clients.matchAll({"includeUncontrolled": true});
+            clients.forEach(function(client) {
+                if (client.url.includes("fetch-destination-iframe")) {
+                    if (event.request.destination == destination) {
+                        client.postMessage("PASS");
+                    } else {
+                        client.postMessage("FAIL");
+                    }
+                }
+            })
+        }());
+    }
+    event.respondWith(fetch(event.request));
+});
+
+