/** * 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, /** * Produces sets from the given id * @var {set.SetFactory} */ 'private _setFactory': null, /** * Render destination element * @var {jQuery} */ 'private _$dest': '', /** * History for the current/last run * @var {Array.} */ 'private _history': [], /** * Function to call with results when test run is complete * @var {function(Array.)} */ 'private _callback': null, /** * Initialize with jQuery instance * * @param {jQuery} jquery jQuery instance * @param {set.SetFactory} set_factory factory to create sets */ __construct: function( jquery, set_factory ) { this._jQuery = jquery; this._setFactory = set_factory; 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( '
' ) .addClass( 'render' ); }, /** * Run the provided test case * * @param {TestCase} test_case test case to be run * @param {Object} options configuration options * * @param {=function(Array.)} 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 = this._setFactory.fromId( options.ralgo, test_case.getSet( options.set ), 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(); } } );