michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function run_test() { michael@0: michael@0: var cps = new ContentPrefInstance(null); michael@0: michael@0: // Make sure disk synchronization checking is turned off by default. michael@0: var statement = cps.DBConnection.createStatement("PRAGMA synchronous"); michael@0: statement.executeStep(); michael@0: do_check_eq(0, statement.getInt32(0)); michael@0: michael@0: // These are the different types of aGroup arguments we'll test. michael@0: var anObject = {"foo":"bar"}; // a simple object michael@0: var uri = ContentPrefTest.getURI("http://www.example.com/"); // nsIURI michael@0: var stringURI = "www.example.com"; // typeof = "string" michael@0: var stringObjectURI = new String("www.example.com"); // typeof = "object" michael@0: michael@0: { michael@0: // First check that all the methods work or don't work. michael@0: function simple_test_methods(aGroup, shouldThrow) { michael@0: var prefName = "test.pref.0"; michael@0: var prefValue = Math.floor(Math.random() * 100); michael@0: michael@0: if (shouldThrow) { michael@0: do_check_thrown(function () { cps.getPref(aGroup, prefName); }); michael@0: do_check_thrown(function () { cps.setPref(aGroup, prefName, prefValue); }); michael@0: do_check_thrown(function () { cps.hasPref(aGroup, prefName); }); michael@0: do_check_thrown(function () { cps.removePref(aGroup, prefName); }); michael@0: do_check_thrown(function () { cps.getPrefs(aGroup); }); michael@0: } else { michael@0: do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined); michael@0: do_check_true(cps.hasPref(aGroup, prefName)); michael@0: do_check_eq(cps.getPref(aGroup, prefName), prefValue); michael@0: do_check_eq(cps.removePref(aGroup, prefName), undefined); michael@0: do_check_false(cps.hasPref(aGroup, prefName)); michael@0: } michael@0: } michael@0: michael@0: simple_test_methods(cps, true); // arbitrary nsISupports object, should throw too michael@0: simple_test_methods(anObject, true); michael@0: simple_test_methods(uri, false); michael@0: simple_test_methods(stringURI, false); michael@0: simple_test_methods(stringObjectURI, false); michael@0: } michael@0: michael@0: { michael@0: // Now we'll check that each argument produces the same result. michael@0: function complex_test_methods(aGroup) { michael@0: var prefName = "test.pref.1"; michael@0: var prefValue = Math.floor(Math.random() * 100); michael@0: michael@0: do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined); michael@0: michael@0: do_check_true(cps.hasPref(uri, prefName)); michael@0: do_check_true(cps.hasPref(stringURI, prefName)); michael@0: do_check_true(cps.hasPref(stringObjectURI, prefName)); michael@0: michael@0: do_check_eq(cps.getPref(uri, prefName), prefValue); michael@0: do_check_eq(cps.getPref(stringURI, prefName), prefValue); michael@0: do_check_eq(cps.getPref(stringObjectURI, prefName), prefValue); michael@0: michael@0: do_check_eq(cps.removePref(aGroup, prefName), undefined); michael@0: michael@0: do_check_false(cps.hasPref(uri, prefName)); michael@0: do_check_false(cps.hasPref(stringURI, prefName)); michael@0: do_check_false(cps.hasPref(stringObjectURI, prefName)); michael@0: } michael@0: michael@0: complex_test_methods(uri); michael@0: complex_test_methods(stringURI); michael@0: complex_test_methods(stringObjectURI); michael@0: } michael@0: michael@0: { michael@0: // test getPrefs returns the same prefs michael@0: do_check_eq(cps.setPref(stringObjectURI, "test.5", 5), undefined); michael@0: do_check_eq(cps.setPref(stringURI, "test.2", 2), undefined); michael@0: do_check_eq(cps.setPref(uri, "test.1", 1), undefined); michael@0: michael@0: enumerateAndCheck(cps.getPrefs(uri), 8, ["test.1","test.2","test.5"]); michael@0: enumerateAndCheck(cps.getPrefs(stringURI), 8, ["test.1","test.2","test.5"]); michael@0: enumerateAndCheck(cps.getPrefs(stringObjectURI), 8, ["test.1","test.2","test.5"]); michael@0: michael@0: do_check_eq(cps.setPref(uri, "test.4", 4), undefined); michael@0: do_check_eq(cps.setPref(stringObjectURI, "test.0", 0), undefined); michael@0: michael@0: enumerateAndCheck(cps.getPrefs(uri), 12, ["test.0","test.1","test.2","test.4","test.5"]); michael@0: enumerateAndCheck(cps.getPrefs(stringURI), 12, ["test.0","test.1","test.2","test.4","test.5"]); michael@0: enumerateAndCheck(cps.getPrefs(stringObjectURI), 12, ["test.0","test.1","test.2","test.4","test.5"]); michael@0: michael@0: do_check_eq(cps.setPref(stringURI, "test.3", 3), undefined); michael@0: michael@0: enumerateAndCheck(cps.getPrefs(uri), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]); michael@0: enumerateAndCheck(cps.getPrefs(stringURI), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]); michael@0: enumerateAndCheck(cps.getPrefs(stringObjectURI), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]); michael@0: } michael@0: } michael@0: michael@0: function do_check_thrown (aCallback) { michael@0: var exThrown = false; michael@0: try { michael@0: aCallback(); michael@0: do_throw("NS_ERROR_ILLEGAL_VALUE should have been thrown here"); michael@0: } catch (e) { michael@0: do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_VALUE); michael@0: exThrown = true; michael@0: } michael@0: do_check_true(exThrown); michael@0: } michael@0: michael@0: function enumerateAndCheck(prefs, expectedSum, expectedNames) { michael@0: var enumerator = prefs.enumerator; michael@0: var sum = 0; michael@0: while (enumerator.hasMoreElements()) { michael@0: var property = enumerator.getNext().QueryInterface(Components.interfaces.nsIProperty); michael@0: sum += parseInt(property.value); michael@0: michael@0: // remove the pref name from the list of expected names michael@0: var index = expectedNames.indexOf(property.name); michael@0: do_check_true(index >= 0); michael@0: expectedNames.splice(index, 1); michael@0: } michael@0: do_check_eq(sum, expectedSum); michael@0: // check all pref names have been removed from the array michael@0: do_check_eq(expectedNames.length, 0); michael@0: }