]> dev.renevier.net Git - syp.git/blob - openlayers/tests/BaseTypes/Bounds.html
initial commit
[syp.git] / openlayers / tests / BaseTypes / Bounds.html
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5     var bounds; 
6     function test_Bounds_constructor (t) {
7         t.plan( 21 );
8         
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" );
16
17
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" );
27         
28         var sz = bounds.getSize();
29         var size = new OpenLayers.Size(10,2);
30         t.ok(sz.equals(size),"bounds.getSize() has correct value" );
31     
32         var center = new OpenLayers.Pixel(5,3);
33         var boundsCenter = bounds.getCenterPixel();
34         t.ok( boundsCenter.equals(center), "bounds.getCenterLonLat() has correct value" );
35
36         var center = new OpenLayers.LonLat(5,3);
37         var boundsCenter = bounds.getCenterLonLat();
38         t.ok( boundsCenter.equals(center), "bounds.getCenterLonLat() has correct value" );
39
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");
47     }
48
49     function test_Bounds_constructorFromStrings(t) {
50         t.plan( 6 );
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" );
58         
59     }
60
61     function test_Bounds_toBBOX(t) {
62         t.plan( 5 );
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);
74     }
75
76     function test_Bounds_toString(t) {
77         t.plan( 1 );
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." ); 
80     }
81     function test_Bounds_toArray(t) {
82         t.plan( 1 );
83         bounds = new OpenLayers.Bounds(1,2,3,4);
84         t.eq( bounds.toArray(), [1,2,3,4], "toArray() returns correct value." ); 
85     }
86
87     function test_Bounds_toGeometry(t) {
88         t.plan(7);
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");
110     }
111
112     function test_Bounds_contains(t) {
113         t.plan( 6 );
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" );
119
120         var px = new OpenLayers.Pixel(15,30);
121         t.eq( bounds.containsPixel(px), bounds.contains(px.x, px.y), "containsPixel works");
122
123         var ll = new OpenLayers.LonLat(15,30);
124         t.eq( bounds.containsLonLat(ll), bounds.contains(ll.lon, ll.lat), "containsLonLat works");
125
126     }
127
128     function test_Bounds_fromString(t) {
129        t.plan( 10 );
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" );
136
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" );
143
144     }
145
146     function test_Bounds_getSize(t) {
147         t.plan( 1 );
148         var bounds = new OpenLayers.Bounds(0,10,100,120);
149
150         t.ok( bounds.getSize().equals(new OpenLayers.Size(100, 110)), "getCenterPixel() works correctly");
151     }
152     
153     function test_Bounds_clone(t) {
154        t.plan( 6 );
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" );
162        
163        oldBounds.left = 100;
164        t.eq( bounds.left, 1, "changing olBounds.left does not change bounds.left" );
165     }
166
167     function test_Bounds_intersectsBounds(t) {
168        t.plan(21);
169
170        var aBounds = new OpenLayers.Bounds(-180, -90, 180, 90);
171
172        //inside
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)." );
180
181        //outside
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" );
186
187        //total intersect
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" );
192
193        //border intersect
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" );
198
199        //no intersect
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" );
204
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");
212         
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");
218         
219     }
220     
221     function test_Bounds_containsBounds(t) {
222         t.plan( 35 );
223         containerBounds = new OpenLayers.Bounds(10,10,40,40);
224
225         //totally outside
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" );
234
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" );
244
245         //partially inside
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" );
254
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" );
264
265         //totally inside
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" );        
274         
275     }
276     
277     function test_Bounds_determineQuadrant(t) {
278
279        t.plan( 4 );
280        var bounds = new OpenLayers.Bounds(0,0,100,100);
281
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);
286
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");
291     }
292
293     function test_Bounds_oppositeQuadrant(t) {
294
295        t.plan( 4 );
296
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'");
301     }
302
303     function test_Bounds_equals(t) {
304         t.plan( 3 );
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);
308
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");
312     }
313
314     function test_Bounds_getHeight_getWidth(t) {
315         t.plan( 2 );
316         var bounds = new OpenLayers.Bounds(10,20,100,120);
317
318         t.eq( bounds.getWidth(), 90, "getWidth() works" );
319         t.eq( bounds.getHeight(), 100, "getHeight() works" );
320
321     }
322
323     function test_Bounds_getCenters(t) {
324         t.plan( 2 );
325         var bounds = new OpenLayers.Bounds(0,20,100,120);
326
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");
329     }
330     
331     function test_getCenterLonLat(t) {
332         t.plan(7);
333         var bounds = new OpenLayers.Bounds(0, 10, 20, 60);
334         
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;
339         
340         // unmodified
341         var center = bounds.getCenterLonLat();
342         t.eq(center.lon, 10, "unmodified: correct x");
343         t.eq(center.lat, 35, "unmodified: correct y");
344         
345         // transformed
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");
350         
351         // extended
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");
356         
357         
358     }
359
360     function test_Bounds_fromArray(t) {
361        t.plan( 5 );
362        
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" );
370     }
371
372     function test_Bounds_fromSize(t) {
373        t.plan( 5 );
374        
375        var height = 15;
376        var width = 16;
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" );
384     }
385
386
387     function test_Bounds_extend(t) {
388         t.plan( 9 );
389     
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");
395
396
397
398         originalBounds = new OpenLayers.Bounds(10,20,50,80);
399
400         bounds = originalBounds.clone();
401         
402       //null obj
403         bounds.extend(null);        
404         t.ok(bounds.equals(originalBounds), "null to extend does not crash or change original bounds");
405
406       //obj with no classname        
407         var object = {};
408         bounds.extend(object);        
409         t.ok(bounds.equals(originalBounds), "extend() passing object with no classname does not crash or change original bounds")
410
411     //obj is bounds
412
413       //pushing all limits with bounds obj
414         var testBounds = new OpenLayers.Bounds(5, 10, 60, 90);
415         object = testBounds.clone();
416         
417         bounds.extend(object);
418         t.ok(bounds.equals(testBounds), "extend by valid bounds, pushing all limits, correctly extends bounds");
419
420       //pushing no limits with bounds obj
421         bounds = originalBounds.clone();
422
423         testBounds = new OpenLayers.Bounds(15, 30, 40, 70);
424         object = testBounds.clone();
425         
426         bounds.extend(object);
427         t.ok(bounds.equals(originalBounds), "extend by valid bounds, pushing no limits, correctly does not extend bounds");
428
429     // obj is lonlat
430     
431       //left, bottom
432         bounds = originalBounds.clone();
433         
434         object = new OpenLayers.LonLat(5, 10);
435         
436         bounds.extend(object);
437                
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");
442               
443       //right, top
444         bounds = originalBounds.clone();
445         
446         object = new OpenLayers.LonLat(60,90);
447         
448         bounds.extend(object);
449
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");
454               
455     // obj is point
456     
457       //left, bottom
458         bounds = originalBounds.clone();
459         
460         object = new OpenLayers.Geometry.Point(5, 10);
461         
462         bounds.extend(object);
463                
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");
468               
469       //right, top
470         bounds = originalBounds.clone();
471         
472         object = new OpenLayers.Geometry.Point(60,90);
473         
474         bounds.extend(object);
475
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");
480
481     }
482      
483      
484     function test_Bounds_wrapDateLine(t) {
485         t.plan( 13 );
486         
487         var testBounds, wrappedBounds, desiredBounds;
488
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);
492
493
494
495     //bad maxextent
496         testBounds = simpleBounds.clone();
497         wrappedBounds = testBounds.wrapDateLine(null);
498         t.ok(wrappedBounds.equals(simpleBounds), "wrapping a bounds with a bad maxextent does nothing");
499
500
501
502     //exactly inside 
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");
506
507
508     //inside 
509         testBounds = simpleBounds.clone();
510         wrappedBounds = testBounds.wrapDateLine(maxExtent);
511         t.ok(wrappedBounds.equals(simpleBounds), "wrapping a bounds within maxextent does nothing");
512     
513 // LEFT //
514
515     //straddling left
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");
519     
520     //left rightTolerance
521         testBounds = simpleBounds.add(-14,0);
522         wrappedBounds = 
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");
526
527     //exactly left
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");
531     
532     //left
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");
536     
537     //way left
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");
541
542 // RIGHT //
543
544     //straddling right
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");
548     
549     //right leftTolerance
550         testBounds = simpleBounds.add(14,0);
551         wrappedBounds = 
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");
555     
556     //exactly right
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");
560     
561     //right
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");
565     
566     //way right
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");
570
571
572
573     }
574     function test_Bounds_transform(t) {
575         t.plan( 3 );
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");
583     }
584
585     function test_Bounds_add(t) {
586         t.plan( 8 );
587
588         origBounds = new OpenLayers.Bounds(1,2,3,4);
589         testBounds = origBounds.clone();
590
591         var bounds = testBounds.add(5, 50);
592         t.ok( testBounds.equals(origBounds), "testBounds is not modified by add operation");
593
594         var b = new OpenLayers.Bounds(6,52,8,54);
595         t.ok( bounds.equals(b), "bounds is set correctly");
596         
597     //null values
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");
601         }
602     
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)");
606  
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)");
610     }
611
612     function test_Bounds_scale(t) {
613         t.plan(3);
614
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")
619
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");
624
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");
629
630     }
631
632   </script>
633 </head>
634 <body>
635 </body>
636 </html>
637