tor-browser

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

test_shared_prefs_lifecycles_methods.js (5231B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_setup(async () => {
      7  startProfileService();
      8  const SelectableProfileService = getSelectableProfileService();
      9  const ProfilesDatastoreService = getProfilesDatastoreService();
     10 
     11  Services.prefs.setIntPref("testPrefInt0", 5);
     12  Services.prefs.setBoolPref("testBoolPref", true);
     13  Services.prefs.setCharPref("testCharPref", "hello");
     14 
     15  SelectableProfileService.constructor.permanentSharedPrefs.splice(
     16    0,
     17    SelectableProfileService.constructor.permanentSharedPrefs.length,
     18    "testPrefInt0",
     19    "testBoolPref",
     20    "testCharPref"
     21  );
     22 
     23  await ProfilesDatastoreService.init();
     24  await SelectableProfileService.init();
     25 
     26  await SelectableProfileService.maybeSetupDataStore();
     27 });
     28 
     29 add_task(async function test_SharedPrefsLifecycle() {
     30  const SelectableProfileService = getSelectableProfileService();
     31  let prefs = await SelectableProfileService.getAllDBPrefs();
     32 
     33  Assert.equal(
     34    prefs.length,
     35    3,
     36    "Should have stored the default prefs into the database."
     37  );
     38 
     39  Assert.equal(
     40    prefs.find(p => p.name == "testPrefInt0")?.value,
     41    5,
     42    "testPrefInt0 should be correct"
     43  );
     44  Assert.equal(
     45    prefs.find(p => p.name == "testBoolPref")?.value,
     46    true,
     47    "testBoolPref should be correct"
     48  );
     49  Assert.equal(
     50    prefs.find(p => p.name == "testCharPref")?.value,
     51    "hello",
     52    "testCharPref should be correct"
     53  );
     54 
     55  Services.prefs.setIntPref("testPrefInt0", 2);
     56  await updateNotified();
     57 
     58  Services.prefs.setBoolPref("testBoolPref", false);
     59  await updateNotified();
     60 
     61  Services.prefs.setCharPref("testCharPref", "goodbye");
     62  await updateNotified();
     63 
     64  prefs = await SelectableProfileService.getAllDBPrefs();
     65 
     66  Assert.equal(
     67    prefs.length,
     68    3,
     69    "Shoulds have stored the default prefs into the database."
     70  );
     71 
     72  Assert.equal(
     73    prefs.find(p => p.name == "testPrefInt0")?.value,
     74    2,
     75    "testPrefInt0 should be correct"
     76  );
     77  Assert.equal(
     78    prefs.find(p => p.name == "testBoolPref")?.value,
     79    false,
     80    "testBoolPref should be correct"
     81  );
     82  Assert.equal(
     83    prefs.find(p => p.name == "testCharPref")?.value,
     84    "goodbye",
     85    "testCharPref should be correct"
     86  );
     87 
     88  Services.prefs.setIntPref("testPrefInt0", 0);
     89  Services.prefs.setIntPref("testPrefInt1", 1);
     90  await SelectableProfileService.trackPref("testPrefInt1");
     91  Services.prefs.setIntPref("testPrefInt2", 2);
     92  await SelectableProfileService.trackPref("testPrefInt2");
     93 
     94  // Notifications are deferred so we should only see one.
     95  await updateNotified();
     96 
     97  await Services.prefs.setCharPref("testPrefString0", "Hello world!");
     98  await SelectableProfileService.trackPref("testPrefString0");
     99  await Services.prefs.setCharPref("testPrefString1", "Hello world 2!");
    100  await SelectableProfileService.trackPref("testPrefString1");
    101 
    102  await Services.prefs.setBoolPref("testPrefBoolTrue", true);
    103  await SelectableProfileService.trackPref("testPrefBoolTrue");
    104  await Services.prefs.setBoolPref("testPrefBoolFalse", false);
    105  await SelectableProfileService.trackPref("testPrefBoolFalse");
    106 
    107  await updateNotified();
    108 
    109  prefs = await SelectableProfileService.getAllDBPrefs();
    110 
    111  Assert.equal(prefs.length, 9, "The right number of  shared prefs exist");
    112 
    113  Assert.equal(
    114    await SelectableProfileService.getDBPref("testPrefInt0"),
    115    0,
    116    "testPrefInt0 value is 0"
    117  );
    118  Assert.equal(
    119    await SelectableProfileService.getDBPref("testPrefInt1"),
    120    1,
    121    "testPrefInt1 value is 1"
    122  );
    123  Assert.equal(
    124    await SelectableProfileService.getDBPref("testPrefInt2"),
    125    2,
    126    "testPrefInt2 value is 2"
    127  );
    128  Assert.equal(
    129    await SelectableProfileService.getDBPref("testPrefString0"),
    130    "Hello world!",
    131    'testPrefString0 value is "Hello world!"'
    132  );
    133  Assert.equal(
    134    await SelectableProfileService.getDBPref("testPrefString1"),
    135    "Hello world 2!",
    136    'testPrefString1 value is "Hello world 2!"'
    137  );
    138  Assert.equal(
    139    await SelectableProfileService.getDBPref("testPrefBoolTrue"),
    140    true,
    141    "testPrefBoolTrue value is true"
    142  );
    143  Assert.equal(
    144    await SelectableProfileService.getDBPref("testPrefBoolFalse"),
    145    false,
    146    "testPrefBoolFalse value is false"
    147  );
    148 
    149  await SelectableProfileService.uninit();
    150 
    151  // Make some changes to the database while the service is shutdown.
    152  let db = await openDatabase();
    153  await db.execute(
    154    "UPDATE SharedPrefs SET value=NULL, isBoolean=FALSE WHERE name=:name;",
    155    { name: "testPrefInt0" }
    156  );
    157  await db.execute(
    158    "UPDATE SharedPrefs SET value=6, isBoolean=FALSE WHERE name=:name;",
    159    { name: "testPrefInt1" }
    160  );
    161  await db.execute(
    162    "UPDATE SharedPrefs SET value=FALSE, isBoolean=TRUE WHERE name=:name;",
    163    { name: "testPrefBoolTrue" }
    164  );
    165  await db.close();
    166 
    167  await SelectableProfileService.init();
    168 
    169  Assert.equal(
    170    Services.prefs.getPrefType("testPrefInt0"),
    171    Ci.nsIPrefBranch.PREF_INVALID,
    172    "Should have cleared the testPrefInt0 pref"
    173  );
    174  Assert.equal(
    175    Services.prefs.getIntPref("testPrefInt1"),
    176    6,
    177    "Should have updated testPrefInt1"
    178  );
    179  Assert.equal(
    180    Services.prefs.getBoolPref("testPrefBoolTrue"),
    181    false,
    182    "Should have updated testPrefBoolTrue"
    183  );
    184 });