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 | Cu.import("resource://services-common/utils.js"); |
michael@0 | 5 | |
michael@0 | 6 | function run_test() { |
michael@0 | 7 | run_next_test(); |
michael@0 | 8 | } |
michael@0 | 9 | |
michael@0 | 10 | add_test(function test_required_args() { |
michael@0 | 11 | try { |
michael@0 | 12 | CommonUtils.namedTimer(function callback() { |
michael@0 | 13 | do_throw("Shouldn't fire."); |
michael@0 | 14 | }, 0); |
michael@0 | 15 | do_throw("Should have thrown!"); |
michael@0 | 16 | } catch(ex) { |
michael@0 | 17 | run_next_test(); |
michael@0 | 18 | } |
michael@0 | 19 | }); |
michael@0 | 20 | |
michael@0 | 21 | add_test(function test_simple() { |
michael@0 | 22 | _("Test basic properties of CommonUtils.namedTimer."); |
michael@0 | 23 | |
michael@0 | 24 | const delay = 200; |
michael@0 | 25 | let that = {}; |
michael@0 | 26 | let t0 = Date.now(); |
michael@0 | 27 | CommonUtils.namedTimer(function callback(timer) { |
michael@0 | 28 | do_check_eq(this, that); |
michael@0 | 29 | do_check_eq(this._zetimer, null); |
michael@0 | 30 | do_check_true(timer instanceof Ci.nsITimer); |
michael@0 | 31 | // Difference should be ~delay, but hard to predict on all platforms, |
michael@0 | 32 | // particularly Windows XP. |
michael@0 | 33 | do_check_true(Date.now() > t0); |
michael@0 | 34 | run_next_test(); |
michael@0 | 35 | }, delay, that, "_zetimer"); |
michael@0 | 36 | }); |
michael@0 | 37 | |
michael@0 | 38 | add_test(function test_delay() { |
michael@0 | 39 | _("Test delaying a timer that hasn't fired yet."); |
michael@0 | 40 | |
michael@0 | 41 | const delay = 100; |
michael@0 | 42 | let that = {}; |
michael@0 | 43 | let t0 = Date.now(); |
michael@0 | 44 | function callback(timer) { |
michael@0 | 45 | // Difference should be ~2*delay, but hard to predict on all platforms, |
michael@0 | 46 | // particularly Windows XP. |
michael@0 | 47 | do_check_true((Date.now() - t0) > delay); |
michael@0 | 48 | run_next_test(); |
michael@0 | 49 | } |
michael@0 | 50 | CommonUtils.namedTimer(callback, delay, that, "_zetimer"); |
michael@0 | 51 | CommonUtils.namedTimer(callback, 2 * delay, that, "_zetimer"); |
michael@0 | 52 | run_next_test(); |
michael@0 | 53 | }); |
michael@0 | 54 | |
michael@0 | 55 | add_test(function test_clear() { |
michael@0 | 56 | _("Test clearing a timer that hasn't fired yet."); |
michael@0 | 57 | |
michael@0 | 58 | const delay = 0; |
michael@0 | 59 | let that = {}; |
michael@0 | 60 | CommonUtils.namedTimer(function callback(timer) { |
michael@0 | 61 | do_throw("Shouldn't fire!"); |
michael@0 | 62 | }, delay, that, "_zetimer"); |
michael@0 | 63 | |
michael@0 | 64 | that._zetimer.clear(); |
michael@0 | 65 | do_check_eq(that._zetimer, null); |
michael@0 | 66 | CommonUtils.nextTick(run_next_test); |
michael@0 | 67 | |
michael@0 | 68 | run_next_test(); |
michael@0 | 69 | }); |