Bug 1175163 - Part 1: Migrate claim_fetch_worker to wpt. r=bkelly
authorHo-Pang Hsu <hopang.hsu@gmail.com>
Wed, 01 Mar 2017 11:30:15 +0800
changeset 345326 b719e03aa6f8cb725e9a55244e92280026407e08
parent 345325 83854fc82f3d96aed2c2fbda01a60023811020ef
child 345327 e7b9fda58dd570add4a4c2d3cacab2a94fd74654
push id31436
push userkwierso@gmail.com
push dateThu, 02 Mar 2017 01:18:52 +0000
treeherdermozilla-central@e91de6fb2b3d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1175163
milestone54.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 1175163 - Part 1: Migrate claim_fetch_worker to wpt. r=bkelly
dom/workers/test/serviceworkers/claim_fetch_worker.js
dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/test_claim_fetch.html
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/service-workers/service-worker/claim-fetch.https.html
testing/web-platform/tests/service-workers/service-worker/resources/claim-worker.js
deleted file mode 100644
--- a/dom/workers/test/serviceworkers/claim_fetch_worker.js
+++ /dev/null
@@ -1,12 +0,0 @@
-onfetch = function(e) {
-  if (e.request.url.indexOf("service_worker_controlled") >= 0) {
-    // pass through
-    e.respondWith(fetch(e.request));
-  } else {
-    e.respondWith(new Response("Fetch was intercepted"));
-  }
-}
-
-onmessage = function(e) {
-  clients.claim();
-}
--- a/dom/workers/test/serviceworkers/mochitest.ini
+++ b/dom/workers/test/serviceworkers/mochitest.ini
@@ -157,17 +157,16 @@ support-files =
   swa/worker_scope_too_deep.js
   swa/worker_scope_too_deep.js^headers^
   swa/worker_scope_too_narrow.js
   swa/worker_scope_too_narrow.js^headers^
   claim_oninstall_worker.js
   claim_worker_1.js
   claim_worker_2.js
   claim_clients/client.html
-  claim_fetch_worker.js
   force_refresh_worker.js
   sw_clients/refresher.html
   sw_clients/refresher_compressed.html
   sw_clients/refresher_compressed.html^headers^
   sw_clients/refresher_cached.html
   sw_clients/refresher_cached_compressed.html
   sw_clients/refresher_cached_compressed.html^headers^
   strict_mode_warning.js
@@ -213,17 +212,16 @@ support-files =
   sharedWorker_fetch.js
   async_waituntil_worker.js
   lazy_worker.js
   nofetch_handler_worker.js
 
 [test_bug1151916.html]
 [test_bug1240436.html]
 [test_claim.html]
-[test_claim_fetch.html]
 [test_claim_oninstall.html]
 [test_controller.html]
 [test_cross_origin_url_after_redirect.html]
 [test_csp_upgrade-insecure_intercept.html]
 [test_empty_serviceworker.html]
 [test_error_reporting.html]
 [test_escapedSlashes.html]
 [test_eval_allowed.html]
deleted file mode 100644
--- a/dom/workers/test/serviceworkers/test_claim_fetch.html
+++ /dev/null
@@ -1,98 +0,0 @@
-<!--
-  Any copyright is dedicated to the Public Domain.
-  http://creativecommons.org/publicdomain/zero/1.0/
--->
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Bug 1130684 - Test fetch events are intercepted after claim </title>
-  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
-  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
-</head>
-<body>
-<p id="display"></p>
-<div id="content" style="display: none">
-  <a ping="ping" href="fetch.txt">link</a>
-</div>
-<pre id="test"></pre>
-<script class="testbody" type="text/javascript">
-  var registration;
-
-  function register() {
-    return navigator.serviceWorker.register("claim_fetch_worker.js",
-                                            { scope: "./" })
-      .then((swr) => registration = swr);
-  }
-
-  function unregister() {
-    return registration.unregister().then(function(result) {
-      ok(result, "Unregister should return true.");
-    });
-  }
-
-  function createClient() {
-    var p = new Promise(function(res, rej){
-      window.onmessage = function(e) {
-        if(e.data === "READY") {
-          res();
-        }
-      }
-    });
-
-    var content = document.getElementById("content");
-    ok(content, "Parent exists.");
-
-    iframe = document.createElement("iframe");
-    iframe.setAttribute('src', "sw_clients/service_worker_controlled.html");
-    content.appendChild(iframe);
-
-    return p;
-  }
-
-  function testFetch(before) {
-    return fetch("fetch/real-file.txt").then(function(res) {
-      ok(res.ok, "Response should be valid.");
-      return res.text().then(function(body) {
-        if (before) {
-          ok(body !== "Fetch was intercepted", "Fetch events should not be intercepted.");
-        } else {
-          ok(body === "Fetch was intercepted", "Fetch events should be intercepted.");
-        }
-      });
-    });
-  }
-
-  function claimThisPage() {
-    ok(registration.active, "Worker is active.");
-    var p = new Promise(function (res, rej) {
-      navigator.serviceWorker.oncontrollerchange = res;
-    });
-
-    registration.active.postMessage("Claim");
-
-    return p;
-  }
-
-  function runTest() {
-    register()
-      .then(createClient)
-      .then(testFetch.bind(this, true))
-      .then(claimThisPage)
-      .then(testFetch.bind(this, false))
-      .then(unregister)
-      .catch(function(e) {
-        ok(false, "Some test failed with error " + e);
-      }).then(SimpleTest.finish);
-  }
-
-  SimpleTest.waitForExplicitFinish();
-  SpecialPowers.pushPrefEnv({"set": [
-    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
-    ["dom.serviceWorkers.enabled", true],
-    ["dom.serviceWorkers.testing.enabled", true]
-  ]}, runTest);
-</script>
-</pre>
-</body>
-</html>
-
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -118577,16 +118577,22 @@
     ]
    ],
    "service-workers/service-worker/appcache-ordering-main.https.html": [
     [
      "/service-workers/service-worker/appcache-ordering-main.https.html",
      {}
     ]
    ],
