test_permmanager_notifications.js (4123B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test that the permissionmanager 'added', 'changed', 'deleted', and 'cleared' 5 // notifications behave as expected. 6 7 var test_generator = do_run_test(); 8 9 function run_test() { 10 Services.prefs.setCharPref("permissions.manager.defaultsUrl", ""); 11 do_test_pending(); 12 test_generator.next(); 13 } 14 15 function* do_run_test() { 16 let pm = Services.perms; 17 let now = Number(Date.now()); 18 let permType = "test/expiration-perm"; 19 let ssm = Services.scriptSecurityManager; 20 let uri = NetUtil.newURI("http://example.com"); 21 let principal = ssm.createContentPrincipal(uri, {}); 22 23 let observer = new permission_observer(test_generator, now, permType); 24 Services.obs.addObserver(observer, "perm-changed"); 25 26 // Add a permission, to test the 'add' notification. Note that we use 27 // do_execute_soon() so that we can use our generator to continue the test 28 // where we left off. 29 executeSoon(function () { 30 pm.addFromPrincipal( 31 principal, 32 permType, 33 pm.ALLOW_ACTION, 34 pm.EXPIRE_TIME, 35 now + 100000 36 ); 37 }); 38 yield; 39 40 // Alter a permission, to test the 'changed' notification. 41 executeSoon(function () { 42 pm.addFromPrincipal( 43 principal, 44 permType, 45 pm.ALLOW_ACTION, 46 pm.EXPIRE_TIME, 47 now + 200000 48 ); 49 }); 50 yield; 51 52 // Remove a permission, to test the 'deleted' notification. 53 executeSoon(function () { 54 pm.removeFromPrincipal(principal, permType); 55 }); 56 yield; 57 58 // Clear permissions, to test the 'cleared' notification. 59 executeSoon(function () { 60 pm.removeAll(); 61 }); 62 yield; 63 64 Services.obs.removeObserver(observer, "perm-changed"); 65 Assert.equal(observer.adds, 1); 66 Assert.equal(observer.changes, 1); 67 Assert.equal(observer.deletes, 1); 68 Assert.ok(observer.cleared); 69 70 do_finish_generator_test(test_generator); 71 } 72 73 function permission_observer(generator, now, type) { 74 // Set up our observer object. 75 this.generator = generator; 76 this.pm = Services.perms; 77 this.now = now; 78 this.type = type; 79 this.adds = 0; 80 this.changes = 0; 81 this.deletes = 0; 82 this.cleared = false; 83 } 84 85 permission_observer.prototype = { 86 observe(subject, topic, data) { 87 Assert.equal(topic, "perm-changed"); 88 89 // "deleted" means a permission was deleted. aPermission is the deleted permission. 90 // "added" means a permission was added. aPermission is the added permission. 91 // "changed" means a permission was altered. aPermission is the new permission. 92 // "cleared" means the entire permission list was cleared. aPermission is null. 93 if (data == "added") { 94 let perm = subject.QueryInterface(Ci.nsIPermission); 95 this.adds++; 96 switch (this.adds) { 97 case 1: 98 Assert.equal(this.type, perm.type); 99 Assert.equal(this.pm.EXPIRE_TIME, perm.expireType); 100 Assert.equal(this.now + 100000, perm.expireTime); 101 break; 102 default: 103 do_throw("too many add notifications posted."); 104 } 105 } else if (data == "changed") { 106 let perm = subject.QueryInterface(Ci.nsIPermission); 107 this.changes++; 108 switch (this.changes) { 109 case 1: 110 Assert.equal(this.type, perm.type); 111 Assert.equal(this.pm.EXPIRE_TIME, perm.expireType); 112 Assert.equal(this.now + 200000, perm.expireTime); 113 break; 114 default: 115 do_throw("too many change notifications posted."); 116 } 117 } else if (data == "deleted") { 118 let perm = subject.QueryInterface(Ci.nsIPermission); 119 this.deletes++; 120 switch (this.deletes) { 121 case 1: 122 Assert.equal(this.type, perm.type); 123 break; 124 default: 125 do_throw("too many delete notifications posted."); 126 } 127 } else if (data == "cleared") { 128 // only clear once: at the end 129 Assert.ok(!this.cleared); 130 Assert.equal(do_count_array(Services.perms.all), 0); 131 this.cleared = true; 132 } else { 133 do_throw("unexpected data '" + data + "'!"); 134 } 135 136 // Continue the test. 137 do_run_generator(this.generator); 138 }, 139 };