toolkit/components/contentprefs/tests/unit/test_stringGroups.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/contentprefs/tests/unit/test_stringGroups.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     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 +function run_test() {
     1.9 +
    1.10 +  var cps = new ContentPrefInstance(null);
    1.11 +
    1.12 +  // Make sure disk synchronization checking is turned off by default.
    1.13 +  var statement = cps.DBConnection.createStatement("PRAGMA synchronous");
    1.14 +  statement.executeStep();
    1.15 +  do_check_eq(0, statement.getInt32(0));
    1.16 +
    1.17 +  // These are the different types of aGroup arguments we'll test.
    1.18 +  var anObject = {"foo":"bar"};                                // a simple object
    1.19 +  var uri = ContentPrefTest.getURI("http://www.example.com/"); // nsIURI
    1.20 +  var stringURI = "www.example.com";                           // typeof = "string"
    1.21 +  var stringObjectURI = new String("www.example.com");         // typeof = "object"
    1.22 +
    1.23 +  {
    1.24 +    // First check that all the methods work or don't work.
    1.25 +    function simple_test_methods(aGroup, shouldThrow) {
    1.26 +      var prefName = "test.pref.0";
    1.27 +      var prefValue = Math.floor(Math.random() * 100);
    1.28 +
    1.29 +      if (shouldThrow) {
    1.30 +        do_check_thrown(function () { cps.getPref(aGroup, prefName); });
    1.31 +        do_check_thrown(function () { cps.setPref(aGroup, prefName, prefValue); });
    1.32 +        do_check_thrown(function () { cps.hasPref(aGroup, prefName); });
    1.33 +        do_check_thrown(function () { cps.removePref(aGroup, prefName); });
    1.34 +        do_check_thrown(function () { cps.getPrefs(aGroup); });
    1.35 +      } else {
    1.36 +        do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined);
    1.37 +        do_check_true(cps.hasPref(aGroup, prefName));
    1.38 +        do_check_eq(cps.getPref(aGroup, prefName), prefValue);
    1.39 +        do_check_eq(cps.removePref(aGroup, prefName), undefined);
    1.40 +        do_check_false(cps.hasPref(aGroup, prefName));
    1.41 +      }
    1.42 +    }
    1.43 +
    1.44 +    simple_test_methods(cps, true); // arbitrary nsISupports object, should throw too
    1.45 +    simple_test_methods(anObject, true);
    1.46 +    simple_test_methods(uri, false);
    1.47 +    simple_test_methods(stringURI, false);
    1.48 +    simple_test_methods(stringObjectURI, false);
    1.49 +  }
    1.50 +
    1.51 +  {
    1.52 +    // Now we'll check that each argument produces the same result.
    1.53 +    function complex_test_methods(aGroup) {
    1.54 +      var prefName = "test.pref.1";
    1.55 +      var prefValue = Math.floor(Math.random() * 100);
    1.56 +
    1.57 +      do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined);
    1.58 +
    1.59 +      do_check_true(cps.hasPref(uri, prefName));
    1.60 +      do_check_true(cps.hasPref(stringURI, prefName));
    1.61 +      do_check_true(cps.hasPref(stringObjectURI, prefName));
    1.62 +
    1.63 +      do_check_eq(cps.getPref(uri, prefName), prefValue);
    1.64 +      do_check_eq(cps.getPref(stringURI, prefName), prefValue);
    1.65 +      do_check_eq(cps.getPref(stringObjectURI, prefName), prefValue);
    1.66 +
    1.67 +      do_check_eq(cps.removePref(aGroup, prefName), undefined);
    1.68 +
    1.69 +      do_check_false(cps.hasPref(uri, prefName));
    1.70 +      do_check_false(cps.hasPref(stringURI, prefName));
    1.71 +      do_check_false(cps.hasPref(stringObjectURI, prefName));
    1.72 +    }
    1.73 +
    1.74 +    complex_test_methods(uri);
    1.75 +    complex_test_methods(stringURI);
    1.76 +    complex_test_methods(stringObjectURI);
    1.77 +  }
    1.78 +
    1.79 +  {
    1.80 +    // test getPrefs returns the same prefs
    1.81 +    do_check_eq(cps.setPref(stringObjectURI, "test.5", 5), undefined);
    1.82 +    do_check_eq(cps.setPref(stringURI, "test.2", 2), undefined);
    1.83 +    do_check_eq(cps.setPref(uri, "test.1", 1), undefined);
    1.84 +
    1.85 +    enumerateAndCheck(cps.getPrefs(uri), 8, ["test.1","test.2","test.5"]);
    1.86 +    enumerateAndCheck(cps.getPrefs(stringURI), 8, ["test.1","test.2","test.5"]);
    1.87 +    enumerateAndCheck(cps.getPrefs(stringObjectURI), 8, ["test.1","test.2","test.5"]);
    1.88 +
    1.89 +    do_check_eq(cps.setPref(uri, "test.4", 4), undefined);
    1.90 +    do_check_eq(cps.setPref(stringObjectURI, "test.0", 0), undefined);
    1.91 +
    1.92 +    enumerateAndCheck(cps.getPrefs(uri), 12, ["test.0","test.1","test.2","test.4","test.5"]);
    1.93 +    enumerateAndCheck(cps.getPrefs(stringURI), 12, ["test.0","test.1","test.2","test.4","test.5"]);
    1.94 +    enumerateAndCheck(cps.getPrefs(stringObjectURI), 12, ["test.0","test.1","test.2","test.4","test.5"]);
    1.95 +
    1.96 +    do_check_eq(cps.setPref(stringURI, "test.3", 3), undefined);
    1.97 +
    1.98 +    enumerateAndCheck(cps.getPrefs(uri), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]);
    1.99 +    enumerateAndCheck(cps.getPrefs(stringURI), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]);
   1.100 +    enumerateAndCheck(cps.getPrefs(stringObjectURI), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]);
   1.101 +  }
   1.102 +}
   1.103 +
   1.104 +function do_check_thrown (aCallback) {
   1.105 +  var exThrown = false;
   1.106 +  try {
   1.107 +    aCallback();
   1.108 +    do_throw("NS_ERROR_ILLEGAL_VALUE should have been thrown here");
   1.109 +  } catch (e) {
   1.110 +    do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.111 +    exThrown = true;
   1.112 +  }
   1.113 +  do_check_true(exThrown);
   1.114 +}
   1.115 +
   1.116 +function enumerateAndCheck(prefs, expectedSum, expectedNames) {
   1.117 +  var enumerator = prefs.enumerator;
   1.118 +  var sum = 0;
   1.119 +  while (enumerator.hasMoreElements()) {
   1.120 +    var property = enumerator.getNext().QueryInterface(Components.interfaces.nsIProperty);
   1.121 +    sum += parseInt(property.value);
   1.122 +
   1.123 +    // remove the pref name from the list of expected names
   1.124 +    var index = expectedNames.indexOf(property.name);
   1.125 +    do_check_true(index >= 0);
   1.126 +    expectedNames.splice(index, 1);
   1.127 +  }
   1.128 +  do_check_eq(sum, expectedSum);
   1.129 +  // check all pref names have been removed from the array
   1.130 +  do_check_eq(expectedNames.length, 0);
   1.131 +}

mercurial