X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Foffset.js;h=61dd93ac80a122f78d75644d3a9c8bd339451352;hb=5e0da8c0365b729f63fbd6d27223776551cb8f46;hp=857e0898e152dd74dfd10bb65bc9c05d783d0b6d;hpb=c424e79ccb3695c6ff599e77bec867c6644ccc98;p=jquery.git diff --git a/test/unit/offset.js b/test/unit/offset.js index 857e089..61dd93a 100644 --- a/test/unit/offset.js +++ b/test/unit/offset.js @@ -1,54 +1,224 @@ module("offset"); -// opens a new window to run the tests against -var testwin = function(name, fn) { - testwin[name] = load_offset_fixture(name); - var interval = setInterval(function() { - if (testwin[name] && testwin[name].$ && testwin[name].$.isReady) { - clearInterval(interval); - test(name, fn); - } - }, 0); - - function load_offset_fixture(name) { - var win = window.open( "./data/offset/" + name + ".html?num"+parseInt(Math.random()*1000), name, 'left=0,top=0,width=500,height=500,toolbar=1,resizable=0' ); - if ( !win ) { - alert("Please disable your popup blocker for the offset test suite"); - throw "Please disable your popup blocker for the offset test suite"; - } - return win; - } -}; +testoffset("absolute", function( jQuery ) { + equals( jQuery('#absolute-1').offset().top, 1, "jQuery('#absolute-1').offset().top" ); + equals( jQuery('#absolute-1').offset().left, 1, "jQuery('#absolute-1').offset().left" ); + + equals( jQuery('#absolute-1-1').offset().top, 5, "jQuery('#absolute-1-1').offset().top" ); + equals( jQuery('#absolute-1-1').offset().left, 5, "jQuery('#absolute-1-1').offset().left" ); + + equals( jQuery('#absolute-1-1-1').offset().top, 9, "jQuery('#absolute-1-1-1').offset().top" ); + equals( jQuery('#absolute-1-1-1').offset().left, 9, "jQuery('#absolute-1-1-1').offset().left" ); + + equals( jQuery('#absolute-2').offset().top, 20, "jQuery('#absolute-2').offset().top" ); + equals( jQuery('#absolute-2').offset().left, 20, "jQuery('#absolute-2').offset().left" ); + + + equals( jQuery('#absolute-1').position().top, 0, "jQuery('#absolute-1').position().top" ); + equals( jQuery('#absolute-1').position().left, 0, "jQuery('#absolute-1').position().left" ); + + equals( jQuery('#absolute-1-1').position().top, 1, "jQuery('#absolute-1-1').position().top" ); + equals( jQuery('#absolute-1-1').position().left, 1, "jQuery('#absolute-1-1').position().left" ); + + equals( jQuery('#absolute-1-1-1').position().top, 1, "jQuery('#absolute-1-1-1').position().top" ); + equals( jQuery('#absolute-1-1-1').position().left, 1, "jQuery('#absolute-1-1-1').position().left" ); + + equals( jQuery('#absolute-2').position().top, 19, "jQuery('#absolute-2').position().top" ); + equals( jQuery('#absolute-2').position().left, 19, "jQuery('#absolute-2').position().left" ); +}); + +testoffset("relative", function( jQuery ) { + var ie = jQuery.browser.msie && parseInt( jQuery.browser.version ) < 8; + + // IE is collapsing the top margin of 1px + equals( jQuery('#relative-1').offset().top, ie ? 6 : 7, "jQuery('#relative-1').offset().top" ); + equals( jQuery('#relative-1').offset().left, 7, "jQuery('#relative-1').offset().left" ); + + // IE is collapsing the top margin of 1px + equals( jQuery('#relative-1-1').offset().top, ie ? 13 : 15, "jQuery('#relative-1-1').offset().top" ); + equals( jQuery('#relative-1-1').offset().left, 15, "jQuery('#relative-1-1').offset().left" ); + + // IE is collapsing the top margin of 1px + equals( jQuery('#relative-2').offset().top, ie ? 141 : 142, "jQuery('#relative-2').offset().top" ); + equals( jQuery('#relative-2').offset().left, 27, "jQuery('#relative-2').offset().left" ); + + + // IE is collapsing the top margin of 1px + equals( jQuery('#relative-1').position().top, ie ? 5 : 6, "jQuery('#relative-1').position().top" ); + equals( jQuery('#relative-1').position().left, 6, "jQuery('#relative-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( jQuery('#relative-1-1').position().top, ie ? 4 : 5, "jQuery('#relative-1-1').position().top" ); + equals( jQuery('#relative-1-1').position().left, 5, "jQuery('#relative-1-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( jQuery('#relative-2').position().top, ie ? 140 : 141, "jQuery('#relative-2').position().top" ); + equals( jQuery('#relative-2').position().left, 26, "jQuery('#relative-2').position().left" ); +}); -testwin("absolute", function() { - var $w = testwin["absolute"].$; +testoffset("static", function( jQuery ) { + var ie = jQuery.browser.msie && parseInt( jQuery.browser.version ) < 8; + + // IE is collapsing the top margin of 1px + equals( jQuery('#static-1').offset().top, ie ? 6 : 7, "jQuery('#static-1').offset().top" ); + equals( jQuery('#static-1').offset().left, 7, "jQuery('#static-1').offset().left" ); - equals( $w('#absolute-1').offset().top, 1, "$('#absolute-1').offset().top" ); - equals( $w('#absolute-1').offset().left, 1, "$('#absolute-1').offset().left" ); + // IE is collapsing the top margin of 1px + equals( jQuery('#static-1-1').offset().top, ie ? 13 : 15, "jQuery('#static-1-1').offset().top" ); + equals( jQuery('#static-1-1').offset().left, 15, "jQuery('#static-1-1').offset().left" ); - equals( $w('#absolute-1-1').offset().top, 5, "$('#absolute-1-1').offset().top" ); - equals( $w('#absolute-1-1').offset().left, 5, "$('#absolute-1-1').offset().left" ); + // IE is collapsing the top margin of 1px + equals( jQuery('#static-1-1-1').offset().top, ie ? 20 : 23, "jQuery('#static-1-1-1').offset().top" ); + equals( jQuery('#static-1-1-1').offset().left, 23, "jQuery('#static-1-1-1').offset().left" ); - equals( $w('#absolute-1-1-1').offset().top, 9, "$('#absolute-1-1-1').offset().top" ); - equals( $w('#absolute-1-1-1').offset().left, 9, "$('#absolute-1-1-1').offset().left" ); + // IE is collapsing the top margin of 1px + equals( jQuery('#static-2').offset().top, ie ? 121 : 122, "jQuery('#static-2').offset().top" ); + equals( jQuery('#static-2').offset().left, 7, "jQuery('#static-2').offset().left" ); - equals( $w('#absolute-2').offset().top, 20, "$('#absolute-2').offset().top" ); - equals( $w('#absolute-2').offset().left, 20, "$('#absolute-2').offset().left" ); - testwin["absolute"].close(); + // IE is collapsing the top margin of 1px + equals( jQuery('#static-1').position().top, ie ? 5 : 6, "jQuery('#static-1').position().top" ); + equals( jQuery('#static-1').position().left, 6, "jQuery('#static-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( jQuery('#static-1-1').position().top, ie ? 12 : 14, "jQuery('#static-1-1').position().top" ); + equals( jQuery('#static-1-1').position().left, 14, "jQuery('#static-1-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( jQuery('#static-1-1-1').position().top, ie ? 19 : 22, "jQuery('#static-1-1-1').position().top" ); + equals( jQuery('#static-1-1-1').position().left, 22, "jQuery('#static-1-1-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( jQuery('#static-2').position().top, ie ? 120 : 121, "jQuery('#static-2').position().top" ); + equals( jQuery('#static-2').position().left, 6, "jQuery('#static-2').position().left" ); }); -testwin("relative", function() { - var $w = testwin["relative"].$; +if ( jQuery.offset.supportsFixedPosition ) { + testoffset("fixed", function( jQuery ) { + equals( jQuery('#fixed-1').offset().top, 1001, "jQuery('#fixed-1').offset().top" ); + equals( jQuery('#fixed-1').offset().left, 1001, "jQuery('#fixed-1').offset().left" ); - equals( $w('#relative-1').offset().top, jQuery.browser.msie ? 6 : 7, "$('#relative-1').offset().top" ); - equals( $w('#relative-1').offset().left, 7, "$('#relative-1').offset().left" ); + equals( jQuery('#fixed-2').offset().top, 1021, "jQuery('#fixed-2').offset().top" ); + equals( jQuery('#fixed-2').offset().left, 1021, "jQuery('#fixed-2').offset().left" ); + }); +} + +testoffset("table", function( jQuery ) { + var ie = jQuery.browser.msie; + + equals( jQuery('#table-1').offset().top, 6, "jQuery('#table-1').offset().top" ); + equals( jQuery('#table-1').offset().left, 6, "jQuery('#table-1').offset().left" ); - equals( $w('#relative-1-1').offset().top, jQuery.browser.msie ? 13 : 15, "$('#relative-1-1').offset().top" ); - equals( $w('#relative-1-1').offset().left, 15, "$('#relative-1-1').offset().left" ); + equals( jQuery('#th-1').offset().top, 10, "jQuery('#th-1').offset().top" ); + equals( jQuery('#th-1').offset().left, 10, "jQuery('#th-1').offset().left" ); - equals( $w('#relative-2').offset().top, jQuery.browser.msie ? 141 : 142, "$('#relative-2').offset().top" ); - equals( $w('#relative-2').offset().left, 27, "$('#relative-2').offset().left" ); + // equals( jQuery('#th-2').offset().top, 10, "jQuery('#th-2').offset().top" ); + // equals( jQuery('#th-2').offset().left, 116, "jQuery('#th-2').offset().left" ); + // + // equals( jQuery('#th-3').offset().top, 10, "jQuery('#th-3').offset().top" ); + // equals( jQuery('#th-3').offset().left, 222, "jQuery('#th-3').offset().left" ); - testwin["relative"].close(); -}); \ No newline at end of file + // equals( jQuery('#td-1').offset().top, ie ? 116 : 112, "jQuery('#td-1').offset().top" ); + // equals( jQuery('#td-1').offset().left, 10, "jQuery('#td-1').offset().left" ); + // + // equals( jQuery('#td-2').offset().top, ie ? 116 : 112, "jQuery('#td-2').offset().top" ); + // equals( jQuery('#td-2').offset().left, 116, "jQuery('#td-2').offset().left" ); + // + // equals( jQuery('#td-3').offset().top, ie ? 116 : 112, "jQuery('#td-3').offset().top" ); + // equals( jQuery('#td-3').offset().left, 222, "jQuery('#td-3').offset().left" ); +}); + +testoffset("scroll", function( jQuery, win ) { + var ie = jQuery.browser.msie && parseInt( jQuery.browser.version ) < 8; + + // IE is collapsing the top margin of 1px + equals( jQuery('#scroll-1').offset().top, ie ? 6 : 7, "jQuery('#scroll-1').offset().top" ); + equals( jQuery('#scroll-1').offset().left, 7, "jQuery('#scroll-1').offset().left" ); + + // IE is collapsing the top margin of 1px + equals( jQuery('#scroll-1-1').offset().top, ie ? 9 : 11, "jQuery('#scroll-1-1').offset().top" ); + equals( jQuery('#scroll-1-1').offset().left, 11, "jQuery('#scroll-1-1').offset().left" ); + + + // scroll offset tests .scrollTop/Left + equals( jQuery('#scroll-1').scrollTop(), 5, "jQuery('#scroll-1').scrollTop()" ); + equals( jQuery('#scroll-1').scrollLeft(), 5, "jQuery('#scroll-1').scrollLeft()" ); + + equals( jQuery('#scroll-1-1').scrollTop(), 0, "jQuery('#scroll-1-1').scrollTop()" ); + equals( jQuery('#scroll-1-1').scrollLeft(), 0, "jQuery('#scroll-1-1').scrollLeft()" ); + + // equals( jQuery('body').scrollTop(), 0, "jQuery('body').scrollTop()" ); + // equals( jQuery('body').scrollLeft(), 0, "jQuery('body').scrollTop()" ); + + win.name = "test"; + + equals( jQuery(win).scrollTop(), 1000, "jQuery(window).scrollTop()" ); + equals( jQuery(win).scrollLeft(), 1000, "jQuery(window).scrollLeft()" ); + + equals( jQuery(win.document).scrollTop(), 1000, "jQuery(document).scrollTop()" ); + equals( jQuery(win.document).scrollLeft(), 1000, "jQuery(document).scrollLeft()" ); +}); + +testoffset("body", function( jQuery ) { + equals( jQuery('body').offset().top, 1, "jQuery('#body').offset().top" ); + equals( jQuery('body').offset().left, 1, "jQuery('#body').offset().left" ); +}); + +test("offsetParent", function(){ + expect(11); + + var body = jQuery("body").offsetParent(); + equals( body.length, 1, "Only one offsetParent found." ); + equals( body[0], document.body, "The body is its own offsetParent." ); + + var header = jQuery("#header").offsetParent(); + equals( header.length, 1, "Only one offsetParent found." ); + equals( header[0], document.body, "The body is the offsetParent." ); + + var div = jQuery("#nothiddendivchild").offsetParent(); + equals( div.length, 1, "Only one offsetParent found." ); + equals( div[0], document.body, "The body is the offsetParent." ); + + jQuery("#nothiddendiv").css("position", "relative"); + + div = jQuery("#nothiddendivchild").offsetParent(); + equals( div.length, 1, "Only one offsetParent found." ); + equals( div[0], jQuery("#nothiddendiv")[0], "The div is the offsetParent." ); + + div = jQuery("body, #nothiddendivchild").offsetParent(); + equals( div.length, 2, "Two offsetParent found." ); + equals( div[0], document.body, "The body is the offsetParent." ); + equals( div[1], jQuery("#nothiddendiv")[0], "The div is the offsetParent." ); +}); + +function testoffset(name, fn) { + + test(name, function() { + // pause execution for now + stop(); + + // load fixture in iframe + var iframe = loadFixture(), + win = iframe.contentWindow, + interval = setInterval( function() { + if ( win && win.jQuery && win.jQuery.isReady ) { + clearInterval( interval ); + // continue + start(); + // call actual tests passing the correct jQuery isntance to use + fn.call( this, win.jQuery, win ); + document.body.removeChild( iframe ); + iframe = null; + } + }, 15 ); + }); + + function loadFixture() { + var src = './data/offset/' + name + '.html?' + parseInt( Math.random()*1000 ), + iframe = jQuery('