summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/unit/test_permmanager_notifications.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /extensions/permissions/test/unit/test_permmanager_notifications.js
parentInitial commit. (diff)
downloadfirefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz
firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'extensions/permissions/test/unit/test_permmanager_notifications.js')
-rw-r--r--extensions/permissions/test/unit/test_permmanager_notifications.js139
1 files changed, 139 insertions, 0 deletions
diff --git a/extensions/permissions/test/unit/test_permmanager_notifications.js b/extensions/permissions/test/unit/test_permmanager_notifications.js
new file mode 100644
index 0000000000..fb851aa04b
--- /dev/null
+++ b/extensions/permissions/test/unit/test_permmanager_notifications.js
@@ -0,0 +1,139 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Test that the permissionmanager 'added', 'changed', 'deleted', and 'cleared'
+// notifications behave as expected.
+
+var test_generator = do_run_test();
+
+function run_test() {
+ Services.prefs.setCharPref("permissions.manager.defaultsUrl", "");
+ do_test_pending();
+ test_generator.next();
+}
+
+function* do_run_test() {
+ let pm = Services.perms;
+ let now = Number(Date.now());
+ let permType = "test/expiration-perm";
+ let ssm = Services.scriptSecurityManager;
+ let uri = NetUtil.newURI("http://example.com");
+ let principal = ssm.createContentPrincipal(uri, {});
+
+ let observer = new permission_observer(test_generator, now, permType);
+ Services.obs.addObserver(observer, "perm-changed");
+
+ // 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.
+ executeSoon(function () {
+ pm.addFromPrincipal(
+ principal,
+ permType,
+ pm.ALLOW_ACTION,
+ pm.EXPIRE_TIME,
+ now + 100000
+ );
+ });
+ yield;
+
+ // Alter a permission, to test the 'changed' notification.
+ executeSoon(function () {
+ pm.addFromPrincipal(
+ principal,
+ permType,
+ pm.ALLOW_ACTION,
+ pm.EXPIRE_TIME,
+ now + 200000
+ );
+ });
+ yield;
+
+ // Remove a permission, to test the 'deleted' notification.
+ executeSoon(function () {
+ pm.removeFromPrincipal(principal, permType);
+ });
+ yield;
+
+ // Clear permissions, to test the 'cleared' notification.
+ executeSoon(function () {
+ pm.removeAll();
+ });
+ yield;
+
+ Services.obs.removeObserver(observer, "perm-changed");
+ Assert.equal(observer.adds, 1);
+ Assert.equal(observer.changes, 1);
+ Assert.equal(observer.deletes, 1);
+ Assert.ok(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.perms;
+ this.now = now;
+ this.type = type;
+ this.adds = 0;
+ this.changes = 0;
+ this.deletes = 0;
+ this.cleared = false;
+}
+
+permission_observer.prototype = {
+ observe(subject, topic, data) {
+ Assert.equal(topic, "perm-changed");
+
+ // "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") {
+ let perm = subject.QueryInterface(Ci.nsIPermission);
+ this.adds++;
+ switch (this.adds) {
+ case 1:
+ Assert.equal(this.type, perm.type);
+ Assert.equal(this.pm.EXPIRE_TIME, perm.expireType);
+ Assert.equal(this.now + 100000, perm.expireTime);
+ break;
+ default:
+ do_throw("too many add notifications posted.");
+ }
+ } else if (data == "changed") {
+ let perm = subject.QueryInterface(Ci.nsIPermission);
+ this.changes++;
+ switch (this.changes) {
+ case 1:
+ Assert.equal(this.type, perm.type);
+ Assert.equal(this.pm.EXPIRE_TIME, perm.expireType);
+ Assert.equal(this.now + 200000, perm.expireTime);
+ break;
+ default:
+ do_throw("too many change notifications posted.");
+ }
+ } else if (data == "deleted") {
+ let perm = subject.QueryInterface(Ci.nsIPermission);
+ this.deletes++;
+ switch (this.deletes) {
+ case 1:
+ Assert.equal(this.type, perm.type);
+ break;
+ default:
+ do_throw("too many delete notifications posted.");
+ }
+ } else if (data == "cleared") {
+ // only clear once: at the end
+ Assert.ok(!this.cleared);
+ Assert.equal(do_count_array(Services.perms.all), 0);
+ this.cleared = true;
+ } else {
+ do_throw("unexpected data '" + data + "'!");
+ }
+
+ // Continue the test.
+ do_run_generator(this.generator);
+ },
+};