Skip to content

Commit e5ffcb0

Browse files
committed
Tests: Refactor testIframe() to make it DRYer and more consistent
Ref jquerygh-3040 Closes jquerygh-3049
1 parent 08d73d7 commit e5ffcb0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+142
-110
lines changed

README.md

+19-11
Original file line numberDiff line numberDiff line change
@@ -340,24 +340,32 @@ url("data/test.php?foo=bar");
340340
```
341341

342342

343-
### Load tests in an iframe (window.iframeCallback) ###
343+
### Run tests in an iframe ###
344+
345+
Some tests may require a document other than the standard test fixture, and
346+
these can be run in a separate iframe. The actual test code and assertions
347+
remain in jQuery's main test files; only the minimal test fixture markup
348+
and setup code should be placed in the iframe file.
344349

345350
```js
346-
testIframeWithCallback( testName, fileName,
347-
function callback( arg1, arg2, ... assert ) {
351+
testIframe( testName, fileName,
352+
function testCallback(
353+
assert, jQuery, window, document,
354+
[ additional args ] ) {
348355
...
349356
} );
350357
```
351358

352-
Loads a given page constructing a url with fileName: `"./data/" + fileName + ".html"`
353-
The iframe page is responsible for determining when `window.parent.iframeCallback`
354-
should be called, for example at document ready or window.onload time.
355-
Arguments passed to the callback are the same as the arguments passed
356-
to `window.parent.iframeCallback` by the iframe, plus the QUnit `assert`
357-
object from the `QUnit.test()` that this wrapper sets up for you.
358-
The iframe should send any objects needed by the unit test via arguments, for example
359-
its `jQuery`, `window`, and `document` objects from the iframe.
359+
This loads a page, constructing a url with fileName `"./data/" + fileName`.
360+
The iframed page determines when the callback occurs in the test by
361+
including the "/test/data/iframeTest.js" script and calling
362+
`startIframeTest( [ additional args ] )` when appropriate. Often this
363+
will be after either document ready or `window.onload` fires.
360364

365+
The `testCallback` receives the QUnit `assert` object created by `testIframe`
366+
for this test, followed by the global `jQuery`, `window`, and `document` from
367+
the iframe. If the iframe code passes any arguments to `startIframeTest`,
368+
they follow the `document` argument.
361369

362370

363371
Questions?

test/data/ajax/onunload.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
55
<title>onunload ajax requests (#14379)</title>
66
<script src="../../jquery.js"></script>
7+
<script src="../iframeTest.js"></script>
78
</head>
89
<body>
910
<form id="navigate" action="../iframe.html"></form>
@@ -17,7 +18,7 @@
1718
ajaxStatus = status;
1819
}
1920
});
20-
parent.iframeCallback( ajaxStatus );
21+
startIframeTest( ajaxStatus );
2122
});
2223

2324
jQuery(function() {

test/data/ajax/unreleasedXHR.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
<meta http-equiv="content-type" content="text/html; charset=utf-8">
55
<title>Attempt to block tests because of dangling XHR requests (IE)</title>
66
<script src="../../jquery.js"></script>
7+
<script src="../iframeTest.js"></script>
78
<script type="text/javascript">
89
window.onunload = function() {};
910
jQuery(function() {
1011
setTimeout(function() {
1112
var parent = window.parent;
1213
document.write("");
13-
parent.iframeCallback();
14+
startIframeTest();
1415
}, 200 );
1516
var number = 50;
1617
while( number-- ) {

test/data/core/aliased.html

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
};
1111
</script>
1212
<script src="../../jquery.js"></script>
13+
<script src="../iframeTest.js"></script>
1314
</head>
1415
<body>
1516
<form>
1617
<input type="text" id="nodeName"/>
1718
</form>
1819
<script>
19-
jQuery(function() {
20-
window.parent.iframeCallback( errors );
21-
});
20+
jQuery( function() {
21+
startIframeTest( errors );
22+
} );
2223
</script>
2324
</body>
2425
</html>

test/data/core/cc_on.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
};
1414
</script>
1515
<script src="../../jquery.js"></script>
16+
<script src="../iframeTest.js"></script>
1617
</head>
1718
<body>
1819
<script>
19-
window.parent.iframeCallback( cc_on, errors, jQuery );
20+
startIframeTest( cc_on, errors );
2021
</script>
2122
</body>
2223
</html>

test/data/core/dynamic_ready.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<meta charset="utf-8">
55
<script src="../../jquery.js"></script>
66
<script>var $j = jQuery.noConflict();</script>
7+
<script src="../iframeTest.js"></script>
78
</head>
89
<body>
910
<iframe id="dont_return" src="dont_return.php"></iframe>
@@ -20,14 +21,14 @@
2021
$(function () {
2122
clearTimeout( timeoutId );
2223
if ( !timeoutFired ) {
23-
window.parent.iframeCallback( true );
24+
startIframeTest( true );
2425
}
2526
});
2627
});
2728

2829
timeoutId = setTimeout(function () {
2930
timeoutFired = true;
30-
window.parent.iframeCallback( false );
31+
startIframeTest( false );
3132
}, 10000);
3233
});
3334
</script>

test/data/core/onready.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
window.onready = function() { error = "Called window.onready"; };
99
</script>
1010
<script src="../../jquery.js"></script>
11+
<script src="../iframeTest.js"></script>
1112
</head>
1213
<body>
1314
<form>
@@ -16,7 +17,7 @@
1617
<script>
1718
jQuery(function() {
1819
setTimeout( function() {
19-
window.parent.iframeCallback( error );
20+
startIframeTest( error );
2021
});
2122
});
2223
</script>

test/data/css/cssWidthBeforeDocReady.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
<body>
1515
<div id="test"></div>
1616
<script src="../../jquery.js"></script>
17+
<script src="../iframeTest.js"></script>
1718
<script>
18-
window.parent.iframeCallback( jQuery( "#test" ).css( 'width' ) );
19+
startIframeTest( jQuery( "#test" ).css( 'width' ) );
1920
</script>
2021
</body>
2122
</html>

test/data/data/dataAttrs.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<title>IE11 onpageshow strangeness (#14894)</title>
66
<script src="../../jquery.js"></script>
7+
<script src="../iframeTest.js"></script>
78
<script>
89
$(function(){
9-
window.parent.iframeCallback( $( "body" ).data().result );
10+
startIframeTest( $( "body" ).data().result );
1011
});
1112
</script>
1213
</head>

test/data/dimensions/documentLarge.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
height: 1000px;
99
}
1010
</style>
11+
<script src="../iframeTest.js"></script>
1112
</head>
1213
<body>
1314
<div>
1415
<script src="../../jquery.js"></script>
1516
<script>
16-
jQuery( function() {
17-
window.parent.iframeCallback( jQuery, window, document );
18-
} );
17+
jQuery( startIframeTest );
1918
</script>
2019
</div>
2120
</body>

test/data/event/focusElem.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
<title>.focus() (activeElement access #13393)</title>
66

77
<script src="../../jquery.js"></script>
8+
<script src="../iframeTest.js"></script>
89
</head>
910
<body>
1011
<a href="#" id="frame-link"></a>
1112
<script>
1213
jQuery( "#frame-link" ).trigger( "focus" );
13-
window.parent.iframeCallback( true );
14+
startIframeTest( true );
1415
</script>
1516
</body>
1617
</html>

test/data/event/focusinCrossFrame.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
<title>focusin event cross-frame (#14180)</title>
66

77
<script src="../../jquery.js"></script>
8+
<script src="../iframeTest.js"></script>
89
</head>
910
<body>
1011
<input type="text" id="frame-input" />
1112
<script>
1213
// Call parent when this frame is fully loaded, it will mess with #frame-input
1314
jQuery( window ).one( "load", function() {
14-
window.parent.iframeCallback( document );
15+
startIframeTest();
1516
});
1617
</script>
1718
</body>

test/data/event/interactiveReady.html

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
<meta http-equiv="content-type" content="text/html; charset=utf-8">
55
<title>Test case for gh-2100</title>
66
<script src="../../jquery.js"></script>
7+
<script src="../iframeTest.js"></script>
78
</head>
89
<body>
910

1011
<script type="text/javascript">
11-
jQuery( document ).ready(function () {
12-
window.parent.iframeCallback( jQuery("#container").length === 1 );
13-
});
12+
jQuery( function () {
13+
startIframeTest( jQuery("#container").length === 1 );
14+
} );
1415
</script>
1516

1617
<!-- external resources that come before elements trick

test/data/event/promiseReady.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
<meta http-equiv="content-type" content="text/html; charset=utf-8">
55
<title>Test case for jQuery ticket #11470</title>
66
<script src="../../jquery.js"></script>
7+
<script src="../iframeTest.js"></script>
78
<script type="text/javascript">
89
jQuery.when( jQuery.ready ).done(function() {
910
jQuery("body").append("<div>modifying DOM</div>");
10-
window.parent.iframeCallback( $("div").text() === "modifying DOM" );
11+
startIframeTest( $("div").text() === "modifying DOM" );
1112
});
1213
</script>
1314
</head>

test/data/event/syncReady.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
<meta http-equiv="content-type" content="text/html; charset=utf-8">
55
<title>Test case for jQuery ticket #10067</title>
66
<script src="../../jquery.js"></script>
7+
<script src="../iframeTest.js"></script>
78
</head>
89
<body>
910

1011
<script type="text/javascript">
1112
jQuery( document ).ready(function () {
12-
window.parent.iframeCallback( jQuery('#container').length === 1 );
13+
startIframeTest( jQuery('#container').length === 1 );
1314
});
1415
</script>
1516

test/data/event/triggerunload.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!doctype html>
22
<html>
33
<script src="../../jquery.js"></script>
4+
<script src="../iframeTest.js"></script>
45
<script>
56
var called = false,
67
error = false;
@@ -12,7 +13,7 @@
1213
return "maybe";
1314
}).on( "load", function( event ) {
1415
$( window ).triggerHandler( "beforeunload" );
15-
window.parent.iframeCallback( called && !error );
16+
startIframeTest( called && !error );
1617
});
1718
</script>
1819
</html>

test/data/iframeTest.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
window.startIframeTest = function() {
3+
var args = Array.prototype.slice.call( arguments );
4+
5+
// Note: jQuery may be undefined if page did not load it
6+
args.unshift( window.jQuery, window, document );
7+
window.parent.iframeCallback.apply( null, args );
8+
};

test/data/manipulation/iframe-denied.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<body>
88
<div id="qunit-fixture"></div>
99
<script src="../../jquery.js"></script>
10+
<script src="../iframeTest.js"></script>
1011
<script>
1112
var script = document.getElementsByTagName( "script" )[ 0 ],
1213
div = document.createElement( "div" ),
@@ -25,7 +26,7 @@
2526
error = e;
2627
}
2728

28-
window.parent.iframeCallback({
29+
startIframeTest({
2930
status: success,
3031
description: "buildFragment sets the context without throwing an exception" +
3132
( error ? ": " + error : "" )

test/data/manipulation/scripts-context.html

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
<body>
88
<div id="qunit-fixture"></div>
99
<script src="../../jquery.js"></script>
10+
<script src="../iframeTest.js"></script>
1011
<script>
11-
window.parent.iframeCallback(
12-
window,
13-
document.body,
14-
"<script>window.scriptTest = true;<\x2fscript>"
15-
);
12+
startIframeTest();
1613
</script>
1714
</body>
1815
</html>

test/data/offset/absolute.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#positionTest { position: absolute; }
1717
</style>
1818
<script src="../../jquery.js"></script>
19+
<script src="../iframeTest.js"></script>
1920
<script type="text/javascript" charset="utf-8">
2021
jQuery(function($) {
2122
$(".absolute").click(function() {
@@ -24,7 +25,7 @@
2425
$(this).css({ top: pos.top, left: pos.left });
2526
return false;
2627
});
27-
window.parent.iframeCallback( jQuery, window, document );
28+
startIframeTest();
2829
});
2930
</script>
3031
</head>

test/data/offset/body.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
#firstElement { width: 50px; height: 50px; background: green; }
1111
</style>
1212
<script src="../../jquery.js"></script>
13+
<script src="../iframeTest.js"></script>
1314
<script type="text/javascript" charset="utf-8">
1415
jQuery(function($) {
1516
$("body").click(function() {
1617
$("marker").css( $(this).offset() );
1718
return false;
1819
});
19-
window.parent.iframeCallback( jQuery, window, document );
20+
startIframeTest();
2021
});
2122
</script>
2223
</head>

test/data/offset/fixed.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
#marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; }
1414
</style>
1515
<script src="../../jquery.js"></script>
16+
<script src="../iframeTest.js"></script>
1617
<script type="text/javascript" charset="utf-8">
1718
jQuery(function($) {
1819
window.scrollTo(1000,1000);
1920
$(".fixed").click(function() {
2021
$("#marker").css( $(this).offset() );
2122
return false;
2223
});
23-
window.parent.iframeCallback( jQuery, window, document );
24+
startIframeTest();
2425
});
2526
</script>
2627
</head>

test/data/offset/relative.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; }
1313
</style>
1414
<script src="../../jquery.js"></script>
15+
<script src="../iframeTest.js"></script>
1516
<script type="text/javascript" charset="utf-8">
1617
jQuery(function($) {
1718
$(".relative").click(function() {
@@ -20,7 +21,7 @@
2021
$(this).css({ position: 'absolute', top: pos.top, left: pos.left });
2122
return false;
2223
});
23-
window.parent.iframeCallback( jQuery, window, document );
24+
startIframeTest();
2425
});
2526
</script>
2627
</head>

0 commit comments

Comments
 (0)