testing.MockClock Extends goog.Disposable
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.

Inheritance

Constructor

goog.testing.MockClock(opt_autoInstall)

Parameters

opt_autoInstall : boolean=
Install the MockClock at construction time.

Instance Methods

Public Protected Private
cancelRequestAnimationFrame_(timeoutKey)
Clears a requestAnimationFrame. Mock implementation for cancelRequestAnimationFrame.
Arguments:
timeoutKey : number
The requestAnimationFrame key to clear.
code »
clearInterval_(timeoutKey)
Clears an interval. Mock implementation for clearInterval.
Arguments:
timeoutKey : number
The interval key to clear.
code »
clearTimeout_(timeoutKey)
Clears a timeout. Mock implementation for clearTimeout.
Arguments:
timeoutKey : number
The timeout key to clear.
code »
disposeInternal()
No description.
code »
fireResetEvent()
Signals that the mock clock has been reset, allowing objects that maintain their own internal state to reset.
code »
getCurrentTime() number
No description.
Returns: number  The MockClock's current time in milliseconds.
code »
getTimeoutDelay() number
No description.
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.
code »
getTimeoutsMade() number
No description.
Returns: number  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
No description.
Arguments:
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.
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.
Arguments:
funcToCall : Function
The function to call.
Returns: number  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.
Arguments:
endTime : number
The latest time in the range, in milliseconds.
code »
scheduleFunction_(timeoutKeyfuncToCallmillisrecurring)
Schedules a function to be run at a certain time.
Arguments:
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.
Arguments:
funcToCall : Function
The function to call.
Returns: number  The number of timeouts created.
code »
setInterval_(funcToCallmillis) number
Schedules a function to be called every millis milliseconds. Mock implementation for setInterval.
Arguments:
funcToCall : Function
The function to call.
millis : number
The number of milliseconds between calls.
Returns: number  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.
Arguments:
delay : number
The delay in milliseconds. May be negative.
code »
setTimeout_(funcToCallmillis) number
Schedules a function to be called after millis milliseconds. Mock implementation for setTimeout.
Arguments:
funcToCall : Function
The function to call.
millis : number
The number of milliseconds to call it after.
Returns: number  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.
Arguments:
opt_millis : number=
Number of milliseconds to increment the counter. If not specified, clock ticks 1 millisecond.
Returns: number  Current mock time in milliseconds.
code »
uninstall()
Removes the MockClock's hooks into the global object's functions and revert to their original values.
code »
addOnDisposeCallback(callbackopt_scope)
Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.
Arguments:
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: void  Nothing.
code »
disposeInternal()
Deletes or nulls out any references to COM objects, DOM nodes, or other disposable objects. Classes that extend goog.Disposable should override this method. Not reentrant. To avoid calling it twice, it must only be called from the subclass' disposeInternal method. Everywhere else the public dispose method must be used. For example:
  mypackage.MyClass = function() {
    mypackage.MyClass.base(this, 'constructor');
    // Constructor logic specific to MyClass.
    ...
  };
  goog.inherits(mypackage.MyClass, goog.Disposable);

  mypackage.MyClass.prototype.disposeInternal = function() {
    // Dispose logic specific to MyClass.
    ...
    // Call superclass's disposeInternal at the end of the subclass's, like
    // in C++, to avoid hard-to-catch issues.
    mypackage.MyClass.base(this, 'disposeInternal');
  };
code »
getDisposed() boolean
Use #isDisposed instead. No description.
Returns: boolean  Whether the object has been disposed of.
code »
isDisposed() boolean
No description.
Returns: boolean  Whether the object has been disposed of.
code »
registerDisposable(disposable)
Associates a disposable object with this object so that they will be disposed together.
Arguments:
disposable : goog.disposable.IDisposable
that will be disposed when this object is disposed.
code »

Instance Properties

constructor :
No description.
Code »
deletedKeys_ :
Map of deleted keys. These keys represents keys that were deleted in a clearInterval, timeoutid -> object.
Code »
nowMillis_ :
The current simulated time in milliseconds.
Code »
queue_ :
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 »
PropertyReplacer instance which overwrites and resets setTimeout, setInterval, etc. or null if the MockClock is not installed.
Code »
timeoutDelay_ :
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_ :
Count of the number of timeouts made.
Code »
creationStack :
If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.
Code »
disposed_ :
Whether the object has been disposed of.
Code »
onDisposeCallbacks_ :
Callbacks to invoke when this object is disposed.
Code »

Static Methods

goog.testing.MockClock.insert_(timeoutqueue)
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.
Arguments:
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.
code »

Static Properties

goog.testing.MockClock.MAX_INT_ :
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 :
Default wait timeout for mocking requestAnimationFrame (in milliseconds).
Code »
goog.testing.MockClock.superClass_ :
No description.
Code »

Package testing

Package Reference