Bug 1174106 - Part 4: Update an existing testcase; r?rickychien draft
authorHo-Pang Hsu <hopang.hsu@gmail.com>
Thu, 10 Nov 2016 17:33:52 +0800
changeset 437088 e281c1d92236ae3d93b4e0bd903cfee099fb5241
parent 437087 166d2df342a77e562914174fb1d0660647f18ecf
child 536552 e9d5beae284d6fa0bd05e3891776cf51ece6d6e0
push id35320
push userbmo:bhsu@mozilla.com
push dateThu, 10 Nov 2016 10:05:34 +0000
reviewersrickychien
bugs1174106
milestone52.0a1
Bug 1174106 - Part 4: Update an existing testcase; r?rickychien MozReview-Commit-ID: 48lSPaI2CvV
testing/web-platform/tests/service-workers/service-worker/registration.https.html
testing/web-platform/tests/service-workers/service-worker/resources/update-max-aged-worker-imported-script.py
testing/web-platform/tests/service-workers/service-worker/resources/update-max-aged-worker.py
--- a/testing/web-platform/tests/service-workers/service-worker/registration.https.html
+++ b/testing/web-platform/tests/service-workers/service-worker/registration.https.html
@@ -360,9 +360,80 @@ promise_test(function(t) {
     var scope = 'filesystem:' + normalizeURL('resources/scope/filesystem-scope-url');
     return assert_promise_rejects(
         navigator.serviceWorker.register(script, {scope: scope}),
         'SecurityError',
         'Registering with the scope that has same-origin filesystem: URL ' +
             'should fail with SecurityError.');
   }, 'Scope URL is same-origin filesystem: URL');
 
+function getMessageFromServiceWorker(sw) {
+    return new Promise(function(resolve) {
+        var messageChannel = new MessageChannel();
+        messageChannel.port1.onmessage = e => resolve(e.data);
+        sw.postMessage({port: messageChannel.port2}, [messageChannel.port2]);
+    });
+}
+
+promise_test(function(t) {
+    var script = 'resources/update-max-aged-worker.py';
+    var scope = 'resources/blank.html';
+    var saved;
+    return Promise.resolve()
+        // Register for the first time.
+        .then(() => navigator.serviceWorker.register(script, {scope: scope}))
+        .then(registration => wait_for_update(t, registration))
+        .then(worker => getMessageFromServiceWorker(worker))
+        .then(value => saved = value)
+
+        // Register for the second time.
+        .then(() => navigator.serviceWorker.register(script, {scope: scope}))
+        .then(registration => wait_for_update(t, registration))
+        .then(worker => getMessageFromServiceWorker(worker))
+        .then(value => assert_true(saved !== value, 'Shouldn\'t be the same'))
+
+        // Tear down
+        .then(() => service_worker_unregister_and_done(t, scope));
+  }, "Test with useCache: default");
+
+promise_test(function(t) {
+    var script = 'resources/update-max-aged-worker.py';
+    var scope = 'resources/blank.html';
+    var saved;
+    return Promise.resolve()
+        // Register for the first time.
+        .then(() => navigator.serviceWorker.register(script, {scope: scope, useCache: false}))
+        .then(registration => wait_for_update(t, registration))
+        .then(worker => getMessageFromServiceWorker(worker))
+        .then(value => saved = value)
+
+        // Register for the second time.
+        .then(() => navigator.serviceWorker.register(script, {scope: scope, useCache: false}))
+        .then(registration => wait_for_update(t, registration))
+        .then(worker => getMessageFromServiceWorker(worker))
+        .then(value => assert_true(saved !== value, 'Shouldn\'t be the same'))
+
+        // Tear down
+        .then(() => service_worker_unregister_and_done(t, scope));
+  }, "Test with useCache: false");
+
+promise_test(function(t) {
+    var script = 'resources/update-max-aged-worker.py';
+    var scope = 'resources/blank.html';
+    return Promise.resolve()
+        // Register for the first time.
+        .then(() => navigator.serviceWorker.register(script, {scope: scope, useCache: true}))
+        .then(registration => wait_for_update(t, registration))
+
+        // Register for the second time.
+        .then(() => navigator.serviceWorker.register(script, {scope: scope, useCache: true}))
+        .then(registration => {
+            let p1 = new Promise(resolve => window.setTimeout(resolve, 1000));
+            let p2 = wait_for_update(t, registration)
+                .then(() => assert_true(false, "Shouldn't be updated when cache is used."));
+            return Promise.race([p1, p2]);
+        })
+
+        // Tear down
+        .then(() => service_worker_unregister_and_done(t, scope));
+  }, "Test with useCache: true");
+
 </script>
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/update-max-aged-worker-imported-script.py
@@ -0,0 +1,13 @@
+import time
+
+body = '''
+self.addEventListener('message', function(e) {
+    e.data.port.postMessage(%s);
+});
+''' % (time.time())
+
+def main(request, response):
+    headers = [('Cache-Control', 'max-age=86400'),
+               ('Content-Type', 'application/javascript')]
+
+    return headers, body
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/update-max-aged-worker.py
@@ -0,0 +1,12 @@
+import time
+
+body = '''
+// %s
+importScripts('update-max-aged-worker-imported-script.py');
+''' % (time.time())
+
+def main(request, response):
+    headers = [('Cache-Control', 'max-age=86400'),
+               ('Content-Type', 'application/javascript')]
+
+    return headers, body