extensions/cookie/test/unit/test_permmanager_notifications.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/cookie/test/unit/test_permmanager_notifications.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Test that the permissionmanager 'added', 'changed', 'deleted', and 'cleared'
     1.8 +// notifications behave as expected.
     1.9 +
    1.10 +let test_generator = do_run_test();
    1.11 +
    1.12 +function run_test() {
    1.13 +  do_test_pending();
    1.14 +  test_generator.next();
    1.15 +}
    1.16 +
    1.17 +function continue_test()
    1.18 +{
    1.19 +  do_run_generator(test_generator);
    1.20 +}
    1.21 +
    1.22 +function do_run_test() {
    1.23 +  // Set up a profile.
    1.24 +  let profile = do_get_profile();
    1.25 +
    1.26 +  let pm = Services.perms;
    1.27 +  let now = Number(Date.now());
    1.28 +  let permType = "test/expiration-perm";
    1.29 +  let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
    1.30 +                    .getService(Ci.nsIScriptSecurityManager)
    1.31 +                    .getNoAppCodebasePrincipal(NetUtil.newURI("http://example.com"));
    1.32 +
    1.33 +  let observer = new permission_observer(test_generator, now, permType);
    1.34 +  Services.obs.addObserver(observer, "perm-changed", false);
    1.35 +
    1.36 +  // Add a permission, to test the 'add' notification. Note that we use
    1.37 +  // do_execute_soon() so that we can use our generator to continue the test
    1.38 +  // where we left off.
    1.39 +  do_execute_soon(function() {
    1.40 +    pm.addFromPrincipal(principal, permType, pm.ALLOW_ACTION, pm.EXPIRE_TIME, now + 100000);
    1.41 +  });
    1.42 +  yield;
    1.43 +
    1.44 +  // Alter a permission, to test the 'changed' notification.
    1.45 +  do_execute_soon(function() {
    1.46 +    pm.addFromPrincipal(principal, permType, pm.ALLOW_ACTION, pm.EXPIRE_TIME, now + 200000);
    1.47 +  });
    1.48 +  yield;
    1.49 +
    1.50 +  // Remove a permission, to test the 'deleted' notification.
    1.51 +  do_execute_soon(function() {
    1.52 +    pm.removeFromPrincipal(principal, permType);
    1.53 +  });
    1.54 +  yield;
    1.55 +
    1.56 +  // Clear permissions, to test the 'cleared' notification.
    1.57 +  do_execute_soon(function() {
    1.58 +    pm.removeAll();
    1.59 +  });
    1.60 +  yield;
    1.61 +
    1.62 +  Services.obs.removeObserver(observer, "perm-changed");
    1.63 +  do_check_eq(observer.adds, 1);
    1.64 +  do_check_eq(observer.changes, 1);
    1.65 +  do_check_eq(observer.deletes, 1);
    1.66 +  do_check_true(observer.cleared);
    1.67 +
    1.68 +  do_finish_generator_test(test_generator);
    1.69 +}
    1.70 +
    1.71 +function permission_observer(generator, now, type) {
    1.72 +  // Set up our observer object.
    1.73 +  this.generator = generator;
    1.74 +  this.pm = Services.perms;
    1.75 +  this.now = now;
    1.76 +  this.type = type;
    1.77 +  this.adds = 0;
    1.78 +  this.changes = 0;
    1.79 +  this.deletes = 0;
    1.80 +  this.cleared = false;
    1.81 +}
    1.82 +
    1.83 +permission_observer.prototype = {
    1.84 +  observe: function(subject, topic, data) {
    1.85 +    do_check_eq(topic, "perm-changed");
    1.86 +
    1.87 +    // "deleted" means a permission was deleted. aPermission is the deleted permission.
    1.88 +    // "added"   means a permission was added. aPermission is the added permission.
    1.89 +    // "changed" means a permission was altered. aPermission is the new permission.
    1.90 +    // "cleared" means the entire permission list was cleared. aPermission is null.
    1.91 +    if (data == "added") {
    1.92 +      var perm = subject.QueryInterface(Ci.nsIPermission);
    1.93 +      this.adds++;
    1.94 +      switch (this.adds) {
    1.95 +        case 1:
    1.96 +          do_check_eq(this.type, perm.type);
    1.97 +          do_check_eq(this.pm.EXPIRE_TIME, perm.expireType);
    1.98 +          do_check_eq(this.now + 100000, perm.expireTime);
    1.99 +          break;
   1.100 +        default:
   1.101 +          do_throw("too many add notifications posted.");
   1.102 +      }
   1.103 +
   1.104 +    } else if (data == "changed") {
   1.105 +      let perm = subject.QueryInterface(Ci.nsIPermission);
   1.106 +      this.changes++;
   1.107 +      switch (this.changes) {
   1.108 +        case 1:
   1.109 +          do_check_eq(this.type, perm.type);
   1.110 +          do_check_eq(this.pm.EXPIRE_TIME, perm.expireType);
   1.111 +          do_check_eq(this.now + 200000, perm.expireTime);
   1.112 +          break;
   1.113 +        default:
   1.114 +          do_throw("too many change notifications posted.");
   1.115 +      }
   1.116 +
   1.117 +    } else if (data == "deleted") {
   1.118 +      var perm = subject.QueryInterface(Ci.nsIPermission);
   1.119 +      this.deletes++;
   1.120 +      switch (this.deletes) {
   1.121 +        case 1:
   1.122 +          do_check_eq(this.type, perm.type);
   1.123 +          break;
   1.124 +        default:
   1.125 +          do_throw("too many delete notifications posted.");
   1.126 +      }
   1.127 +
   1.128 +    } else if (data == "cleared") {
   1.129 +      // only clear once: at the end
   1.130 +      do_check_false(this.cleared);
   1.131 +      do_check_eq(do_count_enumerator(Services.perms.enumerator), 0);
   1.132 +      this.cleared = true;
   1.133 +
   1.134 +    } else {
   1.135 +      do_throw("unexpected data '" + data + "'!");
   1.136 +    }
   1.137 +
   1.138 +    // Continue the test.
   1.139 +    do_run_generator(this.generator);
   1.140 +  },
   1.141 +};
   1.142 +

mercurial