test_SpecialPowersPushPrefEnv.html (7206B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for SpecialPowers extension</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 </head> 8 <body onload="starttest();"> 9 10 <pre id="test"> 11 <script class="testbody" type="text/javascript"> 12 async function starttest() { 13 try { 14 await SpecialPowers.setBoolPref("test.bool", 1); 15 } catch(e) { 16 await SpecialPowers.setBoolPref("test.bool", true); 17 } 18 try { 19 await SpecialPowers.setIntPref("test.int", true); 20 } catch(e) { 21 await SpecialPowers.setIntPref("test.int", 1); 22 } 23 await SpecialPowers.setCharPref("test.char", 'test'); 24 await SpecialPowers.setBoolPref("test.cleanup", false); 25 26 setTimeout(test1, 0, 0); 27 } 28 29 SimpleTest.waitForExplicitFinish(); 30 31 function test1(aCount) { 32 if (aCount >= 20) { 33 ok(false, "Too many times attempting to set pref, aborting"); 34 SimpleTest.finish(); 35 return; 36 } 37 38 try { 39 is(SpecialPowers.getBoolPref('test.bool'), true, 'test.bool should be true'); 40 } catch(e) { 41 setTimeout(test1, 0, ++aCount); 42 return; 43 } 44 45 try { 46 is(SpecialPowers.getIntPref('test.int'), 1, 'test.int should be 1'); 47 } catch(e) { 48 setTimeout(test1, 0, ++aCount); 49 return; 50 } 51 52 try { 53 is(SpecialPowers.getCharPref('test.char'), 'test', 'test.char should be test'); 54 } catch(e) { 55 setTimeout(test1, 0, ++aCount); 56 return; 57 } 58 59 test2(); 60 } 61 62 function test2() { 63 // test non-changing values 64 SpecialPowers.pushPrefEnv({"set": [["test.bool", true], ["test.int", 1], ["test.char", "test"]]}, test3); 65 } 66 67 function test3() { 68 // test changing char pref using the Promise 69 is(SpecialPowers.getBoolPref('test.bool'), true, 'test.bool should be true'); 70 is(SpecialPowers.getIntPref('test.int'), 1, 'test.int should be 1'); 71 is(SpecialPowers.getCharPref('test.char'), 'test', 'test.char should be test'); 72 SpecialPowers.pushPrefEnv({"set": [["test.bool", true], ["test.int", 1], ["test.char", "test2"]]}).then(test4); 73 } 74 75 function test4() { 76 // test changing all values and adding test.char2 pref 77 is(SpecialPowers.getCharPref('test.char'), 'test2', 'test.char should be test2'); 78 SpecialPowers.pushPrefEnv({"set": [["test.bool", false], ["test.int", 10], ["test.char", "test2"], ["test.char2", "test"]]}, test5); 79 } 80 81 function test5() { 82 // test flushPrefEnv 83 is(SpecialPowers.getBoolPref('test.bool'), false, 'test.bool should be false'); 84 is(SpecialPowers.getIntPref('test.int'), 10, 'test.int should be 10'); 85 is(SpecialPowers.getCharPref('test.char'), 'test2', 'test.char should be test2'); 86 is(SpecialPowers.getCharPref('test.char2'), 'test', 'test.char2 should be test'); 87 SpecialPowers.flushPrefEnv(test6); 88 } 89 90 function test6() { 91 // test clearing prefs 92 is(SpecialPowers.getBoolPref('test.bool'), true, 'test.bool should be true'); 93 is(typeof SpecialPowers.getBoolPref('test.bool'), typeof true, 'test.bool should be boolean'); 94 is(SpecialPowers.getIntPref('test.int'), 1, 'test.int should be 1'); 95 is(typeof SpecialPowers.getIntPref('test.int'), typeof 1, 'test.int should be integer'); 96 is(SpecialPowers.getCharPref('test.char'), 'test', 'test.char should be test'); 97 is(typeof SpecialPowers.getCharPref('test.char'), typeof 'test', 'test.char should be String'); 98 try { 99 SpecialPowers.getCharPref('test.char2'); 100 ok(false, 'This ok should not be reached!'); 101 } catch(e) { 102 ok(true, 'getCharPref("test.char2") should throw'); 103 } 104 SpecialPowers.pushPrefEnv({"clear": [["test.bool"], ["test.int"], ["test.char"], ["test.char2"]]}, test6b); 105 } 106 107 function test6b() { 108 // test if clearing another time doesn't cause issues 109 SpecialPowers.pushPrefEnv({"clear": [["test.bool"], ["test.int"], ["test.char"], ["test.char2"]]}, test7); 110 } 111 112 function test7() { 113 try { 114 SpecialPowers.getBoolPref('test.bool'); 115 ok(false, 'This ok should not be reached!'); 116 } catch(e) { 117 ok(true, 'getBoolPref("test.bool") should throw'); 118 } 119 120 try { 121 SpecialPowers.getIntPref('test.int'); 122 ok(false, 'This ok should not be reached!'); 123 } catch(e) { 124 ok(true, 'getIntPref("test.int") should throw'); 125 } 126 127 try { 128 SpecialPowers.getCharPref('test.char'); 129 ok(false, 'This ok should not be reached!'); 130 } catch(e) { 131 ok(true, 'getCharPref("test.char") should throw'); 132 } 133 134 try { 135 SpecialPowers.getCharPref('test.char2'); 136 ok(false, 'This ok should not be reached!'); 137 } catch(e) { 138 ok(true, 'getCharPref("test.char2") should throw'); 139 } 140 141 SpecialPowers.flushPrefEnv().then(test8); 142 } 143 144 function test8() { 145 is(SpecialPowers.getBoolPref('test.bool'), true, 'test.bool should be true'); 146 is(typeof SpecialPowers.getBoolPref('test.bool'), typeof true, 'test.bool should be boolean'); 147 is(SpecialPowers.getIntPref('test.int'), 1, 'test.int should be 1'); 148 is(typeof SpecialPowers.getIntPref('test.int'), typeof 1, 'test.int should be integer'); 149 is(SpecialPowers.getCharPref('test.char'), 'test', 'test.char should be test'); 150 is(typeof SpecialPowers.getCharPref('test.char'), typeof 'test', 'test.char should be String'); 151 try { 152 SpecialPowers.getCharPref('test.char2'); 153 ok(false, 'This ok should not be reached!'); 154 } catch(e) { 155 ok(true, 'getCharPref("test.char2") should throw'); 156 } 157 SpecialPowers.clearUserPref("test.bool"); 158 SpecialPowers.clearUserPref("test.int"); 159 SpecialPowers.clearUserPref("test.char"); 160 setTimeout(test9, 0, 0); 161 } 162 163 function test9(aCount) { 164 if (aCount >= 20) { 165 ok(false, "Too many times attempting to set pref, aborting"); 166 SimpleTest.finish(); 167 return; 168 } 169 170 try { 171 SpecialPowers.getBoolPref('test.bool'); 172 setTimeout(test9, 0, ++aCount); 173 } catch(e) { 174 test10(0); 175 } 176 } 177 178 function test10(aCount) { 179 if (aCount >= 20) { 180 ok(false, "Too many times attempting to set pref, aborting"); 181 SimpleTest.finish(); 182 return; 183 } 184 185 try { 186 SpecialPowers.getIntPref('test.int'); 187 setTimeout(test10, 0, ++aCount); 188 } catch(e) { 189 test11(0); 190 } 191 } 192 193 function test11(aCount) { 194 if (aCount >= 20) { 195 ok(false, "Too many times attempting to set pref, aborting"); 196 SimpleTest.finish(); 197 return; 198 } 199 200 try { 201 SpecialPowers.getCharPref('test.char'); 202 setTimeout(test11, 0, ++aCount); 203 } catch(e) { 204 test12(); 205 } 206 } 207 208 function test12() { 209 // Set test.cleanup to true via pushPrefEnv, while its default value is false. 210 SpecialPowers.pushPrefEnv({"set": [["test.cleanup", true]]}, () => { 211 // Update the preference manually back to its original value. 212 SpecialPowers.setBoolPref("test.cleanup", false); 213 setTimeout(test13, 0); 214 }); 215 } 216 217 function test13() { 218 // Try to flush preferences. Since test.cleanup has manually been set to false, there 219 // will not be any visible update. Check that the flush does not timeout. 220 SpecialPowers.flushPrefEnv(() => { 221 ok(true, 'flushPrefEnv did not time out'); 222 is(SpecialPowers.getBoolPref('test.cleanup'), false, 'test.cleanup should be false'); 223 SimpleTest.finish(); 224 }); 225 } 226 227 // todo - test non-changing values, test complex values, test mixing of pushprefEnv 'set' and 'clear' 228 // When bug 776424 gets fixed, getPref doesn't throw anymore, so this test would have to be changed accordingly 229 </script> 230 </pre> 231 </body> 232 </html>