Permalink
Browse files

Tests: Refactor testIframe() to make it DRYer and more consistent

Ref gh-3040
Closes gh-3049
  • Loading branch information...
1 parent 08d73d7 commit e5ffcb0838c894e26f4ff32dfec162cf624d8d7d @dmethvin dmethvin committed Apr 10, 2016
Showing with 142 additions and 110 deletions.
  1. +19 −11 README.md
  2. +2 −1 test/data/ajax/onunload.html
  3. +2 −1 test/data/ajax/unreleasedXHR.html
  4. +4 −3 test/data/core/aliased.html
  5. +2 −1 test/data/core/cc_on.html
  6. +3 −2 test/data/core/dynamic_ready.html
  7. +2 −1 test/data/core/onready.html
  8. +2 −1 test/data/css/cssWidthBeforeDocReady.html
  9. +2 −1 test/data/data/dataAttrs.html
  10. +2 −3 test/data/dimensions/documentLarge.html
  11. +2 −1 test/data/event/focusElem.html
  12. +2 −1 test/data/event/focusinCrossFrame.html
  13. +4 −3 test/data/event/interactiveReady.html
  14. +2 −1 test/data/event/promiseReady.html
  15. +2 −1 test/data/event/syncReady.html
  16. +2 −1 test/data/event/triggerunload.html
  17. +8 −0 test/data/iframeTest.js
  18. +2 −1 test/data/manipulation/iframe-denied.html
  19. +2 −5 test/data/manipulation/scripts-context.html
  20. +2 −1 test/data/offset/absolute.html
  21. +2 −1 test/data/offset/body.html
  22. +2 −1 test/data/offset/fixed.html
  23. +2 −1 test/data/offset/relative.html
  24. +2 −1 test/data/offset/scroll.html
  25. +2 −1 test/data/offset/static.html
  26. +2 −1 test/data/offset/table.html
  27. +2 −4 test/data/selector/html5_selector.html
  28. +2 −3 test/data/selector/sizzle_cache.html
  29. +2 −1 test/data/support/bodyBackground.html
  30. +1 −1 test/data/support/csp.js
  31. +1 −0 test/data/support/csp.php
  32. +3 −4 test/data/testinit.js
  33. +3 −3 test/unit/ajax.js
  34. +9 −9 test/unit/core.js
  35. +2 −2 test/unit/css.js
  36. +2 −2 test/unit/data.js
  37. +2 −2 test/unit/dimensions.js
  38. +12 −12 test/unit/event.js
  39. +5 −5 test/unit/manipulation.js
  40. +8 −8 test/unit/offset.js
  41. +4 −4 test/unit/selector.js
  42. +4 −4 test/unit/support.js
View
@@ -340,24 +340,32 @@ url("data/test.php?foo=bar");
```
-### Load tests in an iframe (window.iframeCallback) ###
+### Run tests in an iframe ###
+
+Some tests may require a document other than the standard test fixture, and
+these can be run in a separate iframe. The actual test code and assertions
+remain in jQuery's main test files; only the minimal test fixture markup
+and setup code should be placed in the iframe file.
```js
-testIframeWithCallback( testName, fileName,
- function callback( arg1, arg2, ... assert ) {
+testIframe( testName, fileName,
+ function testCallback(
+ assert, jQuery, window, document,
+ [ additional args ] ) {
...
} );
```
-Loads a given page constructing a url with fileName: `"./data/" + fileName + ".html"`
-The iframe page is responsible for determining when `window.parent.iframeCallback`
-should be called, for example at document ready or window.onload time.
-Arguments passed to the callback are the same as the arguments passed
-to `window.parent.iframeCallback` by the iframe, plus the QUnit `assert`
-object from the `QUnit.test()` that this wrapper sets up for you.
-The iframe should send any objects needed by the unit test via arguments, for example
-its `jQuery`, `window`, and `document` objects from the iframe.
+This loads a page, constructing a url with fileName `"./data/" + fileName`.
+The iframed page determines when the callback occurs in the test by
+including the "/test/data/iframeTest.js" script and calling
+`startIframeTest( [ additional args ] )` when appropriate. Often this
+will be after either document ready or `window.onload` fires.
+The `testCallback` receives the QUnit `assert` object created by `testIframe`
+for this test, followed by the global `jQuery`, `window`, and `document` from
+the iframe. If the iframe code passes any arguments to `startIframeTest`,
+they follow the `document` argument.
Questions?
@@ -4,6 +4,7 @@
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>onunload ajax requests (#14379)</title>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
</head>
<body>
<form id="navigate" action="../iframe.html"></form>
@@ -17,7 +18,7 @@
ajaxStatus = status;
}
});
- parent.iframeCallback( ajaxStatus );
+ startIframeTest( ajaxStatus );
});
jQuery(function() {
@@ -4,13 +4,14 @@
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Attempt to block tests because of dangling XHR requests (IE)</title>
<script src="../../jquery.js"></script>
+<script src="../iframeTest.js"></script>
<script type="text/javascript">
window.onunload = function() {};
jQuery(function() {
setTimeout(function() {
var parent = window.parent;
document.write("");
- parent.iframeCallback();
+ startIframeTest();
}, 200 );
var number = 50;
while( number-- ) {
@@ -10,15 +10,16 @@
};
</script>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
</head>
<body>
<form>
<input type="text" id="nodeName"/>
</form>
<script>
- jQuery(function() {
- window.parent.iframeCallback( errors );
- });
+ jQuery( function() {
+ startIframeTest( errors );
+ } );
</script>
</body>
</html>
@@ -13,10 +13,11 @@
};
</script>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
</head>
<body>
<script>
- window.parent.iframeCallback( cc_on, errors, jQuery );
+ startIframeTest( cc_on, errors );
</script>
</body>
</html>
@@ -4,6 +4,7 @@
<meta charset="utf-8">
<script src="../../jquery.js"></script>
<script>var $j = jQuery.noConflict();</script>
+ <script src="../iframeTest.js"></script>
</head>
<body>
<iframe id="dont_return" src="dont_return.php"></iframe>
@@ -20,14 +21,14 @@
$(function () {
clearTimeout( timeoutId );
if ( !timeoutFired ) {
- window.parent.iframeCallback( true );
+ startIframeTest( true );
}
});
});
timeoutId = setTimeout(function () {
timeoutFired = true;
- window.parent.iframeCallback( false );
+ startIframeTest( false );
}, 10000);
});
</script>
@@ -8,6 +8,7 @@
window.onready = function() { error = "Called window.onready"; };
</script>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
</head>
<body>
<form>
@@ -16,7 +17,7 @@
<script>
jQuery(function() {
setTimeout( function() {
- window.parent.iframeCallback( error );
+ startIframeTest( error );
});
});
</script>
@@ -14,8 +14,9 @@
<body>
<div id="test"></div>
<script src="../../jquery.js"></script>
+<script src="../iframeTest.js"></script>
<script>
- window.parent.iframeCallback( jQuery( "#test" ).css( 'width' ) );
+ startIframeTest( jQuery( "#test" ).css( 'width' ) );
</script>
</body>
</html>
@@ -4,9 +4,10 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IE11 onpageshow strangeness (#14894)</title>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
<script>
$(function(){
- window.parent.iframeCallback( $( "body" ).data().result );
+ startIframeTest( $( "body" ).data().result );
});
</script>
</head>
@@ -8,14 +8,13 @@
height: 1000px;
}
</style>
+ <script src="../iframeTest.js"></script>
</head>
<body>
<div>
<script src="../../jquery.js"></script>
<script>
- jQuery( function() {
- window.parent.iframeCallback( jQuery, window, document );
- } );
+ jQuery( startIframeTest );
</script>
</div>
</body>
@@ -5,12 +5,13 @@
<title>.focus() (activeElement access #13393)</title>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
</head>
<body>
<a href="#" id="frame-link"></a>
<script>
jQuery( "#frame-link" ).trigger( "focus" );
- window.parent.iframeCallback( true );
+ startIframeTest( true );
</script>
</body>
</html>
@@ -5,13 +5,14 @@
<title>focusin event cross-frame (#14180)</title>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
</head>
<body>
<input type="text" id="frame-input" />
<script>
// Call parent when this frame is fully loaded, it will mess with #frame-input
jQuery( window ).one( "load", function() {
- window.parent.iframeCallback( document );
+ startIframeTest();
});
</script>
</body>
@@ -4,13 +4,14 @@
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Test case for gh-2100</title>
<script src="../../jquery.js"></script>
+<script src="../iframeTest.js"></script>
</head>
<body>
<script type="text/javascript">
-jQuery( document ).ready(function () {
- window.parent.iframeCallback( jQuery("#container").length === 1 );
-});
+jQuery( function () {
+ startIframeTest( jQuery("#container").length === 1 );
+} );
</script>
<!-- external resources that come before elements trick
@@ -4,10 +4,11 @@
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Test case for jQuery ticket #11470</title>
<script src="../../jquery.js"></script>
+<script src="../iframeTest.js"></script>
<script type="text/javascript">
jQuery.when( jQuery.ready ).done(function() {
jQuery("body").append("<div>modifying DOM</div>");
- window.parent.iframeCallback( $("div").text() === "modifying DOM" );
+ startIframeTest( $("div").text() === "modifying DOM" );
});
</script>
</head>
@@ -4,12 +4,13 @@
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Test case for jQuery ticket #10067</title>
<script src="../../jquery.js"></script>
+<script src="../iframeTest.js"></script>
</head>
<body>
<script type="text/javascript">
jQuery( document ).ready(function () {
- window.parent.iframeCallback( jQuery('#container').length === 1 );
+ startIframeTest( jQuery('#container').length === 1 );
});
</script>
@@ -1,6 +1,7 @@
<!doctype html>
<html>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
<script>
var called = false,
error = false;
@@ -12,7 +13,7 @@
return "maybe";
}).on( "load", function( event ) {
$( window ).triggerHandler( "beforeunload" );
- window.parent.iframeCallback( called && !error );
+ startIframeTest( called && !error );
});
</script>
</html>
@@ -0,0 +1,8 @@
+
+window.startIframeTest = function() {
+ var args = Array.prototype.slice.call( arguments );
+
+ // Note: jQuery may be undefined if page did not load it
+ args.unshift( window.jQuery, window, document );
+ window.parent.iframeCallback.apply( null, args );
+};
@@ -7,6 +7,7 @@
<body>
<div id="qunit-fixture"></div>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
<script>
var script = document.getElementsByTagName( "script" )[ 0 ],
div = document.createElement( "div" ),
@@ -25,7 +26,7 @@
error = e;
}
- window.parent.iframeCallback({
+ startIframeTest({
status: success,
description: "buildFragment sets the context without throwing an exception" +
( error ? ": " + error : "" )
@@ -7,12 +7,9 @@
<body>
<div id="qunit-fixture"></div>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
<script>
- window.parent.iframeCallback(
- window,
- document.body,
- "<script>window.scriptTest = true;<\x2fscript>"
- );
+ startIframeTest();
</script>
</body>
</html>
@@ -16,6 +16,7 @@
#positionTest { position: absolute; }
</style>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
<script type="text/javascript" charset="utf-8">
jQuery(function($) {
$(".absolute").click(function() {
@@ -24,7 +25,7 @@
$(this).css({ top: pos.top, left: pos.left });
return false;
});
- window.parent.iframeCallback( jQuery, window, document );
+ startIframeTest();
});
</script>
</head>
@@ -10,13 +10,14 @@
#firstElement { width: 50px; height: 50px; background: green; }
</style>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
<script type="text/javascript" charset="utf-8">
jQuery(function($) {
$("body").click(function() {
$("marker").css( $(this).offset() );
return false;
});
- window.parent.iframeCallback( jQuery, window, document );
+ startIframeTest();
});
</script>
</head>
@@ -13,14 +13,15 @@
#marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; }
</style>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
<script type="text/javascript" charset="utf-8">
jQuery(function($) {
window.scrollTo(1000,1000);
$(".fixed").click(function() {
$("#marker").css( $(this).offset() );
return false;
});
- window.parent.iframeCallback( jQuery, window, document );
+ startIframeTest();
});
</script>
</head>
@@ -12,6 +12,7 @@
#marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; }
</style>
<script src="../../jquery.js"></script>
+ <script src="../iframeTest.js"></script>
<script type="text/javascript" charset="utf-8">
jQuery(function($) {
$(".relative").click(function() {
@@ -20,7 +21,7 @@
$(this).css({ position: 'absolute', top: pos.top, left: pos.left });
return false;
});
- window.parent.iframeCallback( jQuery, window, document );
+ startIframeTest();
});
</script>
</head>
Oops, something went wrong.

0 comments on commit e5ffcb0

Please sign in to comment.