2015-11-17 05:34:00 +00:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {PushDB, PushService, PushServiceWebSocket} = serviceExports;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
|
|
|
|
const userAgentID = 'aaabf1f8-2f68-44f1-a920-b88e9e7d7559';
|
|
|
|
const nsIPushQuotaManager = Components.interfaces.nsIPushQuotaManager;
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
do_get_profile();
|
|
|
|
setPrefs({
|
|
|
|
userAgentID,
|
2015-12-16 17:21:22 +00:00
|
|
|
'testing.ignorePermission': true,
|
2015-11-17 05:34:00 +00:00
|
|
|
});
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
add_task(function* test_expiration_origin_threshold() {
|
|
|
|
let db = PushServiceWebSocket.newPushDB();
|
|
|
|
do_register_cleanup(() => {
|
2015-12-08 20:41:57 +00:00
|
|
|
PushService.notificationForOriginClosed("https://example.com");
|
2015-12-16 17:21:22 +00:00
|
|
|
return db.drop().then(_ => db.close());
|
2015-11-17 05:34:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Simulate a notification being shown for the origin,
|
|
|
|
// this should relax the quota and allow as many push messages
|
|
|
|
// as we want.
|
2015-12-08 20:41:57 +00:00
|
|
|
PushService.notificationForOriginShown("https://example.com");
|
2015-11-17 05:34:00 +00:00
|
|
|
|
|
|
|
yield db.put({
|
|
|
|
channelID: 'f56645a9-1f32-4655-92ad-ddc37f6d54fb',
|
|
|
|
pushEndpoint: 'https://example.org/push/1',
|
|
|
|
scope: 'https://example.com/quota',
|
|
|
|
pushCount: 0,
|
|
|
|
lastPush: 0,
|
|
|
|
version: null,
|
|
|
|
originAttributes: '',
|
|
|
|
quota: 16,
|
|
|
|
});
|
|
|
|
|
|
|
|
// A visit one day ago should provide a quota of 8 messages.
|
2016-02-08 13:42:07 +00:00
|
|
|
yield PlacesTestUtils.addVisits({
|
2015-11-17 05:34:00 +00:00
|
|
|
uri: 'https://example.com/login',
|
|
|
|
title: 'Sign in to see your auctions',
|
2016-05-10 03:54:05 +00:00
|
|
|
visitDate: (Date.now() - MS_IN_ONE_DAY) * 1000,
|
2016-02-08 13:42:07 +00:00
|
|
|
transition: Ci.nsINavHistoryService.TRANSITION_LINK
|
2015-11-17 05:34:00 +00:00
|
|
|
});
|
|
|
|
|
2016-05-20 00:43:21 +00:00
|
|
|
let numMessages = 10;
|
2015-11-17 05:34:00 +00:00
|
|
|
|
|
|
|
let updates = 0;
|
2016-03-03 22:37:10 +00:00
|
|
|
let notifyPromise = promiseObserverNotification(PushServiceComponent.pushTopic, (subject, data) => {
|
2015-12-08 20:41:57 +00:00
|
|
|
updates++;
|
2015-11-17 05:34:00 +00:00
|
|
|
return updates == numMessages;
|
|
|
|
});
|
|
|
|
|
2016-05-02 15:20:14 +00:00
|
|
|
let modifications = 0;
|
|
|
|
let modifiedPromise = promiseObserverNotification(PushServiceComponent.subscriptionModifiedTopic, (subject, data) => {
|
|
|
|
// Each subscription should be modified twice: once to update the message
|
|
|
|
// count and last push time, and the second time to update the quota.
|
|
|
|
modifications++;
|
|
|
|
return modifications == numMessages * 2;
|
|
|
|
});
|
|
|
|
|
2015-11-17 05:34:00 +00:00
|
|
|
let updateQuotaPromise = new Promise((resolve, reject) => {
|
|
|
|
let quotaUpdateCount = 0;
|
|
|
|
PushService._updateQuotaTestCallback = function() {
|
|
|
|
quotaUpdateCount++;
|
2016-05-10 03:54:05 +00:00
|
|
|
if (quotaUpdateCount == numMessages) {
|
2015-11-17 05:34:00 +00:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// If the origin has visible notifications, the
|
|
|
|
// message should not affect quota.
|
|
|
|
for (let version = 1; version <= 10; version++) {
|
|
|
|
this.serverSendMsg(JSON.stringify({
|
|
|
|
messageType: 'notification',
|
|
|
|
updates: [{
|
|
|
|
channelID: 'f56645a9-1f32-4655-92ad-ddc37f6d54fb',
|
|
|
|
version,
|
|
|
|
}],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onUnregister(request) {
|
|
|
|
ok(false, "Channel should not be unregistered.");
|
|
|
|
},
|
|
|
|
// We expect to receive acks, but don't care about their
|
|
|
|
// contents.
|
|
|
|
onACK(request) {},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-03-19 21:57:15 +00:00
|
|
|
yield notifyPromise;
|
2015-11-17 05:34:00 +00:00
|
|
|
|
2016-03-19 21:57:15 +00:00
|
|
|
yield updateQuotaPromise;
|
2016-05-02 15:20:14 +00:00
|
|
|
yield modifiedPromise;
|
2015-11-17 05:34:00 +00:00
|
|
|
|
|
|
|
let expiredRecord = yield db.getByKeyID('f56645a9-1f32-4655-92ad-ddc37f6d54fb');
|
|
|
|
notStrictEqual(expiredRecord.quota, 0, 'Expired record not updated');
|
|
|
|
});
|