Bug 1516065 [wpt PR 14638] - Add a test for ServiceWorker and AppCache interaction with claim(), a=testonly
authorKinuko Yasuda <kinuko@chromium.org>
Thu, 31 Jan 2019 19:00:42 +0000
changeset 458121 cd4ddb4ba292dcae02efc2d48cfbfe4074c99d99
parent 458120 dbf9649853546371a914d67307d336ac65bc6858
child 458122 1e4f8bca3b311876cea60a86a99861d1c45dd933
push id35518
push useropoprus@mozilla.com
push dateFri, 08 Feb 2019 09:55:14 +0000
treeherdermozilla-central@3a3e393396f4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstestonly
bugs1516065, 14638, 917284, 1388129, 619022
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1516065 [wpt PR 14638] - Add a test for ServiceWorker and AppCache interaction with claim(), a=testonly Automatic update from web-platform-tests Add a test for ServiceWorker and AppCache interaction with claim() Bug: 917284 Change-Id: I953dfecdf1ae825343ae659d496d089ce356e662 Reviewed-on: https://chromium-review.googlesource.com/c/1388129 Commit-Queue: Kinuko Yasuda <kinuko@chromium.org> Reviewed-by: Matt Falkenhagen <falken@chromium.org> Cr-Commit-Position: refs/heads/master@{#619022} -- wpt-commits: 95d10c70fd3ebebcb74f0ca2fe90bb3b495afe26 wpt-pr: 14638
testing/web-platform/tests/service-workers/service-worker/appcache-ordering-main.https.html
testing/web-platform/tests/service-workers/service-worker/claim-fetch-with-appcache.https.html
testing/web-platform/tests/service-workers/service-worker/resources/appcache-ordering.manifest
testing/web-platform/tests/service-workers/service-worker/resources/claim-worker.js
testing/web-platform/tests/service-workers/service-worker/resources/test-helpers.sub.js
--- a/testing/web-platform/tests/service-workers/service-worker/appcache-ordering-main.https.html
+++ b/testing/web-platform/tests/service-workers/service-worker/appcache-ordering-main.https.html
@@ -1,50 +1,24 @@
 <!DOCTYPE html>
 <script src="/resources/testharness.js"></script>
 <script src="/resources/testharnessreport.js"></script>
 <script src="resources/test-helpers.sub.js"></script>
 <body>
 <script>
 
-var INSTALL_APPCACHE_URL = "resources/appcache-ordering.install.html";
 var IS_APPCACHED_URL = "resources/appcache-ordering.is-appcached.html";
 var SERVICE_WORKER_SCOPE = "resources/appcache-ordering";
 var SERVICE_WORKER_SCRIPT = "resources/empty-worker.js";
 
 var resolve_install_appcache = undefined;
 var reject_install_appcache = undefined;
 
 var frames = [];
 
