4 Bug 943251 - Test preferences actor
8 <title>Test Preference Actor
</title>
9 <script src=
"chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel=
"stylesheet" type=
"text/css" href=
"chrome://mochikit/content/tests/SimpleTest/test.css">
18 const {require
} = ChromeUtils
.importESModule("resource://devtools/shared/loader/Loader.sys.mjs");
19 const {DevToolsClient
} = require("devtools/client/devtools-client");
20 const {DevToolsServer
} = require("devtools/server/devtools-server");
22 SimpleTest
.waitForExplicitFinish();
24 DevToolsServer
.init();
25 DevToolsServer
.registerAllActors();
27 const client
= new DevToolsClient(DevToolsServer
.connectPipe());
28 client
.connect().then(function onConnect() {
29 return client
.mainRoot
.getFront("preference");
36 charPref
: "Hello World",
39 function checkValues() {
40 is(prefs
.boolPref
, localPref
.boolPref
, "read/write bool pref");
41 is(prefs
.intPref
, localPref
.intPref
, "read/write int pref");
42 is(prefs
.charPref
, localPref
.charPref
, "read/write string pref");
44 ["test.all.bool", "test.all.int", "test.all.string"].forEach(function(key
) {
46 switch (Services
.prefs
.getPrefType(key
)) {
47 case Ci
.nsIPrefBranch
.PREF_STRING
:
48 expectedValue
= Services
.prefs
.getCharPref(key
);
50 case Ci
.nsIPrefBranch
.PREF_INT
:
51 expectedValue
= Services
.prefs
.getIntPref(key
);
53 case Ci
.nsIPrefBranch
.PREF_BOOL
:
54 expectedValue
= Services
.prefs
.getBoolPref(key
);
57 ok(false, "unexpected pref type (" + key
+ ")");
61 is(prefs
.allPrefs
[key
].value
, expectedValue
,
62 "valid preference value (" + key
+ ")");
63 is(prefs
.allPrefs
[key
].hasUserValue
, Services
.prefs
.prefHasUserValue(key
),
64 "valid hasUserValue (" + key
+ ")");
67 ["test.bool", "test.int", "test.string"].forEach(function(key
) {
68 ok(!prefs
.allPrefs
.hasOwnProperty(key
), "expect no pref (" + key
+ ")");
69 is(Services
.prefs
.getPrefType(key
), Ci
.nsIPrefBranch
.PREF_INVALID
,
70 "pref (" + key
+ ") is clear");
73 client
.close().then(() => {
74 DevToolsServer
.destroy();
79 function checkUndefined() {
80 let next
= p
.getCharPref("test.undefined");
82 () => ok(false, "getCharPref should've thrown for an undefined preference"),
84 const messageRe
= new RegExp(
85 "Protocol error \\(Error\\): preference is not of the right type: " +
86 `test.undefined from: ${p.actorID} ` +
87 "\\(resource://devtools/server/actors/preference.js:\\d+:\\d+\\)"
89 ok(messageRe
.test(ex
.message
), "Error message matches the expected format");
95 function updatePrefsProperty(key
) {
96 return function(value
) {
101 p
.getAllPrefs().then(updatePrefsProperty("allPrefs"))
102 .then(() => p
.setBoolPref("test.bool", localPref
.boolPref
))
103 .then(() => p
.setIntPref("test.int", localPref
.intPref
))
104 .then(() => p
.setCharPref("test.string", localPref
.charPref
))
105 .then(() => p
.getBoolPref("test.bool")).then(updatePrefsProperty("boolPref"))
106 .then(() => p
.getIntPref("test.int")).then(updatePrefsProperty("intPref"))
107 .then(() => p
.getCharPref("test.string")).then(updatePrefsProperty("charPref"))
108 .then(() => p
.clearUserPref("test.bool"))
109 .then(() => p
.clearUserPref("test.int"))
110 .then(() => p
.clearUserPref("test.string"))
111 .then(() => checkUndefined())
116 window
.onload = function() {
117 SpecialPowers
.pushPrefEnv({
119 ["test.all.bool", true],
120 ["test.all.int", 0x4321],
121 ["test.all.string", "allizom"],