4 var custom$ = function() {};
7 <script src="../lib/OpenLayers.js"></script>
8 <script type="text/javascript">
9 var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
14 t.ok($ === custom$, "OpenLayers doesn't clobber existing definition of $.");
17 function test_Util_getImagesLocation (t) {
19 t.ok( OpenLayers.Util.getImagesLocation(), "../img/",
20 "getImagesLocation()" );
23 function test_Util_Array(t) {
26 var array = new Array(1,2,3,4,4,5);
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 ");
34 function test_Util_pagePosition(t) {
36 var pp = OpenLayers.Util.pagePosition(window);
37 t.eq( pp.toString(), "0,0", "Page position doesn't bail if passed 'window'")
41 function test_Util_createDiv(t) {
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";
53 var div = OpenLayers.Util.createDiv(id, px, sz, img, position, border, overflow, opacity);
56 t.ok( true, "skipping element test outside of Mozilla");
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");
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");
66 bImg = div.style.backgroundImage;
67 imgCorrect = ( (bImg == "url(" + img + ")") ||
68 (bImg == "url(\"" + img + "\")") );
69 t.ok(imgCorrect, "div.style.backgroundImage correctly");
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")
77 t.ok( (div.style.border.indexOf(border) != -1), "div.style.border set correctly");
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");
89 var div = OpenLayers.Util.createDiv();
92 t.ok( true, "skipping element test outside of Mozilla");
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");
99 t.eq( div.style.width, "", "div.style.width set correctly");
100 t.eq( div.style.height, "", "div.style.height set correctly");
102 t.eq(div.style.backgroundImage, "", "div.style.backgroundImage correctly");
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")
109 t.eq( div.style.border, "", "div.style.border set correctly");
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");
119 function test_Util_createImage(t) {
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";
127 var border = "1px solid";
130 var image = OpenLayers.Util.createImage(id, xy, sz, img, position, border, opacity);
133 t.ok( true, "skipping element test outside of Mozilla");
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");
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");
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")
148 t.ok( (image.style.border.indexOf(border) != -1), "image.style.border set correctly");
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");
158 var image = OpenLayers.Util.createImage();
161 t.ok( true, "skipping element test outside of Mozilla");
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");
168 t.eq( image.style.width, "", "image.style.width set correctly");
169 t.eq( image.style.height, "", "image.style.height set correctly");
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");
179 function test_Util_applyDefaults(t) {
192 'toString': function() {return 'works'},
196 OpenLayers.Util.applyDefaults(to, from);
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");
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");
212 var from = {rand: Math.random()};
214 var ret = OpenLayers.Util.applyDefaults(to, from);
215 t.eq(ret.rand, from.rand, "works with undefined to");
217 //regression test for #1716 -- allow undefined from
219 OpenLayers.Util.applyDefaults({}, undefined);
220 t.ok(true, "no exception thrown when from is undefined");
222 t.fail("exception thrown when from is undefined:" + err);
227 function test_Util_getParameterString(t) {
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");
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");
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. ");
248 function test_Util_createAlphaImageDiv(t) {
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";
256 var border = "1px solid";
260 var imageDiv = OpenLayers.Util.createAlphaImageDiv(id, xy, sz, img, position, border, sizing, opacity);
263 t.ok( true, "skipping element test outside of Mozilla");
265 t.ok( imageDiv instanceof HTMLDivElement, "createDiv creates a valid HTMLDivElement" );
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");
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");
274 t.eq( imageDiv.style.position, position, "image.style.positionset correctly");
275 t.eq( parseFloat(imageDiv.style.opacity), opacity, "element.style.opacity set correctly");
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)";
281 //Safari 3 returns null for this value, which is okay
282 var filterString = (OpenLayers.Util.getBrowserName() == 'safari') ? null : 'alpha(opacity=' + (opacity * 100) + ')';
284 t.eq( imageDiv.style.filter, filterString, "element.style.filter set correctly");
287 image = imageDiv.firstChild;
289 t.ok( true, "skipping element test outside of Mozilla");
291 t.ok( image.nodeName == "IMG", "createImage creates a valid HTMLImageElement" );
292 t.eq( image.id, id + "_innerImage", "image.id set correctly");
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");
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")
302 t.ok( (image.style.border.indexOf(border) != -1), "image.style.border set correctly");
305 t.eq( image.style.position, "relative", "image.style.positionset correctly");
307 if (OpenLayers.Util.alphaHack()) {
309 t.eq(imageDiv.style.display, "inline-block", "imageDiv.style.display set correctly");
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");
316 filter = "alpha(opacity=0)";
317 t.eq(image.style.filter, filter, "image filter set correctly");
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)");
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");
337 function test_Util_modifyDOMElement_opacity(t) {
342 var element = document.createElement("div");
344 OpenLayers.Util.modifyDOMElement(element, null, null, null, null,
345 null, null, opacity);
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);
354 OpenLayers.Util.modifyDOMElement(element, null, null, null, null,
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");
364 OpenLayers.Util.modifyDOMElement(element, null, null, null, null,
365 null, null, "hello");
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");
375 OpenLayers.Util.modifyDOMElement(element, null, null, null, null,
376 null, null, opacity);
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);
386 function test_Util_modifyDOMElement(t) {
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";
397 var element = document.createElement("div");
399 OpenLayers.Util.modifyDOMElement(element, id, px, sz, position,
400 border, overflow, opacity);
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");
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");
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")
415 t.ok( (element.style.border.indexOf(border) != -1), "element.style.border set correctly");
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");
426 function test_Util_modifyAlphaImageDiv(t) {
429 var imageDiv = OpenLayers.Util.createAlphaImageDiv();
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";
436 var border = "1px solid";
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");
444 t.ok( imageDiv.nodeName == "DIV", "createDiv creates a valid HTMLDivElement" );
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");
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");
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");
458 image = imageDiv.firstChild;
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");
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" );
469 t.eq( imageDiv.style.filter, filterString, "element.style.filter set correctly");
470 t.eq( image.id, id + "_innerImage", "image.id set correctly");
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");
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")
480 t.ok( (image.style.border.indexOf(border) != -1), "image.style.border set correctly");
483 t.eq( image.style.position, "relative", "image.style.positionset correctly");
484 t.eq( image.src, img, "image.style.backgroundImage correctly");
486 if (OpenLayers.Util.alphaHack()) {
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");
493 filter = "alpha(opacity=0)";
494 t.eq(image.style.filter, filter, "image filter set correctly");
497 t.ok(true, "div filter value not set (not in IE)");
498 t.ok(true, "image filter value not set (not in IE)");
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'");
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'");
514 t.ok(true, "inline-block is not part of CSS2 and is not supported by Firefox 2");
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");
531 function test_Util_upperCaseObject(t) {
534 var aKey = "chicken";
535 var aValue = "pot pie";
538 var bValue = "us maximus";
544 var uObj = OpenLayers.Util.upperCaseObject(obj);
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");
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");
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");
557 t.eq(uObj[aKey.toUpperCase()], aValue, "new uppercase value present");
558 t.eq(uObj[bKey.toUpperCase()], bValue, "new uppercase value present");
561 function test_Util_createUniqueID(t) {
564 var id = OpenLayers.Util.createUniqueID();
565 t.ok(OpenLayers.String.startsWith(id, "id_"),
566 "default OpenLayers.Util.createUniqueID starts id correctly");
568 var id = OpenLayers.Util.createUniqueID("chicken");
569 t.ok(OpenLayers.String.startsWith(id, "chicken"),
570 "OpenLayers.Util.createUniqueID starts id correctly");
573 function test_Util_normalizeScale(t) {
578 t.eq( OpenLayers.Util.normalizeScale(scale), scale, "normalizing a normal scale does nothing");
582 t.eq( OpenLayers.Util.normalizeScale(scale), 1/5, "normalizing a wrong scale works!");
585 function test_Util_getScaleResolutionTranslation(t) {
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);
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);
597 resolution = OpenLayers.Util.getResolutionFromScale(scale);
598 t.eq(resolution.toFixed(6), "0.476217", "Calculated correct resolution for " + scale);
601 resolution = OpenLayers.Util.getResolutionFromScale(scale);
602 t.eq(OpenLayers.Util.getScaleFromResolution(resolution), scale, "scale->resolution->scale works");
605 function test_Util_getImgLocation(t) {
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.");
613 OpenLayers.ImgPath = '';
614 t.eq(OpenLayers.Util.getImagesLocation().substr(OpenLayers.Util.getImagesLocation().length-4,4), "img/", "ImgPath works as expected when set to ''.");
617 function test_Util_isEquivalentUrl(t) {
620 var url1, url2, options;
624 url1 = "http://www.openlayers.org";
625 url2 = "HTTP://WWW.OPENLAYERS.ORG";
627 t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreCase works");
631 url1 = "http://www.openlayers.org?foo=5;bar=6";
632 url2 = "http://www.openlayers.org?bar=6;foo=5";
634 t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "shuffled arguments works");
638 url1 = "http://www.openlayers.org:80";
639 url2 = "http://www.openlayers.org";
641 t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignorePort80 works");
644 'ignorePort80': false
646 url1 = "http://www.openlayers.org:80";
647 url2 = "http://www.openlayers.org:50";
649 t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2, options), "port check works");
654 url1 = "http://www.openlayers.org#barf";
655 url2 = "http://www.openlayers.org";
657 t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreHash works");
661 t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2, options), "ignoreHash FALSE works");
665 url1 = "http://www.openlayers.org";
666 url2 = "ftp://www.openlayers.org";
668 t.ok(!OpenLayers.Util.isEquivalentUrl(url1, url2), "default ignoreHash works");
672 url1 = "foo.html?bar=now#go";
673 url2 = "../tests/../tests/foo.html?bar=now#go";
675 t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "relative vs. absolute paths works");
678 url2 = new Array(window.location.pathname.split("/").length-1).join("../")+"foo/bar";
680 t.ok(OpenLayers.Util.isEquivalentUrl(url1, url2), "absolute and relative path without host works for "+url2)
683 function test_createUrlObject(t) {
686 url: "http://example.com/",
696 url: "http://example.com:80/",
697 opt: {ignorePort80: true},
707 url: "http://example.com/",
708 opt: {ignorePort80: true},
718 url: "http://example.com:88/",
728 url: "http://example.com:88/foo#bar",
738 url: "http://example.com:88/?foo=bar",
748 url: "http://example.com/bogus/../bogus/../path",
758 url: "/relative#foo",
760 protocol: window.location.protocol,
761 host: window.location.hostname,
762 port: window.location.port || "80",
763 pathname: "/relative",
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";
782 t.plan(cases.length);
785 for(var i=0; i<cases.length; ++i) {
787 obj = OpenLayers.Util.createUrlObject(c.url, c.opt);
788 t.eq(obj, c.exp, i + ": '" + c.url + "'");
793 function test_Util_createUniqueIDSeq(t) {
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.");
802 function test_Util_getParameters(t) {
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'),
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)");
823 function test_Util_getArgs(t) {
824 //DEPRECATED -- to be removed in 3.0
827 OpenLayers.Lang.setCode(OpenLayers.Lang.defaultCode);
829 var temp = OpenLayers.Console.warn;
830 OpenLayers.Console.warn = function(err) {
831 t.ok(err != null, "warning is fired on use of getArgs()");
834 var temp2 = OpenLayers.Util.getParameters;
835 OpenLayers.Util.getParameters = function(url) {
836 t.eq(url, g_Url, "correct url passed to getParameters()");
843 var ret = OpenLayers.Util.getArgs(g_Url);
844 t.ok( ret == g_Params, "correctly returns value from getParameters");
846 OpenLayers.Console.warn = temp;
847 OpenLayers.Util.getParameters = temp2;
850 function tests_Util_extend(t) {
861 toString: function() {
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.");
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");
886 function test_XX_Util_Try(t) {
889 var func1 = function() {
890 t.ok(true, "func1 executed");
894 var func2 = function() {
895 t.ok(true, "func2 executed");
900 var func3 = function() {
901 t.ok(true, "func3 executed");
906 var func4 = function() {
907 t.fail("func4 should *not* be executed");
911 var ret = OpenLayers.Util.Try(func1, func2);
912 t.ok(ret == null, "if all functions throw exceptions, null returned");
914 var ret = OpenLayers.Util.Try(func1, func2, func3, func4);
915 t.ok(ret == g_TestVal3, "try returns first sucessfully executed function's return");
919 function test_getRenderedDimensions(t) {
921 var content = (new Array(100)).join("foo ");
923 // test with fixed width
924 var fw = OpenLayers.Util.getRenderedDimensions(content, {w: 20});
925 t.eq(fw.w, 20, "got the fixed width");
927 // test with fixed height
928 var fh = OpenLayers.Util.getRenderedDimensions(content, {h: 15});
929 t.eq(fh.h, 15, "got the fixed height");
933 function test_toFloat(t) {
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");
946 <div id="map" style="width: 1024px; height: 512px;"/>