3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_Bounds_constructor (t) {
9 bounds = new OpenLayers.Bounds();
10 t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
11 t.eq( bounds.CLASS_NAME, "OpenLayers.Bounds", "bounds.CLASS_NAME is set correctly" );
12 t.eq( bounds.left, null, "bounds.left is initialized to null" );
13 t.eq( bounds.bottom, null, "bounds.bottom is initialized to null" );
14 t.eq( bounds.right, null, "bounds.right is initialized to null" );
15 t.eq( bounds.top, null, "bounds.top is initialized to null" );
18 bounds = new OpenLayers.Bounds(0,2,10,4);
19 t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
20 t.eq( bounds.CLASS_NAME, "OpenLayers.Bounds", "bounds.CLASS_NAME is set correctly" );
21 t.eq( bounds.left, 0, "bounds.left is set correctly" );
22 t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
23 t.eq( bounds.right, 10, "bounds.right is set correctly" );
24 t.eq( bounds.top, 4, "bounds.top is set correctly" );
25 t.eq( bounds.getWidth(), 10, "bounds.getWidth() returns correct value" );
26 t.eq( bounds.getHeight(), 2, "bounds.getHeight() returns correct value" );
28 var sz = bounds.getSize();
29 var size = new OpenLayers.Size(10,2);
30 t.ok(sz.equals(size),"bounds.getSize() has correct value" );
32 var center = new OpenLayers.Pixel(5,3);
33 var boundsCenter = bounds.getCenterPixel();
34 t.ok( boundsCenter.equals(center), "bounds.getCenterLonLat() has correct value" );
36 var center = new OpenLayers.LonLat(5,3);
37 var boundsCenter = bounds.getCenterLonLat();
38 t.ok( boundsCenter.equals(center), "bounds.getCenterLonLat() has correct value" );
40 // This is an actual use case with Mercator projection at global scale
41 bounds = new OpenLayers.Bounds(-40075016.67999999,-20037508.339999992,
42 40075016.67999999,20037508.339999992);
43 t.eq( bounds.left, -40075016.68, "bounds.left adjusted for floating precision");
44 t.eq( bounds.bottom, -20037508.34, "bounds.bottom adjusted for floating precision");
45 t.eq( bounds.right, 40075016.68, "bounds.right adjusted for floating precision");
46 t.eq( bounds.top, 20037508.34, "bounds.top adjusted for floating precision");
49 function test_Bounds_constructorFromStrings(t) {
51 bounds = new OpenLayers.Bounds("0","2","10","4");
52 t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
53 t.eq( bounds.CLASS_NAME, "OpenLayers.Bounds", "bounds.CLASS_NAME is set correctly" );
54 t.eq( bounds.left, 0, "bounds.left is set correctly" );
55 t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
56 t.eq( bounds.right, 10, "bounds.right is set correctly" );
57 t.eq( bounds.top, 4, "bounds.top is set correctly" );
61 function test_Bounds_toBBOX(t) {
63 bounds = new OpenLayers.Bounds(1,2,3,4);
64 t.eq( bounds.toBBOX(), "1,2,3,4", "toBBOX() returns correct value." );
65 bounds = new OpenLayers.Bounds(1.00000001,2,3,4);
66 t.eq( bounds.toBBOX(), "1,2,3,4", "toBBOX() rounds off small differences." );
67 bounds = new OpenLayers.Bounds(1.00000001,2.5,3,4);
68 t.eq( bounds.toBBOX(), "1,2.5,3,4", "toBBOX() returns correct value. for a half number" );
69 bounds = new OpenLayers.Bounds(1,2.5555555,3,4);
70 t.eq( bounds.toBBOX(), "1,2.555556,3,4", "toBBOX() rounds to correct value." );
71 bounds = new OpenLayers.Bounds(1,2.5555555,3,4);
72 t.eq( bounds.toBBOX(1), "1,2.6,3,4", "toBBOX() rounds to correct value with power provided." );
73 bounds = new OpenLayers.Bounds(1,2.5555555,3,4);
76 function test_Bounds_toString(t) {
78 bounds = new OpenLayers.Bounds(1,2,3,4);
79 t.eq( bounds.toString(), "left-bottom=(1,2) right-top=(3,4)", "toString() returns correct value." );
81 function test_Bounds_toArray(t) {
83 bounds = new OpenLayers.Bounds(1,2,3,4);
84 t.eq( bounds.toArray(), [1,2,3,4], "toArray() returns correct value." );
87 function test_Bounds_toGeometry(t) {
89 var minx = Math.random();
90 var miny = Math.random();
91 var maxx = Math.random();
92 var maxy = Math.random();
93 var bounds = new OpenLayers.Bounds(minx, miny, maxx, maxy);
94 var poly = bounds.toGeometry();
95 t.eq(poly.CLASS_NAME, "OpenLayers.Geometry.Polygon",
96 "polygon instance created");
97 t.eq(poly.components.length, 1,
98 "polygon with one ring created");
99 var ring = poly.components[0];
100 t.eq(ring.components.length, 5,
101 "four sided polygon created");
102 t.eq(ring.components[0].x, OpenLayers.Util.toFloat(minx),
103 "bounds left preserved");
104 t.eq(ring.components[0].y, OpenLayers.Util.toFloat(miny),
105 "bounds bottom preserved");
106 t.eq(ring.components[2].x, OpenLayers.Util.toFloat(maxx),
107 "bounds left preserved");
108 t.eq(ring.components[2].y, OpenLayers.Util.toFloat(maxy),
109 "bounds bottom preserved");
112 function test_Bounds_contains(t) {
114 bounds = new OpenLayers.Bounds(10,10,40,40);
115 t.eq( bounds.contains(20,20), true, "bounds(10,10,40,40) correctly contains LonLat(20,20)" );
116 t.eq( bounds.contains(0,0), false, "bounds(10,10,40,40) correctly does not contain LonLat(0,0)" );
117 t.eq( bounds.contains(40,40), true, "bounds(10,10,40,40) correctly contains LonLat(40,40) with inclusive set to true" );
118 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" );
120 var px = new OpenLayers.Pixel(15,30);
121 t.eq( bounds.containsPixel(px), bounds.contains(px.x, px.y), "containsPixel works");
123 var ll = new OpenLayers.LonLat(15,30);
124 t.eq( bounds.containsLonLat(ll), bounds.contains(ll.lon, ll.lat), "containsLonLat works");
128 function test_Bounds_fromString(t) {
130 bounds = OpenLayers.Bounds.fromString("1,2,3,4");
131 t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
132 t.eq( bounds.left, 1, "bounds.left is set correctly" );
133 t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
134 t.eq( bounds.right, 3, "bounds.right is set correctly" );
135 t.eq( bounds.top, 4, "bounds.top is set correctly" );
137 bounds = OpenLayers.Bounds.fromString("1.1,2.2,3.3,4.4");
138 t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
139 t.eq( bounds.left, 1.1, "bounds.left is set correctly" );
140 t.eq( bounds.bottom, 2.2, "bounds.bottom is set correctly" );
141 t.eq( bounds.right, 3.3, "bounds.right is set correctly" );
142 t.eq( bounds.top, 4.4, "bounds.top is set correctly" );
146 function test_Bounds_getSize(t) {
148 var bounds = new OpenLayers.Bounds(0,10,100,120);
150 t.ok( bounds.getSize().equals(new OpenLayers.Size(100, 110)), "getCenterPixel() works correctly");
153 function test_Bounds_clone(t) {
155 var oldBounds = new OpenLayers.Bounds(1,2,3,4);
156 var bounds = oldBounds.clone();
157 t.ok( bounds instanceof OpenLayers.Bounds, "clone returns new OpenLayers.Bounds object" );
158 t.eq( bounds.left, 1, "bounds.left is set correctly" );
159 t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
160 t.eq( bounds.right, 3, "bounds.right is set correctly" );
161 t.eq( bounds.top, 4, "bounds.top is set correctly" );
163 oldBounds.left = 100;
164 t.eq( bounds.left, 1, "changing olBounds.left does not change bounds.left" );
167 function test_Bounds_intersectsBounds(t) {
170 var aBounds = new OpenLayers.Bounds(-180, -90, 180, 90);
173 var bBounds = new OpenLayers.Bounds(-20, -10, 20, 10);
174 var cBounds = new OpenLayers.Bounds(-181,-90,180,90);
175 t.eq( aBounds.intersectsBounds(bBounds), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + ")" );
176 t.eq( aBounds.intersectsBounds(bBounds, true), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is true" );
177 t.eq( aBounds.intersectsBounds(bBounds, false), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is false" );
178 t.eq( aBounds.intersectsBounds(cBounds, false), true, "aBounds with cBounds adjusted one degree left passes intersect bounds. (3 sides match, 1 side different)." );
179 t.eq( cBounds.intersectsBounds(aBounds, false), true, "cBounds with aBounds adjusted one degree left passes intersect bounds. (3 sides match, 1 side different)." );
182 bBounds = new OpenLayers.Bounds(-181, -91, 181, 91);
183 t.eq( aBounds.intersectsBounds(bBounds), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + ")" );
184 t.eq( aBounds.intersectsBounds(bBounds, true), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is true" );
185 t.eq( aBounds.intersectsBounds(bBounds, false), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is false" );
188 bBounds = new OpenLayers.Bounds(-185, -100, 20, 50);
189 t.eq( aBounds.intersectsBounds(bBounds), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + ")" );
190 t.eq( aBounds.intersectsBounds(bBounds, true), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is true" );
191 t.eq( aBounds.intersectsBounds(bBounds, false), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is false" );
194 bBounds = new OpenLayers.Bounds(-360, -180, -180, -90);
195 t.eq( aBounds.intersectsBounds(bBounds), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + ")" );
196 t.eq( aBounds.intersectsBounds(bBounds, true), true, "(" + aBounds.toBBOX() + ") correctly intersects (" + bBounds.toBBOX() + "), inclusive is true" );
197 t.eq( aBounds.intersectsBounds(bBounds, false), false, "(" + aBounds.toBBOX() + ") does not intersect (" + bBounds.toBBOX() + "), inclusive is false" );
200 bBounds = new OpenLayers.Bounds(-360, -180, -185, -95);
201 t.eq( aBounds.intersectsBounds(bBounds), false, "(" + aBounds.toBBOX() + ") does not intersect (" + bBounds.toBBOX() + ")" );
202 t.eq( aBounds.intersectsBounds(bBounds, true), false, "(" + aBounds.toBBOX() + ") does not intersect (" + bBounds.toBBOX() + "), inclusive is true" );
203 t.eq( aBounds.intersectsBounds(bBounds, false), false, "(" + aBounds.toBBOX() + ") does not intersect (" + bBounds.toBBOX() + "), inclusive is false" );
205 // This is an actual use case with Mercator tiles at global scale
206 var merc_aBounds = new OpenLayers.Bounds(-40075016.67999999,20037508.339999992,
207 -20037508.339999992,40075016.67999999),
208 merc_bBounds = new OpenLayers.Bounds(-20037508.34,-20037508.34,
209 20037508.34,20037508.34);
210 t.eq( merc_aBounds.intersectsBounds(merc_bBounds, true), true, "intersect shouldn't fall prey to floating point errors, inclusive is true");
211 t.eq( merc_aBounds.intersectsBounds(merc_bBounds, false), false, "intersect shouldn't fall prey to floating point errors, inclusive is false");
213 // test for bounds intersection where none of the corners are contained within the other bounds
214 var b1 = new OpenLayers.Bounds(-1, -2, 1, 2);
215 var b2 = new OpenLayers.Bounds(-2, -1, 2, 1);
216 t.eq(b1.intersectsBounds(b2), true, "vertical rectangle intersects horizontal rectangle");
217 t.eq(b2.intersectsBounds(b1), true, "horizontal rectangle intersects vertical rectangle");
221 function test_Bounds_containsBounds(t) {
223 containerBounds = new OpenLayers.Bounds(10,10,40,40);
226 bounds = new OpenLayers.Bounds(0,0,5,5);
227 t.eq( containerBounds.containsBounds(bounds) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ")");
228 t.eq( containerBounds.containsBounds(bounds, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false" );
229 t.eq( containerBounds.containsBounds(bounds, false, true) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
230 t.eq( containerBounds.containsBounds(bounds, false, false), false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
231 t.eq( containerBounds.containsBounds(bounds, true) , false , "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is true" );
232 t.eq( containerBounds.containsBounds(bounds, true, true) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
233 t.eq( containerBounds.containsBounds(bounds, true, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
235 //totally outside on border
236 bounds = new OpenLayers.Bounds(15,0,30,10);
237 t.eq( containerBounds.containsBounds(bounds) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ")");
238 t.eq( containerBounds.containsBounds(bounds, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false" );
239 t.eq( containerBounds.containsBounds(bounds, false, true) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
240 t.eq( containerBounds.containsBounds(bounds, false, false), false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
241 t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" );
242 t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
243 t.eq( containerBounds.containsBounds(bounds, true, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
246 bounds = new OpenLayers.Bounds(20,20,50,30);
247 t.eq( containerBounds.containsBounds(bounds) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ")");
248 t.eq( containerBounds.containsBounds(bounds, false) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false" );
249 t.eq( containerBounds.containsBounds(bounds, false, true) , false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
250 t.eq( containerBounds.containsBounds(bounds, false, false), false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
251 t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" );
252 t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
253 t.eq( containerBounds.containsBounds(bounds, true, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
255 //totally inside on border
256 bounds = new OpenLayers.Bounds(10,20,30,30);
257 t.eq( containerBounds.containsBounds(bounds) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ")");
258 t.eq( containerBounds.containsBounds(bounds, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false" );
259 t.eq( containerBounds.containsBounds(bounds, false, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
260 t.eq( containerBounds.containsBounds(bounds, false, false), false, "(" + containerBounds.toBBOX() + ") correctly does not contain (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
261 t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" );
262 t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
263 t.eq( containerBounds.containsBounds(bounds, true, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
266 bounds = new OpenLayers.Bounds(20,20,30,30);
267 t.eq( containerBounds.containsBounds(bounds) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ")");
268 t.eq( containerBounds.containsBounds(bounds, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false" );
269 t.eq( containerBounds.containsBounds(bounds, false, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false, inclusive is true" );
270 t.eq( containerBounds.containsBounds(bounds, false, false), true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is false, inclusive is false" );
271 t.eq( containerBounds.containsBounds(bounds, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true" );
272 t.eq( containerBounds.containsBounds(bounds, true, true) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is true" );
273 t.eq( containerBounds.containsBounds(bounds, true, false) , true, "(" + containerBounds.toBBOX() + ") correctly contains (" + bounds.toBBOX() + ") when partial is true, inclusive is false" );
277 function test_Bounds_determineQuadrant(t) {
280 var bounds = new OpenLayers.Bounds(0,0,100,100);
282 var tl = new OpenLayers.LonLat(25, 75);
283 var tr = new OpenLayers.LonLat(75, 75);
284 var bl = new OpenLayers.LonLat(25, 25);
285 var br = new OpenLayers.LonLat(75, 25);
287 t.eq( bounds.determineQuadrant(tl), "tl", "bounds.determineQuadrant correctly identifies a coordinate in the top left quadrant");
288 t.eq( bounds.determineQuadrant(tr), "tr", "bounds.determineQuadrant correctly identifies a coordinate in the top right quadrant");
289 t.eq( bounds.determineQuadrant(bl), "bl", "bounds.determineQuadrant correctly identifies a coordinate in the bottom left quadrant");
290 t.eq( bounds.determineQuadrant(br), "br", "bounds.determineQuadrant correctly identifies a coordinate in the bottom right quadrant");
293 function test_Bounds_oppositeQuadrant(t) {
297 t.eq( OpenLayers.Bounds.oppositeQuadrant("tl"), "br", "OpenLayers.Bounds.oppositeQuadrant returns 'br' for 'tl'");
298 t.eq( OpenLayers.Bounds.oppositeQuadrant("tr"), "bl", "OpenLayers.Bounds.oppositeQuadrant returns 'bl' for 'tr'");
299 t.eq( OpenLayers.Bounds.oppositeQuadrant("bl"), "tr", "OpenLayers.Bounds.oppositeQuadrant returns 'tr' for 'bl'");
300 t.eq( OpenLayers.Bounds.oppositeQuadrant("br"), "tl", "OpenLayers.Bounds.oppositeQuadrant returns 'tl' for 'br'");
303 function test_Bounds_equals(t) {
305 var boundsA = new OpenLayers.Bounds(1,2,3,4);
306 var boundsB = new OpenLayers.Bounds(1,2,3,4);
307 var boundsC = new OpenLayers.Bounds(1,5,3,4);
309 t.ok( boundsA.equals(boundsB), "equals() returns true on two equal bounds." );
310 t.ok( !boundsA.equals(boundsC), "equals() returns false on two different bounds." );
311 t.ok( !boundsA.equals(null), "equals() returns false on comparison to null");
314 function test_Bounds_getHeight_getWidth(t) {
316 var bounds = new OpenLayers.Bounds(10,20,100,120);
318 t.eq( bounds.getWidth(), 90, "getWidth() works" );
319 t.eq( bounds.getHeight(), 100, "getHeight() works" );
323 function test_Bounds_getCenters(t) {
325 var bounds = new OpenLayers.Bounds(0,20,100,120);
327 t.ok( bounds.getCenterPixel().equals(new OpenLayers.Pixel(50, 70)), "getCenterPixel() works correctly");
328 t.ok( bounds.getCenterLonLat().equals(new OpenLayers.LonLat(50, 70)), "getCenterLonLat() works correctly");
331 function test_getCenterLonLat(t) {
333 var bounds = new OpenLayers.Bounds(0, 10, 20, 60);
335 // set private centerLonLat to confirm that it is getting returned if set
336 bounds.centerLonLat = "foo";
337 t.eq(bounds.getCenterLonLat(), "foo", "returns cached value");
338 bounds.centerLonLat = null;
341 var center = bounds.getCenterLonLat();
342 t.eq(center.lon, 10, "unmodified: correct x");
343 t.eq(center.lat, 35, "unmodified: correct y");
346 bounds.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
347 center = bounds.getCenterLonLat();
348 t.eq(Math.round(center.lon), 1113195, "transformed: correct x");
349 t.eq(Math.round(center.lat), 4759314, "transformed: correct y");
352 bounds.extend(new OpenLayers.Bounds(-10000000, -10000000, 10000000, 10000000));
353 center = bounds.getCenterLonLat();
354 t.eq(center.lon, 0, "extended: correct x");
355 t.eq(center.lat, 0, "extended: correct y");
360 function test_Bounds_fromArray(t) {
363 var bbox = [1,2,3,4];
364 bounds = OpenLayers.Bounds.fromArray(bbox);
365 t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
366 t.eq( bounds.left, 1, "bounds.left is set correctly" );
367 t.eq( bounds.bottom, 2, "bounds.bottom is set correctly" );
368 t.eq( bounds.right, 3, "bounds.right is set correctly" );
369 t.eq( bounds.top, 4, "bounds.top is set correctly" );
372 function test_Bounds_fromSize(t) {
377 var size = new OpenLayers.Size(width, height);
378 bounds = OpenLayers.Bounds.fromSize(size);
379 t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
380 t.eq( bounds.left, 0, "bounds.left is set correctly" );
381 t.eq( bounds.bottom, height, "bounds.bottom is set correctly" );
382 t.eq( bounds.right, width, "bounds.right is set correctly" );
383 t.eq( bounds.top, 0, "bounds.top is set correctly" );
387 function test_Bounds_extend(t) {
390 var originalBounds = new OpenLayers.Bounds();
391 var bounds = originalBounds.clone();
392 //null bounds to start
393 bounds.extend(new OpenLayers.LonLat(4,5));
394 t.ok(bounds.equals(new OpenLayers.Bounds(4,5,4,5)), "uninitialized bounds can be safely extended");
398 originalBounds = new OpenLayers.Bounds(10,20,50,80);
400 bounds = originalBounds.clone();
404 t.ok(bounds.equals(originalBounds), "null to extend does not crash or change original bounds");
406 //obj with no classname
408 bounds.extend(object);
409 t.ok(bounds.equals(originalBounds), "extend() passing object with no classname does not crash or change original bounds")
413 //pushing all limits with bounds obj
414 var testBounds = new OpenLayers.Bounds(5, 10, 60, 90);
415 object = testBounds.clone();
417 bounds.extend(object);
418 t.ok(bounds.equals(testBounds), "extend by valid bounds, pushing all limits, correctly extends bounds");
420 //pushing no limits with bounds obj
421 bounds = originalBounds.clone();
423 testBounds = new OpenLayers.Bounds(15, 30, 40, 70);
424 object = testBounds.clone();
426 bounds.extend(object);
427 t.ok(bounds.equals(originalBounds), "extend by valid bounds, pushing no limits, correctly does not extend bounds");
432 bounds = originalBounds.clone();
434 object = new OpenLayers.LonLat(5, 10);
436 bounds.extend(object);
438 t.ok( ((bounds.left == object.lon) &&
439 (bounds.bottom == object.lat) &&
440 (bounds.right == originalBounds.right) &&
441 (bounds.top == originalBounds.top)), "obj lonlat to extends correclty modifies left and bottom");
444 bounds = originalBounds.clone();
446 object = new OpenLayers.LonLat(60,90);
448 bounds.extend(object);
450 t.ok( ((bounds.left == originalBounds.left) &&
451 (bounds.bottom == originalBounds.bottom) &&
452 (bounds.right == object.lon) &&
453 (bounds.top == object.lat)), "obj lonlat to extends correclty modifies right and top");
458 bounds = originalBounds.clone();
460 object = new OpenLayers.Geometry.Point(5, 10);
462 bounds.extend(object);
464 t.ok( ((bounds.left == object.x) &&
465 (bounds.bottom == object.y) &&
466 (bounds.right == originalBounds.right) &&
467 (bounds.top == originalBounds.top)), "obj Point to extends correclty modifies left and bottom");
470 bounds = originalBounds.clone();
472 object = new OpenLayers.Geometry.Point(60,90);
474 bounds.extend(object);
476 t.ok( ((bounds.left == originalBounds.left) &&
477 (bounds.bottom == originalBounds.bottom) &&
478 (bounds.right == object.x) &&
479 (bounds.top == object.y)), "obj Point to extends correclty modifies right and top");
484 function test_Bounds_wrapDateLine(t) {
487 var testBounds, wrappedBounds, desiredBounds;
489 var maxExtent = new OpenLayers.Bounds(-10,-10,10,10);
490 var exactBounds = maxExtent.clone();
491 var simpleBounds = new OpenLayers.Bounds( -5,-5,5,5);
496 testBounds = simpleBounds.clone();
497 wrappedBounds = testBounds.wrapDateLine(null);
498 t.ok(wrappedBounds.equals(simpleBounds), "wrapping a bounds with a bad maxextent does nothing");
503 testBounds = exactBounds.clone();
504 wrappedBounds = testBounds.wrapDateLine(maxExtent);
505 t.ok(wrappedBounds.equals(exactBounds), "wrapping a bounds precisely within (equal to) maxextent does nothing");
509 testBounds = simpleBounds.clone();
510 wrappedBounds = testBounds.wrapDateLine(maxExtent);
511 t.ok(wrappedBounds.equals(simpleBounds), "wrapping a bounds within maxextent does nothing");
516 testBounds = simpleBounds.add(-10,0);
517 wrappedBounds = testBounds.wrapDateLine(maxExtent);
518 t.ok(wrappedBounds.equals(testBounds), "wrapping a bounds that straddles the left of maxextent does nothing");
520 //left rightTolerance
521 testBounds = simpleBounds.add(-14,0);
523 testBounds.wrapDateLine(maxExtent, {'rightTolerance': 1} );
524 desiredBounds = simpleBounds.add(6,0);
525 t.ok(wrappedBounds.equals(desiredBounds), "wrapping a bounds rightTolerance left of maxextent works");
528 testBounds = exactBounds.add(-20,0);
529 wrappedBounds = testBounds.wrapDateLine(maxExtent);
530 t.ok(wrappedBounds.equals(exactBounds), "wrapping an exact bounds once left of maxextent works");
533 testBounds = simpleBounds.add(-20,0);
534 wrappedBounds = testBounds.wrapDateLine(maxExtent);
535 t.ok(wrappedBounds.equals(simpleBounds), "wrapping a bounds once left of maxextent works");
538 testBounds = simpleBounds.add(-200,0);
539 wrappedBounds = testBounds.wrapDateLine(maxExtent);
540 t.ok(wrappedBounds.equals(simpleBounds), "wrapping a bounds way left of maxextent works");
545 testBounds = simpleBounds.add(10,0);
546 wrappedBounds = testBounds.wrapDateLine(maxExtent);
547 t.ok(wrappedBounds.equals(testBounds), "wrapping a bounds that straddles the right of maxextent does nothing");
549 //right leftTolerance
550 testBounds = simpleBounds.add(14,0);
552 testBounds.wrapDateLine(maxExtent, {'leftTolerance': 1} );
553 desiredBounds = simpleBounds.add(-6,0);
554 t.ok(wrappedBounds.equals(desiredBounds), "wrapping a bounds leftTolerance right of maxextent works");
557 testBounds = exactBounds.add(20,0);
558 wrappedBounds = testBounds.wrapDateLine(maxExtent);
559 t.ok(wrappedBounds.equals(exactBounds), "wrapping an exact bounds once right of maxextent works");
562 testBounds = simpleBounds.add(20,0);
563 wrappedBounds = testBounds.wrapDateLine(maxExtent);
564 t.ok(wrappedBounds.equals(simpleBounds), "wrapping a bounds once right of maxextent works");
567 testBounds = simpleBounds.add(200,0);
568 wrappedBounds = testBounds.wrapDateLine(maxExtent);
569 t.ok(wrappedBounds.equals(simpleBounds), "wrapping a bounds way right of maxextent works");
574 function test_Bounds_transform(t) {
576 bounds = new OpenLayers.Bounds(10, -10, 20, 10);
577 bounds.transform(new OpenLayers.Projection("foo"), new OpenLayers.Projection("Bar"));
578 t.eq(bounds.toBBOX(), "10,-10,20,10", "null transform okay");
579 bounds.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
580 t.eq(bounds.toBBOX(), "1113194.907778,-1118889.974702,2226389.815556,1118889.974702", "bounds for spherical mercator transform are correct");
581 bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
582 t.eq(bounds.toBBOX(), "10,-10,20,10", "bounds for inverse spherical mercator transform are correct");
585 function test_Bounds_add(t) {
588 origBounds = new OpenLayers.Bounds(1,2,3,4);
589 testBounds = origBounds.clone();
591 var bounds = testBounds.add(5, 50);
592 t.ok( testBounds.equals(origBounds), "testBounds is not modified by add operation");
594 var b = new OpenLayers.Bounds(6,52,8,54);
595 t.ok( bounds.equals(b), "bounds is set correctly");
598 var desiredMsg = "You must pass both x and y values to the add function.";
599 OpenLayers.Console.error = function(msg) {
600 t.eq(msg, desiredMsg, "error correctly reported");
603 bounds = testBounds.add(null, 50);
604 t.ok( testBounds.equals(origBounds), "testBounds is not modified by erroneous add operation (null x)");
605 t.ok(bounds == null, "returns null on erroneous add operation (null x)");
607 bounds = testBounds.add(5, null);
608 t.ok( testBounds.equals(origBounds), "testBounds is not modified by erroneous add operation (null y)");
609 t.ok(bounds == null, "returns null on erroneous add operation (null y)");
612 function test_Bounds_scale(t) {
615 origBounds = new OpenLayers.Bounds(1,2,3,4);
616 bounds = origBounds.scale(2);
617 var b = new OpenLayers.Bounds(0,1,4,5);
618 t.ok(bounds.equals(b), "Bounds scale correctly with default origin at center")
620 var origin = new OpenLayers.Pixel(0,1);
621 bounds = origBounds.scale(2,origin);
622 b = new OpenLayers.Bounds(2,3,6,7);
623 t.ok(bounds.equals(b), "Bounds scale correctly with offset origin");
625 origin = new OpenLayers.Pixel(5,1);
626 bounds = bounds.scale(2, origin);
627 b = new OpenLayers.Bounds(-1, 5, 7, 13);
628 t.ok(bounds.equals(b), "Bounds scale correctly with offset origin");