2015-06-25 21:52:57 +00:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {PushDB, PushService, PushServiceWebSocket} = serviceExports;
|
|
|
|
|
|
|
|
const userAgentID = '28cd09e2-7506-42d8-9e50-b02785adc7ef';
|
|
|
|
|
2015-11-10 22:27:47 +00:00
|
|
|
var db;
|
|
|
|
|
2015-06-25 21:52:57 +00:00
|
|
|
function run_test() {
|
|
|
|
do_get_profile();
|
|
|
|
setPrefs({
|
|
|
|
userAgentID,
|
|
|
|
});
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
2015-11-10 22:27:47 +00:00
|
|
|
let putRecord = Task.async(function* (perm, record) {
|
|
|
|
let uri = Services.io.newURI(record.scope, null, null);
|
|
|
|
|
|
|
|
Services.perms.add(uri, 'desktop-notification',
|
|
|
|
Ci.nsIPermissionManager[perm]);
|
|
|
|
do_register_cleanup(() => {
|
|
|
|
Services.perms.remove(uri, 'desktop-notification');
|
|
|
|
});
|
|
|
|
|
|
|
|
yield db.put(record);
|
|
|
|
});
|
|
|
|
|
2015-06-25 21:52:57 +00:00
|
|
|
add_task(function* test_expiration_history_observer() {
|
2015-11-10 22:27:47 +00:00
|
|
|
db = PushServiceWebSocket.newPushDB();
|
2015-06-25 21:52:57 +00:00
|
|
|
do_register_cleanup(() => db.drop().then(_ => db.close()));
|
|
|
|
|
|
|
|
// A registration that we'll expire...
|
2015-11-10 22:27:47 +00:00
|
|
|
yield putRecord('ALLOW_ACTION', {
|
2015-06-25 21:52:57 +00:00
|
|
|
channelID: '379c0668-8323-44d2-a315-4ee83f1a9ee9',
|
|
|
|
pushEndpoint: 'https://example.org/push/1',
|
|
|
|
scope: 'https://example.com/deals',
|
|
|
|
pushCount: 0,
|
|
|
|
lastPush: 0,
|
|
|
|
version: null,
|
|
|
|
originAttributes: '',
|
|
|
|
quota: 16,
|
|
|
|
});
|
|
|
|
|
2015-10-06 15:14:25 +00:00
|
|
|
// ...And a registration that we'll evict on startup.
|
2015-11-10 22:27:47 +00:00
|
|
|
yield putRecord('ALLOW_ACTION', {
|
2015-10-06 15:14:25 +00:00
|
|
|
channelID: '4cb6e454-37cf-41c4-a013-4e3a7fdd0bf1',
|
|
|
|
pushEndpoint: 'https://example.org/push/3',
|
|
|
|
scope: 'https://example.com/stuff',
|
2015-06-25 21:52:57 +00:00
|
|
|
pushCount: 0,
|
|
|
|
lastPush: 0,
|
|
|
|
version: null,
|
|
|
|
originAttributes: '',
|
|
|
|
quota: 0,
|
|
|
|
});
|
|
|
|
|
2016-02-08 13:42:07 +00:00
|
|
|
yield PlacesTestUtils.addVisits({
|
2015-06-25 21:52:57 +00:00
|
|
|
uri: 'https://example.com/infrequent',
|
|
|
|
title: 'Infrequently-visited page',
|
2016-02-08 13:42:07 +00:00
|
|
|
visitDate: (Date.now() - 14 * 24 * 60 * 60 * 1000) * 1000,
|
|
|
|
transition: Ci.nsINavHistoryService.TRANSITION_LINK
|
2015-06-25 21:52:57 +00:00
|
|
|
});
|
|
|
|
|
2015-08-15 00:26:07 +00:00
|
|
|
let unregisterDone;
|
|
|
|
let unregisterPromise = new Promise(resolve => unregisterDone = resolve);
|
2016-03-03 22:37:10 +00:00
|
|
|
let subChangePromise = promiseObserverNotification(PushServiceComponent.subscriptionChangeTopic, (subject, data) =>
|
2015-10-06 15:14:25 +00:00
|
|
|
data == 'https://example.com/stuff');
|
2015-06-25 21:52:57 +00:00
|
|
|
|
|
|
|
PushService.init({
|
|
|
|
serverURI: 'wss://push.example.org/',
|
|
|
|
db,
|
|
|
|
makeWebSocket(uri) {
|
|
|
|
return new MockWebSocket(uri, {
|
|
|
|
onHello(request) {
|
|
|
|
this.serverSendMsg(JSON.stringify({
|
|
|
|
messageType: 'hello',
|
|
|
|
status: 200,
|
|
|
|
uaid: userAgentID,
|
|
|
|
}));
|
|
|
|
this.serverSendMsg(JSON.stringify({
|
|
|
|
messageType: 'notification',
|
|
|
|
updates: [{
|
|
|
|
channelID: '379c0668-8323-44d2-a315-4ee83f1a9ee9',
|
|
|
|
version: 2,
|
|
|
|
}],
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
onUnregister(request) {
|
|
|
|
equal(request.channelID, '379c0668-8323-44d2-a315-4ee83f1a9ee9', 'Dropped wrong channel ID');
|
2016-03-28 19:29:25 +00:00
|
|
|
equal(request.code, 201, 'Expected quota exceeded unregister reason');
|
2015-08-15 00:26:07 +00:00
|
|
|
unregisterDone();
|
2015-06-25 21:52:57 +00:00
|
|
|
},
|
|
|
|
onACK(request) {},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-19 21:57:15 +00:00
|
|
|
yield subChangePromise;
|
|
|
|
yield unregisterPromise;
|
2015-06-25 21:52:57 +00:00
|
|
|
|
|
|
|
let expiredRecord = yield db.getByKeyID('379c0668-8323-44d2-a315-4ee83f1a9ee9');
|
|
|
|
strictEqual(expiredRecord.quota, 0, 'Expired record not updated');
|
|
|
|
|
|
|
|
let notifiedScopes = [];
|
2016-03-03 22:37:10 +00:00
|
|
|
subChangePromise = promiseObserverNotification(PushServiceComponent.subscriptionChangeTopic, (subject, data) => {
|
2015-06-25 21:52:57 +00:00
|
|
|
notifiedScopes.push(data);
|
|
|
|
return notifiedScopes.length == 2;
|
|
|
|
});
|
|
|
|
|
2016-06-27 15:22:59 +00:00
|
|
|
// Add an expired registration that we'll revive later using the idle
|
|
|
|
// observer.
|
2015-11-10 22:27:47 +00:00
|
|
|
yield putRecord('ALLOW_ACTION', {
|
2015-10-06 15:14:25 +00:00
|
|
|
channelID: 'eb33fc90-c883-4267-b5cb-613969e8e349',
|
|
|
|
pushEndpoint: 'https://example.org/push/2',
|
|
|
|
scope: 'https://example.com/auctions',
|
|
|
|
pushCount: 0,
|
|
|
|
lastPush: 0,
|
|
|
|
version: null,
|
|
|
|
originAttributes: '',
|
|
|
|
quota: 0,
|
|
|
|
});
|
2016-06-27 15:22:59 +00:00
|
|
|
// ...And an expired registration that we'll revive on fetch.
|
|
|
|
yield putRecord('ALLOW_ACTION', {
|
|
|
|
channelID: '6b2d13fe-d848-4c5f-bdda-e9fc89727dca',
|
|
|
|
pushEndpoint: 'https://example.org/push/4',
|
|
|
|
scope: 'https://example.net/sales',
|
|
|
|
pushCount: 0,
|
|
|
|
lastPush: 0,
|
|
|
|
version: null,
|
|
|
|
originAttributes: '',
|
|
|
|
quota: 0,
|
|
|
|
});
|
2015-10-06 15:14:25 +00:00
|
|
|
|
2015-06-25 21:52:57 +00:00
|
|
|
// Now visit the site...
|
2016-02-08 13:42:07 +00:00
|
|
|
yield PlacesTestUtils.addVisits({
|
2015-06-25 21:52:57 +00:00
|
|
|
uri: 'https://example.com/another-page',
|
|
|
|
title: 'Infrequently-visited page',
|
2016-02-08 13:42:07 +00:00
|
|
|
visitDate: Date.now() * 1000,
|
|
|
|
transition: Ci.nsINavHistoryService.TRANSITION_LINK
|
2015-06-25 21:52:57 +00:00
|
|
|
});
|
|
|
|
Services.obs.notifyObservers(null, 'idle-daily', '');
|
|
|
|
|
|
|
|
// And we should receive notifications for both scopes.
|
2016-03-19 21:57:15 +00:00
|
|
|
yield subChangePromise;
|
2015-06-25 21:52:57 +00:00
|
|
|
deepEqual(notifiedScopes.sort(), [
|
|
|
|
'https://example.com/auctions',
|
|
|
|
'https://example.com/deals'
|
|
|
|
], 'Wrong scopes for subscription changes');
|
|
|
|
|
|
|
|
let aRecord = yield db.getByKeyID('379c0668-8323-44d2-a315-4ee83f1a9ee9');
|
|
|
|
ok(!aRecord, 'Should drop expired record');
|
|
|
|
|
|
|
|
let bRecord = yield db.getByKeyID('eb33fc90-c883-4267-b5cb-613969e8e349');
|
|
|
|
ok(!bRecord, 'Should drop evicted record');
|
2016-06-27 15:22:59 +00:00
|
|
|
|
|
|
|
// Simulate a visit to a site with an expired registration, then fetch the
|
|
|
|
// record. This should drop the expired record and fire an observer
|
|
|
|
// notification.
|
|
|
|
yield PlacesTestUtils.addVisits({
|
|
|
|
uri: 'https://example.net/sales',
|
|
|
|
title: 'Firefox plushies, 99% off',
|
|
|
|
visitDate: Date.now() * 1000,
|
|
|
|
transition: Ci.nsINavHistoryService.TRANSITION_LINK
|
|
|
|
});
|
|
|
|
subChangePromise = promiseObserverNotification(PushServiceComponent.subscriptionChangeTopic, (subject, data) => {
|
|
|
|
if (data == 'https://example.net/sales') {
|
|
|
|
ok(subject.isCodebasePrincipal,
|
|
|
|
'Should pass subscription principal as the subject');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let record = yield PushService.registration({
|
|
|
|
scope: 'https://example.net/sales',
|
|
|
|
originAttributes: '',
|
|
|
|
});
|
|
|
|
ok(!record, 'Should not return evicted record');
|
|
|
|
ok(!(yield db.getByKeyID('6b2d13fe-d848-4c5f-bdda-e9fc89727dca')),
|
|
|
|
'Should drop evicted record on fetch');
|
|
|
|
yield subChangePromise;
|
2015-06-25 21:52:57 +00:00
|
|
|
});
|