Skip to content

Commit

Permalink
ServiceWorker: Add new WPT tests to make sure to update a registration
Browse files Browse the repository at this point in the history
with different script type and identical script content.

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

Bug: 824647
Change-Id: I2a3f87da1013f84c6e9495f362899dfe6ab97b45
  • Loading branch information
d0iasm authored and chromium-wpt-export-bot committed Nov 1, 2018
1 parent 751803f commit 8ba3f12
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
1 change: 1 addition & 0 deletions service-workers/service-worker/resources/classic-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
importScripts('./imported-classic-script.js');
1 change: 1 addition & 0 deletions service-workers/service-worker/resources/module-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import * as module from './imported-module-script.js';
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<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
// The following two tests check that a registration is updated correctly
// with different script type. At first Service Worker is registered 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 => {
Expand Down Expand Up @@ -70,5 +70,135 @@
assert_equals(msgEvent.data, 'A classic script.');
assert_equals(firstRegistration, secondRegistration);
}, 'Update the registration with a different script type (module => classic).');

// The following two tests change the script type while keeping
// the script identical.
promise_test(async t => {
const script = 'resources/empty-worker.js';
const scope = 'resources/update-registration-with-type';
await service_worker_unregister(t, scope);
t.add_cleanup(() => service_worker_unregister(t, scope));

// Register with classic script type.
const firstRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'classic'
});
await wait_for_state(t, firstRegistration.installing, 'activated');
const firstActiveWorker = firstRegistration.active;

// Re-register with module script type.
const secondRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'module'
});
await wait_for_state(t, secondRegistration.installing, 'activated');
const secondActiveWorker = secondRegistration.active;

assert_not_equals(firstActiveWorker, secondActiveWorker);
assert_equals(firstRegistration, secondRegistration);
}, 'Update the registration with a different script type (classic => module) '
+ 'and with a same main script.');

promise_test(async t => {
const script = 'resources/empty-worker.js';
const scope = 'resources/update-registration-with-type';
await service_worker_unregister(t, scope);
t.add_cleanup(() => service_worker_unregister(t, scope));

// Register with module script type.
const firstRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'module'
});
await wait_for_state(t, firstRegistration.installing, 'activated');
const firstActiveWorker = firstRegistration.active;

// Re-register with classic script type.
const secondRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'classic'
});
await wait_for_state(t, secondRegistration.installing, 'activated');
const secondActiveWorker = secondRegistration.active;

assert_not_equals(firstActiveWorker, secondActiveWorker);
assert_equals(firstRegistration, secondRegistration);
}, 'Update the registration with a different script type (module => classic) '
+ 'and with a same main script.');

// This test checks that a registration is not updated with the same script
// type and the same main script.
promise_test(async t => {
const script = 'resources/empty-worker.js';
const scope = 'resources/update-registration-with-type';
await service_worker_unregister(t, scope);
t.add_cleanup(() => service_worker_unregister(t, scope));

// Register with module script type.
const firstRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'module'
});
await wait_for_state(t, firstRegistration.installing, 'activated');

// Re-register with module script type.
const secondRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'module'
});
assert_equals(secondRegistration.installing, null);

assert_equals(firstRegistration, secondRegistration);
}, 'Does not update the registration with the same script type and '
+ 'the same main script.');

// In the case (classic => module), a worker script contains importScripts()
// that is disallowed on module scripts, so the second registration is
// expected to fail script evaluation.
promise_test(async t => {
const script = 'resources/classic-worker.js';
const scope = 'resources/update-registration-with-type';
await service_worker_unregister(t, scope);
t.add_cleanup(() => service_worker_unregister(t, scope));

// Register with classic script type.
const firstRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'classic'
});
assert_not_equals(firstRegistration.installing, null);

// Re-register with module script type and expect TypeError.
return promise_rejects(t, new TypeError, navigator.serviceWorker.register(script, {
scope: scope,
type: 'module'
}), 'Registering with invalid evaluation should be failed.');
}, 'Update the registration with a different script type (classic => module) '
+ 'and with a same main script. Expect evaluation failed.');

// In the case (module => classic), a worker script contains static-import
// that is disallowed on classic scripts, so the second registration is
// expected to fail script evaluation.
promise_test(async t => {
const script = 'resources/module-worker.js';
const scope = 'resources/update-registration-with-type';
await service_worker_unregister(t, scope);
t.add_cleanup(() => service_worker_unregister(t, scope));

// Register with module script type.
const firstRegistration = await navigator.serviceWorker.register(script, {
scope: scope,
type: 'module'
});
assert_not_equals(firstRegistration.installing, null);

// Re-register with classic script type and expect TypeError.
return promise_rejects(t, new TypeError, navigator.serviceWorker.register(script, {
scope: scope,
type: 'classic'
}), 'Registering with invalid evaluation should be failed.');
}, 'Update the registration with a different script type (module => classic) '
+ 'and with a same main script. Expect evaluation failed.');
</script>
</body>

0 comments on commit 8ba3f12

Please sign in to comment.