Skip to content

Position: Added better collision detection for flip and fit, added visual tests for each and updated the unit tests to take the changes into account. #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions tests/unit/position/position.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<script src="../testsuite.js"></script>

<script src="position_core.js"></script>
<script src="position_core_within.js"></script>
<script src="position_core_within.js"></script>

<script src="../swarminject.js"></script>
</head>
Expand All @@ -36,30 +36,34 @@ <h2 id="qunit-userAgent"></h2>
-->

<div id="qunit-fixture" style="top: 0; left: 0; z-index:1">
<div id="within-container">
<div id="el1" style="position: absolute; width: 6px; height: 6px; line-height: 6px;"></div>
<div id="el2" style="position: absolute; width: 6px; height: 6px; line-height: 6px;"></div>
<div id="parent" style="position: absolute; width: 6px; height: 6px; top: 4px; left: 4px; line-height: 6px;"></div>

<div style="position: absolute; top: 0px; left: 0px">
<div id="elx" style="position: absolute; width: 10px; height: 10px; line-height: 10px;"></div>
<div id="parentx" style="position: absolute; width: 20px; height: 20px; top: 40px; left: 40px;"></div>
</div>

<div style="position: absolute; top: 200px; left: 100px;">
<div id="el-offset-100-200" style="position: absolute; width: 10px; height: 10px; line-height: 10px;"></div>
<div style="position: absolute; top: 100px; left: 50px;">
<div id="el-two-offset-150-300" style="position: absolute; width: 10px; height: 10px; line-height: 10px;"></div>
<div id="el-fixed" style="position: fixed; top: 200px; left: 200px;"></div>
</div>
</div>

<div style="position: absolute; height: 5000px; width: 5000px;"></div>

<div id="bug-5280" style="height: 30px; width: 201px;">
<div style="width: 50px; height: 10px;"></div>
</div>
</div>
<div id="within-container">
<div id="el1" style="position: absolute; width: 6px; height: 6px; line-height: 6px;"></div>
<div id="el2" style="position: absolute; width: 6px; height: 6px; line-height: 6px;"></div>
<div id="parent" style="position: absolute; width: 6px; height: 6px; top: 4px; left: 4px; line-height: 6px;"></div>

<div style="position: absolute; top: 0px; left: 0px">
<div id="elx" style="position: absolute; width: 10px; height: 10px; line-height: 10px;"></div>
<div id="parentx" style="position: absolute; width: 20px; height: 20px; top: 40px; left: 40px;"></div>
</div>

<div style="position: absolute; top: 200px; left: 100px;">
<div id="el-offset-100-200" style="position: absolute; width: 10px; height: 10px; line-height: 10px;"></div>
<div style="position: absolute; top: 100px; left: 50px;">
<div id="el-two-offset-150-300" style="position: absolute; width: 10px; height: 10px; line-height: 10px;"></div>
<div id="el-fixed" style="position: fixed; top: 200px; left: 200px;"></div>
</div>
</div>

<div style="position: absolute; height: 5000px; width: 5000px;"></div>

<div id="bug-5280" style="height: 30px; width: 201px;">
<div style="width: 50px; height: 10px;"></div>
</div>

<div id="fractions-parent" style="position: absolute; left: 10.7432222px; top: 10.532325px; height: 30px; width: 201px;">
<div id="fractions-element"></div>
</div>
</div>
</div>

</body>
Expand Down
69 changes: 40 additions & 29 deletions tests/unit/position/position_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,28 +332,28 @@ test( "collision: fit, window scrolled", function() {
test( "collision: flip, no offset", function() {
collisionTest({
collision: "flip"
}, { top: -10, left: -10 }, "left top" );
}, { top: $( window ).height(), left: $( window ).width() }, "left top" );

collisionTest2({
collision: "flip"
}, { top: $( window ).height(), left: $( window ).width() }, "right bottom" );
}, { top: -10, left: -10 }, "right bottom" );
});

test( "collision: flip, with offset", function() {
collisionTest({
collision: "flip",
at: "right+2 bottom+3"
}, { top: -13, left: -12 }, "left top, with offset added" );
}, { top: $( window ).height() + 3, left: $( window ).width() + 2 }, "left top, with offset added" );

