toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     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 xpinstall.[whitelist|blacklist].add preferences are emptied when
     1.9 +// converted into permissions.
    1.10 +
    1.11 +const PREF_XPI_WHITELIST_PERMISSIONS  = "xpinstall.whitelist.add";
    1.12 +const PREF_XPI_BLACKLIST_PERMISSIONS  = "xpinstall.blacklist.add";
    1.13 +
    1.14 +function do_check_permission_prefs(preferences) {
    1.15 +  // Check preferences were emptied
    1.16 +  for (let pref of preferences) {
    1.17 +    try {
    1.18 +      do_check_eq(Services.prefs.getCharPref(pref), "");
    1.19 +    }
    1.20 +    catch (e) {
    1.21 +      // Successfully emptied
    1.22 +    }
    1.23 +  }
    1.24 +}
    1.25 +
    1.26 +function clear_imported_preferences_cache() {
    1.27 +  let scope = Components.utils.import("resource://gre/modules/PermissionsUtils.jsm", {});
    1.28 +  scope.gImportedPrefBranches.clear();
    1.29 +}
    1.30 +
    1.31 +function run_test() {
    1.32 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
    1.33 +
    1.34 +  // Create own preferences to test
    1.35 +  Services.prefs.setCharPref("xpinstall.whitelist.add.EMPTY", "");
    1.36 +  Services.prefs.setCharPref("xpinstall.whitelist.add.TEST", "whitelist.example.com");
    1.37 +  Services.prefs.setCharPref("xpinstall.blacklist.add.EMPTY", "");
    1.38 +  Services.prefs.setCharPref("xpinstall.blacklist.add.TEST", "blacklist.example.com");
    1.39 +
    1.40 +  // Get list of preferences to check
    1.41 +  var whitelistPreferences = Services.prefs.getChildList(PREF_XPI_WHITELIST_PERMISSIONS, {});
    1.42 +  var blacklistPreferences = Services.prefs.getChildList(PREF_XPI_BLACKLIST_PERMISSIONS, {});
    1.43 +  var preferences = whitelistPreferences.concat(blacklistPreferences);
    1.44 +
    1.45 +  startupManager();
    1.46 +
    1.47 +  // Permissions are imported lazily - act as thought we're checking an install,
    1.48 +  // to trigger on-deman importing of the permissions.
    1.49 +  let url = Services.io.newURI("http://example.com/file.xpi", null, null);
    1.50 +  AddonManager.isInstallAllowed("application/x-xpinstall", url);
    1.51 +  do_check_permission_prefs(preferences);
    1.52 +
    1.53 +
    1.54 +  // Import can also be triggerred by an observer notification by any other area
    1.55 +  // of code, such as a permissions management UI.
    1.56 +
    1.57 +  // First, request to flush all permissions
    1.58 +  clear_imported_preferences_cache();
    1.59 +  Services.prefs.setCharPref("xpinstall.whitelist.add.TEST2", "whitelist2.example.com");
    1.60 +  Services.obs.notifyObservers(null, "flush-pending-permissions", "install");
    1.61 +  do_check_permission_prefs(preferences);
    1.62 +
    1.63 +  // Then, request to flush just install permissions
    1.64 +  clear_imported_preferences_cache();
    1.65 +  Services.prefs.setCharPref("xpinstall.whitelist.add.TEST3", "whitelist3.example.com");
    1.66 +  Services.obs.notifyObservers(null, "flush-pending-permissions", "");
    1.67 +  do_check_permission_prefs(preferences);
    1.68 +
    1.69 +  // And a request to flush some other permissions sholdn't flush install permissions
    1.70 +  clear_imported_preferences_cache();
    1.71 +  Services.prefs.setCharPref("xpinstall.whitelist.add.TEST4", "whitelist4.example.com");
    1.72 +  Services.obs.notifyObservers(null, "flush-pending-permissions", "lolcats");
    1.73 +  do_check_eq(Services.prefs.getCharPref("xpinstall.whitelist.add.TEST4"), "whitelist4.example.com");
    1.74 +}

mercurial