Home

# Class goog.testing.MockClock

Class for unit testing code that uses setTimeout and clearTimeout. NOTE: If you are using MockClock to test code that makes use of goog.fx.Animation, then you must either: 1. Install and dispose of the MockClock in setUpPage() and tearDownPage() respectively (rather than setUp()/tearDown()). or 2. Ensure that every test clears the animation queue by calling mockClock.tick(x) at the end of each test function (where `x` is large enough to complete all animations). Otherwise, if any animation is left pending at the time that MockClock.dispose() is called, that will permanently prevent any future animations from playing on the page.

extends goog.Disposable

Instance Method Summary
| `clearInterval_(number timeoutKey)` Clears an interval. Mock implementation for window.clearInterval
| `clearTimeout_(number timeoutKey)` Clears a timeout. Mock implementation for window.clearTimeout

`disposeInternal()`

Deletes or nulls out any references to COM objects, DOM nodes, or other disposable objects. Classes that extend {@code goog.Disposable} should override this method. Not reentrant. To avoid calling it twice, it must only be called from the subclass' {@code disposeInternal} method. Everywhere else the public {@code dispose} method must be used. For example:

    mypackage.MyClass = function() {
    goog.base(this);
    // Constructor logic specific to MyClass.
    ...
    };
    goog.inherits(mypackage.MyClass, goog.Disposable);

    mypackage.MyClass.prototype.disposeInternal = function() {
    goog.base(this, 'disposeInternal');
    // Dispose logic specific to MyClass.
    ...
    };

| `getCurrentTime() ⇒ number`
| `getTimeoutDelay() ⇒ number`
| `getTimeoutsMade() ⇒ number`
| `install()` Installs the MockClock by overriding the window object's implementation of setTimeout, setInterval, clearTimeout and clearInterval.
| `isTimeoutSet(number timeoutKey) ⇒ boolean`
| `reset()` Resets the MockClock, removing all timeouts that are scheduled and resets the fake timer count.
| `runFunctionsWithinRange_(number endTime)` Runs any function that is scheduled before a certain time. Timeouts can be made to fire early or late if timeoutDelay_ is non-0.
| `scheduleFunction_(number timeoutKey, ?Function funcToCall, number millis, boolean recurring)` Schedules a function to be run at a certain time.
| `setInterval_(?Function funcToCall, number millis) ⇒ number` Schedules a function to be called every {@code millis} milliseconds. Mock implementation for window.setInterval
| `setTimeoutDelay(number delay)` Sets the amount of time between when a timeout is scheduled to fire and when it actually fires.
| `setTimeout_(?Function funcToCall, number millis) ⇒ number` Schedules a function to be called after {@code millis} milliseconds. Mock implementation for window.setTimeout
| `tick(number= opt_millis) ⇒ number` Increments the MockClock's time by a given number of milliseconds, running any functions that are now overdue.
| `uninstall()` Removes the MockClock's hooks into the window functions and revert to their original values.

Static Method Summary
| `insert_(?Object timeout, ?Array.<(Object|null)> queue)` Inserts a timer descriptor into a descending-order queue. Later-inserted duplicates appear at lower indices. For example, the asterisk in (5,4,*,3,2,1) would be the insertion point for 3.