2015-04-21 21:10:50 +03:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2015-06-02 14:14:00 +03:00
|
|
|
const {PushDB, PushService, PushServiceWebSocket} = serviceExports;
|
2015-04-21 21:10:50 +03:00
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
do_get_profile();
|
|
|
|
setPrefs();
|
|
|
|
disableServiceWorkerEvents(
|
|
|
|
'https://example.com/a',
|
|
|
|
'https://example.com/b',
|
|
|
|
'https://example.com/c'
|
|
|
|
);
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
add_task(function* test_notification_error() {
|
2015-06-02 14:14:00 +03:00
|
|
|
let db = PushServiceWebSocket.newPushDB();
|
2015-05-12 19:08:00 +03:00
|
|
|
do_register_cleanup(() => {return db.drop().then(_ => db.close());});
|
2015-06-24 23:34:54 +03:00
|
|
|
|
|
|
|
let originAttributes = '';
|
2015-04-21 21:10:50 +03:00
|
|
|
let records = [{
|
|
|
|
channelID: 'f04f1e46-9139-4826-b2d1-9411b0821283',
|
|
|
|
pushEndpoint: 'https://example.org/update/success-1',
|
|
|
|
scope: 'https://example.com/a',
|
2015-06-24 23:34:54 +03:00
|
|
|
originAttributes: originAttributes,
|
2015-06-26 00:52:57 +03:00
|
|
|
version: 1,
|
|
|
|
quota: Infinity,
|
2015-04-21 21:10:50 +03:00
|
|
|
}, {
|
|
|
|
channelID: '3c3930ba-44de-40dc-a7ca-8a133ec1a866',
|
|
|
|
pushEndpoint: 'https://example.org/update/error',
|
|
|
|
scope: 'https://example.com/b',
|
2015-06-24 23:34:54 +03:00
|
|
|
originAttributes: originAttributes,
|
2015-06-26 00:52:57 +03:00
|
|
|
version: 2,
|
|
|
|
quota: Infinity,
|
2015-04-21 21:10:50 +03:00
|
|
|
}, {
|
|
|
|
channelID: 'b63f7bef-0a0d-4236-b41e-086a69dfd316',
|
|
|
|
pushEndpoint: 'https://example.org/update/success-2',
|
|
|
|
scope: 'https://example.com/c',
|
2015-06-24 23:34:54 +03:00
|
|
|
originAttributes: originAttributes,
|
2015-06-26 00:52:57 +03:00
|
|
|
version: 3,
|
|
|
|
quota: Infinity,
|
2015-04-21 21:10:50 +03:00
|
|
|
}];
|
|
|
|
for (let record of records) {
|
2015-05-12 19:08:00 +03:00
|
|
|
yield db.put(record);
|
2015-04-21 21:10:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let notifyPromise = Promise.all([
|
|
|
|
promiseObserverNotification(
|
|
|
|
'push-notification',
|
|
|
|
(subject, data) => data == 'https://example.com/a'
|
|
|
|
),
|
|
|
|
promiseObserverNotification(
|
|
|
|
'push-notification',
|
|
|
|
(subject, data) => data == 'https://example.com/c'
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
|
2015-08-15 03:26:07 +03:00
|
|
|
let ackDone;
|
|
|
|
let ackPromise = new Promise(resolve => ackDone = after(records.length, resolve));
|
2015-04-21 21:10:50 +03:00
|
|
|
PushService.init({
|
2015-06-02 14:14:00 +03:00
|
|
|
serverURI: "wss://push.example.org/",
|
2015-04-21 21:10:50 +03:00
|
|
|
networkInfo: new MockDesktopNetworkInfo(),
|
|
|
|
db: makeStub(db, {
|
2015-06-03 15:06:00 +03:00
|
|
|
getByKeyID(prev, channelID) {
|
2015-04-21 21:10:50 +03:00
|
|
|
if (channelID == '3c3930ba-44de-40dc-a7ca-8a133ec1a866') {
|
2015-05-12 19:08:00 +03:00
|
|
|
return Promise.reject('splines not reticulated');
|
2015-04-21 21:10:50 +03:00
|
|
|
}
|
2015-05-12 19:08:00 +03:00
|
|
|
return prev.call(this, channelID);
|
2015-04-21 21:10:50 +03:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
makeWebSocket(uri) {
|
|
|
|
return new MockWebSocket(uri, {
|
|
|
|
onHello(request) {
|
|
|
|
deepEqual(request.channelIDs.sort(), [
|
|
|
|
'3c3930ba-44de-40dc-a7ca-8a133ec1a866',
|
|
|
|
'b63f7bef-0a0d-4236-b41e-086a69dfd316',
|
|
|
|
'f04f1e46-9139-4826-b2d1-9411b0821283'
|
|
|
|
], 'Wrong channel list');
|
|
|
|
this.serverSendMsg(JSON.stringify({
|
|
|
|
messageType: 'hello',
|
|
|
|
status: 200,
|
|
|
|
uaid: '3c7462fc-270f-45be-a459-b9d631b0d093'
|
|
|
|
}));
|
|
|
|
this.serverSendMsg(JSON.stringify({
|
|
|
|
messageType: 'notification',
|
|
|
|
updates: records.map(({channelID, version}) =>
|
|
|
|
({channelID, version: ++version}))
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
// Should acknowledge all received updates, even if updating
|
|
|
|
// IndexedDB fails.
|
|
|
|
onACK: ackDone
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let [a, c] = yield waitForPromise(
|
|
|
|
notifyPromise,
|
|
|
|
DEFAULT_TIMEOUT,
|
|
|
|
'Timed out waiting for notifications'
|
|
|
|
);
|
|
|
|
let aPush = a.subject.QueryInterface(Ci.nsIPushObserverNotification);
|
|
|
|
equal(aPush.pushEndpoint, 'https://example.org/update/success-1',
|
|
|
|
'Wrong endpoint for notification A');
|
|
|
|
equal(aPush.version, 2, 'Wrong version for notification A');
|
|
|
|
|
|
|
|
let cPush = c.subject.QueryInterface(Ci.nsIPushObserverNotification);
|
|
|
|
equal(cPush.pushEndpoint, 'https://example.org/update/success-2',
|
|
|
|
'Wrong endpoint for notification C');
|
|
|
|
equal(cPush.version, 4, 'Wrong version for notification C');
|
|
|
|
|
2015-08-15 03:26:07 +03:00
|
|
|
yield waitForPromise(ackPromise, DEFAULT_TIMEOUT,
|
2015-04-21 21:10:50 +03:00
|
|
|
'Timed out waiting for acknowledgements');
|
|
|
|
|
2015-06-24 23:34:54 +03:00
|
|
|
let aRecord = yield db.getByIdentifiers({scope: 'https://example.com/a',
|
|
|
|
originAttributes: originAttributes });
|
2015-04-21 21:10:50 +03:00
|
|
|
equal(aRecord.channelID, 'f04f1e46-9139-4826-b2d1-9411b0821283',
|
|
|
|
'Wrong channel ID for record A');
|
|
|
|
strictEqual(aRecord.version, 2,
|
|
|
|
'Should return the new version for record A');
|
|
|
|
|
2015-06-24 23:34:54 +03:00
|
|
|
let bRecord = yield db.getByIdentifiers({scope: 'https://example.com/b',
|
|
|
|
originAttributes: originAttributes });
|
2015-04-21 21:10:50 +03:00
|
|
|
equal(bRecord.channelID, '3c3930ba-44de-40dc-a7ca-8a133ec1a866',
|
|
|
|
'Wrong channel ID for record B');
|
|
|
|
strictEqual(bRecord.version, 2,
|
|
|
|
'Should return the previous version for record B');
|
|
|
|
|
2015-06-24 23:34:54 +03:00
|
|
|
let cRecord = yield db.getByIdentifiers({scope: 'https://example.com/c',
|
|
|
|
originAttributes: originAttributes });
|
2015-04-21 21:10:50 +03:00
|
|
|
equal(cRecord.channelID, 'b63f7bef-0a0d-4236-b41e-086a69dfd316',
|
|
|
|
'Wrong channel ID for record C');
|
|
|
|
strictEqual(cRecord.version, 4,
|
|
|
|
'Should return the new version for record C');
|
|
|
|
});
|