Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service worker: self.serviceWorker #17285

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions service-workers/service-worker/global-serviceworker.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// META: title=serviceWorker on service worker global
// META: global=!default,serviceworker
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see there are some tests today with names like "can-make-payment-event-constructor.https.serviceworker.js". Does it work to make this a "serviceworker.js" test, then you don't need this META?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this would be better, but it doesn't seem to work.

https://web-platform-tests.org/writing-tests/testharness.html#auto-generated-test-boilerplate suggests only window and worker work in the URL.

The global=!default,serviceworker line is also directly from their documentation.

Maybe I'm holding it wrong though. I agree it's a little weird having an 'any' test that's limited to just service workers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok.


test(() => {
assert_equals(registration.installing, null, 'registration.installing');
assert_equals(registration.waiting, null, 'registration.waiting');
assert_equals(registration.active, null, 'registration.active');
assert_true('serviceWorker' in self, 'self.serviceWorker exists');
assert_equals(serviceWorker.state, 'parsed', 'serviceWorker.state');
assert_readonly(self, 'serviceWorker', `self.serviceWorker is read only`);
}, 'First run');

// Cache this for later tests.
const initialServiceWorker = self.serviceWorker;

async_test((t) => {
assert_true('serviceWorker' in self, 'self.serviceWorker exists');
serviceWorker.postMessage({ messageTest: true });

// The rest of the rest runs once we receive the above message.
addEventListener('message', t.step_func((event) => {
// Ignore unrelated messages.
if (!event.data.messageTest) return;
assert_equals(event.source, serviceWorker, 'event.source');
t.done();
}));
}, 'Can post message to self during startup');

// The test is registered now so there isn't a race condition when collecting tests, but the asserts
// don't happen until the 'install' event fires.
async_test((t) => {
addEventListener('install', t.step_func_done(() => {
assert_true('serviceWorker' in self, 'self.serviceWorker exists');
assert_equals(serviceWorker, initialServiceWorker, `self.serviceWorker hasn't changed`);
assert_equals(registration.installing, serviceWorker, 'registration.installing');
assert_equals(registration.waiting, null, 'registration.waiting');
assert_equals(registration.active, null, 'registration.active');
assert_equals(serviceWorker.state, 'installing', 'serviceWorker.state');
}));
}, 'During install');

// The test is registered now so there isn't a race condition when collecting tests, but the asserts
// don't happen until the 'activate' event fires.
async_test((t) => {
addEventListener('activate', t.step_func_done(() => {
assert_true('serviceWorker' in self, 'self.serviceWorker exists');
assert_equals(serviceWorker, initialServiceWorker, `self.serviceWorker hasn't changed`);
assert_equals(registration.installing, null, 'registration.installing');
assert_equals(registration.waiting, null, 'registration.waiting');
assert_equals(registration.active, serviceWorker, 'registration.active');
assert_equals(serviceWorker.state, 'activating', 'serviceWorker.state');
}));
}, 'During activate');