]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Util.html
initial commit
[syp.git] / openlayers / tests / Util.html
1 <html>
2 <head>
3   <script>
4     var custom$ = function() {};
5     window.$ = custom$;
6   </script>
7   <script src="../lib/OpenLayers.js"></script>
8   <script type="text/javascript">
9     var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
10     var map; 
11     
12     function test_$(t) {
13         t.plan(1);
14         t.ok($ === custom$, "OpenLayers doesn't clobber existing definition of $.");
15     }
16     
17     function test_Util_getImagesLocation (t) {
18         t.plan( 1 );
19         t.ok( OpenLayers.Util.getImagesLocation(), "../img/",
20                     "getImagesLocation()" );
21     }
22
23     function test_Util_Array(t) {
24         t.plan( 2 );
25
26         var array = new Array(1,2,3,4,4,5);
27
28         OpenLayers.Util.removeItem(array, 3);
29         t.eq( array.toString(), "1,2,4,4,5", "Util.removeItem works on one element");    
30         OpenLayers.Util.removeItem(array, 4);
31         t.eq( array.toString(), "1,2,5", "Util.removeItem works on more than one element ");    
32     }
33     
34     function test_Util_pagePosition(t) {
35         t.plan( 1 );
36         var pp = OpenLayers.Util.pagePosition(window);
37         t.eq( pp.toString(), "0,0", "Page position doesn't bail if passed 'window'")
38         
39     }
40
41     function test_Util_createDiv(t) {
42         t.plan( 24 );
43
44         var id = "boo";
45         var px = new OpenLayers.Pixel(5,5);
46         var sz = new OpenLayers.Size(10,10);
47         var img = "http://www.openlayers.org/images/OpenLayers.trac.png";
48         var position = "absolute";
49         var border = "13px solid";
50         var overflow = "hidden";
51         var opacity = 0.5;
52
53         var div = OpenLayers.Util.createDiv(id, px, sz, img, position, border, overflow, opacity);
54
55         if (!isMozilla)
56             t.ok( true, "skipping element test outside of Mozilla");
57         else
58             t.ok( div instanceof HTMLDivElement, "createDiv creates a valid HTMLDivElement" );
59         t.eq( div.id, id, "div.id set correctly");    
60         t.eq( div.style.left, px.x + "px", "div.style.left set correctly");    
61         t.eq( div.style.top, px.y + "px", "div.style.top set correctly");    
62
63         t.eq( div.style.width, sz.w + "px", "div.style.width set correctly");    
64         t.eq( div.style.height, sz.h + "px", "div.style.height set correctly");    
65
66         bImg = div.style.backgroundImage;
67         imgCorrect = ( (bImg == "url(" + img + ")") ||  
68                        (bImg == "url(\"" + img + "\")") );
69         t.ok(imgCorrect, "div.style.backgroundImage correctly");    
70
71         t.eq( div.style.position, position, "div.style.positionset correctly");    
72         //Safari 3 separates the border style into separate entities when reading it
73         if (OpenLayers.Util.getBrowserName() == 'safari') {
74           var s = border.split(' ');
75           t.ok(div.style.borderTopWidth == s[0] && div.style.borderTopStyle == s[1], "good default popup.border")
76         } else {
77           t.ok( (div.style.border.indexOf(border) != -1), "div.style.border set correctly");
78         }
79             
80         //Safari 3 separates style overflow into overflow-x and overflow-y
81         var prop = (OpenLayers.Util.getBrowserName() == 'safari') ? 'overflowX' : 'overflow';
82         t.eq( div.style[prop], overflow, "div.style.overflow set correctly");    
83         t.eq( parseFloat(div.style.opacity), opacity, "element.style.opacity set correctly");    
84         //Safari 3 returns null for this value, which is okay
85         var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : 'alpha(opacity=' + (opacity * 100) + ')';
86         t.eq( div.style.filter, filterString, "element.style.filter set correctly");
87
88         //test defaults
89         var div = OpenLayers.Util.createDiv();
90
91         if (!isMozilla)
92             t.ok( true, "skipping element test outside of Mozilla");
93         else
94             t.ok( div instanceof HTMLDivElement, "createDiv creates a valid HTMLDivElement" );
95         t.ok( (div.id != ""), "div.id set correctly");    
96         t.eq(div.style.left, "", "div.style.left set correctly");    
97         t.eq(div.style.top, "", "div.style.top set correctly");    
98
99         t.eq( div.style.width, "", "div.style.width set correctly");    
100         t.eq( div.style.height, "", "div.style.height set correctly");    
101
102         t.eq(div.style.backgroundImage, "", "div.style.backgroundImage correctly");    
103
104         t.eq( div.style.position, "absolute", "div.style.positionset correctly");    
105         //Safari 3 separates the border style into separate entities when reading it
106         if (OpenLayers.Util.getBrowserName() == 'safari') {
107           t.ok(div.style.borderTopWidth == '' && div.style.borderTopStyle == '', "good default popup.border")
108         } else {
109           t.eq( div.style.border, "", "div.style.border set correctly");    
110         }
111         //Safari 3 separates style overflow into overflow-x and overflow-y
112         var prop = (OpenLayers.Util.getBrowserName() == 'safari') ? 'overflowX' : 'overflow';
113         t.eq(div.style[prop], "", "div.style.overflow set correctly");    
114         t.ok( !div.style.opacity, "element.style.opacity set correctly");    
115         t.ok( !div.style.filter, "element.style.filter set correctly");
116
117     }
118
119     function test_Util_createImage(t) {
120         t.plan( 22 );
121
122         var img = "http://www.openlayers.org/images/OpenLayers.trac.png";
123         var sz = new OpenLayers.Size(10,10);
124         var xy = new OpenLayers.Pixel(5,5);
125         var position = "absolute";
126         var id = "boo";
127         var border = "1px solid";
128         var opacity = 0.5;
129
130         var image = OpenLayers.Util.createImage(id, xy, sz, img, position, border, opacity);
131
132         if (!isMozilla)
133             t.ok( true, "skipping element test outside of Mozilla");
134         else
135             t.ok( image.nodeName == "IMG", "createImage creates a valid HTMLImageElement" );
136         t.eq( image.id, id, "image.id set correctly");    
137         t.eq( image.style.left, xy.x + "px", "image.style.left set correctly");    
138         t.eq( image.style.top, xy.y + "px", "image.style.top set correctly");    
139
140         t.eq( image.style.width, sz.w + "px", "image.style.width set correctly");    
141         t.eq( image.style.height, sz.h + "px", "image.style.height set correctly");    
142
143         //Safari 3 separates the border style into separate entities when reading it
144         if (OpenLayers.Util.getBrowserName() == 'safari') {
145           var s = border.split(' ');
146           t.ok(image.style.borderTopWidth == s[0] && image.style.borderTopStyle == s[1], "good default popup.border")
147         } else {
148           t.ok( (image.style.border.indexOf(border) != -1), "image.style.border set correctly");
149         }
150         t.eq( image.src, img, "image.style.backgroundImage correctly");    
151         t.eq( image.style.position, position, "image.style.position set correctly");    
152         t.eq( parseFloat(image.style.opacity), opacity, "image.style.opacity set correctly");    
153         //Safari 3 returns null for this value, which is okay
154         var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : 'alpha(opacity=' + (opacity * 100) + ')';
155         t.eq( image.style.filter, filterString, "element.style.filter set correctly");
156
157         //test defaults
158         var image = OpenLayers.Util.createImage();
159
160         if (!isMozilla)
161             t.ok( true, "skipping element test outside of Mozilla");
162         else
163             t.ok( image.nodeName == "IMG", "createDiv creates a valid HTMLDivElement" );
164         t.ok( (image.id != ""), "image.id set to something");    
165         t.eq( image.style.left, "", "image.style.left set correctly");    
166         t.eq( image.style.top, "", "image.style.top set correctly");    
167
168         t.eq( image.style.width, "", "image.style.width set correctly");    
169         t.eq( image.style.height, "", "image.style.height set correctly");    
170
171         t.ok((image.style.border == ""), "image.style.border set correctly");    
172         t.eq(image.src, "", "image.style.backgroundImage correctly");    
173         t.eq( image.style.position, "relative", "image.style.positionset correctly");    
174         t.ok( !image.style.opacity, "element.style.opacity default unset");    
175         t.ok( !image.style.filter, "element.style.filter default unset");
176
177     }
178
179     function test_Util_applyDefaults(t) {
180     
181         t.plan(12);
182         
183         var to = { 
184             'a': "abra",
185             'b': "blorg",
186             'n': null
187         };
188
189         var from = { 
190             'b': "zoink",
191             'c': "press",
192             'toString': function() {return 'works'},
193             'n': "broken"
194         };
195
196         OpenLayers.Util.applyDefaults(to, from);
197
198         t.ok( to instanceof Object, " applyDefaults returns an object");
199         t.eq( to["a"], "abra", "key present in to but not from maintained");
200         t.eq( to["b"], "blorg", "key present in to and from, maintained in to");
201         t.eq( to["c"], "press", "key present in from and not to successfully copied to to");
202
203         var ret = OpenLayers.Util.applyDefaults({'a': "abra",'b': "blorg"}, from);
204         t.ok( ret instanceof Object, " applyDefaults returns an object");
205         t.eq( ret["a"], "abra", "key present in ret but not from maintained");
206         t.eq( ret["b"], "blorg", "key present in ret and from, maintained in ret");
207         t.eq( ret["c"], "press", "key present in from and not ret successfully copied to ret");
208         t.eq(to.toString(), "works", "correctly applies custom toString");
209         t.eq(to.n, null, "correctly preserves null");
210         
211         var to;
212         var from = {rand: Math.random()};
213         
214         var ret = OpenLayers.Util.applyDefaults(to, from);
215         t.eq(ret.rand, from.rand, "works with undefined to");
216
217         //regression test for #1716 -- allow undefined from
218         try {
219             OpenLayers.Util.applyDefaults({}, undefined);
220             t.ok(true, "no exception thrown when from is undefined");
221         } catch(err) {
222             t.fail("exception thrown when from is undefined:" + err);
223         }
224
225     }
226
227     function test_Util_getParameterString(t) {
228         t.plan( 4 );
229
230         var params = { 
231             'foo': "bar",
232             'chicken': 1.5
233         };
234
235         t.eq( OpenLayers.Util.getParameterString(params), "foo=bar&chicken=1.5", "getParameterString returns correctly");    
236         t.eq( OpenLayers.Util.getParameterString({'a:':'b='}), "a%3A=b%3D", "getParameterString returns correctly with non-ascii keys/values");    
237         
238
239         // Parameters which are a list should end up being a comma-seperated
240         // list of the URL encoded strings
241         var params = { foo: ["bar,baz"] };
242         t.eq( OpenLayers.Util.getParameterString(params), "foo=bar%2Cbaz", "getParameterString encodes , correctly in arrays");    
243         
244         var params = { foo: ["bar","baz,"] };
245         t.eq( OpenLayers.Util.getParameterString(params), "foo=bar,baz%2C", "getParameterString returns with list of CSVs when given a list. ");    
246     }
247
248     function test_Util_createAlphaImageDiv(t) {
249         t.plan( 19 );
250
251         var img = "http://www.openlayers.org/images/OpenLayers.trac.png";
252         var sz = new OpenLayers.Size(10,10);
253         var xy = new OpenLayers.Pixel(5,5);
254         var position = "absolute";
255         var id = "boo";
256         var border = "1px solid";
257         var sizing = "crop";
258         var opacity = 0.5;
259
260         var imageDiv = OpenLayers.Util.createAlphaImageDiv(id, xy, sz, img, position, border, sizing, opacity);
261
262         if (!isMozilla)
263             t.ok( true, "skipping element test outside of Mozilla");
264         else
265             t.ok( imageDiv instanceof HTMLDivElement, "createDiv creates a valid HTMLDivElement" );
266
267         t.eq( imageDiv.id, id, "image.id set correctly");    
268         t.eq( imageDiv.style.left, xy.x + "px", "image.style.left set correctly");    
269         t.eq( imageDiv.style.top, xy.y + "px", "image.style.top set correctly");    
270
271         t.eq( imageDiv.style.width, sz.w + "px", "image.style.width set correctly");    
272         t.eq( imageDiv.style.height, sz.h + "px", "image.style.height set correctly");    
273
274         t.eq( imageDiv.style.position, position, "image.style.positionset correctly");    
275         t.eq( parseFloat(imageDiv.style.opacity), opacity, "element.style.opacity set correctly");    
276         
277         var filterString;
278         if (OpenLayers.Util.alphaHack()) {
279             filterString = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://www.openlayers.org/images/OpenLayers.trac.png', sizingMethod='crop') alpha(opacity=50)";
280         } else {
281             //Safari 3 returns null for this value, which is okay
282             var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : 'alpha(opacity=' + (opacity * 100) + ')';
283         }        
284         t.eq( imageDiv.style.filter, filterString, "element.style.filter set correctly");
285
286
287         image = imageDiv.firstChild;
288         if (!isMozilla)
289             t.ok( true, "skipping element test outside of Mozilla");
290         else
291             t.ok( image.nodeName == "IMG", "createImage creates a valid HTMLImageElement" );
292         t.eq( image.id, id + "_innerImage", "image.id set correctly");    
293
294         t.eq( image.style.width, sz.w + "px", "image.style.width set correctly");    
295         t.eq( image.style.height, sz.h + "px", "image.style.height set correctly");    
296
297         //Safari 3 separates the border style into separate entities when reading it
298         if (OpenLayers.Util.getBrowserName() == 'safari') {
299           var s = border.split(' ');
300           t.ok(image.style.borderTopWidth == s[0] && image.style.borderTopStyle == s[1], "good default popup.border")
301         } else {
302           t.ok( (image.style.border.indexOf(border) != -1), "image.style.border set correctly");
303         }
304         
305         t.eq( image.style.position, "relative", "image.style.positionset correctly");    
306
307         if (OpenLayers.Util.alphaHack()) {
308         
309             t.eq(imageDiv.style.display, "inline-block", "imageDiv.style.display set correctly");
310
311             var filter = "progid:DXImageTransform.Microsoft" +
312                          ".AlphaImageLoader(src='" + img + "', " +
313                          "sizingMethod='" + sizing + "') alpha(opacity=50)";
314             t.eq(imageDiv.style.filter, filter, "div filter value correctly set");
315
316             filter = "alpha(opacity=0)";
317             t.eq(image.style.filter, filter, "image filter set correctly");
318
319         } else {
320             t.eq( image.src, img, "image.style.backgroundImage correctly");    
321             t.ok(true, "div filter value not set (not in IE)");
322             t.ok(true, "image filter value not set (not in IE)");
323         }
324
325         var imageDiv = OpenLayers.Util.createAlphaImageDiv(id, xy, sz, img, position, border);
326         if (OpenLayers.Util.alphaHack()) {
327             var filter = "progid:DXImageTransform.Microsoft" +
328                          ".AlphaImageLoader(src='" + img + "', " +
329                          "sizingMethod='scale')";
330             t.eq(imageDiv.style.filter, filter, "sizingMethod default correctly set to scale");
331         } else {
332             t.ok(true);
333         }        
334
335     }
336
337     function test_Util_modifyDOMElement_opacity(t) {
338         t.plan(8);
339
340         var opacity = 0.2;
341
342         var element = document.createElement("div");
343
344         OpenLayers.Util.modifyDOMElement(element, null, null, null, null, 
345                                          null, null, opacity);
346
347         t.eq(parseFloat(element.style.opacity), opacity, 
348              "element.style.opacity set correctly when opacity = " + opacity);
349         //Safari 3 returns null for this value, which is okay
350         var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : 'alpha(opacity=' + (opacity * 100) + ')';
351         t.eq(element.style.filter, filterString, 
352              "element.style.filter set correctly when opacity = " + opacity);
353
354         OpenLayers.Util.modifyDOMElement(element, null, null, null, null, 
355                                          null, null, "5");
356     
357         t.eq(parseFloat(element.style.opacity), opacity, 
358              "element.style.opacity not changed if the value is incorrect");
359         //Safari 3 returns null for this value, which is okay
360         var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : 'alpha(opacity=' + (opacity * 100) + ')';
361         t.eq(element.style.filter, filterString, 
362              "element.style.filter not changed if the value is incorrect");
363
364         OpenLayers.Util.modifyDOMElement(element, null, null, null, null, 
365                                          null, null, "hello");
366     
367         t.eq(parseFloat(element.style.opacity), opacity, 
368              "element.style.opacity not changed if the value is incorrect");
369         //Safari 3 returns null for this value, which is okay
370         var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : 'alpha(opacity=' + (opacity * 100) + ')';
371         t.eq(element.style.filter, filterString, 
372              "element.style.filter not changed if the value is incorrect");
373
374         opacity = 1.00;
375         OpenLayers.Util.modifyDOMElement(element, null, null, null, null, 
376                                          null, null, opacity);
377
378         t.eq(element.style.opacity, '', 
379              "element.style.opacity is removed when opacity = " + opacity);
380         //Safari 3 returns null for this value, which is okay
381         var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : '';
382         t.eq(element.style.filter, filterString, 
383              "element.style.filter is removed when opacity = " + opacity);
384     }
385
386     function test_Util_modifyDOMElement(t) {
387         t.plan( 10 );
388
389         var id = "boo";
390         var px = new OpenLayers.Pixel(5,5);
391         var sz = new OpenLayers.Size(10,10);
392         var position = "absolute";
393         var border = "1px solid";
394         var overflow = "hidden";
395         var opacity = 1/2;
396
397         var element = document.createElement("div");
398
399         OpenLayers.Util.modifyDOMElement(element, id, px, sz, position, 
400                                          border, overflow, opacity);
401
402         t.eq( element.id, id, "element.id set correctly");    
403         t.eq( element.style.left, px.x + "px", "element.style.left set correctly");    
404         t.eq( element.style.top, px.y + "px", "element.style.top set correctly");    
405
406         t.eq( element.style.width, sz.w + "px", "element.style.width set correctly");    
407         t.eq( element.style.height, sz.h + "px", "element.style.height set correctly");    
408
409         t.eq( element.style.position, position, "element.style.position set correctly");    
410         //Safari 3 separates the border style into separate entities when reading it
411         if (OpenLayers.Util.getBrowserName() == 'safari') {
412           var s = border.split(' ');
413           t.ok(element.style.borderTopWidth == s[0] && element.style.borderTopStyle == s[1], "good default popup.border")
414         } else {
415           t.ok( (element.style.border.indexOf(border) != -1), "element.style.border set correctly");
416         }
417         //Safari 3 separates style overflow into overflow-x and overflow-y
418         var prop = (OpenLayers.Util.getBrowserName() == 'safari') ? 'overflowX' : 'overflow';
419         t.eq( element.style[prop], overflow, "element.style.overflow set correctly");    
420         t.eq( parseFloat(element.style.opacity), opacity, "element.style.opacity set correctly");    
421         //Safari 3 returns null for this value, which is okay
422         var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : 'alpha(opacity=' + (opacity * 100) + ')';
423         t.eq( element.style.filter, filterString, "element.style.filter set correctly");
424     }
425
426     function test_Util_modifyAlphaImageDiv(t) {
427         t.plan( 21 );
428
429         var imageDiv = OpenLayers.Util.createAlphaImageDiv();
430
431         var img = "http://www.openlayers.org/images/OpenLayers.trac.png";
432         var sz = new OpenLayers.Size(10,10);
433         var xy = new OpenLayers.Pixel(5,5);
434         var position = "absolute";
435         var id = "boo";
436         var border = "1px solid";
437         var sizing = "crop";
438         var opacity = 0.5;
439
440         OpenLayers.Util.modifyAlphaImageDiv(imageDiv, id, xy, sz, img, position, border, sizing, opacity);
441         if (OpenLayers.Util.alphaHack())
442             t.ok( true, "skipping element test outside of Mozilla");
443         else
444             t.ok( imageDiv.nodeName == "DIV", "createDiv creates a valid HTMLDivElement" );
445
446         t.eq( imageDiv.id, id, "image.id set correctly");    
447         t.eq( imageDiv.style.left, xy.x + "px", "image.style.left set correctly");    
448         t.eq( imageDiv.style.top, xy.y + "px", "image.style.top set correctly");    
449
450         t.eq( imageDiv.style.width, sz.w + "px", "image.style.width set correctly");    
451         t.eq( imageDiv.style.height, sz.h + "px", "image.style.height set correctly");    
452
453         t.eq( imageDiv.style.position, position, "image.style.position set correctly");    
454         t.eq( parseFloat(imageDiv.style.opacity), opacity, "element.style.opacity set correctly");    
455
456         
457
458         image = imageDiv.firstChild;
459
460         var filterString;
461         if (OpenLayers.Util.alphaHack()) {
462             filterString = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://www.openlayers.org/images/OpenLayers.trac.png', sizingMethod='crop') alpha(opacity=50)";
463             t.ok( true, "skipping element test outside of Mozilla");
464         } else {
465             //Safari 3 returns null for this value, which is okay
466             var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : 'alpha(opacity=' + (opacity * 100) + ')';
467             t.ok( image.nodeName == "IMG", "createImage creates a valid HTMLImageElement" );
468         }
469         t.eq( imageDiv.style.filter, filterString, "element.style.filter set correctly");
470         t.eq( image.id, id + "_innerImage", "image.id set correctly");    
471
472         t.eq( image.style.width, sz.w + "px", "image.style.width set correctly");    
473         t.eq( image.style.height, sz.h + "px", "image.style.height set correctly");    
474
475         //Safari 3 separates the border style into separate entities when reading it
476         if (OpenLayers.Util.getBrowserName() == 'safari') {
477           var s = border.split(' ');
478           t.ok(image.style.borderTopWidth == s[0] && image.style.borderTopStyle == s[1], "good default popup.border")
479         } else {
480           t.ok( (image.style.border.indexOf(border) != -1), "image.style.border set correctly");
481         }
482
483         t.eq( image.style.position, "relative", "image.style.positionset correctly");    
484         t.eq( image.src, img, "image.style.backgroundImage correctly");
485         
486         if (OpenLayers.Util.alphaHack()) {
487         
488             var filter = "progid:DXImageTransform.Microsoft" +
489                          ".AlphaImageLoader(src='" + img + "', " +
490                          "sizingMethod='" + sizing + "') alpha(opacity=" + opacity *100 + ")";
491             t.eq(imageDiv.style.filter, filter, "div filter value correctly set");
492
493             filter = "alpha(opacity=0)";
494             t.eq(image.style.filter, filter, "image filter set correctly");
495
496         } else {  
497             t.ok(true, "div filter value not set (not in IE)");
498             t.ok(true, "image filter value not set (not in IE)");
499         }
500
501         var imageDiv = OpenLayers.Util.createAlphaImageDiv();
502         var display = "none";
503         imageDiv.style.display = display;
504         OpenLayers.Util.modifyAlphaImageDiv(imageDiv, id, xy, sz, img, position, border, sizing, opacity);
505         t.eq(imageDiv.style.display, display, "imageDiv.style.display set correctly, if 'none'");
506
507         var imageDiv = OpenLayers.Util.createAlphaImageDiv();
508         var display = "block";
509         imageDiv.style.display = display;
510         OpenLayers.Util.modifyAlphaImageDiv(imageDiv, id, xy, sz, img, position, border, sizing, opacity);
511         if(OpenLayers.Util.alphaHack()) {
512             t.eq(imageDiv.style.display, "inline-block", "imageDiv.style.display set correctly, if not 'none'");
513         } else {
514             t.ok(true, "inline-block is not part of CSS2 and is not supported by Firefox 2");
515         }
516
517         
518
519         var imageDiv = OpenLayers.Util.createAlphaImageDiv(id, xy, sz, img, position, border, "scale", opacity);
520         if (OpenLayers.Util.alphaHack()) {
521             var filter = "progid:DXImageTransform.Microsoft" +
522                          ".AlphaImageLoader(src='" + img + "', " +
523                          "sizingMethod='scale') alpha(opacity=" + opacity *100 + ")";
524             t.eq(imageDiv.style.filter, filter, "sizingMethod default correctly set to scale");
525         } else {
526             t.ok(true);
527         }        
528    
529     }
530        
531     function test_Util_upperCaseObject(t) {
532         t.plan(8);
533         
534         var aKey = "chicken";
535         var aValue = "pot pie";
536
537         var bKey = "blorg";
538         var bValue = "us maximus";
539         
540         var obj = {};
541         obj[aKey] = aValue;        
542         obj[bKey] = bValue;        
543              
544         var uObj = OpenLayers.Util.upperCaseObject(obj);          
545
546         //make sure old object not modified
547         t.eq(obj[aKey], aValue, "old lowercase value still present in old obj");
548         t.eq(obj[bKey], bValue, "old lowercase value still present in old obj");
549
550         t.eq(obj[aKey.toUpperCase()], null, "new uppercase value not present in old obj");
551         t.eq(obj[bKey.toUpperCase()], null, "new uppercase value not present in old obj");
552
553         //make sure new object modified
554         t.eq(uObj[aKey], null, "old lowercase value not present");
555         t.eq(uObj[bKey], null, "old lowercase value not present");
556
557         t.eq(uObj[aKey.toUpperCase()], aValue, "new uppercase value present");
558         t.eq(uObj[bKey.toUpperCase()], bValue, "new uppercase value present");
559     }
560     
561     function test_Util_createUniqueID(t) {
562         t.plan(2);
563         
564         var id = OpenLayers.Util.createUniqueID();
565         t.ok(OpenLayers.String.startsWith(id, "id_"),
566              "default OpenLayers.Util.createUniqueID starts id correctly");
567
568         var id = OpenLayers.Util.createUniqueID("chicken");
569         t.ok(OpenLayers.String.startsWith(id, "chicken"),
570              "OpenLayers.Util.createUniqueID starts id correctly");
571     }
572         
573     function test_Util_normalizeScale(t) {
574         t.plan(2); 
575         
576         //normal scale
577         var scale = 1/5;
578         t.eq( OpenLayers.Util.normalizeScale(scale), scale, "normalizing a normal scale does nothing");
579
580         //funky scale
581         var scale = 5;
582         t.eq( OpenLayers.Util.normalizeScale(scale), 1/5, "normalizing a wrong scale works!");
583     }
584     
585     function test_Util_getScaleResolutionTranslation(t) {
586         t.plan(4); 
587         
588         var scale = 1/150000000; 
589         var resolution = OpenLayers.Util.getResolutionFromScale(scale);
590         t.eq(resolution.toFixed(6), "0.476217", "Calculated correct resolution for " + scale);
591
592         var scale = 1/150000000; 
593         var resolution = OpenLayers.Util.getResolutionFromScale(scale, 'm');
594         t.eq(resolution.toFixed(6), "52916.638092", "Calculated correct resolution for " + scale);
595
596         scale = 150000000; 
597         resolution = OpenLayers.Util.getResolutionFromScale(scale);
598         t.eq(resolution.toFixed(6), "0.476217", "Calculated correct resolution for " + scale);
599
600         scale = 150000000; 
601         resolution = OpenLayers.Util.getResolutionFromScale(scale);
602         t.eq(OpenLayers.Util.getScaleFromResolution(resolution), scale, "scale->resolution->scale works");
603     }
604     
605     function test_Util_getImgLocation(t) {
606         t.plan(3);
607
608         OpenLayers.ImgPath = "foo/";
609         t.eq(OpenLayers.Util.getImagesLocation(), "foo/", "ImgPath works as expected."); 
610         OpenLayers.ImgPath = null;
611         t.eq(OpenLayers.Util.getImagesLocation().substr(OpenLayers.Util.getImagesLocation().length-4,4), "img/", "ImgPath works as expected when not set."); 
612
613         OpenLayers.ImgPath = '';
614         t.eq(OpenLayers.Util.getImagesLocation().substr(OpenLayers.Util.getImagesLocation().length-4,4), "img/", "ImgPath works as expected when set to ''."); 
615     }
616
617     function test_Util_isEquivalentUrl(t) {
618         t.plan(9);
619         
620         var url1, url2, options;
621
622   //CASE
623
624         url1 = "http://www.openlayers.org";
625         url2 = "HTTP://WWW.OPENLAYERS.ORG";
626
627         t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreCase works"); 
628
629   //ARGS
630
631         url1 = "http://www.openlayers.org?foo=5;bar=6";
632         url2 = "http://www.openlayers.org?bar=6;foo=5";
633
634         t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "shuffled arguments works"); 
635
636   //PORT
637
638         url1 = "http://www.openlayers.org:80";
639         url2 = "http://www.openlayers.org";
640
641         t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignorePort80 works"); 
642
643         options = {
644             'ignorePort80': false
645         }        
646         url1 = "http://www.openlayers.org:80";
647         url2 = "http://www.openlayers.org:50";
648
649         t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2, options), "port check works"); 
650
651
652   //HASH
653
654         url1 = "http://www.openlayers.org#barf";
655         url2 = "http://www.openlayers.org";
656
657         t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreHash works"); 
658         options = {
659             'ignoreHash': false
660         }        
661         t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2, options), "ignoreHash FALSE works"); 
662
663   //PROTOCOL
664
665         url1 = "http://www.openlayers.org";
666         url2 = "ftp://www.openlayers.org";
667
668         t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreHash works"); 
669
670
671   //PATHNAME
672         url1 = "foo.html?bar=now#go";
673         url2 = "../tests/../tests/foo.html?bar=now#go";
674
675         t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "relative vs. absolute paths works");
676         
677         url1 = "/foo/bar";
678         url2 = new Array(window.location.pathname.split("/").length-1).join("../")+"foo/bar";
679         
680         t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "absolute and relative path without host works for "+url2) 
681     }
682     
683     function test_createUrlObject(t) {
684         
685         var cases = [{
686             url: "http://example.com/",
687             exp: {
688                 protocol: "http:",
689                 host: "example.com",
690                 port: "80",
691                 pathname: "/",
692                 args: {},
693                 hash: ""
694             }
695         }, {
696             url: "http://example.com:80/",
697             opt: {ignorePort80: true},
698             exp: {
699                 protocol: "http:",
700                 host: "example.com",
701                 port: "",
702                 pathname: "/",
703                 args: {},
704                 hash: ""
705             }
706         }, {
707             url: "http://example.com/",
708             opt: {ignorePort80: true},
709             exp: {
710                 protocol: "http:",
711                 host: "example.com",
712                 port: "",
713                 pathname: "/",
714                 args: {},
715                 hash: ""
716             }
717         }, {
718             url: "http://example.com:88/",
719             exp: {
720                 protocol: "http:",
721                 host: "example.com",
722                 port: "88",
723                 pathname: "/",
724                 args: {},
725                 hash: ""
726             }
727         }, {
728             url: "http://example.com:88/foo#bar",
729             exp: {
730                 protocol: "http:",
731                 host: "example.com",
732                 port: "88",
733                 pathname: "/foo",
734                 args: {},
735                 hash: "#bar"
736             }
737         }, {
738             url: "http://example.com:88/?foo=bar",
739             exp: {
740                 protocol: "http:",
741                 host: "example.com",
742                 port: "88",
743                 pathname: "/",
744                 args: {foo: "bar"},
745                 hash: ""
746             }
747         }, {
748             url: "http://example.com/bogus/../bogus/../path",
749             exp: {
750                 protocol: "http:",
751                 host: "example.com",
752                 port: "80",
753                 pathname: "/path",
754                 args: {},
755                 hash: ""
756             }
757         }, {
758             url: "/relative#foo",
759             exp: {
760                 protocol: window.location.protocol,
761                 host: window.location.hostname,
762                 port: window.location.port || "80",
763                 pathname: "/relative",
764                 args: {},
765                 hash: "#foo"
766             }
767         }, {
768             url: "../foo",
769             exp: {
770                 protocol: window.location.protocol,
771                 host: window.location.hostname,
772                 port: window.location.port || "80",
773                 pathname: (function() {
774                     var parts = window.location.pathname.split("/");
775                     return parts.slice(0, parts.length -2).join("/") + "/foo";
776                 })(),
777                 args: {},
778                 hash: ""
779             }
780         }];
781         
782         t.plan(cases.length);
783
784         var c, obj;
785         for(var i=0; i<cases.length; ++i) {
786             c = cases[i];
787             obj = OpenLayers.Util.createUrlObject(c.url, c.opt);
788             t.eq(obj, c.exp, i + ": '" + c.url + "'");
789         }
790         
791     }
792     
793     function test_Util_createUniqueIDSeq(t) {
794         t.plan(1);
795
796         OpenLayers.Util.lastSeqID = 0;
797         OpenLayers.Util.createDiv();
798         OpenLayers.Util.createDiv();
799         t.eq(OpenLayers.Util.createDiv().id, "OpenLayersDiv3", "Div created is sequential, starting at lastSeqID in Util.");
800     }
801
802     function test_Util_getParameters(t) {
803         t.plan(6);
804
805         t.eq(OpenLayers.Util.getParameters('http://www.example.com'), {},
806              "getParameters works when args = ''");
807         t.eq(OpenLayers.Util.getParameters('http://www.example.com?'), {},
808              "getParameters works when args = '?'");
809         t.eq(OpenLayers.Util.getParameters('http://www.example.com?hello=world&foo=bar'),
810              {'hello' : 'world', 'foo': 'bar'},
811              "getParameters works when args = '?hello=world&foo=bar'");
812         t.eq(OpenLayers.Util.getParameters('http://www.example.com?hello=&foo=bar'),
813              {'hello' : '', 'foo': 'bar'},
814              "getParameters works when args = '?hello=&foo=bar'");
815         t.eq(OpenLayers.Util.getParameters('http://www.example.com?foo=bar#bugssucks'),
816              {'foo': 'bar'},
817              "getParameters works when using a fragment identifier");
818         t.eq(OpenLayers.Util.getParameters('http://www.example.com?foo=bar,pub,disco'),
819              {'foo': ['bar', 'pub', 'disco']},
820              "getParameters works with a comma-separated value (parses into array)");
821     }
822
823     function test_Util_getArgs(t) {
824         //DEPRECATED -- to be removed in 3.0
825         t.plan(3);
826         
827         OpenLayers.Lang.setCode(OpenLayers.Lang.defaultCode);
828         
829         var temp = OpenLayers.Console.warn;
830         OpenLayers.Console.warn = function(err) {
831             t.ok(err != null, "warning is fired on use of getArgs()");
832         }
833
834         var temp2 = OpenLayers.Util.getParameters;
835         OpenLayers.Util.getParameters = function(url) {
836             t.eq(url, g_Url, "correct url passed to getParameters()");
837             return g_Params;
838         }
839         
840         g_Params = {};
841         g_Url = {};
842         
843         var ret = OpenLayers.Util.getArgs(g_Url);
844         t.ok( ret == g_Params, "correctly returns value from getParameters");
845
846         OpenLayers.Console.warn = temp;
847         OpenLayers.Util.getParameters = temp2;
848     }
849
850     function tests_Util_extend(t) {
851         t.plan(7);
852
853         var source = {
854             num: Math.random(),
855             obj: {
856                 foo: "bar"
857             },
858             method: function() {
859                 return "method";
860             },
861             toString: function() {
862                 return "source";
863             },
864             nada: undefined
865         };
866         var destination = OpenLayers.Util.extend({nada: "untouched"}, source);
867         t.eq(destination.num, source.num,
868              "extend properly sets primitive property on destination");
869         t.eq(destination.obj, source.obj,
870              "extend properly sets object property on destination");
871         t.eq(destination.method(), "method",
872              "extend properly sets function property on destination");
873         t.eq(destination.toString(), "source",
874              "extend properly sets custom toString method");
875         t.eq(destination.nada, "untouched",
876              "undefined source properties don't clobber existing properties");
877         t.eq(window.property, undefined, "Property variable not clobbered.");
878         
879         var destination;
880         var source = {rand: Math.random()};
881         var ret = OpenLayers.Util.extend(destination, source);
882         t.eq(destination.rand, source.rand, "works with undefined destination");
883         
884     }
885     
886     function test_XX_Util_Try(t) {
887         t.plan(7);
888
889         var func1 = function() {
890             t.ok(true, "func1 executed");
891             throw "error";
892         };
893         
894         var func2 = function() {
895             t.ok(true, "func2 executed");
896             throw "error";
897         };
898
899         g_TestVal3 = {};
900         var func3 = function() {
901             t.ok(true, "func3 executed");
902             return g_TestVal3;            
903         };
904
905         g_TestVal4 = {};
906         var func4 = function() {
907             t.fail("func4 should *not* be executed");
908             return g_TestVal4;            
909         };
910
911         var ret = OpenLayers.Util.Try(func1, func2);
912         t.ok(ret == null, "if all functions throw exceptions, null returned");
913
914         var ret = OpenLayers.Util.Try(func1, func2, func3, func4);
915         t.ok(ret == g_TestVal3, "try returns first sucessfully executed function's return");
916
917     }
918     
919     function test_getRenderedDimensions(t) {
920         t.plan(2);
921         var content = (new Array(100)).join("foo ");
922         
923         // test with fixed width
924         var fw = OpenLayers.Util.getRenderedDimensions(content, {w: 20});
925         t.eq(fw.w, 20, "got the fixed width");
926         
927         // test with fixed height
928         var fh = OpenLayers.Util.getRenderedDimensions(content, {h: 15});
929         t.eq(fh.h, 15, "got the fixed height");
930         
931     }
932
933     function test_toFloat(t) {
934         t.plan(2);
935         // actual possible computed Mercator tile coordinates, more or less
936         var a1=40075016.67999999, b1=-20037508.33999999,
937             a2=40075016.68, b2=-20037508.34;
938         t.eq(OpenLayers.Util.toFloat(a1), OpenLayers.Util.toFloat(a2),
939             "toFloat rounds large floats correctly #1");
940         t.eq(OpenLayers.Util.toFloat(b1), OpenLayers.Util.toFloat(b2),
941             "toFloat rounds large floats correctly #2");
942     }
943   </script>
944 </head>
945 <body>
946     <div id="map" style="width: 1024px; height: 512px;"/>
947 </body>
948 </html>