test_record.js (3293B)
1 "use strict"; 2 3 const { PushRecord } = ChromeUtils.importESModule( 4 "resource://gre/modules/PushRecord.sys.mjs" 5 ); 6 7 add_task(async function test_updateQuota() { 8 let record = new PushRecord({ 9 quota: 8, 10 lastPush: Date.now() - 1 * MS_IN_ONE_DAY, 11 }); 12 13 record.updateQuota(Date.now() - 2 * MS_IN_ONE_DAY); 14 equal( 15 record.quota, 16 8, 17 "Should not update quota if last visit is older than last push" 18 ); 19 20 record.updateQuota(Date.now()); 21 equal( 22 record.quota, 23 16, 24 "Should reset quota if last visit is newer than last push" 25 ); 26 27 record.reduceQuota(); 28 equal(record.quota, 15, "Should reduce quota"); 29 30 // Make sure we calculate the quota correctly for visit dates in the 31 // future (bug 1206424). 32 record.updateQuota(Date.now() + 1 * MS_IN_ONE_DAY); 33 equal( 34 record.quota, 35 16, 36 "Should reset quota to maximum if last visit is in the future" 37 ); 38 39 record.updateQuota(-1); 40 strictEqual(record.quota, 0, "Should set quota to 0 if history was cleared"); 41 ok(record.isExpired(), "Should expire records once the quota reaches 0"); 42 record.reduceQuota(); 43 strictEqual(record.quota, 0, "Quota should never be negative"); 44 }); 45 46 add_task(async function test_systemRecord_updateQuota() { 47 let systemRecord = new PushRecord({ 48 quota: Infinity, 49 systemRecord: true, 50 }); 51 systemRecord.updateQuota(Date.now() - 3 * MS_IN_ONE_DAY); 52 equal( 53 systemRecord.quota, 54 Infinity, 55 "System subscriptions should ignore quota updates" 56 ); 57 systemRecord.updateQuota(-1); 58 equal( 59 systemRecord.quota, 60 Infinity, 61 "System subscriptions should ignore the last visit time" 62 ); 63 systemRecord.reduceQuota(); 64 equal( 65 systemRecord.quota, 66 Infinity, 67 "System subscriptions should ignore quota reductions" 68 ); 69 }); 70 71 function testPermissionCheck(props) { 72 let record = new PushRecord(props); 73 let originSuffix; 74 equal( 75 record.uri.spec, 76 props.scope, 77 `Record URI should match scope URL for ${JSON.stringify(props)}` 78 ); 79 if (props.originAttributes) { 80 originSuffix = ChromeUtils.originAttributesToSuffix( 81 record.principal.originAttributes 82 ); 83 equal( 84 originSuffix, 85 props.originAttributes, 86 `Origin suffixes should match for ${JSON.stringify(props)}` 87 ); 88 } 89 ok( 90 !record.hasPermission(), 91 `Record ${JSON.stringify(props)} should not have permission yet` 92 ); 93 // Adding permission from origin string 94 PermissionTestUtils.add( 95 props.scope + (originSuffix || ""), 96 "desktop-notification", 97 Ci.nsIPermissionManager.ALLOW_ACTION 98 ); 99 try { 100 ok( 101 record.hasPermission(), 102 `Record ${JSON.stringify(props)} should have permission` 103 ); 104 } finally { 105 PermissionTestUtils.remove( 106 props.scope + (originSuffix || ""), 107 "desktop-notification" 108 ); 109 } 110 } 111 112 add_task(async function test_principal_permissions() { 113 let testProps = [ 114 { 115 scope: "https://example.com/", 116 }, 117 { 118 scope: "https://example.com/", 119 originAttributes: "^userContextId=1", 120 }, 121 { 122 scope: "https://xn--90aexm.xn--80ag3aejvc.xn--p1ai/", 123 }, 124 { 125 scope: "https://xn--90aexm.xn--80ag3aejvc.xn--p1ai/", 126 originAttributes: "^userContextId=1", 127 }, 128 ]; 129 for (let props of testProps) { 130 testPermissionCheck(props); 131 } 132 });