test_utils_namedTimer.js (1774B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 add_test(function test_required_args() { 5 try { 6 CommonUtils.namedTimer(function callback() { 7 do_throw("Shouldn't fire."); 8 }, 0); 9 do_throw("Should have thrown!"); 10 } catch (ex) { 11 run_next_test(); 12 } 13 }); 14 15 add_test(function test_simple() { 16 _("Test basic properties of CommonUtils.namedTimer."); 17 18 const delay = 200; 19 let that = {}; 20 let t0 = Date.now(); 21 CommonUtils.namedTimer( 22 function callback(timer) { 23 Assert.equal(this, that); 24 Assert.equal(this._zetimer, null); 25 Assert.ok(timer instanceof Ci.nsITimer); 26 // Difference should be ~delay, but hard to predict on all platforms, 27 // particularly Windows XP. 28 Assert.greater(Date.now(), t0); 29 run_next_test(); 30 }, 31 delay, 32 that, 33 "_zetimer" 34 ); 35 }); 36 37 add_test(function test_delay() { 38 _("Test delaying a timer that hasn't fired yet."); 39 40 const delay = 100; 41 let that = {}; 42 let t0 = Date.now(); 43 function callback() { 44 // Difference should be ~2*delay, but hard to predict on all platforms, 45 // particularly Windows XP. 46 Assert.greater(Date.now() - t0, delay); 47 run_next_test(); 48 } 49 CommonUtils.namedTimer(callback, delay, that, "_zetimer"); 50 CommonUtils.namedTimer(callback, 2 * delay, that, "_zetimer"); 51 run_next_test(); 52 }); 53 54 add_test(function test_clear() { 55 _("Test clearing a timer that hasn't fired yet."); 56 57 const delay = 0; 58 let that = {}; 59 CommonUtils.namedTimer( 60 function callback() { 61 do_throw("Shouldn't fire!"); 62 }, 63 delay, 64 that, 65 "_zetimer" 66 ); 67 68 that._zetimer.clear(); 69 Assert.equal(that._zetimer, null); 70 CommonUtils.nextTick(run_next_test); 71 72 run_next_test(); 73 });