Bug 1191931, Part 2 - Test resubscribing from a worker. r=mt

--HG--
extra : commitid : 6ZboHyRd5dR
extra : rebase_source : 074381c7b5133f7c4f37ab660cfd7afa54a23c65
This commit is contained in:
Kit Cambridge 2016-01-28 08:06:32 -08:00
parent 02f444f5c0
commit 3439243de8
2 changed files with 59 additions and 11 deletions

View File

@ -49,10 +49,20 @@ http://creativecommons.org/licenses/publicdomain/
});
var pushSubscription;
add_task(function* setupPushNotification() {
add_task(function* subscribe() {
pushSubscription = yield registration.pushManager.subscribe();
});
add_task(function* resubscribe() {
var data = yield sendRequestToWorker({
type: "resubscribe",
endpoint: pushSubscription.endpoint,
});
pushSubscription = yield registration.pushManager.getSubscription();
is(data.endpoint, pushSubscription.endpoint,
"Subscription endpoints should match after resubscribing in worker");
});
add_task(function* waitForPushNotification() {
yield Promise.all([
controlledFrame.waitOnPushMessage(),

View File

@ -1,6 +1,13 @@
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/
// This worker is used for two types of tests. `handlePush` sends messages to
// `frame.html`, which verifies that the worker can receive push messages.
// `handleMessage` receives messages from `test_push_manager_worker.html`
// and `test_data.html`, and verifies that `PushManager` can be used from
// the worker.
this.onpush = handlePush;
this.onmessage = handleMessage;
@ -17,6 +24,22 @@ function getJSON(data) {
return result;
}
function assert(value, message) {
if (!value) {
throw new Error(message);
}
}
function reply(event, promise) {
event.waitUntil(promise.then(result => {
event.ports[0].postMessage(result);
}).catch(error => {
event.ports[0].postMessage({
error: String(error),
});
}));
}
function handlePush(event) {
event.waitUntil(self.clients.matchAll().then(function(result) {
@ -46,19 +69,34 @@ function handlePush(event) {
function handleMessage(event) {
if (event.data.type == "publicKey") {
event.waitUntil(self.registration.pushManager.getSubscription().then(subscription => {
event.ports[0].postMessage({
reply(event, self.registration.pushManager.getSubscription().then(
subscription => ({
p256dh: subscription.getKey("p256dh"),
auth: subscription.getKey("auth"),
});
}).catch(error => {
event.ports[0].postMessage({
error: String(error),
});
})
));
return;
}
if (event.data.type == "resubscribe") {
reply(event, self.registration.pushManager.getSubscription().then(
subscription => {
assert(subscription.endpoint == event.data.endpoint,
"Wrong push endpoint in worker");
return subscription.unsubscribe();
}
).then(result => {
assert(result, "Error unsubscribing in worker");
return self.registration.pushManager.getSubscription();
}).then(subscription => {
assert(!subscription, "Subscription not removed in worker");
return self.registration.pushManager.subscribe();
}).then(subscription => {
return {
endpoint: subscription.endpoint,
};
}));
return;
}
event.ports[0].postMessage({
error: "Invalid message type: " + event.data.type,
});
reply(event, Promise.reject(
"Invalid message type: " + event.data.type));
}