Skip to content

Commit

Permalink
Add multi-global tests for service worker URL parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Aug 29, 2016
1 parent f9ee769 commit 4b90437
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!DOCTYPE html>
<title>Current page used as a test helper</title>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
this.addEventListener('fetch', event => {
if (event.request.url.includes('test.txt')) {
event.respondWith(new Response('current'));
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<title>Incumbent page used as a test helper</title>

<iframe src="../current/current.https.html" id="c"></iframe>
<iframe src="../relevant/relevant.https.html" id="r"></iframe>

<script>
'use strict';

const current = document.querySelector('#c').contentWindow;
const relevant = document.querySelector('#r').contentWindow;

window.testRegister = options => {
return current.navigator.serviceWorker.register.call(relevant.navigator.serviceWorker, 'test-sw.js', options);
};

window.testGetRegistration = () => {
return current.navigator.serviceWorker.getRegistration.call(relevant.navigator.serviceWorker, 'test-sw.js');
};
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
this.addEventListener('fetch', event => {
if (event.request.url.includes('test.txt')) {
event.respondWith(new Response('incumbent'));
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!DOCTYPE html>
<title>Relevant page used as a test helper</title>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
this.addEventListener('fetch', event => {
if (event.request.url.includes('test.txt')) {
event.respondWith(new Response('relevant'));
}
});
5 changes: 5 additions & 0 deletions service-workers/service-worker/multi-globals/test-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
this.addEventListener('fetch', event => {
if (event.request.url.includes('test.txt')) {
event.respondWith(new Response('entry'));
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<title>register()/getRegistration() URL parsing, with multiple globals in play</title>
<link rel="help" href="https://w3c.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-register-method">
<link rel="help" href="https://w3c.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-getregistration-method">
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">
<script src="/resources/testharness.js"></script>
<script src="../resources/testharness-helpers.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.sub.js"></script>

<!-- This is the entry global -->

<iframe src="incumbent/incumbent.https.html"></iframe>

<script>
'use strict';

const loadPromise = new Promise(resolve => {
window.addEventListener('load', () => resolve());
});

promise_test(t => {
let registration;

return loadPromise.then(() => {
return frames[0].testRegister();
}).then(r => {
registration = r;
return fetch('test.txt');
})
.then(response => response.text())
.then(text => {
assert_equals(text, 'relevant',
'the service worker found at relevant/test-sw.js must have been the one to intercept the fetch');

return registration.unregister();
});
}, 'register should use the relevant global of the object it was called on to resolve the script URL');

promise_test(t => {
return loadPromise.then(() => {
return frames[0].testRegister({ scope: 'scope' });
}).then(registration => {
assert_equals(registration.scope, normalizeURL('relevant/scope'), 'the scope URL should be relevant/scope');

return registration.unregister();
});
}, 'register should use the relevant global of the object it was called on to resolve the scope URL');

promise_test(t => {
let registration;

return loadPromise.then(() => {
return navigator.serviceWorker.register(normalizeURL('relevant/test-sw.js'));
}).then(r => {
registration = r;
return frames[0].testGetRegistration();
})
.then(gottenRegistration => {
assert_not_equals(registration, null, 'the registration should not be null');
assert_equals(gottenRegistration, registration, 'the retrieved registration should be equal to the original');

return registration.unregister();
});
}, 'getRegistration should use the relevant global of the object it was called on to resolve the script URL');

</script>

0 comments on commit 4b90437

Please sign in to comment.