michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Test that the permissionmanager 'added', 'changed', 'deleted', and 'cleared' michael@0: // notifications behave as expected. michael@0: michael@0: let test_generator = do_run_test(); michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: test_generator.next(); michael@0: } michael@0: michael@0: function continue_test() michael@0: { michael@0: do_run_generator(test_generator); michael@0: } michael@0: michael@0: function do_run_test() { michael@0: // Set up a profile. michael@0: let profile = do_get_profile(); michael@0: michael@0: let pm = Services.perms; michael@0: let now = Number(Date.now()); michael@0: let permType = "test/expiration-perm"; michael@0: let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"] michael@0: .getService(Ci.nsIScriptSecurityManager) michael@0: .getNoAppCodebasePrincipal(NetUtil.newURI("http://example.com")); michael@0: michael@0: let observer = new permission_observer(test_generator, now, permType); michael@0: Services.obs.addObserver(observer, "perm-changed", false); michael@0: michael@0: // Add a permission, to test the 'add' notification. Note that we use michael@0: // do_execute_soon() so that we can use our generator to continue the test michael@0: // where we left off. michael@0: do_execute_soon(function() { michael@0: pm.addFromPrincipal(principal, permType, pm.ALLOW_ACTION, pm.EXPIRE_TIME, now + 100000); michael@0: }); michael@0: yield; michael@0: michael@0: // Alter a permission, to test the 'changed' notification. michael@0: do_execute_soon(function() { michael@0: pm.addFromPrincipal(principal, permType, pm.ALLOW_ACTION, pm.EXPIRE_TIME, now + 200000); michael@0: }); michael@0: yield; michael@0: michael@0: // Remove a permission, to test the 'deleted' notification. michael@0: do_execute_soon(function() { michael@0: pm.removeFromPrincipal(principal, permType); michael@0: }); michael@0: yield; michael@0: michael@0: // Clear permissions, to test the 'cleared' notification. michael@0: do_execute_soon(function() { michael@0: pm.removeAll(); michael@0: }); michael@0: yield; michael@0: michael@0: Services.obs.removeObserver(observer, "perm-changed"); michael@0: do_check_eq(observer.adds, 1); michael@0: do_check_eq(observer.changes, 1); michael@0: do_check_eq(observer.deletes, 1); michael@0: do_check_true(observer.cleared); michael@0: michael@0: do_finish_generator_test(test_generator); michael@0: } michael@0: michael@0: function permission_observer(generator, now, type) { michael@0: // Set up our observer object. michael@0: this.generator = generator; michael@0: this.pm = Services.perms; michael@0: this.now = now; michael@0: this.type = type; michael@0: this.adds = 0; michael@0: this.changes = 0; michael@0: this.deletes = 0; michael@0: this.cleared = false; michael@0: } michael@0: michael@0: permission_observer.prototype = { michael@0: observe: function(subject, topic, data) { michael@0: do_check_eq(topic, "perm-changed"); michael@0: michael@0: // "deleted" means a permission was deleted. aPermission is the deleted permission. michael@0: // "added" means a permission was added. aPermission is the added permission. michael@0: // "changed" means a permission was altered. aPermission is the new permission. michael@0: // "cleared" means the entire permission list was cleared. aPermission is null. michael@0: if (data == "added") { michael@0: var perm = subject.QueryInterface(Ci.nsIPermission); michael@0: this.adds++; michael@0: switch (this.adds) { michael@0: case 1: michael@0: do_check_eq(this.type, perm.type); michael@0: do_check_eq(this.pm.EXPIRE_TIME, perm.expireType); michael@0: do_check_eq(this.now + 100000, perm.expireTime); michael@0: break; michael@0: default: michael@0: do_throw("too many add notifications posted."); michael@0: } michael@0: michael@0: } else if (data == "changed") { michael@0: let perm = subject.QueryInterface(Ci.nsIPermission); michael@0: this.changes++; michael@0: switch (this.changes) { michael@0: case 1: michael@0: do_check_eq(this.type, perm.type); michael@0: do_check_eq(this.pm.EXPIRE_TIME, perm.expireType); michael@0: do_check_eq(this.now + 200000, perm.expireTime); michael@0: break; michael@0: default: michael@0: do_throw("too many change notifications posted."); michael@0: } michael@0: michael@0: } else if (data == "deleted") { michael@0: var perm = subject.QueryInterface(Ci.nsIPermission); michael@0: this.deletes++; michael@0: switch (this.deletes) { michael@0: case 1: michael@0: do_check_eq(this.type, perm.type); michael@0: break; michael@0: default: michael@0: do_throw("too many delete notifications posted."); michael@0: } michael@0: michael@0: } else if (data == "cleared") { michael@0: // only clear once: at the end michael@0: do_check_false(this.cleared); michael@0: do_check_eq(do_count_enumerator(Services.perms.enumerator), 0); michael@0: this.cleared = true; michael@0: michael@0: } else { michael@0: do_throw("unexpected data '" + data + "'!"); michael@0: } michael@0: michael@0: // Continue the test. michael@0: do_run_generator(this.generator); michael@0: }, michael@0: }; michael@0: