Project

General

Profile

1
<html>
2
<head>
3
  <script src="../lib/OpenLayers.js"></script>
4
  <script type="text/javascript"><!--
5
    var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
6
    var map; 
7
    function test_01_Map_constructor (t) {
8
        t.plan( 9 );
9
        
10
        map = new OpenLayers.Map('map'); // no longer need to call $(), constructor does it
11
        var baseLayer = new OpenLayers.Layer.WMS("Test Layer", 
12
            "http://octo.metacarta.com/cgi-bin/mapserv?",
13
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
14
        map.addLayer(baseLayer);
15

    
16
        t.ok( map instanceof OpenLayers.Map, "new OpenLayers.Map returns object" );
17
        if (!isMozilla) {
18
            t.ok( true, "skipping element test outside of Mozilla");
19
            t.ok( true, "skipping element test outside of Mozilla");
20
            t.ok( true, "skipping element test outside of Mozilla");
21
        } else {
22
            t.ok( map.div instanceof HTMLDivElement, "map.div is an HTMLDivElement" );
23
            t.ok( map.viewPortDiv instanceof HTMLDivElement, "map.viewPortDiv is an HTMLDivElement" );
24
            t.ok( map.layerContainerDiv instanceof HTMLDivElement, "map.layerContainerDiv is an HTMLDivElement" );
25
        }
26
        t.ok( map.layers instanceof Array, "map.layers is an Array" );
27
        t.ok( map.controls instanceof Array, "map.controls is an Array" );
28
        t.ok( map.events instanceof OpenLayers.Events, "map.events is an OpenLayers.Events" );
29
        t.ok( map.getMaxExtent() instanceof OpenLayers.Bounds, "map.maxExtent is an OpenLayers.Bounds" );
30
        t.ok( map.getNumZoomLevels() > 0, "map has a default numZoomLevels" );
31
    }
32
    function test_02_Map_center(t) {
33
        t.plan(3);
34
        map = new OpenLayers.Map($('map'));
35
        var baseLayer = new OpenLayers.Layer.WMS("Test Layer", 
36
            "http://octo.metacarta.com/cgi-bin/mapserv?",
37
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
38
        map.addLayer(baseLayer);
39
        var ll = new OpenLayers.LonLat(2,1);
40
        map.setCenter(ll, 0);
41
        t.ok( map.getCenter() instanceof OpenLayers.LonLat, "map.getCenter returns a LonLat");
42
        t.eq( map.getZoom(), 0, "map.zoom is correct after calling setCenter");
43
        t.ok( map.getCenter().equals(ll), "map center is correct after calling setCenter");
44
    }
45
    function test_03_Map_add_layers(t) {
46
        t.plan(6);
47
        map = new OpenLayers.Map($('map'));
48
        var layer1 = new OpenLayers.Layer.WMS("Layer 1", 
49
            "http://octo.metacarta.com/cgi-bin/mapserv?",
50
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
51
        var layer2 = new OpenLayers.Layer.WMS("Layer 2",
52
            "http://wms.jpl.nasa.gov/wms.cgi", {layers: "modis,global_mosaic"});
53
        // this uses map.addLayer internally
54
        map.addLayers([layer1, layer2])
55
        t.eq( map.layers.length, 2, "map has exactly two layers" );
56
        t.ok( map.layers[0] === layer1, "1st layer is layer1" );
57
        t.ok( map.layers[1] === layer2, "2nd layer is layer2" );
58
        t.ok( layer1.map === map, "layer.map is map" ); 
59
        t.eq( parseInt(layer1.div.style.zIndex), map.Z_INDEX_BASE['BaseLayer'], 
60
                "layer1 zIndex is set" );
61
        t.eq( parseInt(layer2.div.style.zIndex), map.Z_INDEX_BASE['BaseLayer'] + 5, 
62
                "layer2 zIndex is set" );
63
    }
64
    function test_04_Map_options(t) {
65
        t.plan(2);
66
        map = new OpenLayers.Map($('map'), {numZoomLevels: 6, maxResolution: 3.14159});
67
        t.eq( map.numZoomLevels, 6, "map.numZoomLevels set correctly via options hashtable" );
68
        t.eq( map.maxResolution, 3.14159, "map.maxResolution set correctly via options hashtable" );
69
    }
70
    function test_05_Map_center(t) {
71
        t.plan(4);
72
        map = new OpenLayers.Map($('map'));
73
        var baseLayer = new OpenLayers.Layer.WMS("Test Layer", 
74
            "http://octo.metacarta.com/cgi-bin/mapserv?",
75
            {map: "/mapdata/vmap_wms.map", layers: "basic"} );
76
        map.addLayer(baseLayer);
77
        var ll = new OpenLayers.LonLat(2,1);
78
        map.setCenter(ll, 0);
79
        map.zoomIn();
80
        t.eq( map.getZoom(), 1, "map.zoom is correct after calling setCenter,zoom in");
81
        t.ok( map.getCenter().equals(ll), "map center is correct after calling setCenter, zoom in");
82
        map.zoomOut();
83
        t.eq( map.getZoom(), 0, "map.zoom is correct after calling setCenter,zoom in, zoom out");
84

    
85
    map.zoomTo(5);
86
    t.eq( map.getZoom(), 5, "map.zoom is correct after calling zoomTo" );
87
/**
88
    map.zoomToMaxExtent();
89
    t.eq( map.getZoom(), 2, "map.zoom is correct after calling zoomToMaxExtent" );
90
    var lonlat = map.getCenter();
91
    var zero = new OpenLayers.LonLat(0, 0);
92
    t.ok( lonlat.equals(zero), "map center is correct after calling zoomToFullExtent" );
93

    
94
    */
95
    }
96

    
97
    function test_06_Map_zoomend_event (t) {
98
        t.plan(2);
99
        map = new OpenLayers.Map('map');
100
        var baseLayer = new OpenLayers.Layer.WMS("Test Layer", 
101
            "http://octo.metacarta.com/cgi-bin/mapserv?",
102
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
103
        map.addLayer(baseLayer);
104
        map.events.register("zoomend", {count: 0}, function() { 
105
            this.count++; 
106
            t.ok(true, "zoomend event was triggered " + this.count + " times");
107
            });
108
        map.setCenter(new OpenLayers.LonLat(2, 1), 0);
109
        map.zoomIn();
110
        map.zoomOut();
111
    }
112
    
113
    function test_07_Map_add_remove_popup (t) {
114
        t.plan(4);
115

    
116
        map = new OpenLayers.Map('map');
117
        var baseLayer = new OpenLayers.Layer.WMS("Test Layer", 
118
            "http://octo.metacarta.com/cgi-bin/mapserv?",
119
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
120
        map.addLayer(baseLayer);
121
        
122
        var popup = new OpenLayers.Popup("chicken", 
123
                                         new OpenLayers.LonLat(0,0),
124
                                         new OpenLayers.Size(200,200));
125
        map.setCenter(new OpenLayers.LonLat(0, 0), 0);
126

    
127
        map.addPopup(popup);
128
        t.eq(map.popups.indexOf(popup), 0, "popup successfully added to Map's internal popups array");
129

    
130
        var nodes = map.layerContainerDiv.childNodes;
131
        
132
        var found = false;
133
        for (var i=0; i < nodes.length; i++) {
134
            if (nodes.item(i) == popup.div) {
135
                found = true;
136
                break;
137
            }
138
        }
139
        t.ok(found, "popup.div successfully added to the map's viewPort");
140

    
141
        
142
        map.removePopup(popup);
143
        t.eq(map.popups.indexOf(popup), -1, "popup successfully removed from Map's internal popups array");
144

    
145
        var found = false;
146
        for (var i=0; i < nodes.length; i++) {
147
            if (nodes.item(i) == popup.div) {
148
                found = true;
149
                break;
150
            }
151
        }
152
        t.ok(!found, "popup.div successfully removed from the map's viewPort");
153
    }
154
/***  THIS IS A GOOD TEST, BUT IT SHOULD BE MOVED TO WMS. 
155
 *     Also, it won't work until we figure out the viewSize bug
156

    
157
    function 08_Map_px_lonlat_translation (t) {
158
        t.plan( 6 );    
159
        map = new OpenLayers.Map($('map'));
160
        var baseLayer = new OpenLayers.Layer.WMS("Test Layer", 
161
            "http://octo.metacarta.com/cgi-bin/mapserv?",
162
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
163
        map.addLayer(baseLayer);
164
        map.setCenter(new OpenLayers.LonLat(0, 0), 0);
165
 
166
        var pixel = new OpenLayers.Pixel(50,150);
167
        var lonlat = map.getLonLatFromViewPortPx(pixel);
168
        t.ok( lonlat instanceof OpenLayers.LonLat, "getLonLatFromViewPortPx returns valid OpenLayers.LonLat" );
169

    
170
        var newPixel = map.getViewPortPxFromLonLat(lonlat);
171
        t.ok( newPixel instanceof OpenLayers.Pixel, "getViewPortPxFromLonLat returns valid OpenLayers.Pixel" );
172

    
173
        // WARNING!!!  I'm faily sure that the following test's validity 
174
        //             depends highly on rounding and the resolution. For now,
175
        //             in the default case, it seems to work. This may not 
176
        //             always be so.
177
        t.ok( newPixel.equals(pixel), "Translation to pixel and back to lonlat is consistent");
178

    
179
        lonlat = map.getLonLatFromPixel(pixel);
180
        t.ok( lonlat instanceof OpenLayers.LonLat, "getLonLatFromPixel returns valid OpenLayers.LonLat" );
181

    
182
        newPixel = map.getPixelFromLonLat(lonlat);
183
        t.ok( newPixel instanceof OpenLayers.Pixel, "getPixelFromLonLat returns valid OpenLayers.Pixel" );
184

    
185
        t.ok( newPixel.equals(pixel), "2nd translation to pixel and back to lonlat is consistent");
186
    }
187
 */
188
    function test_09_Map_isValidLonLat(t) {
189
        t.plan( 3 );    
190
        map = new OpenLayers.Map($('map'));
191
        layer = new OpenLayers.Layer.WMS('Test Layer', 
192
                    "http://octo.metacarta.com/cgi-bin/mapserv", 
193
                    {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}, 
194
                    {maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
195
        map.addLayer(layer);
196
        
197
        t.ok( !map.isValidLonLat(null), "null lonlat is not valid" );
198
        t.ok( map.isValidLonLat(new OpenLayers.LonLat(33862, 717606)), "lonlat outside max extent is valid" );
199
        t.ok( !map.isValidLonLat(new OpenLayers.LonLat(10, 10)), "lonlat outside max extent is not valid" );
200
    }
201

    
202
    function test_10_Map_getLayer(t) {
203
        t.plan( 2 );    
204
        map = new OpenLayers.Map($('map'));
205
        layer = new OpenLayers.Layer.WMS('Test Layer', 
206
                    "http://octo.metacarta.com/cgi-bin/mapserv", 
207
                    {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}, 
208
                    {maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
209

    
210
        map.addLayer(layer);
211
        var gotLayer = map.getLayer(layer.id);
212

    
213
        t.ok( layer == gotLayer, "getLayer correctly returns layer" );
214

    
215
        gotLayer = map.getLayer("chicken");
216

    
217
        t.ok( gotLayer == null, "getLayer correctly returns null when layer not found");
218
    }
219

    
220
    function test_10_Map_setBaseLayer(t) {
221
        t.plan( 4 );
222
        
223
        map = new OpenLayers.Map($('map'));
224

    
225
        var wmslayer = new OpenLayers.Layer.WMS('Test Layer', 
226
                    "http://octo.metacarta.com/cgi-bin/mapserv", 
227
                    {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}, 
228
                    {maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
229

    
230
        var wmslayer2 = new OpenLayers.Layer.WMS('Test Layer2', 
231
                    "http://octo.metacarta.com/cgi-bin/mapserv", 
232
                    {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}, 
233
                    {maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
234

    
235
        map.addLayers([wmslayer, wmslayer2]);
236

    
237
        t.ok(map.baseLayer == wmslayer, "default base layer is first one added");
238
        
239
        map.setBaseLayer(null);
240
        t.ok(map.baseLayer == wmslayer, "setBaseLayer on null object does nothing (and does not break)");
241
        
242
        map.setBaseLayer("chicken");
243
        t.ok(map.baseLayer == wmslayer, "setBaseLayer on non-layer object does nothing (and does not break)");
244
        
245
        map.setBaseLayer(wmslayer2);
246
        t.ok(map.baseLayer == wmslayer2, "setbaselayer correctly sets 'baseLayer' property");
247

    
248
    }
249
    
250
    
251
    function test_99_Map_destroy (t) {
252
        t.plan( 2 );    
253
        map = new OpenLayers.Map($('map'));
254
        map.destroy();
255
        t.eq( map.layers, null, "map.layers is null after destroy" );
256
        t.eq( map.controls, null, "map.controls is null after destroy" );
257
    }
258
  // -->
259
  </script>
260
</head>
261
<body>
262
    <div id="map" style="width: 1080px; height: 600px;"/>
263
</body>
264
</html>
(27-27/35)