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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 function run_test() {
     7   var cps = new ContentPrefInstance(null);
     9   // Make sure disk synchronization checking is turned off by default.
    10   var statement = cps.DBConnection.createStatement("PRAGMA synchronous");
    11   statement.executeStep();
    12   do_check_eq(0, statement.getInt32(0));
    14   // These are the different types of aGroup arguments we'll test.
    15   var anObject = {"foo":"bar"};                                // a simple object
    16   var uri = ContentPrefTest.getURI("http://www.example.com/"); // nsIURI
    17   var stringURI = "www.example.com";                           // typeof = "string"
    18   var stringObjectURI = new String("www.example.com");         // typeof = "object"
    20   {
    21     // First check that all the methods work or don't work.
    22     function simple_test_methods(aGroup, shouldThrow) {
    23       var prefName = "test.pref.0";
    24       var prefValue = Math.floor(Math.random() * 100);
    26       if (shouldThrow) {
    27         do_check_thrown(function () { cps.getPref(aGroup, prefName); });
    28         do_check_thrown(function () { cps.setPref(aGroup, prefName, prefValue); });
    29         do_check_thrown(function () { cps.hasPref(aGroup, prefName); });
    30         do_check_thrown(function () { cps.removePref(aGroup, prefName); });
    31         do_check_thrown(function () { cps.getPrefs(aGroup); });
    32       } else {
    33         do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined);
    34         do_check_true(cps.hasPref(aGroup, prefName));
    35         do_check_eq(cps.getPref(aGroup, prefName), prefValue);
    36         do_check_eq(cps.removePref(aGroup, prefName), undefined);
    37         do_check_false(cps.hasPref(aGroup, prefName));
    38       }
    39     }
    41     simple_test_methods(cps, true); // arbitrary nsISupports object, should throw too
    42     simple_test_methods(anObject, true);
    43     simple_test_methods(uri, false);
    44     simple_test_methods(stringURI, false);
    45     simple_test_methods(stringObjectURI, false);
    46   }
    48   {
    49     // Now we'll check that each argument produces the same result.
    50     function complex_test_methods(aGroup) {
    51       var prefName = "test.pref.1";
    52       var prefValue = Math.floor(Math.random() * 100);
    54       do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined);
    56       do_check_true(cps.hasPref(uri, prefName));
    57       do_check_true(cps.hasPref(stringURI, prefName));
    58       do_check_true(cps.hasPref(stringObjectURI, prefName));
    60       do_check_eq(cps.getPref(uri, prefName), prefValue);
    61       do_check_eq(cps.getPref(stringURI, prefName), prefValue);
    62       do_check_eq(cps.getPref(stringObjectURI, prefName), prefValue);
    64       do_check_eq(cps.removePref(aGroup, prefName), undefined);
    66       do_check_false(cps.hasPref(uri, prefName));
    67       do_check_false(cps.hasPref(stringURI, prefName));
    68       do_check_false(cps.hasPref(stringObjectURI, prefName));
    69     }
    71     complex_test_methods(uri);
    72     complex_test_methods(stringURI);
    73     complex_test_methods(stringObjectURI);
    74   }
    76   {
    77     // test getPrefs returns the same prefs
    78     do_check_eq(cps.setPref(stringObjectURI, "test.5", 5), undefined);
    79     do_check_eq(cps.setPref(stringURI, "test.2", 2), undefined);
    80     do_check_eq(cps.setPref(uri, "test.1", 1), undefined);
    82     enumerateAndCheck(cps.getPrefs(uri), 8, ["test.1","test.2","test.5"]);
    83     enumerateAndCheck(cps.getPrefs(stringURI), 8, ["test.1","test.2","test.5"]);
    84     enumerateAndCheck(cps.getPrefs(stringObjectURI), 8, ["test.1","test.2","test.5"]);
    86     do_check_eq(cps.setPref(uri, "test.4", 4), undefined);
    87     do_check_eq(cps.setPref(stringObjectURI, "test.0", 0), undefined);
    89     enumerateAndCheck(cps.getPrefs(uri), 12, ["test.0","test.1","test.2","test.4","test.5"]);
    90     enumerateAndCheck(cps.getPrefs(stringURI), 12, ["test.0","test.1","test.2","test.4","test.5"]);
    91     enumerateAndCheck(cps.getPrefs(stringObjectURI), 12, ["test.0","test.1","test.2","test.4","test.5"]);
    93     do_check_eq(cps.setPref(stringURI, "test.3", 3), undefined);
    95     enumerateAndCheck(cps.getPrefs(uri), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]);
    96     enumerateAndCheck(cps.getPrefs(stringURI), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]);
    97     enumerateAndCheck(cps.getPrefs(stringObjectURI), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]);
    98   }
    99 }
   101 function do_check_thrown (aCallback) {
   102   var exThrown = false;
   103   try {
   104     aCallback();
   105     do_throw("NS_ERROR_ILLEGAL_VALUE should have been thrown here");
   106   } catch (e) {
   107     do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
   108     exThrown = true;
   109   }
   110   do_check_true(exThrown);
   111 }
   113 function enumerateAndCheck(prefs, expectedSum, expectedNames) {
   114   var enumerator = prefs.enumerator;
   115   var sum = 0;
   116   while (enumerator.hasMoreElements()) {
   117     var property = enumerator.getNext().QueryInterface(Components.interfaces.nsIProperty);
   118     sum += parseInt(property.value);
   120     // remove the pref name from the list of expected names
   121     var index = expectedNames.indexOf(property.name);
   122     do_check_true(index >= 0);
   123     expectedNames.splice(index, 1);
   124   }
   125   do_check_eq(sum, expectedSum);
   126   // check all pref names have been removed from the array
   127   do_check_eq(expectedNames.length, 0);
   128 }

mercurial