Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | Cu.import("resource://gre/modules/Preferences.jsm"); |
michael@0 | 7 | Cu.import("resource://services-common/utils.js"); |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | let prefs = new Preferences("servicescommon.tests."); |
michael@0 | 11 | |
michael@0 | 12 | function DummyLogger() { |
michael@0 | 13 | this.messages = []; |
michael@0 | 14 | } |
michael@0 | 15 | DummyLogger.prototype.warn = function warn(message) { |
michael@0 | 16 | this.messages.push(message); |
michael@0 | 17 | }; |
michael@0 | 18 | |
michael@0 | 19 | function run_test() { |
michael@0 | 20 | run_next_test(); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | add_test(function test_set_basic() { |
michael@0 | 24 | let now = new Date(); |
michael@0 | 25 | |
michael@0 | 26 | CommonUtils.setDatePref(prefs, "test00", now); |
michael@0 | 27 | let value = prefs.get("test00"); |
michael@0 | 28 | do_check_eq(value, "" + now.getTime()); |
michael@0 | 29 | |
michael@0 | 30 | let now2 = CommonUtils.getDatePref(prefs, "test00"); |
michael@0 | 31 | |
michael@0 | 32 | do_check_eq(now.getTime(), now2.getTime()); |
michael@0 | 33 | |
michael@0 | 34 | run_next_test(); |
michael@0 | 35 | }); |
michael@0 | 36 | |
michael@0 | 37 | add_test(function test_set_bounds_checking() { |
michael@0 | 38 | let d = new Date(2342354); |
michael@0 | 39 | |
michael@0 | 40 | let failed = false; |
michael@0 | 41 | try { |
michael@0 | 42 | CommonUtils.setDatePref(prefs, "test01", d); |
michael@0 | 43 | } catch (ex) { |
michael@0 | 44 | do_check_true(ex.message.startsWith("Trying to set")); |
michael@0 | 45 | failed = true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | do_check_true(failed); |
michael@0 | 49 | run_next_test(); |
michael@0 | 50 | }); |
michael@0 | 51 | |
michael@0 | 52 | add_test(function test_get_bounds_checking() { |
michael@0 | 53 | prefs.set("test_bounds_checking", "13241431"); |
michael@0 | 54 | |
michael@0 | 55 | let log = new DummyLogger(); |
michael@0 | 56 | let d = CommonUtils.getDatePref(prefs, "test_bounds_checking", 0, log); |
michael@0 | 57 | do_check_eq(d.getTime(), 0); |
michael@0 | 58 | do_check_eq(log.messages.length, 1); |
michael@0 | 59 | |
michael@0 | 60 | run_next_test(); |
michael@0 | 61 | }); |
michael@0 | 62 | |
michael@0 | 63 | add_test(function test_get_bad_default() { |
michael@0 | 64 | let failed = false; |
michael@0 | 65 | try { |
michael@0 | 66 | CommonUtils.getDatePref(prefs, "get_bad_default", new Date()); |
michael@0 | 67 | } catch (ex) { |
michael@0 | 68 | do_check_true(ex.message.startsWith("Default value is not a number")); |
michael@0 | 69 | failed = true; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | do_check_true(failed); |
michael@0 | 73 | run_next_test(); |
michael@0 | 74 | }); |
michael@0 | 75 | |
michael@0 | 76 | add_test(function test_get_invalid_number() { |
michael@0 | 77 | prefs.set("get_invalid_number", "hello world"); |
michael@0 | 78 | |
michael@0 | 79 | let log = new DummyLogger(); |
michael@0 | 80 | let d = CommonUtils.getDatePref(prefs, "get_invalid_number", 42, log); |
michael@0 | 81 | do_check_eq(d.getTime(), 42); |
michael@0 | 82 | do_check_eq(log.messages.length, 1); |
michael@0 | 83 | |
michael@0 | 84 | run_next_test(); |
michael@0 | 85 | }); |