/services/sync/tests/unit/test_utils_namedTimer.js

http://github.com/zpao/v8monkey · JavaScript · 69 lines · 52 code · 11 blank · 6 comment · 0 complexity · a39b62ade30b0634e2bb1d3b95901f24 MD5 · raw file

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