-// Called by the INSTALL_APPCACHE_URL child frame.
-function notify_appcache_installed(success) {
-  if (success)
-    resolve_install_appcache();
-  else
-    reject_install_appcache();
-}
-
-function install_appcache() {
-  return new Promise(function(resolve, reject) {
-      var frame = document.createElement('iframe');
-      frames.push(frame);
-      frame.src = INSTALL_APPCACHE_URL;
-      document.body.appendChild(frame);
-      resolve_install_appcache = function() {
-          document.body.removeChild(frame);
-          resolve();
-        };
-      reject_install_appcache = function() {
-          document.body.removeChild(frame);
-          reject();
-        };
-  });
-}
-
 var resolve_is_appcached = undefined;
 
 // Called by the IS_APPCACHED_URL child frame.
 function notify_is_appcached(is) {
   resolve_is_appcached(is);
 }
 
 function is_appcached() {
@@ -58,17 +32,17 @@ function is_appcached() {
           resolve(is);
         };
   });
 }
 
 promise_test(function(t) {
     return service_worker_unregister(t, SERVICE_WORKER_SCOPE)
       .then(function() {
-          return install_appcache();
+          return install_appcache_ordering_manifest();
         })
       .then(function() {
           return is_appcached();
         })
       .then(function(result) {
           assert_true(result, 'appcache should initially be utilized');
           return service_worker_unregister_and_register(
               t, SERVICE_WORKER_SCRIPT, SERVICE_WORKER_SCOPE);
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/claim-fetch-with-appcache.https.html
@@ -0,0 +1,81 @@
+<!doctype html>
+<meta charset=utf-8>
+<title></title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/test-helpers.sub.js"></script>
+<body>
+<script>
+
+// This test makes a frame controlled by AppCache, then registers a service
+// worker that calls claim() to control the frame. AppCache should be completely
+// bypassed once the service worker claims the frame.
+
+const fetch_text = async frame => {
+  const response = await
+    frame.contentWindow.fetch('appcache-ordering.is-appcached.js');
+  return await response.text();
+};
+
+const check_is_appcached = async frame => {
+  // This should FALLBACK to ordering.is_appcached.js as in the manifest
+  // if the appcache is effective.
+  const response = await
+    frame.contentWindow.fetch('appcache-ordering.is-appcached404.js');
+  return await response.ok;
+};
+
+promise_test(async t => {
+  const scope = 'resources/';
+  const script = 'resources/claim-worker.js';
+
+  await install_appcache_ordering_manifest();
+
+  // Create the test iframe.
+  const frame = await with_iframe('resources/blank.html');
+  t.add_cleanup(async () => {
+    if (frame) frame.remove();
+    return service_worker_unregister(t, scope);
+  });
+
+  // Check that the appcache controls the frame.
+  assert_equals(await check_is_appcached(frame), true,
+                'AppCache should be present');
+
+  // Check the controller and test with fetch.
+  assert_equals(frame.contentWindow.navigator.controller, undefined,
+                'Should have no controller.');
+  assert_equals(await fetch_text(frame), 'var is_appcached = true;\n',
+                'fetch() should not be intercepted.');
+
+  // Register a service worker.
+  let registration = await service_worker_unregister_and_register(t, script, scope);
+  const worker = registration.installing;
+  await wait_for_state(t, worker, 'activated');
+
+  // Let the service worker claim the iframe.
+  const channel = new MessageChannel();
+  const check_message = new Promise(resolve => {
+    channel.port1.onmessage = async e => {
+      assert_equals(e.data, 'PASS', 'Worker call to claim() should fulfill.');
+      resolve();
+    };
+  });
+  worker.postMessage({port: channel.port2}, [channel.port2]);
+  await check_message;
+
+  // Check that the appcache does NOT control the frame.
+  assert_equals(await check_is_appcached(frame), false,
+                'AppCache should not be present');
+
+  // Check the controller and test with fetch.
+  registration = await
+    frame.contentWindow.navigator.serviceWorker.getRegistration(scope);
+  assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
+                registration.active, 'iframe should be claimed.');
+  assert_equals(await fetch_text(frame), 'Intercepted!',
+                'fetch() should be intercepted.');
+}, 'fetch() should be intercepted after the client is claimed.')
+
+</script>
+</body>
--- a/testing/web-platform/tests/service-workers/service-worker/resources/appcache-ordering.manifest
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/appcache-ordering.manifest
@@ -1,7 +1,8 @@
 CACHE MANIFEST
 
 appcache-ordering.is-appcached.html
+blank.html
 
 FALLBACK:
 appcache-ordering.is-appcached404.js appcache-ordering.is-appcached.js
 
--- a/testing/web-platform/tests/service-workers/service-worker/resources/claim-worker.js
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/claim-worker.js
@@ -9,10 +9,11 @@ self.addEventListener('message', functio
           event.data.port.postMessage('PASS');
         })
       .catch(function(error) {
           event.data.port.postMessage('FAIL: exception: ' + error.name);
         });
   });
 
 self.addEventListener('fetch', function(event) {
-    event.respondWith(new Response('Intercepted!'));
-});
+    if (!/404/.test(event.request.url))
+      event.respondWith(new Response('Intercepted!'));
+  });
--- a/testing/web-platform/tests/service-workers/service-worker/resources/test-helpers.sub.js
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/test-helpers.sub.js
@@ -271,8 +271,38 @@ function with_sandboxed_iframe(url, sand
 // arbitrary delay.
 async function wait_for_activation_on_dummy_scope(t, window_or_workerglobalscope) {
   const script = '/service-workers/service-worker/resources/empty-worker.js';
   const scope = 'resources/there/is/no/there/there?' + Date.now();
   let registration = await window_or_workerglobalscope.navigator.serviceWorker.register(script, { scope });
   await wait_for_state(t, registration.installing, 'activated');
   await registration.unregister();
 }
+
+// This installs resources/appcache-ordering.manifest.
+function install_appcache_ordering_manifest() {
+  let resolve_install_appcache;
+  let reject_install_appcache;
+
+  // This is notified by the child iframe, i.e. appcache-ordering.install.html,
+  // that's to be created below.
+  window.notify_appcache_installed = success => {
+    if (success)
+      resolve_install_appcache();
+    else
+      reject_install_appcache();
+  };
+
+  return new Promise((resolve, reject) => {
+      const frame = document.createElement('iframe');
+      frame.src = 'resources/appcache-ordering.install.html';
+      document.body.appendChild(frame);
+      resolve_install_appcache = function() {
+          document.body.removeChild(frame);
+          resolve();
+        };
+      reject_install_appcache = function() {
+          document.body.removeChild(frame);
+          reject();
+        };
+  });
+}
+