|
1 | | -(function(){ |
2 | | - var test = function(data){ |
3 | | - var $frameElem = $("#testFrame"), |
4 | | - template = $frameElem.attr("data-src"), |
5 | | - updateFrame = function(dir){ |
6 | | - return $frameElem.attr("src", template.replace("{{testdir}}", dir)); |
7 | | - }; |
8 | | - |
9 | | - $.each(data.directories, function(i, dir){ |
10 | | - asyncTest( dir, function(){ |
11 | | - var testTimeout = 3 * 60 * 1000, checkInterval = 2000; |
| 1 | +$(function() { |
| 2 | + var Runner = function( ) { |
| 3 | + var self = this; |
12 | 4 |
|
| 5 | + $.extend( self, { |
| 6 | + frame: window.frames[ "testFrame" ], |
| 7 | + |
| 8 | + testTimeout: 3 * 60 * 1000, |
| 9 | + |
| 10 | + $frameElem: $( "#testFrame" ), |
| 11 | + |
| 12 | + assertionResultPrefix: "assertion result for test:", |
| 13 | + |
| 14 | + onTimeout: QUnit.start, |
| 15 | + |
| 16 | + onFrameLoad: function() { |
13 | 17 | // establish a timeout for a given suite in case of async tests hanging |
14 | | - var testTimer = setTimeout( function(){ |
15 | | - // prevent any schedule checks for completion |
16 | | - clearTimeouts(); |
17 | | - start(); |
18 | | - }, testTimeout ), |
19 | | - |
20 | | - checkTimer = setInterval( check, checkInterval ), |
21 | | - |
22 | | - clearTimeouts = function(){ |
23 | | - // prevent the next interval of the check function and the test timeout |
24 | | - clearTimeout( checkTimer ); |
25 | | - clearTimeout( testTimer ); |
26 | | - }; |
27 | | - |
28 | | - // check the iframe for success or failure and respond accordingly |
29 | | - function check(){ |
30 | | - // check for the frames jquery object each time |
31 | | - var framejQuery = window.frames["testFrame"].jQuery; |
32 | | - |
33 | | - // if the iframe hasn't loaded (ie loaded jQuery) check back again shortly |
34 | | - if( !framejQuery ) return; |
35 | | - |
36 | | - // grab the result of the iframe test suite |
37 | | - // TODO strip extra white space |
38 | | - var result = framejQuery( "#qunit-banner" ).attr( "class" ); |
39 | | - |
40 | | - // if we have a result check it, otherwise check back shortly |
41 | | - if( result ){ |
42 | | - ok( result === "qunit-pass" ); |
43 | | - clearTimeouts(); |
44 | | - start(); |
45 | | - } |
46 | | - }; |
47 | | - |
48 | | - expect( 1 ); |
49 | | - |
50 | | - // set the test suite page on the iframe |
51 | | - updateFrame( dir ); |
52 | | - }); |
| 18 | + self.testTimer = setTimeout( self.onTimeout, self.testTimeout ); |
| 19 | + |
| 20 | + // when the QUnit object reports done in the iframe |
| 21 | + // run the onFrameDone method |
| 22 | + self.frame.QUnit.done = self.onFrameDone; |
| 23 | + self.frame.QUnit.testDone = self.onTestDone; |
| 24 | + }, |
| 25 | + |
| 26 | + onTestDone: function( name, bad, assertCount ) { |
| 27 | + QUnit.ok( !bad, name ); |
| 28 | + self.recordAssertions( assertCount - 1, name ); |
| 29 | + }, |
| 30 | + |
| 31 | + onFrameDone: function( failed, passed, total, runtime ){ |
| 32 | + // make sure we don't time out the tests |
| 33 | + clearTimeout( self.testTimer ); |
| 34 | + |
| 35 | + // TODO decipher actual cause of multiple test results firing twice |
| 36 | + // clear the done call to prevent early completion of other test cases |
| 37 | + self.frame.QUnit.done = $.noop; |
| 38 | + self.frame.QUnit.testDone = $.noop; |
| 39 | + |
| 40 | + // hide the extra assertions made to propogate the count |
| 41 | + // to the suite level test |
| 42 | + self.hideAssertionResults(); |
| 43 | + |
| 44 | + // continue on to the next suite |
| 45 | + QUnit.start(); |
| 46 | + }, |
| 47 | + |
| 48 | + recordAssertions: function( count, parentTest ) { |
| 49 | + for( var i = 0; i < count; i++ ) { |
| 50 | + ok( true, self.assertionResultPrefix + parentTest ); |
| 51 | + } |
| 52 | + }, |
| 53 | + |
| 54 | + hideAssertionResults: function() { |
| 55 | + $( "li:not([id]):contains('" + self.assertionResultPrefix + "')" ).hide(); |
| 56 | + }, |
| 57 | + |
| 58 | + exec: function( data ) { |
| 59 | + var template = self.$frameElem.attr( "data-src" ); |
| 60 | + |
| 61 | + $.each( data.testPages, function(i, dir) { |
| 62 | + QUnit.asyncTest( dir, function() { |
| 63 | + self.dir = dir; |
| 64 | + self.$frameElem.one( "load", self.onFrameLoad ); |
| 65 | + self.$frameElem.attr( "src", template.replace("{{testdir}}", dir) ); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + // having defined all suite level tests let QUnit run |
| 70 | + QUnit.start(); |
| 71 | + } |
53 | 72 | }); |
54 | 73 | }; |
55 | 74 |
|
| 75 | + // prevent qunit from starting the test suite until all tests are defined |
| 76 | + QUnit.begin = function( ) { |
| 77 | + this.config.autostart = false; |
| 78 | + }; |
| 79 | + |
56 | 80 | // get the test directories |
57 | | - $.get("ls.php", test); |
58 | | -})(); |
| 81 | + $.get( "ls.php", (new Runner()).exec ); |
| 82 | +}); |
0 commit comments