modules/libpref/test/unit/test_libPrefs.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/libpref/test/unit/test_libPrefs.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,392 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const PREF_INVALID = 0;
     1.9 +const PREF_BOOL    = 128;
    1.10 +const PREF_INT     = 64;
    1.11 +const PREF_STRING  = 32;
    1.12 +
    1.13 +const MAX_PREF_LENGTH = 1 * 1024 * 1024;
    1.14 +
    1.15 +function makeList(a)
    1.16 +{
    1.17 +  var o = {};
    1.18 +  for(var i=0; i<a.length; i++)
    1.19 +  {
    1.20 +    o[a[i]] = '';
    1.21 +  }
    1.22 +  return o;
    1.23 +}
    1.24 +
    1.25 +function run_test() {
    1.26 +
    1.27 +  var ps = Cc["@mozilla.org/preferences-service;1"].
    1.28 +            getService(Ci.nsIPrefService);
    1.29 +
    1.30 +  var pb2= Cc["@mozilla.org/preferences-service;1"].
    1.31 +            getService(Ci.nsIPrefBranch);
    1.32 +
    1.33 +  var pb = Cc["@mozilla.org/preferences-service;1"].
    1.34 +            getService(Ci.nsIPrefBranch);
    1.35 +
    1.36 +  //**************************************************************************//
    1.37 +  // Nullsafety
    1.38 +
    1.39 +  do_check_throws(function() {
    1.40 +    pb.getPrefType(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.41 +  do_check_throws(function() {
    1.42 +    pb.getBoolPref(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.43 +  do_check_throws(function() {
    1.44 +    pb.setBoolPref(null, false); },  Cr.NS_ERROR_INVALID_ARG);
    1.45 +  do_check_throws(function() {
    1.46 +    pb.getIntPref(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.47 +  do_check_throws(function() {
    1.48 +    pb.setIntPref(null, 0); },  Cr.NS_ERROR_INVALID_ARG);
    1.49 +  do_check_throws(function() {
    1.50 +    pb.getCharPref(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.51 +  do_check_throws(function() {
    1.52 +    pb.setCharPref(null, null); },  Cr.NS_ERROR_INVALID_ARG);
    1.53 +  do_check_throws(function() {
    1.54 +    pb.getComplexValue(null, Components.interfaces.nsISupportsString); },  Cr.NS_ERROR_INVALID_ARG);
    1.55 +  do_check_throws(function() {
    1.56 +    pb.setComplexValue(null, Components.interfaces.nsISupportsString, pb); },  Cr.NS_ERROR_INVALID_ARG);
    1.57 +  do_check_throws(function() {
    1.58 +    pb.clearUserPref(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.59 +  do_check_throws(function() {
    1.60 +    pb.prefHasUserValue(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.61 +  do_check_throws(function() {
    1.62 +    pb.lockPref(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.63 +  do_check_throws(function() {
    1.64 +    pb.prefIsLocked(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.65 +  do_check_throws(function() {
    1.66 +    pb.unlockPref(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.67 +  do_check_throws(function() {
    1.68 +    pb.deleteBranch(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.69 +  do_check_throws(function() {
    1.70 +    pb.getChildList(null); },  Cr.NS_ERROR_INVALID_ARG);
    1.71 +
    1.72 +  //**************************************************************************//
    1.73 +  // Nonexisting user preferences
    1.74 +
    1.75 +  do_check_eq(pb.prefHasUserValue("UserPref.nonexistent.hasUserValue"), false);
    1.76 +  pb.clearUserPref("UserPref.nonexistent.clearUserPref"); // shouldn't throw
    1.77 +  do_check_eq(pb.getPrefType("UserPref.nonexistent.getPrefType"), PREF_INVALID);
    1.78 +  do_check_eq(pb.root, "");
    1.79 +
    1.80 +  // bool...
    1.81 +  do_check_throws(function() {
    1.82 +    pb.getBoolPref("UserPref.nonexistent.getBoolPref");}, Cr.NS_ERROR_UNEXPECTED);
    1.83 +  pb.setBoolPref("UserPref.nonexistent.setBoolPref", false);
    1.84 +  do_check_eq(pb.getBoolPref("UserPref.nonexistent.setBoolPref"), false);
    1.85 +
    1.86 +  // int...
    1.87 +  do_check_throws(function() {
    1.88 +    pb.getIntPref("UserPref.nonexistent.getIntPref");}, Cr.NS_ERROR_UNEXPECTED);
    1.89 +  pb.setIntPref("UserPref.nonexistent.setIntPref", 5);
    1.90 +  do_check_eq(pb.getIntPref("UserPref.nonexistent.setIntPref"), 5);
    1.91 +
    1.92 +  // char
    1.93 +  do_check_throws(function() {
    1.94 +    pb.getCharPref("UserPref.nonexistent.getCharPref");}, Cr.NS_ERROR_UNEXPECTED);
    1.95 +  pb.setCharPref("UserPref.nonexistent.setCharPref", "_test");
    1.96 +  do_check_eq(pb.getCharPref("UserPref.nonexistent.setCharPref"), "_test");
    1.97 +
    1.98 +  //**************************************************************************//
    1.99 +  // Existing user Prefs and data integrity test (round-trip match)
   1.100 +
   1.101 +  pb.setBoolPref("UserPref.existing.bool", true);
   1.102 +  pb.setIntPref("UserPref.existing.int", 23);
   1.103 +  pb.setCharPref("UserPref.existing.char", "hey");
   1.104 +
   1.105 +  // getPref should return the pref value
   1.106 +  do_check_eq(pb.getBoolPref("UserPref.existing.bool"), true);
   1.107 +  do_check_eq(pb.getIntPref("UserPref.existing.int"), 23);
   1.108 +  do_check_eq(pb.getCharPref("UserPref.existing.char"), "hey");
   1.109 +
   1.110 +  // setPref should not complain and should change the value of the pref
   1.111 +  pb.setBoolPref("UserPref.existing.bool", false);
   1.112 +  do_check_eq(pb.getBoolPref("UserPref.existing.bool"), false);
   1.113 +  pb.setIntPref("UserPref.existing.int", 24);
   1.114 +  do_check_eq(pb.getIntPref("UserPref.existing.int"), 24);
   1.115 +  pb.setCharPref("UserPref.existing.char", "hej då!");
   1.116 +  do_check_eq(pb.getCharPref("UserPref.existing.char"), "hej då!");
   1.117 +
   1.118 +  // prefHasUserValue should return true now
   1.119 +  do_check_true(pb.prefHasUserValue("UserPref.existing.bool"));
   1.120 +  do_check_true(pb.prefHasUserValue("UserPref.existing.int"));
   1.121 +  do_check_true(pb.prefHasUserValue("UserPref.existing.char"));
   1.122 +
   1.123 +  // clearUserPref should remove the pref
   1.124 +  pb.clearUserPref("UserPref.existing.bool");
   1.125 +  do_check_false(pb.prefHasUserValue("UserPref.existing.bool"));
   1.126 +  pb.clearUserPref("UserPref.existing.int");
   1.127 +  do_check_false(pb.prefHasUserValue("UserPref.existing.int"));
   1.128 +  pb.clearUserPref("UserPref.existing.char");
   1.129 +  do_check_false(pb.prefHasUserValue("UserPref.existing.char"));
   1.130 +
   1.131 +  //**************************************************************************//
   1.132 +  // Large value test
   1.133 +
   1.134 +  let largeStr = new Array(MAX_PREF_LENGTH + 1).join('x');
   1.135 +  pb.setCharPref("UserPref.large.char", largeStr);
   1.136 +  largeStr += 'x';
   1.137 +  do_check_throws(function() {
   1.138 +    pb.setCharPref("UserPref.large.char", largeStr); }, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.139 +
   1.140 +  //**************************************************************************//
   1.141 +  // getPrefType test
   1.142 +
   1.143 +  // bool...
   1.144 +  pb.setBoolPref("UserPref.getPrefType.bool", true);
   1.145 +  do_check_eq(pb.getPrefType("UserPref.getPrefType.bool"), PREF_BOOL);
   1.146 +
   1.147 +  // int...
   1.148 +  pb.setIntPref("UserPref.getPrefType.int", -234);
   1.149 +  do_check_eq(pb.getPrefType("UserPref.getPrefType.int"), PREF_INT);
   1.150 +
   1.151 +  // char...
   1.152 +  pb.setCharPref("UserPref.getPrefType.char", "testing1..2");
   1.153 +  do_check_eq(pb.getPrefType("UserPref.getPrefType.char"), PREF_STRING);
   1.154 +
   1.155 +  //**************************************************************************//
   1.156 +  // getBranch tests
   1.157 +  
   1.158 +  do_check_eq(ps.root, "");
   1.159 +
   1.160 +  // bool ...
   1.161 +  pb.setBoolPref("UserPref.root.boolPref", true);
   1.162 +  let pb_1 = ps.getBranch("UserPref.root.");
   1.163 +  do_check_eq(pb_1.getBoolPref("boolPref"), true);
   1.164 +  let pb_2 = ps.getBranch("UserPref.root.boolPref");
   1.165 +  do_check_eq(pb_2.getBoolPref(""), true);
   1.166 +  pb_2.setBoolPref(".anotherPref", false);
   1.167 +  let pb_3 = ps.getBranch("UserPref.root.boolPre");
   1.168 +  do_check_eq(pb_3.getBoolPref("f.anotherPref"), false);
   1.169 +
   1.170 +  // int ...
   1.171 +  pb.setIntPref("UserPref.root.intPref", 23);
   1.172 +  let pb_1 = ps.getBranch("UserPref.root.");
   1.173 +  do_check_eq(pb_1.getIntPref("intPref"), 23);
   1.174 +  let pb_2 = ps.getBranch("UserPref.root.intPref");
   1.175 +  do_check_eq(pb_2.getIntPref(""), 23);
   1.176 +  pb_2.setIntPref(".anotherPref", 69);
   1.177 +  let pb_3 = ps.getBranch("UserPref.root.intPre");
   1.178 +  do_check_eq(pb_3.getIntPref("f.anotherPref"), 69);
   1.179 +
   1.180 +  // char...
   1.181 +  pb.setCharPref("UserPref.root.charPref", "_char");
   1.182 +  let pb_1 = ps.getBranch("UserPref.root.");
   1.183 +  do_check_eq(pb_1.getCharPref("charPref"), "_char");
   1.184 +  let pb_2 = ps.getBranch("UserPref.root.charPref");
   1.185 +  do_check_eq(pb_2.getCharPref(""), "_char");
   1.186 +  pb_2.setCharPref(".anotherPref", "_another");
   1.187 +  let pb_3 = ps.getBranch("UserPref.root.charPre");
   1.188 +  do_check_eq(pb_3.getCharPref("f.anotherPref"), "_another");
   1.189 +
   1.190 +  //**************************************************************************//
   1.191 +  // getChildlist tests
   1.192 +
   1.193 +  // get an already set prefBranch
   1.194 +  pb1 = ps.getBranch("UserPref.root.");
   1.195 +  let prefList = pb1.getChildList("");
   1.196 +  do_check_eq(prefList.length, 6);
   1.197 +
   1.198 +  // check for specific prefs in the array : the order is not important
   1.199 +  do_check_true("boolPref" in makeList(prefList));
   1.200 +  do_check_true("intPref" in makeList(prefList));
   1.201 +  do_check_true("charPref" in makeList(prefList));
   1.202 +  do_check_true("boolPref.anotherPref" in makeList(prefList));
   1.203 +  do_check_true("intPref.anotherPref" in makeList(prefList));
   1.204 +  do_check_true("charPref.anotherPref" in makeList(prefList));
   1.205 +
   1.206 +  //**************************************************************************//
   1.207 +  // Default branch tests
   1.208 +
   1.209 +  // bool...
   1.210 +  pb1 = ps.getDefaultBranch("");
   1.211 +  pb1.setBoolPref("DefaultPref.bool", true);
   1.212 +  do_check_eq(pb1.getBoolPref("DefaultPref.bool"), true);  
   1.213 +  do_check_false(pb1.prefHasUserValue("DefaultPref.bool"));
   1.214 +  ps.setBoolPref("DefaultPref.bool", false);
   1.215 +  do_check_true(pb1.prefHasUserValue("DefaultPref.bool"));
   1.216 +  do_check_eq(ps.getBoolPref("DefaultPref.bool"), false); 
   1.217 +
   1.218 +  // int...
   1.219 +  pb1 = ps.getDefaultBranch("");
   1.220 +  pb1.setIntPref("DefaultPref.int", 100);
   1.221 +  do_check_eq(pb1.getIntPref("DefaultPref.int"), 100);  
   1.222 +  do_check_false(pb1.prefHasUserValue("DefaultPref.int"));
   1.223 +  ps.setIntPref("DefaultPref.int", 50);
   1.224 +  do_check_true(pb1.prefHasUserValue("DefaultPref.int"));
   1.225 +  do_check_eq(ps.getIntPref("DefaultPref.int"), 50); 
   1.226 +
   1.227 +  // char...
   1.228 +  pb1 = ps.getDefaultBranch("");
   1.229 +  pb1.setCharPref("DefaultPref.char", "_default");
   1.230 +  do_check_eq(pb1.getCharPref("DefaultPref.char"), "_default");  
   1.231 +  do_check_false(pb1.prefHasUserValue("DefaultPref.char"));
   1.232 +  ps.setCharPref("DefaultPref.char", "_user");
   1.233 +  do_check_true(pb1.prefHasUserValue("DefaultPref.char"));
   1.234 +  do_check_eq(ps.getCharPref("DefaultPref.char"), "_user"); 
   1.235 +
   1.236 +  //**************************************************************************//
   1.237 +  // pref Locking/Unlocking tests
   1.238 +
   1.239 +  // locking and unlocking a nonexistent pref should throw
   1.240 +  do_check_throws(function() {
   1.241 +    ps.lockPref("DefaultPref.nonexistent");}, Cr.NS_ERROR_UNEXPECTED);
   1.242 +  do_check_throws(function() {
   1.243 +    ps.unlockPref("DefaultPref.nonexistent");}, Cr.NS_ERROR_UNEXPECTED);
   1.244 +
   1.245 +  // getting a locked pref branch should return the "default" value
   1.246 +  do_check_false(ps.prefIsLocked("DefaultPref.char"));
   1.247 +  ps.lockPref("DefaultPref.char");
   1.248 +  do_check_eq(ps.getCharPref("DefaultPref.char"), "_default"); 
   1.249 +  do_check_true(ps.prefIsLocked("DefaultPref.char"));
   1.250 +
   1.251 +  // getting an unlocked pref branch should return the "user" value 
   1.252 +  ps.unlockPref("DefaultPref.char");
   1.253 +  do_check_eq(ps.getCharPref("DefaultPref.char"), "_user"); 
   1.254 +  do_check_false(ps.prefIsLocked("DefaultPref.char"));
   1.255 +
   1.256 +  // setting the "default" value to a user pref branch should
   1.257 +  // make prefHasUserValue return false (documented behavior)
   1.258 +  ps.setCharPref("DefaultPref.char", "_default");
   1.259 +  do_check_false(pb1.prefHasUserValue("DefaultPref.char"));
   1.260 +
   1.261 +  // unlocking and locking multiple times shouldn't throw
   1.262 +  ps.unlockPref("DefaultPref.char");
   1.263 +  ps.lockPref("DefaultPref.char");
   1.264 +  ps.lockPref("DefaultPref.char");
   1.265 +
   1.266 +  //**************************************************************************//
   1.267 +  // resetBranch test
   1.268 +
   1.269 +  // NOT IMPLEMENTED YET in module/libpref. So we're not testing !
   1.270 +  // uncomment the following if resetBranch ever gets implemented.
   1.271 +  /*ps.resetBranch("DefaultPref");
   1.272 +  do_check_eq(ps.getBoolPref("DefaultPref.bool"), true);
   1.273 +  do_check_eq(ps.getIntPref("DefaultPref.int"), 100);
   1.274 +  do_check_eq(ps.getCharPref("DefaultPref.char"), "_default");*/
   1.275 +
   1.276 +  //**************************************************************************//
   1.277 +  // deleteBranch tests
   1.278 +
   1.279 +  // TODO : Really, this should throw!, by documentation.
   1.280 +  // do_check_throws(function() {
   1.281 +  // ps.deleteBranch("UserPref.nonexistent.deleteBranch");}, Cr.NS_ERROR_UNEXPECTED);
   1.282 +
   1.283 +  ps.deleteBranch("DefaultPref");
   1.284 +  pb = ps.getBranch("DefaultPref");
   1.285 +  pb1 = ps.getDefaultBranch("DefaultPref");
   1.286 +
   1.287 +  // getting prefs on deleted user branches should throw
   1.288 +  do_check_throws(function() {
   1.289 +    pb.getBoolPref("DefaultPref.bool");}, Cr.NS_ERROR_UNEXPECTED);
   1.290 +  do_check_throws(function() {
   1.291 +    pb.getIntPref("DefaultPref.int");}, Cr.NS_ERROR_UNEXPECTED);
   1.292 +  do_check_throws(function() {
   1.293 +    pb.getCharPref("DefaultPref.char");}, Cr.NS_ERROR_UNEXPECTED);
   1.294 +
   1.295 +  // getting prefs on deleted default branches should throw
   1.296 +  do_check_throws(function() {
   1.297 +    pb1.getBoolPref("DefaultPref.bool");}, Cr.NS_ERROR_UNEXPECTED);
   1.298 +  do_check_throws(function() {
   1.299 +    pb1.getIntPref("DefaultPref.int");}, Cr.NS_ERROR_UNEXPECTED);
   1.300 +  do_check_throws(function() {
   1.301 +    pb1.getCharPref("DefaultPref.char");}, Cr.NS_ERROR_UNEXPECTED);
   1.302 +
   1.303 +  //**************************************************************************//
   1.304 +  // savePrefFile & readPrefFile tests
   1.305 +
   1.306 +  // set some prefs
   1.307 +  ps.setBoolPref("ReadPref.bool", true);
   1.308 +  ps.setIntPref("ReadPref.int", 230);
   1.309 +  ps.setCharPref("ReadPref.char", "hello");
   1.310 +
   1.311 +  // save those prefs in a file
   1.312 +  let savePrefFile = do_get_cwd();
   1.313 +  savePrefFile.append("data");
   1.314 +  savePrefFile.append("savePref.js");
   1.315 +  if (savePrefFile.exists())
   1.316 +    savePrefFile.remove(false);
   1.317 +  savePrefFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
   1.318 +  ps.savePrefFile(savePrefFile);
   1.319 +  ps.resetPrefs();
   1.320 +
   1.321 +  // load a preexisting pref file
   1.322 +  let prefFile = do_get_file("data/testPref.js");
   1.323 +  ps.readUserPrefs(prefFile);
   1.324 +
   1.325 +  // former prefs should have been replaced/lost
   1.326 +  do_check_throws(function() {
   1.327 +    do_check_eq(pb.getBoolPref("ReadPref.bool"));}, Cr.NS_ERROR_UNEXPECTED);
   1.328 +  do_check_throws(function() {
   1.329 +    do_check_eq(pb.getIntPref("ReadPref.int"));}, Cr.NS_ERROR_UNEXPECTED);
   1.330 +  do_check_throws(function() {
   1.331 +    do_check_eq(pb.getCharPref("ReadPref.char"));}, Cr.NS_ERROR_UNEXPECTED);
   1.332 +
   1.333 +  // loaded prefs should read ok.
   1.334 +  pb = ps.getBranch("testPref.");
   1.335 +  do_check_eq(pb.getBoolPref("bool1"), true);
   1.336 +  do_check_eq(pb.getBoolPref("bool2"), false);
   1.337 +  do_check_eq(pb.getIntPref("int1"), 23);
   1.338 +  do_check_eq(pb.getIntPref("int2"), -1236);
   1.339 +  do_check_eq(pb.getCharPref("char1"), "_testPref");
   1.340 +  do_check_eq(pb.getCharPref("char2"), "älskar");
   1.341 +
   1.342 +  // loading our former savePrefFile should allow us to read former prefs
   1.343 +  ps.readUserPrefs(savePrefFile);
   1.344 +  // cleanup the file now we don't need it
   1.345 +  savePrefFile.remove(false);
   1.346 +  do_check_eq(ps.getBoolPref("ReadPref.bool"), true);
   1.347 +  do_check_eq(ps.getIntPref("ReadPref.int"), 230);
   1.348 +  do_check_eq(ps.getCharPref("ReadPref.char"), "hello");
   1.349 +
   1.350 +  // ... and still be able to access "prior-to-readUserPrefs" preferences
   1.351 +  do_check_eq(pb.getBoolPref("bool1"), true);
   1.352 +  do_check_eq(pb.getBoolPref("bool2"), false);
   1.353 +  do_check_eq(pb.getIntPref("int1"), 23);
   1.354 +
   1.355 +  //**************************************************************************//
   1.356 +  // preference Observers
   1.357 +
   1.358 +  // an observer...
   1.359 +  var observer = {
   1.360 +    QueryInterface: function QueryInterface(aIID) {
   1.361 +      if (aIID.equals(Ci.nsIObserver) ||
   1.362 +          aIID.equals(Ci.nsISupports))
   1.363 +         return this;
   1.364 +      throw Components.results.NS_NOINTERFACE;
   1.365 +    },
   1.366 +
   1.367 +    observe: function observe(aSubject, aTopic, aState) {
   1.368 +      do_check_eq(aTopic, "nsPref:changed");
   1.369 +      do_check_eq(aState, "ReadPref.int");
   1.370 +      do_check_eq(ps.getIntPref(aState), 76);
   1.371 +      ps.removeObserver("ReadPref.int", this);
   1.372 +
   1.373 +      // notification received, we may go on...
   1.374 +      do_test_finished();
   1.375 +    }
   1.376 +  }
   1.377 +
   1.378 +  pb2.addObserver("ReadPref.int", observer, false);
   1.379 +  ps.setIntPref("ReadPref.int", 76);
   1.380 +
   1.381 +  // test will continue upon notification...
   1.382 +  do_test_pending();
   1.383 +
   1.384 +  // removed observer should not fire
   1.385 +  pb2.removeObserver("ReadPref.int", observer);
   1.386 +  ps.setIntPref("ReadPref.int", 32);
   1.387 +
   1.388 +  // let's test observers once more with a non-root prefbranch
   1.389 +  pb2.getBranch("ReadPref.");
   1.390 +  pb2.addObserver("int", observer, false);
   1.391 +  ps.setIntPref("ReadPref.int", 76);
   1.392 +
   1.393 +  // test will complete upon notification...
   1.394 +  do_test_pending();
   1.395 +}

mercurial