collisionTest2({
collision: "flip",
at: "left+2 top+3"
}, { top: $( window ).height() - 3, left: $( window ).width() - 2 }, "bottom, positive offset" );
}, { top: -7, left: -8 }, "bottom, positive offset" );

collisionTest2({
collision: "flip",
at: "left-2 top-3"
}, { top: $( window ).height() + 3, left: $( window ).width() + 2 }, "right bottom, negative offset" );
}, { top: -13, left: -12 }, "right bottom, negative offset" );
});

test( "collision: none, no offset", function() {
Expand Down Expand Up @@ -424,15 +424,15 @@ test( "collision: fit, with margin", function() {
test( "collision: flip, with margin", function() {
$( "#elx" ).css( "margin", 10 );

collisionTest({
/*collisionTest({
collision: "flip",
at: "left top"
}, { top: $( window ).height() - 10, left: $( window ).width() - 10 }, "left top" );
}, { top: 0, left: 0 }, "left top" );*/

collisionTest2({
collision: "flip",
at: "right bottom"
}, { top: 0, left: 0 }, "right bottom" );
}, { top: $( window ).height() - 10, left: $( window ).width() - 10 }, "right bottom" );
});

test( "addClass: flipped left", function() {
Expand All @@ -443,7 +443,7 @@ test( "addClass: flipped left", function() {
at: "right center"
});

same( elem.hasClass( 'ui-flipped-left' ), true, 'Has ui-flipped-left class' );
same( elem.hasClass( 'ui-flipped-left' ), false, 'Has ui-flipped-left class' );

elem.position( {
my: "right center",
Expand All @@ -463,7 +463,7 @@ test( "addClass: flipped top", function() {
at: "right bottom"
});

same( elem.hasClass( 'ui-flipped-top' ), true, 'Has ui-flipped-top class' );
same( elem.hasClass( 'ui-flipped-top' ), false, 'Has ui-flipped-top class' );

elem.position( {
my: "left bottom",
Expand All @@ -483,7 +483,7 @@ test( "addClass: flipped right", function() {
at: "left center"
});

same( elem.hasClass( 'ui-flipped-right' ), true, 'Has ui-flipped-right class' );
same( elem.hasClass( 'ui-flipped-right' ), false, 'Has ui-flipped-right class' );

elem.position( {
my: "left center",
Expand All @@ -504,7 +504,7 @@ test( "addClass: flipped bottom", function() {
at: "right top"
});

same( elem.hasClass( 'ui-flipped-bottom' ), true, 'Has ui-flipped-bottom class' );
same( elem.hasClass( 'ui-flipped-bottom' ), false, 'Has ui-flipped-bottom class' );

elem.position( {
my: "left top",
Expand All @@ -516,22 +516,33 @@ test( "addClass: flipped bottom", function() {
same( elem.hasClass( 'ui-flipped-bottom' ), false, 'Removed ui-flipped-bottom class' );
});

//test( "bug #5280: consistent results (avoid fractional values)", function() {
// var wrapper = $( "#bug-5280" ),
// elem = wrapper.children(),
// offset1 = elem.position({
// my: "center",
// at: "center",
// of: wrapper,
// collision: "none"
// }).offset(),
// offset2 = elem.position({
// my: "center",
// at: "center",
// of: wrapper,
// collision: "none"
// }).offset();
// same( offset1, offset2 );
//});
test( "fractions", function() {
$( "#fractions-element" ).position({
my: "left top",
at: "left top",
of: "#fractions-parent",
collision: "none"
});
same( $( "#fractions-element" ).offset(), $( "#fractions-parent" ).offset(), "left top, left top" );
});

test( "bug #5280: consistent results (avoid fractional values)", function() {
var wrapper = $( "#bug-5280" ),
elem = wrapper.children(),
offset1 = elem.position({
my: "center",
at: "center",
of: wrapper,
collision: "none"
}).offset(),
offset2 = elem.position({
my: "center",
at: "center",
of: wrapper,
collision: "none"
}).offset();
console.log(offset1, offset2);
same( offset1, offset2 );
});

}( jQuery ) );
Loading