Skip to content

Commit

Permalink
ServiceWorker: Add new tests for updating the registration with
Browse files Browse the repository at this point in the history
different script type.

These tests check that a registration is updated correctly with
different script type. At first Service Worker is register as classic
script type, then it is re-registered as module script type, and
vice versa. A main script is also updated at the same time.

Bug: 824647
Change-Id: I3ab9391791508662de8dc73ac932a6105c4d7fe6
  • Loading branch information
d0iasm authored and chromium-wpt-export-bot committed Oct 25, 2018
1 parent 17b2f79 commit ec6574d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
@@ -0,0 +1 @@
const imported = 'A classic script.';
@@ -0,0 +1 @@
export const imported = 'A module script.';
@@ -0,0 +1,33 @@
def classic_script():
return """
importScripts('./imported-classic-script.js');
self.onmessage = e => {
e.source.postMessage(imported);
};
"""

def module_script():
return """
import * as module from './imported-module-script.js';
self.onmessage = e => {
e.source.postMessage(module.imported);
};
"""

# Returns the classic script for a first request and
# returns the module script for second and subsequent requests.
def main(request, response):
headers = [('Content-Type', 'application/javascript'),
('Pragma', 'no-store'),
('Cache-Control', 'no-store')]

classic_first = request.GET['classic_first']
key = request.GET['key']
requested_once = request.server.stash.take(key)
if requested_once is None:
request.server.stash.put(key, True)
body = classic_script() if classic_first == '1' else module_script()
else:
body = module_script() if classic_first == '1' else classic_script()

return 200, headers, body
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Service Worker: Update the registration with a different script type.</title>
<!-- common.js is for guid() -->
<script src="/mixed-content/generic/common.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
// These tests check that a registration is updated correctly with
// different script type. At first Service Worker is register as
// classic script type, then it is re-registered as module script type,
// and vice versa. A main script is also updated at the same time.
promise_test(async t => {
const key = guid();
const script = `resources/update-registration-with-type.py?classic_first=1&key=${key}`;
const scope = 'resources/update-registration-with-type';
await service_worker_unregister(t, scope);

// Register with classic script type.
const firstRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'classic'
});
firstRegistration.installing.postMessage(' ');
let msgEvent = await new Promise(resolve => {
navigator.serviceWorker.onmessage = resolve;
});
assert_equals(msgEvent.data, 'A classic script.');

// Re-register with module script type.
const secondRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'module'
});
secondRegistration.installing.postMessage(' ');
msgEvent = await new Promise(resolve => {
navigator.serviceWorker.onmessage = resolve;
});
assert_equals(msgEvent.data, 'A module script.');
assert_equals(firstRegistration, secondRegistration);
}, 'Update the registration with a different script type (classic => module).');

promise_test(async t => {
const key = guid();
const script = `resources/update-registration-with-type.py?classic_first=0&key=${key}`;
const scope = 'resources/update-registration-with-type';
await service_worker_unregister(t, scope);

// Register with module script type.
const firstRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'module'
});
firstRegistration.installing.postMessage(' ');
let msgEvent = await new Promise(resolve => {
navigator.serviceWorker.onmessage = resolve;
});
assert_equals(msgEvent.data, 'A module script.');

// Re-register with classic script type.
const secondRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'classic'
});
secondRegistration.installing.postMessage(' ');
msgEvent = await new Promise(resolve => {
navigator.serviceWorker.onmessage = resolve;
});
assert_equals(msgEvent.data, 'A classic script.');
assert_equals(firstRegistration, secondRegistration);
}, 'Update the registration with a different script type (module => classic).');
</script>
</body>

0 comments on commit ec6574d

Please sign in to comment.