test_quota_with_notification.js (3508B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const userAgentID = "aaabf1f8-2f68-44f1-a920-b88e9e7d7559"; 7 8 function run_test() { 9 do_get_profile(); 10 setPrefs({ 11 userAgentID, 12 "testing.ignorePermission": true, 13 }); 14 run_next_test(); 15 } 16 17 add_task(async function test_expiration_origin_threshold() { 18 let db = PushServiceWebSocket.newPushDB(); 19 registerCleanupFunction(() => { 20 PushService.notificationForOriginClosed("https://example.com"); 21 return db.drop().then(_ => db.close()); 22 }); 23 24 // Simulate a notification being shown for the origin, 25 // this should relax the quota and allow as many push messages 26 // as we want. 27 PushService.notificationForOriginShown("https://example.com"); 28 29 await db.put({ 30 channelID: "f56645a9-1f32-4655-92ad-ddc37f6d54fb", 31 pushEndpoint: "https://example.org/push/1", 32 scope: "https://example.com/quota", 33 pushCount: 0, 34 lastPush: 0, 35 version: null, 36 originAttributes: "", 37 quota: 16, 38 }); 39 40 // A visit one day ago should provide a quota of 8 messages. 41 await PlacesTestUtils.addVisits({ 42 uri: "https://example.com/login", 43 title: "Sign in to see your auctions", 44 visitDate: (Date.now() - MS_IN_ONE_DAY) * 1000, 45 transition: Ci.nsINavHistoryService.TRANSITION_LINK, 46 }); 47 48 let numMessages = 10; 49 50 let updates = 0; 51 let notifyPromise = promiseObserverNotification( 52 PushServiceComponent.pushTopic, 53 () => { 54 updates++; 55 return updates == numMessages; 56 } 57 ); 58 59 let modifications = 0; 60 let modifiedPromise = promiseObserverNotification( 61 PushServiceComponent.subscriptionModifiedTopic, 62 () => { 63 // Each subscription should be modified twice: once to update the message 64 // count and last push time, and the second time to update the quota. 65 modifications++; 66 return modifications == numMessages * 2; 67 } 68 ); 69 70 let updateQuotaPromise = new Promise(resolve => { 71 let quotaUpdateCount = 0; 72 PushService._updateQuotaTestCallback = function () { 73 quotaUpdateCount++; 74 if (quotaUpdateCount == numMessages) { 75 resolve(); 76 } 77 }; 78 }); 79 80 PushService.init({ 81 serverURI: "wss://push.example.org/", 82 db, 83 makeWebSocket(uri) { 84 return new MockWebSocket(uri, { 85 onHello() { 86 this.serverSendMsg( 87 JSON.stringify({ 88 messageType: "hello", 89 status: 200, 90 uaid: userAgentID, 91 }) 92 ); 93 94 // If the origin has visible notifications, the 95 // message should not affect quota. 96 for (let version = 1; version <= 10; version++) { 97 this.serverSendMsg( 98 JSON.stringify({ 99 messageType: "notification", 100 updates: [ 101 { 102 channelID: "f56645a9-1f32-4655-92ad-ddc37f6d54fb", 103 version, 104 }, 105 ], 106 }) 107 ); 108 } 109 }, 110 onUnregister() { 111 ok(false, "Channel should not be unregistered."); 112 }, 113 // We expect to receive acks, but don't care about their 114 // contents. 115 onACK() {}, 116 }); 117 }, 118 }); 119 120 await notifyPromise; 121 122 await updateQuotaPromise; 123 await modifiedPromise; 124 125 let expiredRecord = await db.getByKeyID( 126 "f56645a9-1f32-4655-92ad-ddc37f6d54fb" 127 ); 128 notStrictEqual(expiredRecord.quota, 0, "Expired record not updated"); 129 });