Files
openlayers/tests/test_Bounds.html
2006-09-28 13:05:36 +00:00

252 lines
16 KiB
HTML

<html>
<head>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript"><!--
var bounds;
function test_01_Bounds_constructor (t) {
t.plan( 11 );
bounds = new OpenLayers.Bounds(0,2,10,4);
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
t.eq( bounds.CLASS_NAME, "OpenLayers.Bounds", "bounds.CLASS_NAME is set correctly" );
t.eq( bounds.left, 0, "bounds.left is set correctly" );
t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
t.eq( bounds.right, 10, "bounds.right is set correctly" );
t.eq( bounds.top, 4, "bounds.top is set correctly" );
t.eq( bounds.getWidth(), 10, "bounds.getWidth() returns correct value" );
t.eq( bounds.getHeight(), 2, "bounds.getHeight() returns correct value" );
var sz = bounds.getSize();
var size = new OpenLayers.Size(10,2);
t.ok(sz.equals(size),"bounds.getSize() has correct value" );
var center = new OpenLayers.Pixel(5,3);
var boundsCenter = bounds.getCenterPixel();
t.ok( boundsCenter.equals(center), "bounds.getCenterLonLat() has correct value" );
var center = new OpenLayers.LonLat(5,3);
var boundsCenter = bounds.getCenterLonLat();
t.ok( boundsCenter.equals(center), "bounds.getCenterLonLat() has correct value" );
}
function test_01a_Bounds_constructorFromStrings(t) {
t.plan( 6 );
bounds = new OpenLayers.Bounds("0","2","10","4");
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
t.eq( bounds.CLASS_NAME, "OpenLayers.Bounds", "bounds.CLASS_NAME is set correctly" );
t.eq( bounds.left, 0, "bounds.left is set correctly" );
t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
t.eq( bounds.right, 10, "bounds.right is set correctly" );
t.eq( bounds.top, 4, "bounds.top is set correctly" );
}
function test_02_Bounds_toBBOX(t) {
t.plan( 1 );
bounds = new OpenLayers.Bounds(1,2,3,4);
t.eq( bounds.toBBOX(), "1,2,3,4", "toBBOX() returns correct value." );
}
function test_03_Bounds_toString(t) {
t.plan( 1 );
bounds = new OpenLayers.Bounds(1,2,3,4);
t.eq( bounds.toString(), "left-bottom=(1,2) right-top=(3,4)", "toString() returns correct value." );
}
function test_04_Bounds_contains(t) {
t.plan( 6 );
bounds = new OpenLayers.Bounds(10,10,40,40);
t.eq( bounds.contains(20,20), true, "bounds(10,10,40,40) correctly contains LonLat(20,20)" );
t.eq( bounds.contains(0,0), false, "bounds(10,10,40,40) correctly does not contain LonLat(0,0)" );
t.eq( bounds.contains(40,40), true, "bounds(10,10,40,40) correctly contains LonLat(40,40) with inclusive set to true" );
t.eq( bounds.contains(40,40, false), false, "bounds(10,10,40,40) correctly does not contain LonLat(40,40) with inclusive set to false" );
var px = new OpenLayers.Pixel(15,30);
t.eq( bounds.containsPixel(px), bounds.contains(px.x, px.y), "containsPixel works");
var ll = new OpenLayers.LonLat(15,30);
t.eq( bounds.containsLonLat(ll), bounds.contains(ll.lon, ll.lat), "containsLonLat works");
}
function test_05_Bounds_fromString(t) {
t.plan( 10 );
bounds = OpenLayers.Bounds.fromString("1,2,3,4");
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
t.eq( bounds.left, 1, "bounds.left is set correctly" );
t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
t.eq( bounds.right, 3, "bounds.right is set correctly" );
t.eq( bounds.top, 4, "bounds.top is set correctly" );
bounds = OpenLayers.Bounds.fromString("1.1,2.2,3.3,4.4");
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
t.eq( bounds.left, 1.1, "bounds.left is set correctly" );
t.eq( bounds.bottom, 2.2, "bounds.bottom is set correctly" );
t.eq( bounds.right, 3.3, "bounds.right is set correctly" );
t.eq( bounds.top, 4.4, "bounds.top is set correctly" );
}
function test_06_Bounds_getSize(t) {
t.plan( 1 );
var bounds = new OpenLayers.Bounds(0,10,100,120);
t.ok( bounds.getSize().equals(new OpenLayers.Size(100, 110)), "getCenterPixel() works correctly");
}
function test_07_Bounds_clone(t) {
t.plan( 6 );
var oldBounds = new OpenLayers.Bounds(1,2,3,4);
var bounds = oldBounds.clone();
t.ok( bounds instanceof OpenLayers.Bounds, "clone returns new OpenLayers.Bounds object" );
t.eq( bounds.left, 1, "bounds.left is set correctly" );
t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
t.eq( bounds.right, 3, "bounds.right is set correctly" );
t.eq( bounds.top, 4, "bounds.top is set correctly" );
oldBounds.left = 100;
t.eq( bounds.left, 1, "changing olBounds.left does not change bounds.left" );
}
function test_08_Bounds_containsBounds(t) {
t.plan( 35 );
containerBounds = new OpenLayers.Bounds(10,10,40,40);
//totally outside
bounds = new OpenLayers.Bounds(0,0,5,5);
t.eq( containerBounds.containsBounds(bounds) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ")");
t.eq( containerBounds.containsBounds(bounds, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false" );
t.eq( containerBounds.containsBounds(bounds, false, true) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, false, false), false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
t.eq( containerBounds.containsBounds(bounds, true) , false , "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is true" );
t.eq( containerBounds.containsBounds(bounds, true, true) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, true, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
//totally outside on border
bounds = new OpenLayers.Bounds(15,0,30,10);
t.eq( containerBounds.containsBounds(bounds) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ")");
t.eq( containerBounds.containsBounds(bounds, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false" );
t.eq( containerBounds.containsBounds(bounds, false, true) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, false, false), false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" );
t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, true, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
//partially inside
bounds = new OpenLayers.Bounds(20,20,50,30);
t.eq( containerBounds.containsBounds(bounds) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ")");
t.eq( containerBounds.containsBounds(bounds, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false" );
t.eq( containerBounds.containsBounds(bounds, false, true) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, false, false), false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" );
t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, true, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
//totally inside on border
bounds = new OpenLayers.Bounds(10,20,30,30);
t.eq( containerBounds.containsBounds(bounds) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ")");
t.eq( containerBounds.containsBounds(bounds, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false" );
t.eq( containerBounds.containsBounds(bounds, false, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, false, false), false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" );
t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, true, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
//totally inside
bounds = new OpenLayers.Bounds(20,20,30,30);
t.eq( containerBounds.containsBounds(bounds) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ")");
t.eq( containerBounds.containsBounds(bounds, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false" );
t.eq( containerBounds.containsBounds(bounds, false, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, false, false), true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" );
t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
t.eq( containerBounds.containsBounds(bounds, true, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
}
function test_09_Bounds_determineQuadrant(t) {
t.plan( 4 );
var bounds = new OpenLayers.Bounds(0,0,100,100);
var tl = new OpenLayers.LonLat(25, 75);
var tr = new OpenLayers.LonLat(75, 75);
var bl = new OpenLayers.LonLat(25, 25);
var br = new OpenLayers.LonLat(75, 25);
t.eq( bounds.determineQuadrant(tl), "tl", "bounds.determineQuadrant correctly identifies a coordinate in the top left quadrant");
t.eq( bounds.determineQuadrant(tr), "tr", "bounds.determineQuadrant correctly identifies a coordinate in the top right quadrant");
t.eq( bounds.determineQuadrant(bl), "bl", "bounds.determineQuadrant correctly identifies a coordinate in the bottom left quadrant");
t.eq( bounds.determineQuadrant(br), "br", "bounds.determineQuadrant correctly identifies a coordinate in the bottom right quadrant");
}
function test_10_Bounds_oppositeQuadrant(t) {
t.plan( 4 );
t.eq( OpenLayers.Bounds.oppositeQuadrant("tl"), "br", "OpenLayers.Bounds.oppositeQuadrant returns 'br' for 'tl'");
t.eq( OpenLayers.Bounds.oppositeQuadrant("tr"), "bl", "OpenLayers.Bounds.oppositeQuadrant returns 'bl' for 'tr'");
t.eq( OpenLayers.Bounds.oppositeQuadrant("bl"), "tr", "OpenLayers.Bounds.oppositeQuadrant returns 'tr' for 'bl'");
t.eq( OpenLayers.Bounds.oppositeQuadrant("br"), "tl", "OpenLayers.Bounds.oppositeQuadrant returns 'tl' for 'br'");
}
function test_11_Bounds_equals(t) {
t.plan( 3 );
var boundsA = new OpenLayers.Bounds(1,2,3,4);
var boundsB = new OpenLayers.Bounds(1,2,3,4);
var boundsC = new OpenLayers.Bounds(1,5,3,4);
t.ok( boundsA.equals(boundsB), "equals() returns true on two equal bounds." );
t.ok( !boundsA.equals(boundsC), "equals() returns false on two different bounds." );
t.ok( !boundsA.equals(null), "equals() returns false on comparison to null");
}
function test_12_Bounds_getHeight_getWidth(t) {
t.plan( 2 );
var bounds = new OpenLayers.Bounds(10,20,100,120);
t.eq( bounds.getWidth(), 90, "getWidth() works" );
t.eq( bounds.getHeight(), 100, "getHeight() works" );
}
function test_13_Bounds_getCenters(t) {
t.plan( 2 );
var bounds = new OpenLayers.Bounds(0,20,100,120);
t.ok( bounds.getCenterPixel().equals(new OpenLayers.Pixel(50, 70)), "getCenterPixel() works correctly");
t.ok( bounds.getCenterLonLat().equals(new OpenLayers.LonLat(50, 70)), "getCenterLonLat() works correctly");
}
function test_14_Bounds_fromArray(t) {
t.plan( 5 );
var bbox = [1,2,3,4];
bounds = OpenLayers.Bounds.fromArray(bbox);
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
t.eq( bounds.left, 1, "bounds.left is set correctly" );
t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
t.eq( bounds.right, 3, "bounds.right is set correctly" );
t.eq( bounds.top, 4, "bounds.top is set correctly" );
}
function test_14_Bounds_fromSize(t) {
t.plan( 5 );
var height = 15;
var width = 16;
var size = new OpenLayers.Size(width, height);
bounds = OpenLayers.Bounds.fromSize(size);
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
t.eq( bounds.left, 0, "bounds.left is set correctly" );
t.eq( bounds.bottom, height, "bounds.bottom is set correctly" );
t.eq( bounds.right, width, "bounds.right is set correctly" );
t.eq( bounds.top, 0, "bounds.top is set correctly" );
}
// -->
</script>
</head>
<body>
</body>
</html>