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

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial