# Class goog.testing.MockClock

code »

    goog.Disposable
      └ goog.testing.MockClock

All implemented interfaces:
  `goog.disposable.IDisposable`

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.

## Constructor

goog.testing.MockClock ( opt_autoInstall )

Parameters
opt_autoInstall: `boolean=` Install the MockClock at construction time.

Show:Public Protected Private

## Instance Methods

### Defined in `goog.testing.MockClock`

code »cancelRequestAnimationFrame_ ( timeoutKey )

Clears a requestAnimationFrame. Mock implementation for cancelRequestAnimationFrame.

Parameters
timeoutKey: `number` The requestAnimationFrame key to clear.

code »clearInterval_ ( timeoutKey )

Clears an interval. Mock implementation for clearInterval.

Parameters
timeoutKey: `number` The interval key to clear.

code »clearTimeout_ ( timeoutKey )

Clears a timeout. Mock implementation for clearTimeout.

Parameters
timeoutKey: `number` The timeout key to clear.

code »disposeInternal ( )
code »fireResetEvent ( )

Signals that the mock clock has been reset, allowing objects that maintain their own internal state to reset.

code »getCurrentTime ( ) ⇒ `number`

Returns
The MockClock's current time in milliseconds.

code »getTimeoutDelay ( ) ⇒ `number`

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

code »getTimeoutsMade ( ) ⇒ `number`

Returns
The number of timeouts that have been scheduled.

code »install ( )

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

code »isTimeoutSet ( timeoutKey ) ⇒ `boolean`

Parameters
timeoutKey: `number` The timeout key.
Returns
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.

code »replaceRequestAnimationFrame_ ( )

Installs the mocks for requestAnimationFrame and cancelRequestAnimationFrame.

code »requestAnimationFrame_ ( funcToCall ) ⇒ `number`

Schedules a function to be called when an animation frame is triggered. Mock implementation for requestAnimationFrame.

Parameters
funcToCall: `Function` The function to call.
Returns
The number of timeouts created.

code »reset ( )

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

code »runFunctionsWithinRange_ ( 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.

Parameters
endTime: `number` The latest time in the range, in milliseconds.

code »scheduleFunction_ ( timeoutKey, funcToCall, millis, recurring )

Schedules a function to be run at a certain time.

Parameters
timeoutKey: `number` The timeout key. funcToCall: `Function` The function to call. millis: `number` The number of milliseconds to call it in. recurring: `boolean` Whether to function call should recur.

code »setImmediate_ ( funcToCall ) ⇒ `number`

Schedules a function to be called immediately after the current JS execution. Mock implementation for setImmediate.

Parameters
funcToCall: `Function` The function to call.
Returns
The number of timeouts created.

code »setInterval_ ( funcToCall, millis ) ⇒ `number`

Schedules a function to be called every `millis` milliseconds. Mock implementation for setInterval.

Parameters
funcToCall: `Function` The function to call. millis: `number` The number of milliseconds between calls.
Returns
The number of timeouts created.

code »setTimeoutDelay ( delay )

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

Parameters
delay: `number` The delay in milliseconds. May be negative.

code »setTimeout_ ( funcToCall, millis ) ⇒ `number`

Schedules a function to be called after `millis` milliseconds. Mock implementation for setTimeout.

Parameters
funcToCall: `Function` The function to call. millis: `number` The number of milliseconds to call it after.
Returns
The number of timeouts created.

code »tick ( opt_millis ) ⇒ `number`

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

Parameters
opt_millis: `number=` Number of milliseconds to increment the counter. If not specified, clock ticks 1 millisecond.
Returns
Current mock time in milliseconds.

code »uninstall ( )

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

### Defined in `goog.Disposable`

code »`<T>` addOnDisposeCallback ( callback, opt_scope )

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

Parameters
callback: `function(this: T): ?` The callback function. opt_scope: `T=` An optional scope to call the callback in.

code »dispose ( ) ⇒ `void`

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

Returns
Nothing.

code »getDisposed ( ) ⇒ `boolean`
Deprecated: Use `#isDisposed` instead.

Returns
Whether the object has been disposed of.

code »isDisposed ( ) ⇒ `boolean`

Returns
Whether the object has been disposed of.

code »registerDisposable ( disposable )

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

Parameters
disposable: `goog.disposable.IDisposable` that will be disposed when this object is disposed.

## Instance Properties

### Defined in `goog.testing.MockClock`

code »deletedKeys_ : `Object`

Map of deleted keys. These keys represents keys that were deleted in a clearInterval, timeoutid -> object.

code »nowMillis_ : `number`

The current simulated time in milliseconds.

code »queue_ : `Array.<Object>`

Reverse-order queue of timers to fire. The last item of the queue is popped off. Insertion happens from the right. For example, the expiration times for each element of the queue might be in the order 300, 200, 200.

code »replacer_ : `goog.testing.PropertyReplacer`

PropertyReplacer instance which overwrites and resets setTimeout, setInterval, etc. or null if the MockClock is not installed.

code »timeoutDelay_ : `number`

Additional delay between the time a timeout was set to fire, and the time it actually fires. Useful for testing workarounds for this Firefox 2 bug: https://bugzilla.mozilla.org/show_bug.cgi?id=291386 May be negative.

code »timeoutsMade_ : `number`

Count of the number of timeouts made.

### Defined in `goog.Disposable`

code »creationStack : `string`

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

code »disposed_ : `boolean`

Whether the object has been disposed of.

code »onDisposeCallbacks_ : `Array`

Callbacks to invoke when this object is disposed.

## Static Functions

code »goog.testing.MockClock.insert_ ( timeout, 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.

Parameters
timeout: `Object` The timeout to insert, with numerical runAtMillis property. queue: `Array.<Object>` The queue to insert into, with each element having a numerical runAtMillis property.

## Static Properties

code »goog.testing.MockClock.MAX_INT_ : `number`

Maximum 32-bit signed integer. Timeouts over this time return immediately in many browsers, due to integer overflow. Such known browsers include Firefox, Chrome, and Safari, but not IE.

code »goog.testing.MockClock.REQUEST_ANIMATION_FRAME_TIMEOUT : `number`

Default wait timeout for mocking requestAnimationFrame (in milliseconds).

code »goog.testing.MockClock.superClass_ : `goog.Disposable.prototype`