tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_RecommendedPreferences.js (4466B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 const { RecommendedPreferences } = ChromeUtils.importESModule(
      6  "chrome://remote/content/shared/RecommendedPreferences.sys.mjs"
      7 );
      8 
      9 const COMMON_PREF = "toolkit.startup.max_resumed_crashes";
     10 
     11 const PROTOCOL_1_PREF = "dom.disable_beforeunload";
     12 const PROTOCOL_1_RECOMMENDED_PREFS = new Map([[PROTOCOL_1_PREF, true]]);
     13 
     14 const PROTOCOL_2_PREF = "browser.contentblocking.features.standard";
     15 const PROTOCOL_2_RECOMMENDED_PREFS = new Map([
     16  [PROTOCOL_2_PREF, "-tp,tpPrivate,cookieBehavior0,-cm,-fp"],
     17 ]);
     18 
     19 function cleanup() {
     20  info("Restore recommended preferences and test preferences");
     21  Services.prefs.clearUserPref("remote.prefs.recommended");
     22  RecommendedPreferences.restoreAllPreferences();
     23 }
     24 
     25 // cleanup() should be called:
     26 // - explicitly after each test to avoid side effects
     27 // - via registerCleanupFunction in case a test crashes/times out
     28 registerCleanupFunction(cleanup);
     29 
     30 add_task(async function test_multipleClients() {
     31  info("Check initial values for the test preferences");
     32  checkPreferences({ common: false, protocol_1: false, protocol_2: false });
     33 
     34  checkPreferences({ common: false, protocol_1: false, protocol_2: false });
     35 
     36  info("Apply recommended preferences for a protocol_1 client");
     37  RecommendedPreferences.applyPreferences(PROTOCOL_1_RECOMMENDED_PREFS);
     38  checkPreferences({ common: true, protocol_1: true, protocol_2: false });
     39 
     40  info("Apply recommended preferences for a protocol_2 client");
     41  RecommendedPreferences.applyPreferences(PROTOCOL_2_RECOMMENDED_PREFS);
     42  checkPreferences({ common: true, protocol_1: true, protocol_2: true });
     43 
     44  info("Restore protocol_1 preferences");
     45  RecommendedPreferences.restorePreferences(PROTOCOL_1_RECOMMENDED_PREFS);
     46  checkPreferences({ common: true, protocol_1: false, protocol_2: true });
     47 
     48  info("Restore protocol_2 preferences");
     49  RecommendedPreferences.restorePreferences(PROTOCOL_2_RECOMMENDED_PREFS);
     50  checkPreferences({ common: true, protocol_1: false, protocol_2: false });
     51 
     52  info("Restore all the altered preferences");
     53  RecommendedPreferences.restoreAllPreferences();
     54  checkPreferences({ common: false, protocol_1: false, protocol_2: false });
     55 
     56  info("Attempts to restore again");
     57  RecommendedPreferences.restoreAllPreferences();
     58  checkPreferences({ common: false, protocol_1: false, protocol_2: false });
     59 
     60  cleanup();
     61 });
     62 
     63 add_task(async function test_disabled() {
     64  info("Disable RecommendedPreferences");
     65  Services.prefs.setBoolPref("remote.prefs.recommended", false);
     66 
     67  info("Check initial values for the test preferences");
     68  checkPreferences({ common: false, protocol_1: false, protocol_2: false });
     69 
     70  info("Recommended preferences are not applied, applyPreferences is a no-op");
     71  RecommendedPreferences.applyPreferences(PROTOCOL_1_RECOMMENDED_PREFS);
     72  checkPreferences({ common: false, protocol_1: false, protocol_2: false });
     73 
     74  cleanup();
     75 });
     76 
     77 add_task(async function test_noCustomPreferences() {
     78  info("Applying preferences without any custom preference should not throw");
     79 
     80  // First call invokes setting of default preferences
     81  RecommendedPreferences.applyPreferences();
     82 
     83  // Second call does nothing
     84  RecommendedPreferences.applyPreferences();
     85 
     86  cleanup();
     87 });
     88 
     89 // Check that protocols can override common preferences.
     90 add_task(async function test_override() {
     91  info("Make sure the common preference has no user value");
     92  Services.prefs.clearUserPref(COMMON_PREF);
     93 
     94  const OVERRIDE_VALUE = 42;
     95  const OVERRIDE_COMMON_PREF = new Map([[COMMON_PREF, OVERRIDE_VALUE]]);
     96 
     97  info("Apply a map of preferences overriding a common preference");
     98  RecommendedPreferences.applyPreferences(OVERRIDE_COMMON_PREF);
     99 
    100  equal(
    101    Services.prefs.getIntPref(COMMON_PREF),
    102    OVERRIDE_VALUE,
    103    "The common preference was set to the expected value"
    104  );
    105 
    106  cleanup();
    107 });
    108 
    109 function checkPreferences({ common, protocol_1, protocol_2 }) {
    110  checkPreference(COMMON_PREF, { hasValue: common });
    111  checkPreference(PROTOCOL_1_PREF, { hasValue: protocol_1 });
    112  checkPreference(PROTOCOL_2_PREF, { hasValue: protocol_2 });
    113 }
    114 
    115 function checkPreference(pref, { hasValue }) {
    116  equal(
    117    Services.prefs.prefHasUserValue(pref),
    118    hasValue,
    119    hasValue
    120      ? `The preference ${pref} has a user value`
    121      : `The preference ${pref} has no user value`
    122  );
    123 }