tor-browser

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

browser_prefs-02.js (2004B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Tests that preference helpers work properly with custom types of Float and Json.
      7 
      8 const { PrefsHelper } = require("resource://devtools/client/shared/prefs.js");
      9 
     10 function test() {
     11  const Prefs = new PrefsHelper("prefs.helper.test", {
     12    float: ["Float", "float"],
     13    json: ["Json", "json"],
     14    jsonWithSpecialChar: ["Json", "jsonWithSpecialChar"],
     15  });
     16 
     17  Prefs.registerObserver();
     18 
     19  // JSON
     20  Services.prefs.setCharPref("prefs.helper.test.json", '{"a":1}');
     21  is(Prefs.json.a, 1, "The JSON pref value is correctly casted on get.");
     22 
     23  Prefs.json = { b: 2 };
     24  is(
     25    Prefs.json.a,
     26    undefined,
     27    "The JSON pref value is correctly casted on set (1)."
     28  );
     29  is(Prefs.json.b, 2, "The JSON pref value is correctly casted on set (2).");
     30 
     31  // JSON with special character
     32  Services.prefs.setStringPref(
     33    "prefs.helper.test.jsonWithSpecialChar",
     34    `{"hello":"おはよう皆!"}`
     35  );
     36  is(
     37    Prefs.jsonWithSpecialChar.hello,
     38    "おはよう皆!",
     39    "The JSON pref value with a special character is correctly stored."
     40  );
     41 
     42  Prefs.jsonWithSpecialChar = { bye: "さよなら!" };
     43  is(
     44    Prefs.jsonWithSpecialChar.hello,
     45    undefined,
     46    "The JSON with the special characters pref value is correctly set. (1)"
     47  );
     48  is(
     49    Prefs.jsonWithSpecialChar.bye,
     50    "さよなら!",
     51    "The JSON with the special characters pref value is correctly set. (2)"
     52  );
     53 
     54  // Float
     55  Services.prefs.setCharPref("prefs.helper.test.float", "3.14");
     56  is(Prefs.float, 3.14, "The float pref value is correctly casted on get.");
     57 
     58  Prefs.float = 6.28;
     59  is(Prefs.float, 6.28, "The float pref value is correctly casted on set.");
     60 
     61  Prefs.unregisterObserver();
     62 
     63  Services.prefs.clearUserPref("prefs.helper.test.float");
     64  Services.prefs.clearUserPref("prefs.helper.test.json");
     65  Services.prefs.clearUserPref("prefs.helper.test.jsonWithSpecialChar");
     66  finish();
     67 }