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