test_preference.html (4239B)
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"; 16 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"); 21 22 SimpleTest.waitForExplicitFinish(); 23 24 DevToolsServer.init(); 25 DevToolsServer.registerAllActors(); 26 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 = {}; 32 33 const localPref = { 34 boolPref: true, 35 intPref: 0x1234, 36 charPref: "Hello World", 37 }; 38 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"); 43 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; 59 } 60 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 }); 66 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 }); 72 73 client.close().then(() => { 74 DevToolsServer.destroy(); 75 SimpleTest.finish(); 76 }); 77 } 78 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+\\)" 88 ); 89 ok(messageRe.test(ex.message), "Error message matches the expected format"); 90 } 91 ); 92 return next; 93 } 94 95 function updatePrefsProperty(key) { 96 return function(value) { 97 prefs[key] = value; 98 }; 99 } 100 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); 113 }); 114 } 115 116 window.onload = function() { 117 SpecialPowers.pushPrefEnv({ 118 "set": [ 119 ["test.all.bool", true], 120 ["test.all.int", 0x4321], 121 ["test.all.string", "allizom"], 122 ], 123 }, runTests); 124 }; 125 </script> 126 </pre> 127 </body> 128 </html>