xpcom/tests/unit/test_bug325418.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 const Cc = Components.classes;
     2 const Ci = Components.interfaces;
     4 // 5 seconds.
     5 const kExpectedDelay1 = 5;
     6 // 1 second.
     7 const kExpectedDelay2 = 1;
     9 var gStartTime1;
    10 var gStartTime2;
    11 var timer;
    13 var observer1 = {
    14   observe: function observeTC1(subject, topic, data) {
    15     if (topic == "timer-callback") {
    16       // Stop timer, so it doesn't repeat (if test runs slowly).
    17       timer.cancel();
    19       // Actual delay may not be exact, so convert to seconds and round.
    20       do_check_eq(Math.round((Date.now() - gStartTime1) / 1000),
    21                   kExpectedDelay1);
    23       timer = null;
    25       do_print("1st timer triggered (before being cancelled). Should not have happened!");
    26       do_check_true(false);
    27     }
    28   }
    29 };
    31 var observer2 = {
    32   observe: function observeTC2(subject, topic, data) {
    33     if (topic == "timer-callback") {
    34       // Stop timer, so it doesn't repeat (if test runs slowly).
    35       timer.cancel();
    37       // Actual delay may not be exact, so convert to seconds and round.
    38       do_check_eq(Math.round((Date.now() - gStartTime2) / 1000),
    39                   kExpectedDelay2);
    41       timer = null;
    43       do_test_finished();
    44     }
    45   }
    46 };
    48 function run_test() {
    49   do_test_pending();
    51   timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    53   // Initialize the timer (with some delay), then cancel it.
    54   gStartTime1 = Date.now();
    55   timer.init(observer1, kExpectedDelay1 * 1000, timer.TYPE_REPEATING_PRECISE);
    56   timer.cancel();
    58   // Re-initialize the timer (with a different delay).
    59   gStartTime2 = Date.now();
    60   timer.init(observer2, kExpectedDelay2 * 1000, timer.TYPE_REPEATING_PRECISE);
    61 }

mercurial