summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Layer/MapServer.html
blob: 9ae8d01d6a2deb3653ea9735ede32f17ba4951a2 (plain)
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
<html>
<head>
    <script type="text/javascript">var oldAlert = window.alert, gMess; window.alert = function(message) {gMess = message; return true;};</script>
    <script type="text/javascript">window.alert = oldAlert;</script>
    

<script src="../OLLoader.js"></script>
  <script type="text/javascript">
    // turn off animation frame handling, so we can check img urls in tests
    delete OpenLayers.Layer.Grid.prototype.queueTileDraw;
    
    var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
    var layer; 

    var name = 'Test Layer';
    var url = "http://labs.metacarta.com/cgi-bin/mapserv";
    var params = { map: '/mapdata/vmap_wms.map', 
                   layers: 'basic'};

    function test_Layer_MapServer_constructor (t) {
        t.plan( 4 );

        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        t.ok( layer instanceof OpenLayers.Layer.MapServer, "new OpenLayers.Layer.MapServer returns object" );
        t.eq( layer.url, "http://labs.metacarta.com/cgi-bin/mapserv", "layer.url is correct (HTTPRequest inited)" );

        t.eq( layer.params.mode, "map", "default mode param correctly copied");
        t.eq( layer.params.map_imagetype, "png", "default imagetype correctly copied");


    }
    
    function test_Layer_MapServer_addtile (t) {
        t.plan( 6 );
    
        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        var map = new OpenLayers.Map('map', {tileManager: null});
        map.addLayer(layer);
        var pixel = new OpenLayers.Pixel(5,6);
        var tile = layer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
        tile.draw();

        var img = tile.imgDiv;
        var tParams = OpenLayers.Util.extend({},params);
        tParams = OpenLayers.Util.extend(tParams, {
                 layers: 'basic', 
                 mode: 'map',
                 map_imagetype: 'png',
                 mapext:[1,2,3,4],
                 imgext:[1,2,3,4],
                 map_size:[256, 256],
                 imgx:128,
                 imgy:128,
                 imgxy:[256,256]
        });
        t.eq( tile.url,
             url + "?" + OpenLayers.Util.getParameterString(tParams).replace(/,/g, "+"),
             "image src is created correctly via addtile" );
        t.eq( tile.getTile().style.top, "6px", "image top is set correctly via addtile" );
        t.eq( tile.getTile().style.left, "5px", "image top is set correctly via addtile" );

        var firstChild = layer.div.firstChild;
        t.eq( firstChild.nodeName.toLowerCase(), "img", "div first child is an image object" );
        t.ok( firstChild == img, "div first child is correct image object" );
        t.eq( tile.position.toString(), "x=5,y=6", "Position of tile is set correctly." );
        map.destroy();
    }
    
    function test_Layer_MapServer_inittiles (t) {
        t.plan( 2 );
        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.MapServer(name, url, params, {buffer: 0});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0),5);
        t.eq( layer.grid.length, 4, "Grid rows is correct." );
        t.eq( layer.grid[0].length, 3, "Grid cols is correct." );
        map.destroy();
        
    }


    function test_Layer_MapServer_clone (t) {
        t.plan(4);
        
        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        var options = {tileSize: new OpenLayers.Size(500,50)};
        var map = new OpenLayers.Map('map', options);
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        map.addLayer(layer);

        layer.grid = [ [6, 7], 
                       [8, 9]];

        var clone = layer.clone();

        t.ok( clone.grid != layer.grid, "clone does not copy grid");

        t.ok( clone.tileSize.equals(layer.tileSize), "tileSize correctly cloned");

        layer.tileSize.w += 40;

        t.eq( clone.tileSize.w, 500, "changing layer.tileSize does not change clone.tileSize -- a fresh copy was made, not just copied reference");

        t.eq( clone.alpha, layer.alpha, "alpha copied correctly");

        layer.grid = null;
        map.destroy();
    }

    function test_Layer_MapServer_isBaseLayer(t) {
        t.plan(3);
        
        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        t.ok( layer.isBaseLayer, "baselayer is true by default");

        var newParams = OpenLayers.Util.extend({}, params);
        newParams.transparent = "true";
        layer = new OpenLayers.Layer.MapServer(name, url, newParams);
        t.ok( !layer.isBaseLayer, "baselayer is false when transparent is set to true");

        layer = new OpenLayers.Layer.MapServer(name, url, params, {isBaseLayer: false});
        t.ok( !layer.isBaseLayer, "baselayer is false when option is set to false" );
    }

    function test_Layer_MapServer_mergeNewParams (t) {
        t.plan( 4 );

        var map = new OpenLayers.Map("map");
        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        
        var newParams = { layers: 'sooper', 
                          chickpeas: 'image/png'};

        map.addLayer(layer);
        map.zoomToMaxExtent();
        
        layer.redraw = function() {
            t.ok(true, "layer is redrawn after new params merged");
        }
        layer.mergeNewParams(newParams);
        
        t.eq( layer.params.layers, "sooper", "mergeNewParams() overwrites well");
        t.eq( layer.params.chickpeas, "image/png", "mergeNewParams() adds well");
        
        newParams.chickpeas = 151;

        t.eq( layer.params.chickpeas, "image/png", "mergeNewParams() makes clean copy of hashtable");
        map.destroy();
    }

    function test_Layer_MapServer_getFullRequestString (t) {
        t.plan( 3 );
        var map = new OpenLayers.Map('map');
        tUrl = "http://labs.metacarta.com/cgi-bin/mapserv";
        tParams = { layers: 'basic', 
                    format: 'png'};
        var tLayer = new OpenLayers.Layer.MapServer(name, tUrl, tParams);
        map.addLayer(tLayer);
        str = tLayer.getFullRequestString();
        var tParams = {
                 layers: 'basic', 
                 format: 'png',
                 mode: 'map',
                 map_imagetype: 'png'
        };

            var sStr = tUrl + "?" + OpenLayers.Util.getParameterString(tParams);
            sStr = sStr.replace(/,/g, "+");
        
        t.eq(str, sStr , "getFullRequestString() works");
        map.destroy();
        
        tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv","http://labs.metacarta.com/cgi-bin/mapserv"];
        layer = new OpenLayers.Layer.MapServer(name, tUrl, tParams, null);
        str = layer.getFullRequestString({'c':'d'});
        t.eq(str, tUrl[1] + '?' + OpenLayers.Util.getParameterString(OpenLayers.Util.extend(tParams,{'c':'d'})), "getFullRequestString() works for list of two urls and is deterministic");
        layer.destroy();
        var tParams = {
                 layers: 'basic', 
                 format: 'png',
                 mode: 'map',
                 map_imagetype: 'png'
        };
        tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv","http://labs.metacarta.com/cgi-bin/mapserv"];
        layer = new OpenLayers.Layer.MapServer(name, tUrl, tParams, null);
        str = layer.getFullRequestString({'a':'b'});
        t.eq(str, tUrl[0] + '?' + OpenLayers.Util.getParameterString(OpenLayers.Util.extend(tParams,{'a':'b'})), "getFullRequestString() works for list of two urls and is deterministic");
        layer.destroy();

    }

    function test_Layer_MapServer_singleTile (t) {
        t.plan( 5 );
        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.MapServer(name, url, params, {singleTile: true});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0),5);
        t.eq( layer.singleTile, true, "layer has singleTile property, great!" );
        t.eq( layer.grid.length, 1, "Grid has only a single row, good enough!" );
        t.eq( layer.grid[0].length, 1, "Grid has only a single column, good enough!" );
        t.eq( layer.tileSize.w, 750, "Image width is correct" );
        t.eq( layer.tileSize.h, 825, "Image height is correct" );
        map.destroy();
    }

    
    
    function test_Layer_MapServer_destroy (t) {

        t.plan( 1 );

        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        map.addLayer(layer);

        map.setCenter(new OpenLayers.LonLat(0,0), 5);

        //grab a reference to one of the tiles
        var tile = layer.grid[0][0];        

        layer.destroy();
        
    // checks to make sure superclass (grid) destroy() was called    
        
        t.ok( layer.grid == null, "grid set to null");
        map.destroy();
    }
    
  </script>
</head>
<body>
<div id="map" style="width:500px;height:550px"></div>
</body>
</html>