tor-browser

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

test_runtime_default_preferences.js (6524B)


      1 "use strict";
      2 
      3 const { sinon } = ChromeUtils.importESModule(
      4  "resource://testing-common/Sinon.sys.mjs"
      5 );
      6 const {
      7  setDefaultPreferencesIfNeeded,
      8  PREFERENCE_TYPES,
      9 } = require("resource://devtools/client/aboutdebugging/src/modules/runtime-default-preferences.js");
     10 
     11 const CHAR_PREF = "some.char.pref";
     12 const BOOL_PREF = "some.bool.pref";
     13 const INT_PREF = "some.int.pref";
     14 
     15 const TEST_PREFERENCES = [
     16  {
     17    prefName: BOOL_PREF,
     18    defaultValue: false,
     19    trait: "boolPrefTrait",
     20    type: PREFERENCE_TYPES.BOOL,
     21  },
     22  {
     23    prefName: CHAR_PREF,
     24    defaultValue: "",
     25    trait: "charPrefTrait",
     26    type: PREFERENCE_TYPES.CHAR,
     27  },
     28  {
     29    prefName: INT_PREF,
     30    defaultValue: 1,
     31    trait: "intPrefTrait",
     32    type: PREFERENCE_TYPES.INT,
     33  },
     34 ];
     35 
     36 add_task(async function test_with_traits() {
     37  // Create a front that indicates that the preferences should be safe to query.
     38  // We should not perform any additional call to the preferences front.
     39  const preferencesFront = {
     40    getTraits: () => ({
     41      boolPrefTrait: true,
     42      charPrefTrait: true,
     43      intPrefTrait: true,
     44    }),
     45 
     46    setBoolPref: sinon.spy(),
     47    getBoolPref: sinon.spy(),
     48    setCharPref: sinon.spy(),
     49    getCharPref: sinon.spy(),
     50    setIntPref: sinon.spy(),
     51    getIntPref: sinon.spy(),
     52  };
     53 
     54  const clientWrapper = createClientWrapper(preferencesFront);
     55  await setDefaultPreferencesIfNeeded(clientWrapper, TEST_PREFERENCES);
     56 
     57  // Check get/setBoolPref spies
     58  ok(preferencesFront.getBoolPref.notCalled, "getBoolPref was not called");
     59  ok(preferencesFront.setBoolPref.notCalled, "setBoolPref was not called");
     60 
     61  // Check get/setCharPref spies
     62  ok(preferencesFront.getCharPref.notCalled, "getCharPref was not called");
     63  ok(preferencesFront.setCharPref.notCalled, "setCharPref was not called");
     64 
     65  // Check get/setIntPref spies
     66  ok(preferencesFront.getIntPref.notCalled, "getIntPref was not called");
     67  ok(preferencesFront.setIntPref.notCalled, "setIntPref was not called");
     68 });
     69 
     70 add_task(async function test_without_traits_no_error() {
     71  // Create a front that indicates that the preferences are missing, but which
     72  // doesn't fail when getting the preferences. This will typically happen when
     73  // the user managed to set the preference on the remote runtime.
     74  // We should not erase user values, so we should not call the set*Pref APIs.
     75  const preferencesFront = {
     76    getTraits: () => ({
     77      boolPrefTrait: false,
     78      charPrefTrait: false,
     79      intPrefTrait: false,
     80    }),
     81 
     82    setBoolPref: sinon.spy(),
     83    getBoolPref: sinon.spy(),
     84    setCharPref: sinon.spy(),
     85    getCharPref: sinon.spy(),
     86    setIntPref: sinon.spy(),
     87    getIntPref: sinon.spy(),
     88  };
     89 
     90  const clientWrapper = createClientWrapper(preferencesFront);
     91  await setDefaultPreferencesIfNeeded(clientWrapper, TEST_PREFERENCES);
     92 
     93  // Check get/setBoolPref spies
     94  ok(
     95    preferencesFront.getBoolPref.calledWith(BOOL_PREF),
     96    "getBoolPref was called with the proper preference name"
     97  );
     98  ok(preferencesFront.getBoolPref.calledOnce, "getBoolPref was called once");
     99  ok(preferencesFront.setBoolPref.notCalled, "setBoolPref was not called");
    100 
    101  // Check get/setCharPref spies
    102  ok(
    103    preferencesFront.getCharPref.calledWith(CHAR_PREF),
    104    "getCharPref was called with the proper preference name"
    105  );
    106  ok(preferencesFront.getCharPref.calledOnce, "getCharPref was called once");
    107  ok(preferencesFront.setCharPref.notCalled, "setCharPref was not called");
    108 
    109  // Check get/setIntPref spies
    110  ok(
    111    preferencesFront.getIntPref.calledWith(INT_PREF),
    112    "getIntPref was called with the proper preference name"
    113  );
    114  ok(preferencesFront.getIntPref.calledOnce, "getIntPref was called once");
    115  ok(preferencesFront.setIntPref.notCalled, "setIntPref was not called");
    116 });
    117 
    118 add_task(async function test_without_traits_with_error() {
    119  // Create a front that indicates that the preferences are missing, and which
    120  // will also throw when attempting to get said preferences.
    121  // This should lead to create default values for the preferences.
    122  const preferencesFront = {
    123    getTraits: () => ({
    124      boolPrefTrait: false,
    125      charPrefTrait: false,
    126      intPrefTrait: false,
    127    }),
    128 
    129    setBoolPref: sinon.spy(),
    130    getBoolPref: sinon.spy(pref => {
    131      if (pref === BOOL_PREF) {
    132        throw new Error("Invalid preference");
    133      }
    134    }),
    135    setCharPref: sinon.spy(),
    136    getCharPref: sinon.spy(pref => {
    137      if (pref === CHAR_PREF) {
    138        throw new Error("Invalid preference");
    139      }
    140    }),
    141    setIntPref: sinon.spy(),
    142    getIntPref: sinon.spy(pref => {
    143      if (pref === INT_PREF) {
    144        throw new Error("Invalid preference");
    145      }
    146    }),
    147  };
    148 
    149  const clientWrapper = createClientWrapper(preferencesFront);
    150  await setDefaultPreferencesIfNeeded(clientWrapper, TEST_PREFERENCES);
    151 
    152  // Check get/setBoolPref spies
    153  ok(preferencesFront.getBoolPref.calledOnce, "getBoolPref was called once");
    154  ok(preferencesFront.getBoolPref.threw(), "getBoolPref threw");
    155  ok(
    156    preferencesFront.getBoolPref.calledWith(BOOL_PREF),
    157    "getBoolPref was called with the proper preference name"
    158  );
    159 
    160  ok(preferencesFront.setBoolPref.calledOnce, "setBoolPref was called once");
    161  ok(
    162    preferencesFront.setBoolPref.calledWith(BOOL_PREF, false),
    163    "setBoolPref was called with the proper preference name and value"
    164  );
    165 
    166  // Check get/setCharPref spies
    167  ok(preferencesFront.getCharPref.calledOnce, "getCharPref was called once");
    168  ok(preferencesFront.getCharPref.threw(), "getCharPref threw");
    169  ok(
    170    preferencesFront.getCharPref.calledWith(CHAR_PREF),
    171    "getCharPref was called with the proper preference name"
    172  );
    173 
    174  ok(preferencesFront.setCharPref.calledOnce, "setCharPref was called once");
    175  ok(
    176    preferencesFront.setCharPref.calledWith(CHAR_PREF, ""),
    177    "setCharPref was called with the proper preference name and value"
    178  );
    179 
    180  // Check get/setIntPref spies
    181  ok(preferencesFront.getIntPref.calledOnce, "getIntPref was called once");
    182  ok(preferencesFront.getIntPref.threw(), "getIntPref threw");
    183  ok(
    184    preferencesFront.getIntPref.calledWith(INT_PREF),
    185    "getIntPref was called with the proper preference name"
    186  );
    187 
    188  ok(preferencesFront.setIntPref.calledOnce, "setIntPref was called once");
    189  ok(
    190    preferencesFront.setIntPref.calledWith(INT_PREF, 1),
    191    "setIntPref was called with the proper preference name and value"
    192  );
    193 });
    194 
    195 function createClientWrapper(preferencesFront) {
    196  const clientWrapper = {
    197    getFront: () => {
    198      return preferencesFront;
    199    },
    200  };
    201 
    202  return clientWrapper;
    203 }