summaryrefslogtreecommitdiffstats
path: root/extensions/cookie/test/unit/test_permmanager_removepermission.js
blob: e8f5817e241507f9a0acbde11a40e2974d918aa3 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

function run_test() {
  // initialize the permission manager service
  let pm = Cc["@mozilla.org/permissionmanager;1"].
        getService(Ci.nsIPermissionManager);

  do_check_eq(perm_count(), 0);

  // add some permissions
  let uri = NetUtil.newURI("http://amazon.com:8080/foobarbaz", null, null);
  let uri2 = NetUtil.newURI("http://google.com:2048/quxx", null, null);

  pm.add(uri, "apple", 0);
  pm.add(uri, "apple", 3);
  pm.add(uri, "pear", 3);
  pm.add(uri, "pear", 1);
  pm.add(uri, "cucumber", 1);
  pm.add(uri, "cucumber", 1);
  pm.add(uri, "cucumber", 1);

  pm.add(uri2, "apple", 2);
  pm.add(uri2, "pear", 0);
  pm.add(uri2, "pear", 2);

  // Make sure that removePermission doesn't remove more than one permission each time
  do_check_eq(perm_count(), 5);

  remove_one_by_type("apple");
  do_check_eq(perm_count(), 4);

  remove_one_by_type("apple");
  do_check_eq(perm_count(), 3);

  remove_one_by_type("pear");
  do_check_eq(perm_count(), 2);

  remove_one_by_type("cucumber");
  do_check_eq(perm_count(), 1);

  remove_one_by_type("pear");
  do_check_eq(perm_count(), 0);


  function perm_count() {
    let enumerator = pm.enumerator;
    let count = 0;
    while (enumerator.hasMoreElements()) {
      count++;
      enumerator.getNext();
    }

    return count;
  }

  function remove_one_by_type(type) {
    let enumerator = pm.enumerator;
    while (enumerator.hasMoreElements()) {
      let it = enumerator.getNext().QueryInterface(Ci.nsIPermission);
      if (it.type == type) {
        pm.removePermission(it);
        break;
      }
    }
  }
}