Bug 1226441 - Part 1: Add wpt test verifying fetch event waits for activate to complete; r=catalinb
authorBen Kelly <ben@wanderview.com>
Thu, 26 Nov 2015 19:56:19 +0200
changeset 284858 bab785163ce40e7514be377da2686a1b7bdcf158
parent 284857 1624a69d46d7d711c6a3abfdfb604989baf70ac9
child 284859 7ea5152b84175b42bfe7519e450d24e546e014db
push id187
push usercbook@mozilla.com
push dateFri, 27 Nov 2015 09:39:21 +0000
reviewerscatalinb
bugs1226441
milestone44.0a2
Bug 1226441 - Part 1: Add wpt test verifying fetch event waits for activate to complete; r=catalinb
testing/web-platform/mozilla/meta/MANIFEST.json
testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-waits-for-activate.https.html
testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-waits-for-activate-worker.js
--- a/testing/web-platform/mozilla/meta/MANIFEST.json
+++ b/testing/web-platform/mozilla/meta/MANIFEST.json
@@ -224,16 +224,23 @@
           }
         ],
         "service-workers/service-worker/fetch-response-xhr.https.html": [
           {
             "path": "service-workers/service-worker/fetch-response-xhr.https.html",
             "url": "/_mozilla/service-workers/service-worker/fetch-response-xhr.https.html"
           }
         ],
+        "service-workers/service-worker/fetch-waits-for-activate.https.html": [
+          {
+            "path": "service-workers/service-worker/fetch-waits-for-activate.https.html",
+            "timeout": "long",
+            "url": "/_mozilla/service-workers/service-worker/fetch-waits-for-activate.https.html"
+          }
+        ],
         "service-workers/service-worker/getregistration.https.html": [
           {
             "path": "service-workers/service-worker/getregistration.https.html",
             "url": "/_mozilla/service-workers/service-worker/getregistration.https.html"
           }
         ],
         "service-workers/service-worker/getregistrations.https.html": [
           {
@@ -551,9 +558,9 @@
       }
     },
     "reftest_nodes": {}
   },
   "reftest_nodes": {},
   "rev": null,
   "url_base": "/_mozilla/",
   "version": 2
-}
\ No newline at end of file
+}
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-waits-for-activate.https.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<title>Service Worker: Fetch Event Waits for Activate Event</title>
+<meta name=timeout content=long>
+<script src="/resources/testharness.js"></script>
+<script src="resources/testharness-helpers.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="resources/get-host-info.sub.js"></script>
+<script src="resources/test-helpers.sub.js"></script>
+<body>
+<script>
+
+var worker = 'resources/fetch-waits-for-activate-worker.js';
+var expected_url = normalizeURL(worker);
+var scope = 'resources/fetch-waits-for-activate/';
+
+async_test(function(t) {
+  var registration;
+  var frameLoadPromise;
+  var frame;
+  service_worker_unregister_and_register(t, worker, scope).then(function(reg) {
+    registration = reg;
+    return wait_for_state(t, reg.installing, 'activating');
+  }).then(function() {
+    assert_equals(registration.active.scriptURL, expected_url,
+                  'active worker should be present');
+    assert_equals(registration.active.state, 'activating',
+                  'active worker should be in activating state');
+
+    // This should block until we message the worker to tell it to complete
+    // the activate event.
+    frameLoadPromise = with_iframe(scope).then(function(f) {
+      frame = f;
+    });
+
+    // Wait some time to allow frame loading to proceed.  It should not,
+    // however, if the fetch event is blocked on the activate.  I don't
+    // see any way to force this race without a timeout, unfortunately.
+    return new Promise(function(resolve) {
+      setTimeout(resolve, 1000);
+    });
+  }).then(function() {
+    assert_equals(frame, undefined, 'frame should not be loaded');
+    assert_equals(registration.active.scriptURL, expected_url,
+                  'active worker should be present');
+    assert_equals(registration.active.state, 'activating',
+                  'active worker should be in activating state');
+
+    // This signals the activate event to complete.  The frame should now
+    // load.
+    registration.active.postMessage('GO');
+    return frameLoadPromise;
+  }).then(function() {
+    assert_equals(frame.contentWindow.navigator.serviceWorker.controller.scriptURL,
+                  expected_url, 'frame should now be loaded and controlled');
+    assert_equals(registration.active.state, 'activated',
+                  'active worker should be in activated state');
+    return service_worker_unregister_and_done(t, scope);
+  }).catch(unreached_rejection(t));
+}, 'Fetch events should wait for the activate event to complete.');
+
+</script>
+</body>
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-waits-for-activate-worker.js
@@ -0,0 +1,17 @@
+var activatePromiseResolve;
+
+addEventListener('activate', function(evt) {
+  evt.waitUntil(new Promise(function(resolve) {
+    activatePromiseResolve = resolve;
+  }));
+});
+
+addEventListener('message', function(evt) {
+  if (typeof activatePromiseResolve === 'function') {
+    activatePromiseResolve();
+  }
+});
+
+addEventListener('fetch', function(evt) {
+  evt.respondWith(new Response('Hello world'));
+});