rectest/scripts/TestRunner.js

191 lines
4.6 KiB
JavaScript
Raw Normal View History

2012-03-03 12:06:40 -05:00
/**
2012-03-03 14:04:36 -05:00
* Runs a given test configuration, returning the samples displayed to the user
2012-03-03 12:06:40 -05:00
*/
rectest.TestRunner = Class( 'TestRunner',
{
/**
* jQuery instance
* @var {jQuery}
*/
'private _jQuery': null,
/**
* TestRun constructor
* @var {Function}
*/
'private _TestRun': null,
/**
* Render destination element
* @var {jQuery}
*/
'private _$dest': '',
2012-03-03 14:04:36 -05:00
/**
* History for the current/last run
* @var {Array.<Object>}
*/
'private _history': [],
/**
* Function to call with results when test run is complete
* @var {function(Array.<Object>)}
*/
'private _callback': null,
2012-03-03 12:06:40 -05:00
/**
* Initialize with jQuery instance
*
* @param {jQuery} jquery jQuery instance
* @param {Function} TestRun TestRun constructor
*/
__construct: function( jquery, TestRun )
{
this._jQuery = jquery;
this._TestRun = TestRun;
this._$dest = this._createDestElement();
},
/**
* Create the element to which all test cases will render
*
* @return {jQuery} destination element for all renderings
*/
'private _createDestElement': function()
{
return this._jQuery( '<div>' )
.addClass( 'render' );
2012-03-03 12:06:40 -05:00
},
/**
* Run the provided test case
*
* @param {TestCase} test_case test case to be run
* @param {Object} options configuration options
*
2012-03-03 14:04:36 -05:00
* @param {=function(Array.<Object>)} callback completion callback for
* results array
*
2012-03-03 12:06:40 -05:00
* @return {TestRunner} self
*/
2012-03-03 14:04:36 -05:00
'public run': function( test_case, options, callback )
2012-03-03 12:06:40 -05:00
{
2012-03-03 14:04:36 -05:00
options = options || {};
2012-03-03 12:06:40 -05:00
if ( !( easejs.Class.isA( rectest.TestCase, test_case ) ) )
{
throw TypeError( "Invalid test case: " + test_case );
}
2012-03-03 14:04:36 -05:00
// clear history from any previous runs
this._history = [];
this._callback = callback || function() {};
var _self = this,
run = _self._TestRun( test_case, options );
2012-03-03 12:06:40 -05:00
// allow test case to initialize its display
test_case.initRender( this._$dest );
2012-03-03 12:06:40 -05:00
// start test after the given delay
setTimeout( function()
{
_self._startInterval(
test_case,
2012-03-03 14:04:36 -05:00
run,
2012-03-03 12:06:40 -05:00
( parseFloat( options.interval ) * 1000 ),
( parseFloat( options.blank ) * 1000 )
);
}, ( parseFloat( options.delay ) * 1000 ) );
return this;
},
'private _startInterval': function( test_case, run, interval, blank )
{
var _self = this,
render = function( noblank )
{
// blank if a blank period was provided as it was not
// suppressed
if ( blank && !noblank )
{
_self._blank();
setTimeout( function()
{
// render withour blank period
render( true );
}, blank );
return;
}
// continuously render so long as we have another element
if ( _self._renderNext( test_case, run ) !== null )
{
setTimeout( function()
{
render();
}, interval );
}
else _self._complete( run );
};
this._showDest();
// begin rendering; we will render the first element immediately and
// all subsequent elements will be subject to to the blank period
// and interval
render( true );
},
'private _blank': function()
{
// clear dest element
this._$dest.hide();
2012-03-03 12:06:40 -05:00
},
'private _renderNext': function( test_case, run )
{
var next;
// if there is no next element, then we have retrieved the requested
// number of samples
if ( ( next = run.getNextElement() ) !== null )
{
// render this element
2012-03-03 14:04:36 -05:00
this._history.push( next );
test_case.render( next, this._$dest.show() );
2012-03-03 12:06:40 -05:00
}
return next;
},
'private _complete': function( run )
{
this._hideDest();
2012-03-03 14:04:36 -05:00
this._callback( this._history );
2012-03-03 12:06:40 -05:00
},
'private _showDest': function()
{
this._jQuery( 'body' ).append( this._$dest );
},
'private _hideDest': function()
{
this._$dest.detach();
this._blank();
}
} );