xpcom/tests/unit/test_bug325418.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/unit/test_bug325418.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,61 @@
     1.4 +const Cc = Components.classes;
     1.5 +const Ci = Components.interfaces;
     1.6 +
     1.7 +// 5 seconds.
     1.8 +const kExpectedDelay1 = 5;
     1.9 +// 1 second.
    1.10 +const kExpectedDelay2 = 1;
    1.11 +
    1.12 +var gStartTime1;
    1.13 +var gStartTime2;
    1.14 +var timer;
    1.15 +
    1.16 +var observer1 = {
    1.17 +  observe: function observeTC1(subject, topic, data) {
    1.18 +    if (topic == "timer-callback") {
    1.19 +      // Stop timer, so it doesn't repeat (if test runs slowly).
    1.20 +      timer.cancel();
    1.21 +
    1.22 +      // Actual delay may not be exact, so convert to seconds and round.
    1.23 +      do_check_eq(Math.round((Date.now() - gStartTime1) / 1000),
    1.24 +                  kExpectedDelay1);
    1.25 +
    1.26 +      timer = null;
    1.27 +
    1.28 +      do_print("1st timer triggered (before being cancelled). Should not have happened!");
    1.29 +      do_check_true(false);
    1.30 +    }
    1.31 +  }
    1.32 +};
    1.33 +
    1.34 +var observer2 = {
    1.35 +  observe: function observeTC2(subject, topic, data) {
    1.36 +    if (topic == "timer-callback") {
    1.37 +      // Stop timer, so it doesn't repeat (if test runs slowly).
    1.38 +      timer.cancel();
    1.39 +
    1.40 +      // Actual delay may not be exact, so convert to seconds and round.
    1.41 +      do_check_eq(Math.round((Date.now() - gStartTime2) / 1000),
    1.42 +                  kExpectedDelay2);
    1.43 +
    1.44 +      timer = null;
    1.45 +
    1.46 +      do_test_finished();
    1.47 +    }
    1.48 +  }
    1.49 +};
    1.50 +
    1.51 +function run_test() {
    1.52 +  do_test_pending();
    1.53 +
    1.54 +  timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.55 +
    1.56 +  // Initialize the timer (with some delay), then cancel it.
    1.57 +  gStartTime1 = Date.now();
    1.58 +  timer.init(observer1, kExpectedDelay1 * 1000, timer.TYPE_REPEATING_PRECISE);
    1.59 +  timer.cancel();
    1.60 +
    1.61 +  // Re-initialize the timer (with a different delay).
    1.62 +  gStartTime2 = Date.now();
    1.63 +  timer.init(observer2, kExpectedDelay2 * 1000, timer.TYPE_REPEATING_PRECISE);
    1.64 +}

mercurial