michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ */ michael@0: michael@0: function run_test() { michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: const PREF_NAME = "testPref"; michael@0: michael@0: var ps = Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(Ci.nsIPrefService); michael@0: var prefs = ps.getDefaultBranch(null); michael@0: var userprefs = ps.getBranch(null); michael@0: michael@0: /* First, test to make sure we can parse a float from a string properly. */ michael@0: prefs.setCharPref(PREF_NAME, "9.674"); michael@0: prefs.lockPref(PREF_NAME); michael@0: var myFloat = 9.674; michael@0: var fudge = 0.001; michael@0: var floatPref = userprefs.getFloatPref(PREF_NAME); michael@0: do_check_true(myFloat+fudge >= floatPref); michael@0: do_check_true(myFloat-fudge <= floatPref); michael@0: michael@0: /* Now test some failure conditions. */ michael@0: prefs.unlockPref(PREF_NAME); michael@0: prefs.setCharPref(PREF_NAME, ""); michael@0: prefs.lockPref(PREF_NAME); michael@0: do_check_throws(function() { michael@0: userprefs.getFloatPref(PREF_NAME); michael@0: }, Cr.NS_ERROR_ILLEGAL_VALUE); michael@0: michael@0: prefs.unlockPref(PREF_NAME); michael@0: prefs.setCharPref(PREF_NAME, "18.0a1"); michael@0: prefs.lockPref(PREF_NAME); michael@0: michael@0: do_check_throws(function() { michael@0: userprefs.getFloatPref(PREF_NAME); michael@0: }, Cr.NS_ERROR_ILLEGAL_VALUE); michael@0: michael@0: prefs.unlockPref(PREF_NAME); michael@0: prefs.setCharPref(PREF_NAME, "09.25.2012"); michael@0: prefs.lockPref(PREF_NAME); michael@0: michael@0: do_check_throws(function() { michael@0: userprefs.getFloatPref(PREF_NAME); michael@0: }, Cr.NS_ERROR_ILLEGAL_VALUE); michael@0: michael@0: prefs.unlockPref(PREF_NAME); michael@0: prefs.setCharPref(PREF_NAME, "aString"); michael@0: prefs.lockPref(PREF_NAME); michael@0: michael@0: do_check_throws(function() { michael@0: userprefs.getFloatPref(PREF_NAME); michael@0: }, Cr.NS_ERROR_ILLEGAL_VALUE); michael@0: }