Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / devtools / server / tests / chrome / test_preference.html
blobb4d23a24aaf6338ec1a65e8d4d4518e988914522
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 Bug 943251 - Test preferences actor
5 -->
6 <head>
7 <meta charset="utf-8">
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">
11 </head>
12 <body>
13 <pre id="test">
14 <script>
15 "use strict";
17 function runTests() {
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");
30 }).then(function(p) {
31 const prefs = {};
33 const localPref = {
34 boolPref: true,
35 intPref: 0x1234,
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) {
45 let expectedValue;
46 switch (Services.prefs.getPrefType(key)) {
47 case Ci.nsIPrefBranch.PREF_STRING:
48 expectedValue = Services.prefs.getCharPref(key);
49 break;
50 case Ci.nsIPrefBranch.PREF_INT:
51 expectedValue = Services.prefs.getIntPref(key);
52 break;
53 case Ci.nsIPrefBranch.PREF_BOOL:
54 expectedValue = Services.prefs.getBoolPref(key);
55 break;
56 default:
57 ok(false, "unexpected pref type (" + key + ")");
58 break;
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 + ")");
65 });
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");
71 });
73 client.close().then(() => {
74 DevToolsServer.destroy();
75 SimpleTest.finish();
76 });
79 function checkUndefined() {
80 let next = p.getCharPref("test.undefined");
81 next = next.then(
82 () => ok(false, "getCharPref should've thrown for an undefined preference"),
83 (ex) => {
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");
92 return next;
95 function updatePrefsProperty(key) {
96 return function(value) {
97 prefs[key] = 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())
112 .then(checkValues);
116 window.onload = function() {
117 SpecialPowers.pushPrefEnv({
118 "set": [
119 ["test.all.bool", true],
120 ["test.all.int", 0x4321],
121 ["test.all.string", "allizom"],
123 }, runTests);
125 </script>
126 </pre>
127 </body>
128 </html>