summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/unit/test_permmanager_default_pref.js
blob: 3759594bf5190723cf4f6fb03a8252617bc291f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

function run_test() {
  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      "https://example.org"
    );

  // Check that without a pref the default return value is UNKNOWN.
  Assert.equal(
    Services.perms.testPermissionFromPrincipal(principal, "camera"),
    Services.perms.UNKNOWN_ACTION
  );

  // Check that the default return value changed after setting the pref.
  Services.prefs.setIntPref(
    "permissions.default.camera",
    Services.perms.DENY_ACTION
  );
  Assert.equal(
    Services.perms.testPermissionFromPrincipal(principal, "camera"),
    Services.perms.DENY_ACTION
  );

  // Check that functions that do not directly return a permission value still
  // consider the permission as being set to its default.
  Assert.equal(
    null,
    Services.perms.getPermissionObject(principal, "camera", false)
  );

  // Check that other permissions still return UNKNOWN.
  Assert.equal(
    Services.perms.testPermissionFromPrincipal(principal, "geo"),
    Services.perms.UNKNOWN_ACTION
  );

  // Check that the default return value changed after changing the pref.
  Services.prefs.setIntPref(
    "permissions.default.camera",
    Services.perms.ALLOW_ACTION
  );
  Assert.equal(
    Services.perms.testPermissionFromPrincipal(principal, "camera"),
    Services.perms.ALLOW_ACTION
  );

  // Check that the preference is ignored if there is a value.
  Services.perms.addFromPrincipal(
    principal,
    "camera",
    Services.perms.DENY_ACTION
  );
  Assert.equal(
    Services.perms.testPermissionFromPrincipal(principal, "camera"),
    Services.perms.DENY_ACTION
  );
  Assert.ok(
    Services.perms.getPermissionObject(principal, "camera", false) != null
  );

  // The preference should be honored again, after resetting the permissions.
  Services.perms.removeAll();
  Assert.equal(
    Services.perms.testPermissionFromPrincipal(principal, "camera"),
    Services.perms.ALLOW_ACTION
  );

  // Should be UNKNOWN after clearing the pref.
  Services.prefs.clearUserPref("permissions.default.camera");
  Assert.equal(
    Services.perms.testPermissionFromPrincipal(principal, "camera"),
    Services.perms.UNKNOWN_ACTION
  );
}