-
Notifications
You must be signed in to change notification settings - Fork 38
/
Map.html
1893 lines (1568 loc) · 71.9 KB
/
Map.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html>
<head>
<script src="OLLoader.js"></script>
<script type="text/javascript">
var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
var map;
function test_Map_constructor (t) {
t.plan( 11 );
map = new OpenLayers.Map('map');
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayer(baseLayer);
t.ok( OpenLayers.Element.hasClass(map.div, "olMap"), "Map div has olMap class");
t.ok( map instanceof OpenLayers.Map, "new OpenLayers.Map returns object" );
if (!isMozilla) {
t.ok( true, "skipping element test outside of Mozilla");
t.ok( true, "skipping element test outside of Mozilla");
t.ok( true, "skipping element test outside of Mozilla");
} else {
t.ok( map.div instanceof HTMLDivElement, "map.div is an HTMLDivElement" );
t.ok( map.viewPortDiv instanceof HTMLDivElement, "map.viewPortDiv is an HTMLDivElement" );
t.ok( map.layerContainerDiv instanceof HTMLDivElement, "map.layerContainerDiv is an HTMLDivElement" );
}
t.ok( map.layers instanceof Array, "map.layers is an Array" );
t.ok( map.controls instanceof Array, "map.controls is an Array" );
t.eq( map.controls.length, 4, "Default map has 4 controls." );
t.ok( map.events instanceof OpenLayers.Events, "map.events is an OpenLayers.Events" );
t.ok( map.getMaxExtent() instanceof OpenLayers.Bounds, "map.maxExtent is an OpenLayers.Bounds" );
t.ok( map.getNumZoomLevels() > 0, "map has a default numZoomLevels" );
map.destroy();
}
function test_Map_constructor_late_rendering(t) {
t.plan( 4 );
map = new OpenLayers.Map();
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayer(baseLayer);
t.ok(map.div != null, "Map has a div even though none was specified.");
t.ok(map.viewPortDiv.parentNode == map.div, "Map is attached to a temporary div that holds the viewPortDiv.");
var mapDiv = document.getElementById("map");
// clean up the effects of other tests
while(OpenLayers.Element.hasClass(mapDiv, "olMap")) {
OpenLayers.Element.removeClass(mapDiv, "olMap");
}
map.render(mapDiv); // Can also take a string.
t.ok(map.div == mapDiv, "Map is now rendered to the 'map' div.")
t.ok( OpenLayers.Element.hasClass(map.div, "olMap"), "Map div has olMap class");
map.destroy();
}
function test_Map_constructor_renderTo(t) {
t.plan( 1 );
map = new OpenLayers.Map({
div: "map"
});
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayer(baseLayer);
var mapDiv = document.getElementById("map");
t.ok(map.div == mapDiv, "Map is rendered to the 'map' div.")
map.destroy();
}
function test_Map_setOptions(t) {
t.plan(2);
map = new OpenLayers.Map('map', {maxExtent: new OpenLayers.Bounds(100, 200, 300, 400)});
map.setOptions({theme: 'foo'});
t.eq(map.theme, 'foo', "theme is correctly set by setOptions");
t.ok(map.maxExtent.equals(new OpenLayers.Bounds(100, 200, 300, 400)),
"maxExtent is correct after calling setOptions");
map.destroy();
}
function test_Map_add_layers(t) {
t.plan(8);
map = new OpenLayers.Map('map');
var layer1 = new OpenLayers.Layer.WMS("Layer 1",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
var layer2 = new OpenLayers.Layer.WMS("Layer 2",
"http://wms.jpl.nasa.gov/wms.cgi", {layers: "modis,global_mosaic"});
// this uses map.addLayer internally
map.addLayers([layer1, layer2])
t.eq( map.layers.length, 2, "map has exactly two layers" );
t.ok( map.layers[0] === layer1, "1st layer is layer1" );
t.ok( map.layers[1] === layer2, "2nd layer is layer2" );
t.ok( layer1.map === map, "layer.map is map" );
t.eq( parseInt(layer1.div.style.zIndex), map.Z_INDEX_BASE['BaseLayer'],
"layer1 zIndex is set" );
t.eq( parseInt(layer2.div.style.zIndex), map.Z_INDEX_BASE['BaseLayer'] + 5,
"layer2 zIndex is set" );
map.events.register('preaddlayer', this, function(evt) {
return !(evt.layer.name === 'donotadd');
});
var layer3 = new OpenLayers.Layer.WMS("donotadd",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayers([layer3]);
t.eq(map.layers.length, 2, "layer is not added since preaddlayer returns false");
layer3.name = 'pleaseadd';
map.addLayers([layer3]);
t.eq(map.layers.length, 3, "layer is added since preaddlayer returns true");
map.destroy();
}
function test_Map_options(t) {
t.plan(3);
map = new OpenLayers.Map('map', {numZoomLevels: 6, maxResolution: 3.14159, theme: 'foo'});
t.eq( map.numZoomLevels, 6, "map.numZoomLevels set correctly via options hashtable" );
t.eq( map.maxResolution, 3.14159, "map.maxResolution set correctly via options hashtable" );
t.eq( map.theme, 'foo', "map theme set correctly." );
map.destroy();
}
function test_eventListeners(t) {
t.plan(1);
var method = OpenLayers.Events.prototype.on;
// test that events.on is called at map construction
var options = {
eventListeners: {foo: "bar"},
controls: []
};
OpenLayers.Events.prototype.on = function(obj) {
t.eq(obj, options.eventListeners, "events.on called with eventListeners");
}
var map = new OpenLayers.Map('map', options);
OpenLayers.Events.prototype.on = method;
map.destroy();
// if events.on is called again, this will fail due to an extra test
// test map without eventListeners
OpenLayers.Events.prototype.on = function(obj) {
t.fail("events.on called without eventListeners");
}
var map2 = new OpenLayers.Map("map", {controls: []});
OpenLayers.Events.prototype.on = method;
map2.destroy();
}
function test_Map_center(t) {
t.plan(11);
var log = [];
map = new OpenLayers.Map('map', {
eventListeners: {
"movestart": function() {log.push("movestart");},
"move": function() {log.push("move");},
"moveend": function() {log.push("moveend");}
}
});
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"} );
map.addLayer(baseLayer);
var ll = new OpenLayers.LonLat(2,1);
map.setCenter(ll, 0);
t.ok( map.getCenter() instanceof OpenLayers.LonLat, "map.getCenter returns a LonLat");
t.eq( map.getZoom(), 0, "map.zoom is correct after calling setCenter");
t.ok( map.getCenter().equals(ll), "map center is correct after calling setCenter");
map.zoomIn();
t.eq( map.getZoom(), 1, "map.zoom is correct after calling setCenter,zoom in");
t.ok( map.getCenter().equals(ll), "map center is correct after calling setCenter, zoom in");
map.zoomOut();
t.eq( map.getZoom(), 0, "map.zoom is correct after calling setCenter,zoom in, zoom out");
log = [];
map.zoomTo(5);
t.eq(log[0], "movestart", "zoomTo fires movestart event");
t.eq(log[1], "move", "zoomTo fires move event");
t.eq(log[2], "moveend", "zoomTo fires moveend event");
t.eq( map.getZoom(), 5, "map.zoom is correct after calling zoomTo" );
/**
map.zoomToMaxExtent();
t.eq( map.getZoom(), 2, "map.zoom is correct after calling zoomToMaxExtent" );
var lonlat = map.getCenter();
var zero = new OpenLayers.LonLat(0, 0);
t.ok( lonlat.equals(zero), "map center is correct after calling zoomToFullExtent" );
*/
map.getCenter().lon = 10;
t.ok( map.getCenter().equals(ll), "map.getCenter returns a clone of map.center");
map.destroy();
}
function test_Map_zoomend_event (t) {
t.plan(2);
map = new OpenLayers.Map('map');
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayer(baseLayer);
map.events.register("zoomend", {count: 0}, function() {
this.count++;
t.ok(true, "zoomend event was triggered " + this.count + " times");
});
map.setCenter(new OpenLayers.LonLat(2, 1), 0);
map.zoomIn();
map.zoomOut();
map.destroy();
}
function test_Map_add_remove_popup (t) {
t.plan(4);
map = new OpenLayers.Map('map');
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayer(baseLayer);
var popup = new OpenLayers.Popup("chicken",
new OpenLayers.LonLat(0,0),
new OpenLayers.Size(200,200));
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
map.addPopup(popup);
var pIndex = OpenLayers.Util.indexOf(map.popups, popup);
t.eq(pIndex, 0, "popup successfully added to Map's internal popups array");
var nodes = map.layerContainerDiv.childNodes;
var found = false;
for (var i=0; i < nodes.length; i++) {
if (nodes.item(i) == popup.div) {
found = true;
break;
}
}
t.ok(found, "popup.div successfully added to the map's viewPort");
map.removePopup(popup);
var pIndex = OpenLayers.Util.indexOf(map.popups, popup);
t.eq(pIndex, -1, "popup successfully removed from Map's internal popups array");
var found = false;
for (var i=0; i < nodes.length; i++) {
if (nodes.item(i) == popup.div) {
found = true;
break;
}
}
t.ok(!found, "popup.div successfully removed from the map's viewPort");
map.destroy();
}
function test_Map_add_popup_exclusive(t) {
t.plan(2);
map = new OpenLayers.Map('map');
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayer(baseLayer);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
for (var i = 0; i < 10; i++) {
var popup = new OpenLayers.Popup("chicken",
new OpenLayers.LonLat(0,0),
new OpenLayers.Size(200,200));
map.addPopup(popup);
}
t.eq(map.popups.length, 10, "addPopup non exclusive mode works");
var popup = new OpenLayers.Popup("chicken",
new OpenLayers.LonLat(0,0),
new OpenLayers.Size(200,200));
map.addPopup(popup, true);
t.eq(map.popups.length, 1, "addPopup exclusive mode works");
map.destroy();
}
/*** THIS IS A GOOD TEST, BUT IT SHOULD BE MOVED TO WMS.
* Also, it won't work until we figure out the viewSize bug
function 08_Map_px_lonlat_translation (t) {
t.plan( 6 );
map = new OpenLayers.Map('map');
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayer(baseLayer);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
var pixel = new OpenLayers.Pixel(50,150);
var lonlat = map.getLonLatFromViewPortPx(pixel);
t.ok( lonlat instanceof OpenLayers.LonLat, "getLonLatFromViewPortPx returns valid OpenLayers.LonLat" );
var newPixel = map.getViewPortPxFromLonLat(lonlat);
t.ok( newPixel instanceof OpenLayers.Pixel, "getViewPortPxFromLonLat returns valid OpenLayers.Pixel" );
// WARNING!!! I'm faily sure that the following test's validity
// depends highly on rounding and the resolution. For now,
// in the default case, it seems to work. This may not
// always be so.
t.ok( newPixel.equals(pixel), "Translation to pixel and back to lonlat is consistent");
lonlat = map.getLonLatFromPixel(pixel);
t.ok( lonlat instanceof OpenLayers.LonLat, "getLonLatFromPixel returns valid OpenLayers.LonLat" );
newPixel = map.getPixelFromLonLat(lonlat);
t.ok( newPixel instanceof OpenLayers.Pixel, "getPixelFromLonLat returns valid OpenLayers.Pixel" );
t.ok( newPixel.equals(pixel), "2nd translation to pixel and back to lonlat is consistent");
}
*/
function test_Map_isValidZoomLevel(t) {
t.plan(4);
var map = new OpenLayers.Map("map");
map.addLayer(new OpenLayers.Layer(null, {
isBaseLayer: true, numZoomLevels: 19
}))
var valid;
valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [-1]);
t.eq(valid, false, "-1 is not a valid zoomLevel");
valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [0]);
t.eq(valid, true, "0 is a valid zoomLevel");
valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [18]);
t.eq(valid, true, "18 is a valid zoomLevel");
valid = OpenLayers.Map.prototype.isValidZoomLevel.apply(map, [19]);
t.eq(valid, false, "19 is not a valid zoomLevel");
map.destroy();
}
function test_Map_isValidLonLat(t) {
t.plan( 3 );
map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.WMS('Test Layer',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
map.addLayer(layer);
t.ok( !map.isValidLonLat(null), "null lonlat is not valid" );
t.ok( map.isValidLonLat(new OpenLayers.LonLat(33862, 717606)), "lonlat outside max extent is valid" );
t.ok( !map.isValidLonLat(new OpenLayers.LonLat(10, 10)), "lonlat outside max extent is not valid" );
map.destroy();
}
function test_Map_getLayer(t) {
var numLayers = 3;
t.plan( numLayers + 1 );
var m = {
layers: []
};
for(var i = 0; i < numLayers; i++) {
m.layers.push( { 'id': i } );
}
for(var i = 0; i < numLayers; i++) {
var layer = OpenLayers.Map.prototype.getLayer.apply(m, [i]);
t.ok( layer == m.layers[i], "getLayer correctly returns layer " + i);
}
var gotLayer = OpenLayers.Map.prototype.getLayer.apply(m, ["chicken"]);
t.ok( gotLayer == null, "getLayer correctly returns null when layer not found");
map.destroy();
}
function test_Map_getLayersBy(t) {
var map = {
getBy: OpenLayers.Map.prototype.getBy,
getLayersBy: OpenLayers.Map.prototype.getLayersBy,
layers: [
{foo: "foo", id: Math.random()},
{foo: "bar", id: Math.random()},
{foo: "foobar", id: Math.random()},
{foo: "foo bar", id: Math.random()},
{foo: "foo", id: Math.random()}
]
};
var cases = [
{
got: map.getLayersBy("foo", "foo"),
expected: [map.layers[0], map.layers[4]],
message: "(string literal) got two layers matching foo"
}, {
got: map.getLayersBy("foo", "bar"),
expected: [map.layers[1]],
message: "(string literal) got one layer matching foo"
}, {
got: map.getLayersBy("foo", "barfoo"),
expected: [],
message: "(string literal) got empty array for no foo match"
}, {
got: map.getLayersBy("foo", /foo/),
expected: [map.layers[0], map.layers[2], map.layers[3], map.layers[4]],
message: "(regexp literal) got three layers containing string"
}, {
got: map.getLayersBy("foo", /foo$/),
expected: [map.layers[0], map.layers[4]],
message: "(regexp literal) got three layers ending with string"
}, {
got: map.getLayersBy("foo", /\s/),
expected: [map.layers[3]],
message: "(regexp literal) got layer containing space"
}, {
got: map.getLayersBy("foo", new RegExp("BAR", "i")),
expected: [map.layers[1], map.layers[2], map.layers[3]],
message: "(regexp object) got layers ignoring case"
}, {
got: map.getLayersBy("foo", {test: function(str) {return str.length > 3;}}),
expected: [map.layers[2], map.layers[3]],
message: "(custom object) got layers with foo length greater than 3"
}
];
t.plan(cases.length);
for(var i=0; i<cases.length; ++i) {
t.eq(cases[i].got, cases[i].expected, cases[i].message);
}
}
function test_Map_getLayersByName(t) {
var map = {
getBy: OpenLayers.Map.prototype.getBy,
getLayersBy: OpenLayers.Map.prototype.getLayersBy,
getLayersByName: OpenLayers.Map.prototype.getLayersByName,
layers: [
{name: "foo", id: Math.random()},
{name: "bar", id: Math.random()},
{name: "foobar", id: Math.random()},
{name: "foo bar", id: Math.random()},
{name: "foo", id: Math.random()}
]
};
var cases = [
{
got: map.getLayersByName("foo"),
expected: [map.layers[0], map.layers[4]],
message: "(string literal) got two layers matching name"
}, {
got: map.getLayersByName("bar"),
expected: [map.layers[1]],
message: "(string literal) got one layer matching name"
}, {
got: map.getLayersByName("barfoo"),
expected: [],
message: "(string literal) got empty array for no match"
}, {
got: map.getLayersByName(/foo/),
expected: [map.layers[0], map.layers[2], map.layers[3], map.layers[4]],
message: "(regexp literal) got three layers containing string"
}, {
got: map.getLayersByName(/foo$/),
expected: [map.layers[0], map.layers[4]],
message: "(regexp literal) got three layers ending with string"
}, {
got: map.getLayersByName(/\s/),
expected: [map.layers[3]],
message: "(regexp literal) got layer containing space"
}, {
got: map.getLayersByName(new RegExp("BAR", "i")),
expected: [map.layers[1], map.layers[2], map.layers[3]],
message: "(regexp object) got layers ignoring case"
}, {
got: map.getLayersByName({test: function(str) {return str.length > 3;}}),
expected: [map.layers[2], map.layers[3]],
message: "(custom object) got layers with name length greater than 3"
}
];
t.plan(cases.length);
for(var i=0; i<cases.length; ++i) {
t.eq(cases[i].got, cases[i].expected, cases[i].message);
}
}
function test_Map_getLayersByClass(t) {
var map = {
getBy: OpenLayers.Map.prototype.getBy,
getLayersBy: OpenLayers.Map.prototype.getLayersBy,
getLayersByClass: OpenLayers.Map.prototype.getLayersByClass,
layers: [
{CLASS_NAME: "foo", id: Math.random()},
{CLASS_NAME: "bar", id: Math.random()},
{CLASS_NAME: "foobar", id: Math.random()},
{CLASS_NAME: "foo bar", id: Math.random()},
{CLASS_NAME: "foo", id: Math.random()}
]
};
var cases = [
{
got: map.getLayersByClass("foo"),
expected: [map.layers[0], map.layers[4]],
message: "(string literal) got two layers matching type"
}, {
got: map.getLayersByClass("bar"),
expected: [map.layers[1]],
message: "(string literal) got one layer matching type"
}, {
got: map.getLayersByClass("barfoo"),
expected: [],
message: "(string literal) got empty array for no match"
}, {
got: map.getLayersByClass(/foo/),
expected: [map.layers[0], map.layers[2], map.layers[3], map.layers[4]],
message: "(regexp literal) got three layers containing string"
}, {
got: map.getLayersByClass(/foo$/),
expected: [map.layers[0], map.layers[4]],
message: "(regexp literal) got three layers ending with string"
}, {
got: map.getLayersByClass(/\s/),
expected: [map.layers[3]],
message: "(regexp literal) got layer containing space"
}, {
got: map.getLayersByClass(new RegExp("BAR", "i")),
expected: [map.layers[1], map.layers[2], map.layers[3]],
message: "(regexp object) got layers ignoring case"
}, {
got: map.getLayersByClass({test: function(str) {return str.length > 3;}}),
expected: [map.layers[2], map.layers[3]],
message: "(custom object) got layers with type length greater than 3"
}
];
t.plan(cases.length);
for(var i=0; i<cases.length; ++i) {
t.eq(cases[i].got, cases[i].expected, cases[i].message);
}
}
function test_Map_getControlsBy(t) {
var map = {
getBy: OpenLayers.Map.prototype.getBy,
getControlsBy: OpenLayers.Map.prototype.getControlsBy,
controls: [
{foo: "foo", id: Math.random()},
{foo: "bar", id: Math.random()},
{foo: "foobar", id: Math.random()},
{foo: "foo bar", id: Math.random()},
{foo: "foo", id: Math.random()}
]
};
var cases = [
{
got: map.getControlsBy("foo", "foo"),
expected: [map.controls[0], map.controls[4]],
message: "(string literal) got two controls matching foo"
}, {
got: map.getControlsBy("foo", "bar"),
expected: [map.controls[1]],
message: "(string literal) got one control matching foo"
}, {
got: map.getControlsBy("foo", "barfoo"),
expected: [],
message: "(string literal) got empty array for no foo match"
}, {
got: map.getControlsBy("foo", /foo/),
expected: [map.controls[0], map.controls[2], map.controls[3], map.controls[4]],
message: "(regexp literal) got three controls containing string"
}, {
got: map.getControlsBy("foo", /foo$/),
expected: [map.controls[0], map.controls[4]],
message: "(regexp literal) got three controls ending with string"
}, {
got: map.getControlsBy("foo", /\s/),
expected: [map.controls[3]],
message: "(regexp literal) got control containing space"
}, {
got: map.getControlsBy("foo", new RegExp("BAR", "i")),
expected: [map.controls[1], map.controls[2], map.controls[3]],
message: "(regexp object) got layers ignoring case"
}, {
got: map.getControlsBy("foo", {test: function(str) {return str.length > 3;}}),
expected: [map.controls[2], map.controls[3]],
message: "(custom object) got controls with foo length greater than 3"
}
];
t.plan(cases.length);
for(var i=0; i<cases.length; ++i) {
t.eq(cases[i].got, cases[i].expected, cases[i].message);
}
}
function test_Map_getControlsByClass(t) {
var map = {
getBy: OpenLayers.Map.prototype.getBy,
getControlsBy: OpenLayers.Map.prototype.getControlsBy,
getControlsByClass: OpenLayers.Map.prototype.getControlsByClass,
controls: [
{CLASS_NAME: "foo", id: Math.random()},
{CLASS_NAME: "bar", id: Math.random()},
{CLASS_NAME: "foobar", id: Math.random()},
{CLASS_NAME: "foo bar", id: Math.random()},
{CLASS_NAME: "foo", id: Math.random()}
]
};
var cases = [
{
got: map.getControlsByClass("foo"),
expected: [map.controls[0], map.controls[4]],
message: "(string literal) got two controls matching type"
}, {
got: map.getControlsByClass("bar"),
expected: [map.controls[1]],
message: "(string literal) got one control matching type"
}, {
got: map.getControlsByClass("barfoo"),
expected: [],
message: "(string literal) got empty array for no match"
}, {
got: map.getControlsByClass(/foo/),
expected: [map.controls[0], map.controls[2], map.controls[3], map.controls[4]],
message: "(regexp literal) got three controls containing string"
}, {
got: map.getControlsByClass(/foo$/),
expected: [map.controls[0], map.controls[4]],
message: "(regexp literal) got three controls ending with string"
}, {
got: map.getControlsByClass(/\s/),
expected: [map.controls[3]],
message: "(regexp literal) got control containing space"
}, {
got: map.getControlsByClass(new RegExp("BAR", "i")),
expected: [map.controls[1], map.controls[2], map.controls[3]],
message: "(regexp object) got controls ignoring case"
}, {
got: map.getControlsByClass({test: function(str) {return str.length > 3;}}),
expected: [map.controls[2], map.controls[3]],
message: "(custom object) got controls with type length greater than 3"
}
];
t.plan(cases.length);
for(var i=0; i<cases.length; ++i) {
t.eq(cases[i].got, cases[i].expected, cases[i].message);
}
}
function test_Map_double_addLayer(t) {
t.plan( 1 );
map = new OpenLayers.Map($('map'));
layer = new OpenLayers.Layer.WMS('Test Layer',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}
);
map.addLayers([layer,layer]);
t.eq( map.layers.length, 1, "Map does not allow double adding of layers." );
map.destroy();
}
function test_Map_setBaseLayer(t) {
t.plan( 4 );
map = new OpenLayers.Map('map');
var wmslayer = new OpenLayers.Layer.WMS('Test Layer',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
var wmslayer2 = new OpenLayers.Layer.WMS('Test Layer2',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
map.addLayers([wmslayer, wmslayer2]);
t.ok(map.baseLayer == wmslayer, "default base layer is first one added");
map.setBaseLayer(null);
t.ok(map.baseLayer == wmslayer, "setBaseLayer on null object does nothing (and does not break)");
map.setBaseLayer("chicken");
t.ok(map.baseLayer == wmslayer, "setBaseLayer on non-layer object does nothing (and does not break)");
map.setBaseLayer(wmslayer2);
t.ok(map.baseLayer == wmslayer2, "setbaselayer correctly sets 'baseLayer' property");
map.destroy();
}
function test_Map_removeLayer(t) {
t.plan(1);
var f = function() {};
var events = {triggerEvent: f};
var layers = [
{name: "fee", removeMap: f, events: events},
{name: "fi", removeMap: f, events: events},
{name: "fo", removeMap: f, events: events},
{name: "fum", removeMap: f, events: events}
];
var map = {
layers: layers,
baseLayer: layers[0],
layerContainerDiv: {removeChild: f},
events: {triggerEvent: f},
resetLayersZIndex: function() {}
};
OpenLayers.Map.prototype.removeLayer.apply(map, [map.baseLayer, false]);
t.eq(map.baseLayer, null,
"removing the baselayer sets baseLayer to null");
}
function test_Map_removeLayer_res(t) {
t.plan(2);
map = new OpenLayers.Map('map');
var layer0 = new OpenLayers.Layer.WMS(
'Test Layer 0',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{resolutions: [4, 2, 1]}
);
var layer1 = new OpenLayers.Layer.WMS(
'Test Layer 1',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{resolutions: [4, 2]}
);
map.addLayers([layer0, layer1]);
map.zoomToMaxExtent();
map.zoomTo(2);
t.eq(map.getResolution(), layer0.resolutions[2],
"correct resolution before removal");
map.removeLayer(layer0);
t.eq(map.getResolution(), layer0.resolutions[1],
"correct resolution after removal");
map.destroy();
}
function test_Map_removeLayer_zindex(t) {
t.plan(2);
map = new OpenLayers.Map('map');
var layer0 = new OpenLayers.Layer('Test Layer 0', {isBaseLayer:true});
var layer1 = new OpenLayers.Layer('Test Layer 1', {isBaseLayer:true});
var layer2 = new OpenLayers.Layer('Test Layer 2', {isBaseLayer:false});
map.addLayers([layer0, layer1, layer2]);
map.removeLayer(layer0);
t.eq(parseInt(layer1.div.style.zIndex), map.Z_INDEX_BASE['BaseLayer'],
"correct z-index after removeLayer");
t.eq(parseInt(layer2.div.style.zIndex), map.Z_INDEX_BASE['Overlay'] + 5,
"correct z-index after removeLayer");
map.destroy();
}
function test_Map_removeLayer_preremovelayer(t) {
t.plan(4);
map = new OpenLayers.Map('map');
map.addLayer(new OpenLayers.Layer());
map.removeLayer(map.layers[0]);
// one test: standard behaviour without listener
t.eq(map.layers.length, 0, "without registered preremovelayer-listener layers can be removed as usual");
var callCnt = 0;
map.events.register('preremovelayer', this, function(evt) {
callCnt++;
return !(evt.layer.name === 'donotremove');
});
var layer1 = new OpenLayers.Layer('donotremove');
var layer2 = new OpenLayers.Layer('doremove');
map.addLayers([layer1,layer2]);
// two tests: remove action can be canceled
map.removeLayer(layer1);
t.eq(map.layers.length, 2, "layer is not removed since preremovelayer returns false");
map.removeLayer(layer2);
t.eq(map.layers.length, 1, "layer is removed since preremovelayer returns true");
// one test: listener was called twice
t.eq(callCnt, 2, "preremovelayer-listener was called exactly twice");
map.destroy();
}
function test_Map_setBaseLayer_after_pan (t) {
t.plan(1);
map = new OpenLayers.Map('map');
var wmsLayer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} );
var tmsLayer = new OpenLayers.Layer.TMS("TMS",
"http://labs.metacarta.com/wms-c/Basic.py/",
{'layername':'basic', 'type':'png'});
map.addLayers([wmsLayer,tmsLayer]);
map.setBaseLayer(wmsLayer);
map.zoomToMaxExtent();
map.setBaseLayer(tmsLayer);
map.zoomIn();
map.pan(0, -200, {animate:false});
map.setBaseLayer(wmsLayer);
t.eq(map.layerContainerDiv.style.top, "0px", "layerContainer is recentered after setBaseLayer");
map.destroy();
}
function test_Map_moveLayer (t) {
t.plan(10);
var ct = 0;
map = new OpenLayers.Map('map');
var wmslayer = new OpenLayers.Layer.WMS('Test Layer',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
var wmslayer2 = new OpenLayers.Layer.WMS('Test Layer2',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
var wmslayer3 = new OpenLayers.Layer.WMS('Test Layer2',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
map.addLayers([wmslayer, wmslayer2, wmslayer3]);
map.events.register("changelayer", map, function (e) { ct++; });
t.eq( map.getNumLayers(), 3, "getNumLayers returns the number of layers" );
t.eq( map.getLayerIndex(wmslayer3), 2, "getLayerIndex returns the right index" );
map.raiseLayer(wmslayer3, 1);
t.eq( map.getLayerIndex(wmslayer3), 2, "can't moveLayer up past the top of the stack" );
map.raiseLayer(wmslayer, -1);
t.eq( map.getLayerIndex(wmslayer), 0, "can't moveLayer down past the bottom of the stack" );
map.raiseLayer(wmslayer3, -1);
t.eq( map.getLayerIndex(wmslayer3), 1, "can moveLayer down from the top" );
t.eq( parseInt(wmslayer3.div.style.zIndex), map.Z_INDEX_BASE['BaseLayer'] + 5,
"layer div has the right zIndex after moving down" );
map.raiseLayer(wmslayer, 2);
t.eq( map.getLayerIndex(wmslayer), 2, "can moveLayer up from the bottom" );
t.eq( parseInt(wmslayer.div.style.zIndex), map.Z_INDEX_BASE['BaseLayer'] + 2 * 5,
"layer div has the right zIndex after moving up" );
t.eq( map.getLayerIndex(wmslayer3), 0, "top layer is now on the bottom" );
t.eq( ct, 3, "raiseLayer triggered changelayer the right # of times" );
map.destroy();
}
function test_Map_moveTo(t) {
t.plan(2);
map = new OpenLayers.Map('map');
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"},
{maxResolution: 'auto', maxExtent: new OpenLayers.Bounds(-10,-10,10,10)});
baseLayer.events.on({
move: function() {
t.ok(true, "move listener called");
},
moveend: function(e) {
t.eq(e.zoomChanged, true, "moveend listener called with expected value");
}
});
map.addLayer(baseLayer);
var ll = new OpenLayers.LonLat(-100,-150);
map.moveTo(ll, 2);
t.ok(map.getCenter().equals(new OpenLayers.LonLat(0,0)), "safely sets out-of-bounds lonlat");
map.destroy();
}
function test_Map_defaultTheme(t) {
t.plan(5);
var links = document.getElementsByTagName('link');
map = new OpenLayers.Map('map');
var gotNodes = 0;
var themeNode = null;
for(var i=0; i<links.length; ++i) {
if(OpenLayers.Util.isEquivalentUrl(map.theme, links.item(i).href)) {
gotNodes += 1;
themeNode = links.item(i);
}
}
t.eq(gotNodes, 1, "by default, a single link node is added to document");
t.ok(themeNode != null, "a link node with the theme href was added");
t.eq(themeNode.rel, "stylesheet", "node added has rel set to stylesheet");
t.eq(themeNode.type, "text/css", "node added has type set to text/css");
// reconstruct the map to prove that another link is not added
map = new OpenLayers.Map('map');
t.eq(links.length, document.getElementsByTagName('link').length,
"calling the map constructor twice with the same theme doesn't add duplicate link nodes");
map.destroy();
}
function test_Map_customTheme(t) {
t.plan(5);
var customTheme = 'foo';
var options = {theme: customTheme};
map = new OpenLayers.Map('map', options);
var links = document.getElementsByTagName('link');
var gotNodes = 0;
var themeNode = null;
for(var i=0; i<links.length; ++i) {
if(OpenLayers.Util.isEquivalentUrl(map.theme, links.item(i).href)) {
gotNodes += 1;
themeNode = links.item(i);
}
}
t.eq(map.theme, customTheme, "map theme is properly set");
t.eq(gotNodes, 1, "with custom theme, a single link node is added to document");
t.ok(themeNode != null, "a link node with the theme href was added");
t.eq(themeNode.rel, "stylesheet", "node added has rel set to stylesheet");
t.eq(themeNode.type, "text/css", "node added has type set to text/css");
map.destroy();
}
function test_Map_noTheme(t) {
t.plan(1);
var head = document.getElementsByTagName('head')[0];
var nodeCount = head.childNodes.length;
var options = {theme: null};
map = new OpenLayers.Map('map', options);
t.eq(nodeCount, head.childNodes.length, "with no theme, a node is not added to document head" );
map.destroy();
}
function test_Map_addControls(t) {
t.plan(5);
var map = new OpenLayers.Map('map', {
controls: []
});
var controls = [
new OpenLayers.Control({id:'firstctrl'}),
new OpenLayers.Control({id:'secondctrl'})
];