michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://services-common/utils.js"); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_required_args() { michael@0: try { michael@0: CommonUtils.namedTimer(function callback() { michael@0: do_throw("Shouldn't fire."); michael@0: }, 0); michael@0: do_throw("Should have thrown!"); michael@0: } catch(ex) { michael@0: run_next_test(); michael@0: } michael@0: }); michael@0: michael@0: add_test(function test_simple() { michael@0: _("Test basic properties of CommonUtils.namedTimer."); michael@0: michael@0: const delay = 200; michael@0: let that = {}; michael@0: let t0 = Date.now(); michael@0: CommonUtils.namedTimer(function callback(timer) { michael@0: do_check_eq(this, that); michael@0: do_check_eq(this._zetimer, null); michael@0: do_check_true(timer instanceof Ci.nsITimer); michael@0: // Difference should be ~delay, but hard to predict on all platforms, michael@0: // particularly Windows XP. michael@0: do_check_true(Date.now() > t0); michael@0: run_next_test(); michael@0: }, delay, that, "_zetimer"); michael@0: }); michael@0: michael@0: add_test(function test_delay() { michael@0: _("Test delaying a timer that hasn't fired yet."); michael@0: michael@0: const delay = 100; michael@0: let that = {}; michael@0: let t0 = Date.now(); michael@0: function callback(timer) { michael@0: // Difference should be ~2*delay, but hard to predict on all platforms, michael@0: // particularly Windows XP. michael@0: do_check_true((Date.now() - t0) > delay); michael@0: run_next_test(); michael@0: } michael@0: CommonUtils.namedTimer(callback, delay, that, "_zetimer"); michael@0: CommonUtils.namedTimer(callback, 2 * delay, that, "_zetimer"); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_clear() { michael@0: _("Test clearing a timer that hasn't fired yet."); michael@0: michael@0: const delay = 0; michael@0: let that = {}; michael@0: CommonUtils.namedTimer(function callback(timer) { michael@0: do_throw("Shouldn't fire!"); michael@0: }, delay, that, "_zetimer"); michael@0: michael@0: that._zetimer.clear(); michael@0: do_check_eq(that._zetimer, null); michael@0: CommonUtils.nextTick(run_next_test); michael@0: michael@0: run_next_test(); michael@0: });