+   "service-workers/service-worker/claim-fetch.https.html": [
+    [
+     "/service-workers/service-worker/claim-fetch.https.html",
+     {}
+    ]
+   ],
    "service-workers/service-worker/claim-not-using-registration.https.html": [
     [
      "/service-workers/service-worker/claim-not-using-registration.https.html",
      {}
     ]
    ],
    "service-workers/service-worker/claim-using-registration.https.html": [
     [
@@ -199762,16 +199768,20 @@
   "service-workers/service-worker/active.https.html": [
    "974fc8372db8f06f87919d35be48f922166d6652",
    "testharness"
   ],
   "service-workers/service-worker/appcache-ordering-main.https.html": [
    "a71f51cde17f9d209750877dfbe1bacd26412ab3",
    "testharness"
   ],
+  "service-workers/service-worker/claim-fetch.https.html": [
+   "79b4a278f0e35646cfdffeebf8f0523e2772bc9b",
+   "testharness"
+  ],
   "service-workers/service-worker/claim-not-using-registration.https.html": [
    "72da19e038b6ab32ea04a0e91d117bc67d25a301",
    "testharness"
   ],
   "service-workers/service-worker/claim-using-registration.https.html": [
    "fb56cc3ae802669bb7898e76ac55e75ba6ac1441",
    "testharness"
   ],
@@ -200131,17 +200141,17 @@
    "963e8436d3c2571fb971f05aaa4710f5d3dd7abe",
    "support"
   ],
   "service-workers/service-worker/resources/blank.html": [
    "0ddb4f1cf84729ed673295719ec58a3e5d600a12",
    "support"
   ],
   "service-workers/service-worker/resources/claim-worker.js": [
-   "2839599adca9049d31085e50504623576dc81643",
+   "e779a28c42928ff10219073171c1216c6623b4d4",
    "support"
   ],
   "service-workers/service-worker/resources/client-navigate-frame.html": [
    "ecad40948e5d00ca737ea91b702ebbecc268e53b",
    "support"
   ],
   "service-workers/service-worker/resources/client-navigate-worker.js": [
    "6b277e1dcde40babec32046f9e637a47830bf29b",
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/claim-fetch.https.html
@@ -0,0 +1,67 @@
+<!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>
+<script>
+
+promise_test(function(t) {
+  var frame;
+  var resource = 'resources/simple.txt';
+
+  var worker;
+  var scope = 'resources/';
+  var script = 'resources/claim-worker.js';
+
+  return Promise.resolve()
+    // Create the test iframe.
+    .then(() => with_iframe('resources/blank.html'))
+    .then(f => frame = f)
+
+    // Check the controller and test with fetch.
+    .then(() => assert_equals(frame.contentWindow.navigator.controller,
+                              undefined,
+                              'Should have no controller.'))
+    .then(() => frame.contentWindow.fetch(resource))
+    .then(response => response.text())
+    .then(response_text => assert_equals(response_text,
+                                         'a simple text file\n',
+                                         'fetch() should not be intercepted.'))
+
+    // Register a service worker.
+    .then(() => service_worker_unregister_and_register(t, script, scope))
+    .then(r => worker = r.installing)
+    .then(() => wait_for_state(t, worker, 'activated'))
+
+    // Let the service worker claim the iframe.
+    .then(() => {
+      var channel = new MessageChannel();
+      var saw_message = new Promise(function(resolve) {
+        channel.port1.onmessage = t.step_func(function(e) {
+          assert_equals(e.data, 'PASS',
+                        'Worker call to claim() should fulfill.');
+          resolve();
+        });
+      });
+      worker.postMessage({port: channel.port2}, [channel.port2]);
+      return saw_message;
+    })
+
+    // Check the controller and test with fetch.
+    .then(() => frame.contentWindow.navigator.serviceWorker.getRegistration(scope))
+    .then(r => assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
+                             r.active,
+                             'Test iframe should be claimed.'))
+    .then(() => frame.contentWindow.fetch(resource))
+    .then(response => response.text())
+    .then(response_text => assert_equals(response_text,
+                                         'Intercepted!',
+                                         'fetch() should be intercepted.'))
+
+    // Cleanup this testcase.
+    .then(() => frame.remove())
+    .then(() => service_worker_unregister_and_done(t, scope));
+}, 'fetch() should be intercepted after the client is claimed.')
+
+</script>
--- 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
@@ -7,8 +7,12 @@ self.addEventListener('message', functio
               return;
           }
           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!'));
+});