summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/browser_prefs-02.js
blob: ad32aa29ce63b7037168e7b38319f68ac683a578 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that preference helpers work properly with custom types of Float and Json.

const { PrefsHelper } = require("resource://devtools/client/shared/prefs.js");

function test() {
  const Prefs = new PrefsHelper("prefs.helper.test", {
    float: ["Float", "float"],
    json: ["Json", "json"],
    jsonWithSpecialChar: ["Json", "jsonWithSpecialChar"],
  });

  Prefs.registerObserver();

  // JSON
  Services.prefs.setCharPref("prefs.helper.test.json", '{"a":1}');
  is(Prefs.json.a, 1, "The JSON pref value is correctly casted on get.");

  Prefs.json = { b: 2 };
  is(
    Prefs.json.a,
    undefined,
    "The JSON pref value is correctly casted on set (1)."
  );
  is(Prefs.json.b, 2, "The JSON pref value is correctly casted on set (2).");

  // JSON with special character
  Services.prefs.setStringPref(
    "prefs.helper.test.jsonWithSpecialChar",
    `{"hello":"おはよう皆!"}`
  );
  is(
    Prefs.jsonWithSpecialChar.hello,
    "おはよう皆!",
    "The JSON pref value with a special character is correctly stored."
  );

  Prefs.jsonWithSpecialChar = { bye: "さよなら!" };
  is(
    Prefs.jsonWithSpecialChar.hello,
    undefined,
    "The JSON with the special characters pref value is correctly set. (1)"
  );
  is(
    Prefs.jsonWithSpecialChar.bye,
    "さよなら!",
    "The JSON with the special characters pref value is correctly set. (2)"
  );

  // Float
  Services.prefs.setCharPref("prefs.helper.test.float", "3.14");
  is(Prefs.float, 3.14, "The float pref value is correctly casted on get.");

  Prefs.float = 6.28;
  is(Prefs.float, 6.28, "The float pref value is correctly casted on set.");

  Prefs.unregisterObserver();

  Services.prefs.clearUserPref("prefs.helper.test.float");
  Services.prefs.clearUserPref("prefs.helper.test.json");
  Services.prefs.clearUserPref("prefs.helper.test.jsonWithSpecialChar");
  finish();
}