rectest/scripts/TestRunner.js

192 lines
4.6 KiB
JavaScript

/**
* Runs a given test configuration, returning the samples displayed to the user
*/
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': '',
/**
* 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,
/**
* 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' );
},
/**
* Run the provided test case
*
* @param {TestCase} test_case test case to be run
* @param {Object} options configuration options
*
* @param {=function(Array.<Object>)} callback completion callback for
* results array
*
* @return {TestRunner} self
*/
'public run': function( test_case, options, callback )
{
options = options || {};
if ( !( easejs.Class.isA( rectest.TestCase, test_case ) ) )
{
throw TypeError( "Invalid test case: " + test_case );
}
// clear history from any previous runs
this._history = [];
this._callback = callback || function() {};
this._$dest.html( '' );
var _self = this,
run = _self._TestRun( test_case, options );
// allow test case to initialize its display
test_case.initRender( this._$dest );
// start test after the given delay
setTimeout( function()
{
_self._startInterval(
test_case,
run,
( 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();
},
'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
this._history.push( next );
test_case.render( next, this._$dest.show() );
}
return next;
},
'private _complete': function( run )
{
this._hideDest();
this._callback( this._history );
},
'private _showDest': function()
{
this._jQuery( 'body' ).append( this._$dest );
},
'private _hideDest': function()
{
this._$dest.detach();
this._blank();
}
} );