Bug 1174106 - Part 4: Update an existing testcase; r?rickychien
MozReview-Commit-ID: 48lSPaI2CvV
--- 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