2010-06-21 17:50:35 -07:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
2010-11-12 09:32:36 -08:00
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
2009-10-16 14:01:04 -07:00
|
|
|
|
2010-10-21 11:36:14 -07:00
|
|
|
// Test that the permissionmanager 'added', 'changed', 'deleted', and 'cleared'
|
|
|
|
// notifications behave as expected.
|
|
|
|
|
|
|
|
let test_generator = do_run_test();
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
do_test_pending();
|
|
|
|
test_generator.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
function continue_test()
|
|
|
|
{
|
|
|
|
do_run_generator(test_generator);
|
|
|
|
}
|
2009-10-16 14:01:04 -07:00
|
|
|
|
2010-10-21 11:36:14 -07:00
|
|
|
function do_run_test() {
|
|
|
|
// Set up a profile.
|
|
|
|
let profile = do_get_profile();
|
|
|
|
|
|
|
|
let pm = Services.permissions;
|
|
|
|
let permURI = NetUtil.newURI("http://example.com");
|
|
|
|
let now = Number(Date.now());
|
|
|
|
let permType = "test/expiration-perm";
|
|
|
|
|
|
|
|
let observer = new permission_observer(test_generator, now, permType);
|
|
|
|
Services.obs.addObserver(observer, "perm-changed", false);
|
|
|
|
|
|
|
|
// Add a permission, to test the 'add' notification. Note that we use
|
|
|
|
// do_execute_soon() so that we can use our generator to continue the test
|
|
|
|
// where we left off.
|
|
|
|
do_execute_soon(function() {
|
|
|
|
pm.add(permURI, permType, pm.ALLOW_ACTION, pm.EXPIRE_TIME, now + 100000);
|
|
|
|
});
|
|
|
|
yield;
|
|
|
|
|
|
|
|
// Alter a permission, to test the 'changed' notification.
|
|
|
|
do_execute_soon(function() {
|
|
|
|
pm.add(permURI, permType, pm.ALLOW_ACTION, pm.EXPIRE_TIME, now + 200000);
|
|
|
|
});
|
|
|
|
yield;
|
|
|
|
|
|
|
|
// Remove a permission, to test the 'deleted' notification.
|
|
|
|
do_execute_soon(function() {
|
|
|
|
pm.remove(permURI.asciiHost, permType);
|
|
|
|
});
|
|
|
|
yield;
|
|
|
|
|
|
|
|
// Clear permissions, to test the 'cleared' notification.
|
|
|
|
do_execute_soon(function() {
|
|
|
|
pm.removeAll();
|
|
|
|
});
|
|
|
|
yield;
|
|
|
|
|
|
|
|
Services.obs.removeObserver(observer, "perm-changed");
|
|
|
|
do_check_eq(observer.adds, 1);
|
|
|
|
do_check_eq(observer.changes, 1);
|
|
|
|
do_check_eq(observer.deletes, 1);
|
|
|
|
do_check_true(observer.cleared);
|
|
|
|
|
|
|
|
do_finish_generator_test(test_generator);
|
|
|
|
}
|
|
|
|
|
|
|
|
function permission_observer(generator, now, type) {
|
|
|
|
// Set up our observer object.
|
|
|
|
this.generator = generator;
|
|
|
|
this.pm = Services.permissions;
|
|
|
|
this.now = now;
|
|
|
|
this.type = type;
|
|
|
|
this.adds = 0;
|
|
|
|
this.changes = 0;
|
|
|
|
this.deletes = 0;
|
|
|
|
this.cleared = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
permission_observer.prototype = {
|
|
|
|
observe: function(subject, topic, data) {
|
|
|
|
do_check_eq(topic, "perm-changed");
|
2009-10-16 14:01:04 -07:00
|
|
|
|
|
|
|
// "deleted" means a permission was deleted. aPermission is the deleted permission.
|
|
|
|
// "added" means a permission was added. aPermission is the added permission.
|
|
|
|
// "changed" means a permission was altered. aPermission is the new permission.
|
|
|
|
// "cleared" means the entire permission list was cleared. aPermission is null.
|
|
|
|
if (data == "added") {
|
|
|
|
var perm = subject.QueryInterface(Ci.nsIPermission);
|
2010-10-21 11:36:14 -07:00
|
|
|
this.adds++;
|
|
|
|
switch (this.adds) {
|
|
|
|
case 1:
|
|
|
|
do_check_eq(this.type, perm.type);
|
|
|
|
do_check_eq(this.pm.EXPIRE_TIME, perm.expireType);
|
|
|
|
do_check_eq(this.now + 100000, perm.expireTime);
|
2009-10-16 14:01:04 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
do_throw("too many add notifications posted.");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (data == "changed") {
|
2010-10-21 11:36:14 -07:00
|
|
|
let perm = subject.QueryInterface(Ci.nsIPermission);
|
|
|
|
this.changes++;
|
|
|
|
switch (this.changes) {
|
2009-10-16 14:01:04 -07:00
|
|
|
case 1:
|
2010-10-21 11:36:14 -07:00
|
|
|
do_check_eq(this.type, perm.type);
|
|
|
|
do_check_eq(this.pm.EXPIRE_TIME, perm.expireType);
|
|
|
|
do_check_eq(this.now + 200000, perm.expireTime);
|
2009-10-16 14:01:04 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
do_throw("too many change notifications posted.");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (data == "deleted") {
|
|
|
|
var perm = subject.QueryInterface(Ci.nsIPermission);
|
2010-10-21 11:36:14 -07:00
|
|
|
this.deletes++;
|
|
|
|
switch (this.deletes) {
|
2009-10-16 14:01:04 -07:00
|
|
|
case 1:
|
2010-10-21 11:36:14 -07:00
|
|
|
do_check_eq(this.type, perm.type);
|
2009-10-16 14:01:04 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
do_throw("too many delete notifications posted.");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (data == "cleared") {
|
|
|
|
// only clear once: at the end
|
2010-10-21 11:36:14 -07:00
|
|
|
do_check_false(this.cleared);
|
|
|
|
do_check_eq(do_count_enumerator(Services.permissions.enumerator), 0);
|
|
|
|
this.cleared = true;
|
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
} else {
|
2010-10-21 11:36:14 -07:00
|
|
|
do_throw("unexpected data '" + data + "'!");
|
2009-10-16 14:01:04 -07:00
|
|
|
}
|
2010-10-21 11:36:14 -07:00
|
|
|
|
|
|
|
// Continue the test.
|
|
|
|
do_run_generator(this.generator);
|
2009-10-16 14:01:04 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|