test_permmanager_default_pref.js (2769B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 function run_test() { 5 let principal = 6 Services.scriptSecurityManager.createContentPrincipalFromOrigin( 7 "https://example.org" 8 ); 9 10 // Check that without a pref the default return value is UNKNOWN. 11 Assert.equal( 12 Services.perms.testPermissionFromPrincipal(principal, "camera"), 13 Services.perms.UNKNOWN_ACTION 14 ); 15 16 // Check that invalid prefs are ignored. 17 Services.prefs.setIntPref( 18 "permissions.default.camera", 19 Services.perms.MAX_VALID_ACTION + 1 20 ); 21 Assert.equal( 22 Services.perms.testPermissionFromPrincipal(principal, "camera"), 23 Services.perms.UNKNOWN_ACTION 24 ); 25 26 Services.prefs.setIntPref("permissions.default.camera", -1); 27 Assert.equal( 28 Services.perms.testPermissionFromPrincipal(principal, "camera"), 29 Services.perms.UNKNOWN_ACTION 30 ); 31 32 // Check that the default return value changed after setting the pref. 33 Services.prefs.setIntPref( 34 "permissions.default.camera", 35 Services.perms.DENY_ACTION 36 ); 37 Assert.equal( 38 Services.perms.testPermissionFromPrincipal(principal, "camera"), 39 Services.perms.DENY_ACTION 40 ); 41 42 // Check that functions that do not directly return a permission value still 43 // consider the permission as being set to its default. 44 Assert.equal( 45 null, 46 Services.perms.getPermissionObject(principal, "camera", false) 47 ); 48 49 // Check that other permissions still return UNKNOWN. 50 Assert.equal( 51 Services.perms.testPermissionFromPrincipal(principal, "geo"), 52 Services.perms.UNKNOWN_ACTION 53 ); 54 55 // Check that the default return value changed after changing the pref. 56 Services.prefs.setIntPref( 57 "permissions.default.camera", 58 Services.perms.ALLOW_ACTION 59 ); 60 Assert.equal( 61 Services.perms.testPermissionFromPrincipal(principal, "camera"), 62 Services.perms.ALLOW_ACTION 63 ); 64 65 // Check that the preference is ignored if there is a value. 66 Services.perms.addFromPrincipal( 67 principal, 68 "camera", 69 Services.perms.DENY_ACTION 70 ); 71 Assert.equal( 72 Services.perms.testPermissionFromPrincipal(principal, "camera"), 73 Services.perms.DENY_ACTION 74 ); 75 Assert.notEqual( 76 Services.perms.getPermissionObject(principal, "camera", false), 77 null 78 ); 79 80 // The preference should be honored again, after resetting the permissions. 81 Services.perms.removeAll(); 82 Assert.equal( 83 Services.perms.testPermissionFromPrincipal(principal, "camera"), 84 Services.perms.ALLOW_ACTION 85 ); 86 87 // Should be UNKNOWN after clearing the pref. 88 Services.prefs.clearUserPref("permissions.default.camera"); 89 Assert.equal( 90 Services.perms.testPermissionFromPrincipal(principal, "camera"), 91 Services.perms.UNKNOWN_ACTION 92 ); 93 }