tor-browser

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

test_stickyprefs.js (6818B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/licenses/publicdomain/  */
      3 
      4 const ps = Services.prefs;
      5 
      6 // It is necessary to manually disable `xpc::IsInAutomation` since
      7 // `resetPrefs` will flip the preference to re-enable `once`-synced
      8 // preference change assertions, and also change the value of those
      9 // preferences.
     10 Services.prefs.setBoolPref(
     11  "security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
     12  false
     13 );
     14 
     15 // A little helper to reset the service and load one pref file.
     16 function resetAndLoadDefaults() {
     17  ps.resetPrefs();
     18  ps.readDefaultPrefsFromFile(do_get_file("data/testPrefSticky.js"));
     19 }
     20 
     21 // A little helper to reset the service and load two pref files.
     22 function resetAndLoadAll() {
     23  ps.resetPrefs();
     24  ps.readDefaultPrefsFromFile(do_get_file("data/testPrefSticky.js"));
     25  ps.readUserPrefsFromFile(do_get_file("data/testPrefStickyUser.js"));
     26 }
     27 
     28 // A little helper that saves the current state to a file in the profile
     29 // dir, then resets the service and re-reads the file it just saved.
     30 // Used to test what gets actually written - things the pref service decided
     31 // not to write don't exist at all after this call.
     32 function saveAndReload() {
     33  let file = do_get_profile();
     34  file.append("prefs.js");
     35  ps.savePrefFile(file);
     36 
     37  // Now reset the pref service and re-read what we saved.
     38  ps.resetPrefs();
     39 
     40  // Hack alert: on Windows nsLocalFile caches the size of savePrefFile from
     41  // the .create() call above as 0. We call .exists() to reset the cache.
     42  file.exists();
     43 
     44  ps.readUserPrefsFromFile(file);
     45 }
     46 
     47 // A sticky pref should not be written if the value is unchanged.
     48 add_test(function notWrittenWhenUnchanged() {
     49  resetAndLoadDefaults();
     50  Assert.strictEqual(ps.getBoolPref("testPref.unsticky.bool"), true);
     51  Assert.strictEqual(ps.getBoolPref("testPref.sticky.bool"), false);
     52 
     53  // write prefs - but we haven't changed the sticky one, so it shouldn't be written.
     54  saveAndReload();
     55  // sticky should not have been written to the new file.
     56  try {
     57    ps.getBoolPref("testPref.sticky.bool");
     58    Assert.ok(false, "expected failure reading this pref");
     59  } catch (ex) {
     60    Assert.ok(ex, "exception reading regular pref");
     61  }
     62  run_next_test();
     63 });
     64 
     65 // Loading a sticky `pref` then a `user_pref` for the same pref means it should
     66 // always be written.
     67 add_test(function writtenOnceLoadedWithoutChange() {
     68  // Load the same pref file *as well as* a pref file that has a user_pref for
     69  // our sticky with the default value. It should be re-written without us
     70  // touching it.
     71  resetAndLoadAll();
     72  // reset and re-read what we just wrote - it should be written.
     73  saveAndReload();
     74  Assert.strictEqual(
     75    ps.getBoolPref("testPref.sticky.bool"),
     76    false,
     77    "user_pref was written with default value"
     78  );
     79  run_next_test();
     80 });
     81 
     82 // If a sticky pref is explicicitly changed, even to the default, it is written.
     83 add_test(function writtenOnceLoadedWithChangeNonDefault() {
     84  // Load the same pref file *as well as* a pref file that has a user_pref for
     85  // our sticky - then change the pref. It should be written.
     86  resetAndLoadAll();
     87  // Set a new val and check we wrote it.
     88  ps.setBoolPref("testPref.sticky.bool", false);
     89  saveAndReload();
     90  Assert.strictEqual(
     91    ps.getBoolPref("testPref.sticky.bool"),
     92    false,
     93    "user_pref was written with custom value"
     94  );
     95  run_next_test();
     96 });
     97 
     98 // If a sticky pref is changed to the non-default value, it is written.
     99 add_test(function writtenOnceLoadedWithChangeNonDefault() {
    100  // Load the same pref file *as well as* a pref file that has a user_pref for
    101  // our sticky - then change the pref. It should be written.
    102  resetAndLoadAll();
    103  // Set a new val and check we wrote it.
    104  ps.setBoolPref("testPref.sticky.bool", true);
    105  saveAndReload();
    106  Assert.strictEqual(
    107    ps.getBoolPref("testPref.sticky.bool"),
    108    true,
    109    "user_pref was written with custom value"
    110  );
    111  run_next_test();
    112 });
    113 
    114 // Test that prefHasUserValue always returns true whenever there is a sticky
    115 // value, even when that value matches the default. This is mainly for
    116 // about:config semantics - prefs with a sticky value always remain bold and
    117 // always offer "reset" (which fully resets and drops the sticky value as if
    118 // the pref had never changed.)
    119 add_test(function hasUserValue() {
    120  // sticky pref without user value.
    121  resetAndLoadDefaults();
    122  Assert.strictEqual(ps.getBoolPref("testPref.sticky.bool"), false);
    123  Assert.ok(
    124    !ps.prefHasUserValue("testPref.sticky.bool"),
    125    "should not initially reflect a user value"
    126  );
    127 
    128  ps.setBoolPref("testPref.sticky.bool", false);
    129  Assert.ok(
    130    ps.prefHasUserValue("testPref.sticky.bool"),
    131    "should reflect a user value after set to default"
    132  );
    133 
    134  ps.setBoolPref("testPref.sticky.bool", true);
    135  Assert.ok(
    136    ps.prefHasUserValue("testPref.sticky.bool"),
    137    "should reflect a user value after change to non-default"
    138  );
    139 
    140  ps.clearUserPref("testPref.sticky.bool");
    141  Assert.ok(
    142    !ps.prefHasUserValue("testPref.sticky.bool"),
    143    "should reset to no user value"
    144  );
    145  ps.setBoolPref("testPref.sticky.bool", false, "expected default");
    146 
    147  // And make sure the pref immediately reflects a user value after load.
    148  resetAndLoadAll();
    149  Assert.strictEqual(ps.getBoolPref("testPref.sticky.bool"), false);
    150  Assert.ok(
    151    ps.prefHasUserValue("testPref.sticky.bool"),
    152    "should have a user value when loaded value is the default"
    153  );
    154  run_next_test();
    155 });
    156 
    157 // Test that clearUserPref removes the "sticky" value.
    158 add_test(function clearUserPref() {
    159  // load things such that we have a sticky value which is the same as the
    160  // default.
    161  resetAndLoadAll();
    162  ps.clearUserPref("testPref.sticky.bool");
    163 
    164  // Once we save prefs the sticky pref should no longer be written.
    165  saveAndReload();
    166  try {
    167    ps.getBoolPref("testPref.sticky.bool");
    168    Assert.ok(false, "expected failure reading this pref");
    169  } catch (ex) {
    170    Assert.ok(ex, "pref doesn't have a sticky value");
    171  }
    172  run_next_test();
    173 });
    174 
    175 // Test that a pref observer gets a notification fired when a sticky pref
    176 // has it's value changed to the same value as the default. The reason for
    177 // this behaviour is that later we might have other code that cares about a
    178 // pref being sticky (IOW, we notify due to the "state" of the pref changing
    179 // even if the value has not)
    180 add_test(function observerFires() {
    181  // load things so there's no sticky value.
    182  resetAndLoadDefaults();
    183 
    184  function observe(subject, topic, data) {
    185    Assert.equal(data, "testPref.sticky.bool");
    186    ps.removeObserver("testPref.sticky.bool", observe);
    187    run_next_test();
    188  }
    189  ps.addObserver("testPref.sticky.bool", observe);
    190 
    191  ps.setBoolPref(
    192    "testPref.sticky.bool",
    193    ps.getBoolPref("testPref.sticky.bool")
    194  );
    195  // and the observer will fire triggering the next text.
    196 });