The goog.testing.MockClock Class

goog.testing.MockClock
> goog.Disposable

goog.testing.MockClock(opt_autoInstall)

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.

opt_autoInstall {boolean=}
Install the MockClock at construction time.

.REQUEST_ANIMATION_FRAME_TIMEOUT {number}

Default wait timeout for mocking requestAnimationFrame (in milliseconds).

@const

.addOnDisposeCallback(callback, opt_scope)

Inherited from goog.Disposable .

Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.

callback {!Function}
The callback function.
opt_scope {Object=}
An optional scope to call the callback in.

.creationStack {string}

Inherited from goog.Disposable .

If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.

.dispose()

Inherited from goog.Disposable .

Disposes of the object. If the object hasn't already been disposed of, calls {@link #disposeInternal}. Classes that extend {@code goog.Disposable} should override {@link #disposeInternal} in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.

returns {void}
Nothing.

.getCurrentTime()

returns {number}
The MockClock's current time in milliseconds.

.getTimeoutDelay()

returns {number}
delay The amount of time between when a timeout is scheduled to fire and when it actually fires, in milliseconds. May be negative.

.getTimeoutsMade()

returns {number}
The number of timeouts that have been scheduled.

.install()

Installs the MockClock by overriding the global object's implementation of setTimeout, setInterval, clearTimeout and clearInterval.

.isDisposed()

Inherited from goog.Disposable .

returns {boolean}
Whether the object has been disposed of.

.isTimeoutSet(timeoutKey)

timeoutKey {number}
The timeout key.
returns {boolean}
Whether the timer has been set and not cleared, independent of the timeout's expiration. In other words, the timeout could have passed or could be scheduled for the future. Either way, this function returns true or false depending only on whether the provided timeoutKey represents a timeout that has been set and not cleared.

.registerDisposable(disposable)

Inherited from goog.Disposable .

Associates a disposable object with this object so that they will be disposed together.

disposable {goog.disposable.IDisposable}
that will be disposed when this object is disposed.

.reset()

Resets the MockClock, removing all timeouts that are scheduled and resets the fake timer count.

.setTimeoutDelay(delay)

Sets the amount of time between when a timeout is scheduled to fire and when it actually fires.

delay {number}
The delay in milliseconds. May be negative.

.tick(opt_millis)

Increments the MockClock's time by a given number of milliseconds, running any functions that are now overdue.

opt_millis {number=}
Number of milliseconds to increment the counter. If not specified, clock ticks 1 millisecond.
returns {number}
Current mock time in milliseconds.

.uninstall()

Removes the MockClock's hooks into the global object's functions and revert to their original values.