toolkit/modules/tests/xpcshell/test_PermissionsUtils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/tests/xpcshell/test_PermissionsUtils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,58 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +// Tests that PerrmissionsUtils.jsm works as expected, including:
     1.9 +// * PermissionsUtils.importfromPrefs()
    1.10 +//      <ROOT>.[whitelist|blacklist].add preferences are emptied when
    1.11 +//       converted into permissions on startup.
    1.12 +
    1.13 +
    1.14 +const PREF_ROOT = "testpermissions.";
    1.15 +const TEST_PERM = "text-permission";
    1.16 +
    1.17 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.18 +Components.utils.import("resource://gre/modules/PermissionsUtils.jsm");
    1.19 +
    1.20 +function run_test() {
    1.21 +  test_importfromPrefs();
    1.22 +}
    1.23 +
    1.24 +
    1.25 +function test_importfromPrefs() {
    1.26 +  // Create own preferences to test
    1.27 +  Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY", "");
    1.28 +  Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY2", ",");
    1.29 +  Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST", "whitelist.example.com");
    1.30 +  Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST2", "whitelist2-1.example.com,whitelist2-2.example.com");
    1.31 +  Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.EMPTY", "");
    1.32 +  Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST", "blacklist.example.com,");
    1.33 +  Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST2", ",blacklist2-1.example.com,blacklist2-2.example.com");
    1.34 +
    1.35 +  // Import them
    1.36 +  PermissionsUtils.importFromPrefs(PREF_ROOT, TEST_PERM);
    1.37 +
    1.38 +  // Get list of preferences to check
    1.39 +  let preferences = Services.prefs.getChildList(PREF_ROOT, {});
    1.40 +
    1.41 +  // Check preferences were emptied
    1.42 +  for (let pref of preferences) {
    1.43 +    do_check_eq(Services.prefs.getCharPref(pref), "");
    1.44 +  }
    1.45 +
    1.46 +  // Check they were imported into the permissions manager
    1.47 +  let whitelisted = ["whitelist.example.com",
    1.48 +                     "whitelist2-1.example.com",
    1.49 +                     "whitelist2-2.example.com"];
    1.50 +  let blacklisted = ["blacklist.example.com",
    1.51 +                     "blacklist2-1.example.com",
    1.52 +                     "blacklist2-2.example.com"];
    1.53 +  for (let url of whitelisted) {
    1.54 +    let uri = Services.io.newURI("http://" + url, null, null);
    1.55 +    do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.ALLOW_ACTION);
    1.56 +  }
    1.57 +  for (let url of blacklisted) {
    1.58 +    let uri = Services.io.newURI("http://" + url, null, null);
    1.59 +    do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.DENY_ACTION);
    1.60 +  }
    1.61 +}

mercurial