Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ServiceWorker: Add new tests for updating the registration with
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
1 parent
17b2f79
commit ec6574d
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
service-workers/service-worker/resources/imported-classic-script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const imported = 'A classic script.'; |
1 change: 1 addition & 0 deletions
1
service-workers/service-worker/resources/imported-module-script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const imported = 'A module script.'; |
33 changes: 33 additions & 0 deletions
33
service-workers/service-worker/resources/update-registration-with-type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
74 changes: 74 additions & 0 deletions
74
service-workers/service-worker/update-registration-with-type.https.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |