extensions/cookie/test/unit/test_permmanager_notifications.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial