3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
8 var name = 'Test Layer';
9 var url = "http://octo.metacarta.com/cgi-bin/mapserv";
10 var params = { map: '/mapdata/vmap_wms.map',
17 * Some of the tests on the Grid class actually use the WMS class.
18 * This is because WMS is a subclass of Grid and it implements the
19 * core functions which are necessary to test the tile-generation
25 function test_Layer_Grid_constructor (t) {
28 layer = new OpenLayers.Layer.Grid(name, url, params, null);
29 t.ok( layer instanceof OpenLayers.Layer.Grid, "returns OpenLayers.Layer.Grid object" );
30 t.eq( layer.buffer, 2, "buffer default is 2");
31 t.eq( layer.ratio, 1.5, "ratio default is 1.5");
32 t.eq( layer.numLoadingTiles, 0, "numLoadingTiles starts at 0");
33 t.ok( layer.events.listeners["tileloaded"] != null, "'tileloaded' event added to layer's event types");
34 t.ok( OpenLayers.Util.indexOf(layer.events.eventTypes, 'tileloaded') != -1 ,"'tileloaded' event added to layer's event types");
36 //regression testing for #1502
37 t.ok( layer.events.listeners['tileloaded'].length == 0, "no listeners for tileloaded preregister");
40 var func = function() {};
41 layer.events.register('tileloaded', obj, func);
43 t.ok( layer.events.listeners['tileloaded'].length == 1, "one listener for tileloaded after register");
47 function test_Layer_Grid_inittiles (t) {
49 var map = new OpenLayers.Map('map');
50 layer = new OpenLayers.Layer.WMS(name, url, params);
52 map.setCenter(new OpenLayers.LonLat(0,0),5);
53 t.eq( layer.grid.length, 7, "Grid rows is correct." );
54 t.eq( layer.grid[0].length, 6, "Grid cols is correct." );
58 function test_Layer_Grid_clearTiles (t) {
61 var map = new OpenLayers.Map('map');
62 layer = new OpenLayers.Layer.WMS(name, url, params);
65 map.setCenter(new OpenLayers.LonLat(0,0));
67 var numTiles = layer.grid.length * layer.grid[0].length;
69 //our count of how many times tile.destroy() is called
72 //this will get set to false if we try to destroy a tile that has
74 allTilesUnhooked = true;
76 OpenLayers.Tile.Image.prototype._destroy =
77 OpenLayers.Tile.Image.prototype.destroy;
79 OpenLayers.Tile.Image.prototype.destroy = function() {
81 allTilesUnhooked = false;
86 layer.removeTileMonitoringHooks = function(tile) {
92 t.ok( layer.grid != null, "layer.grid does not get nullified" );
93 t.eq(tilesDeleted, numTiles, "all tiles destroy()ed properly");
94 t.ok(allTilesUnhooked, "all tiles unhooked before being destroyed");
96 OpenLayers.Tile.Image.prototype.destroy =
97 OpenLayers.Tile.Image.prototype._destroy;
102 function test_Layer_Grid_getTilesBounds(t) {
105 layer = new OpenLayers.Layer.WMS(name, url, params);
109 var bl = { bounds: new OpenLayers.Bounds(1,2,0,0)};
110 var tr = { bounds: new OpenLayers.Bounds(0,0,3,4)};
111 layer.grid = [ [6, tr],
114 var bounds = layer.getTilesBounds();
115 var testBounds = new OpenLayers.Bounds(1,2,3,4);
117 t.ok( bounds.equals(testBounds), "getTilesBounds() returns correct bounds");
119 var bounds = layer.getGridBounds();
121 t.ok( bounds.equals(testBounds), "getGridBounds() wrapper works the same as getTilesBounds.");
125 bounds = layer.getTilesBounds();
127 t.ok(bounds == null, "getTilesBounds() on a tile-less grid returns null");
131 var singleTile = { bounds: new OpenLayers.Bounds(1,2,3,4)};
132 layer.grid = [ [ singleTile ] ];
133 bounds = layer.getTilesBounds();
135 t.ok( bounds.equals(testBounds), "getTilesBounds() returns correct bounds");
139 function test_Layer_Grid_getResolution(t) {
142 var map = new OpenLayers.Map('map');
143 layer = new OpenLayers.Layer.WMS(name, url, params);
148 t.eq( layer.getResolution(), 0.0439453125, "getResolution() returns correct value");
151 function test_Layer_Grid_getZoomForExtent(t) {
155 var map = new OpenLayers.Map('map');
156 layer = new OpenLayers.Layer.WMS(name, url, params);
159 bounds = new OpenLayers.Bounds(10,10,12,12);
160 zoom = layer.getZoomForExtent(bounds);
162 t.eq( zoom, 8, "getZoomForExtent() returns correct value");
164 bounds = new OpenLayers.Bounds(10,10,100,100);
165 zoom = layer.getZoomForExtent(bounds);
167 t.eq( zoom, 2, "getZoomForExtent() returns correct value");
170 function test_Layer_Grid_moveTo(t) {
174 var map = new OpenLayers.Map('map');
175 layer = new OpenLayers.Layer.WMS(name, url, params);
176 layer.destroy = function() {}; //we're going to do funky things with the grid
179 //make sure null bounds doesnt cause script error.
180 // no test necessary, just action
181 map.getExtent = function() { return null; }
182 layer.singleTile = false;
183 layer.moveTo(); //checks to make sure null bounds doesnt break us
188 layer.initSingleTile = function(bounds) {
189 g_WhichFunc = "InitSingle";
192 layer.initGriddedTiles = function(bounds) {
193 g_WhichFunc = "InitGridded";
196 layer.moveGriddedTiles = function(bounds) {
197 g_WhichFunc = "MoveGridded";
200 var clearTestBounds = function() {
205 //default map extent (tested every time below)
206 b = new OpenLayers.Bounds(0,0,100,100);
207 map.getExtent = function() {
210 var tilesBounds = null;
211 layer.getTilesBounds = function() {
222 layer.singleTile = false;
224 t.ok(g_Bounds.equals(b), "if grid is empty, initGridded called");
228 layer.singleTile = true;
230 t.ok(g_Bounds.equals(b), "if grid is empty, initSingleTile called");
234 layer.grid = [ [ {} ] ];
238 layer.singleTile = false;
239 layer.moveTo(null, zoomChanged);
240 t.ok(g_Bounds.equals(b), "if layer has grid but zoomChanged is called, initGridded called");
244 layer.singleTile = true;
245 layer.moveTo(null, zoomChanged);
246 t.ok(g_Bounds.equals(b), "if layer has grid but zoomChanged is called, initSingleTile called");
249 layer.getTilesBounds = function() {
257 layer.grid = [ [ {} ] ];
260 layer.singleTile = true;
267 tilesBounds = new OpenLayers.Bounds(-10,-10,110,110);
268 layer.moveTo(null, zoomChanged, dragging);
269 t.ok(g_Bounds == null, "if dragging and tile in bounds, no init()");
273 tilesBounds = new OpenLayers.Bounds(10,10,120,120);
274 layer.moveTo(null, zoomChanged, dragging);
275 t.ok(g_Bounds == null, "if dragging and tile out of bounds, no init()");
282 tilesBounds = new OpenLayers.Bounds(-10,-10,110,110);
283 layer.moveTo(null, zoomChanged, dragging);
284 t.ok(g_Bounds == null, "if dragging and tile in bounds, no init()");
288 tilesBounds = new OpenLayers.Bounds(10,10,120,120);
289 layer.moveTo(null, zoomChanged, dragging);
290 t.ok(g_WhichFunc == "InitSingle", "if not dragging and tile out of bounds, we call initSingleTile()");
291 t.ok(g_Bounds.equals(b), "if not dragging and tile out of bounds, we call initSingleTile() with correct bounds");
295 layer.grid = [ [ {} ] ];
296 layer.singleTile = false;
300 tilesBounds = new OpenLayers.Bounds(-150,-150,-120,-120);
301 layer.moveTo(null, zoomChanged);
302 t.ok(g_WhichFunc == "InitGridded", "if tiles drastically out of bounds, we call initGriddedTile()");
303 t.ok(g_Bounds.equals(b), "if tiles drastically out of bounds, we call initGriddedTile() with correct bounds");
307 tilesBounds = new OpenLayers.Bounds(10,10,120,120);
308 layer.moveTo(null, zoomChanged);
309 t.ok(g_WhichFunc == "MoveGridded", "if tiles not drastically out of bounds, we call moveGriddedTile()");
310 t.ok(g_Bounds.equals(b), "if tiles not drastically out of bounds, we call moveGriddedTile() with correct bounds");
313 /** THIS WOULD BE WHERE THE TESTS WOULD GO FOR
319 function 08_Layer_Grid_insertColumn(t) {
322 function 09_Layer_Grid_insertRow(t) {
328 function test_Layer_Grid_clone(t) {
331 var options = {tileSize: new OpenLayers.Size(500,50)};
332 var map = new OpenLayers.Map('map', options);
333 layer = new OpenLayers.Layer.Grid(name, url, params);
336 layer.grid = [ [6, 7],
339 var clone = layer.clone();
341 t.ok( clone.grid != layer.grid, "clone does not copy grid");
342 t.ok( clone.grid.length == 0, "clone creates a new array instead");
344 t.ok( clone.tileSize.equals(layer.tileSize), "tileSize correctly cloned");
346 layer.tileSize.w += 40;
348 t.eq( clone.tileSize.w, 500, "changing layer.tileSize does not change clone.tileSize -- a fresh copy was made, not just copied reference");
350 t.eq( clone.alpha, layer.alpha, "alpha copied correctly");
355 function test_Layer_Grid_setTileSize(t) {
358 OpenLayers.Layer.HTTPRequest.prototype._setTileSize =
359 OpenLayers.Layer.HTTPRequest.prototype.setTileSize;
361 OpenLayers.Layer.HTTPRequest.prototype.setTileSize = function(size) {
366 layer = new OpenLayers.Layer.Grid(name, url, params, {
369 mapSize = new OpenLayers.Size(100,1000);
371 getSize: function() { return mapSize; }
377 var idealSize = new OpenLayers.Size(150,1500);
378 t.ok( g_Size && g_Size.equals(idealSize), "correctly calculated tile size passed to superclass setTileSize() function");
380 OpenLayers.Layer.HTTPRequest.prototype.setTileSize =
381 OpenLayers.Layer.HTTPRequest.prototype._setTileSize;
384 function test_Layer_Grid_initSingleTile(t) {
387 layer = new OpenLayers.Layer.Grid(name, url, params, {
392 var bounds = new OpenLayers.Bounds(-10,10,50,100);
394 var desiredTileBounds = new OpenLayers.Bounds(-40,-35,80,145);
395 var desiredUL = new OpenLayers.LonLat(-40,145);
399 getLayerPxFromLonLat: function(ul) {
400 t.ok(ul.equals(desiredUL), "correct ul passed to translation");
407 t.ok(true, "newly created tile has been drawn");
410 layer.addTile = function(tileBounds, px) {
411 t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to addTile to create new tile");
412 t.ok(px == translatedPX, "correct tile px passed to addTile to create new tile");
415 layer.addTileMonitoringHooks = function(tile) {
416 t.ok(tile == newTile, "adding monitoring hooks to the newly added tile");
418 layer.removeExcessTiles = function(x,y) {
419 t.ok(x == 1 && y == 1, "removeExcessTiles called")
424 layer.initSingleTile(bounds);
426 t.ok(layer.grid[0][0] == newTile, "grid's 0,0 is set to the newly created tile");
429 moveTo: function(tileBounds, px) {
430 t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to tile.moveTo()");
431 t.ok(px == translatedPX, "correct tile px passed to tile.moveTo()");
434 layer.grid = [[ tile ]];
435 layer.initSingleTile(bounds);
439 function test_Layer_Grid_addTileMonitoringHooks(t) {
442 layer = new OpenLayers.Layer.Grid();
444 'triggerEvent': function(str) {
451 register: function(name, obj, func) {
452 g_registered[name] = [obj, func];
460 layer.addTileMonitoringHooks(tile);
463 t.ok(tile.onLoadStart != null, "onLoadStart function created and added to tile");
464 entry = g_registered["loadstart"];
465 t.ok( entry && entry[0] == layer && entry[1] == tile.onLoadStart, "loadstart correctly registered");
467 layer.numLoadingTiles = 0;
469 tile.onLoadStart.apply(layer);
471 t.eq(g_events[0], "loadstart", "loadstart event triggered when numLoadingTiles is 0");
472 t.eq(layer.numLoadingTiles, 1, "numLoadingTiles incremented");
475 tile.onLoadStart.apply(layer);
476 t.eq(g_events.length, 0, "loadstart event not triggered when numLoadingTiles is not 0");
477 t.eq(layer.numLoadingTiles, 2, "numLoadingTiles incremented");
481 t.ok(tile.onLoadEnd != null, "onLoadEnd function created and added to tile");
482 entry = g_registered["loadend"];
483 t.ok( entry && entry[0] == layer && entry[1] == tile.onLoadEnd, "loadend correctly registered");
485 layer.numLoadingTiles = 2;
487 tile.onLoadEnd.apply(layer);
488 t.eq(g_events[0], "tileloaded", "tileloaded triggered when numLoadingTiles is > 0");
489 t.eq(g_events.length, 1, "loadend event not triggered when numLoadingTiles is > 0");
490 t.eq(layer.numLoadingTiles, 1, "numLoadingTiles decremented");
494 tile.onLoadEnd.apply(layer);
495 t.eq(g_events[0], "tileloaded", "tileloaded triggered when numLoadingTiles is 0");
496 t.eq(g_events[1], "loadend", "loadend event triggered when numLoadingTiles is 0");
497 t.eq(layer.numLoadingTiles, 0, "numLoadingTiles decremented");
500 function test_Layer_Grid_removeTileMonitoringHooks(t) {
503 layer = new OpenLayers.Layer.Grid();
508 unload: function() {},
510 unregister: function(name, obj, func) {
511 g_unregistered[name] = [obj, func];
513 un: OpenLayers.Events.prototype.un
519 layer.removeTileMonitoringHooks(tile);
521 entry = g_unregistered["loadstart"];
522 t.ok( entry && entry[0] == layer && entry[1] == tile.onLoadStart, "loadstart correctly unregistered");
524 entry = g_unregistered["loadend"];
525 t.ok( entry && entry[0] == layer && entry[1] == tile.onLoadEnd, "loadend correctly unregistered");
528 function test_Layer_Grid_tileSizeIsInteger(t) {
531 var map = new OpenLayers.Map('map');
532 var layer = new OpenLayers.Layer.Grid(name, url, params, {
536 map.addLayers([layer]);
538 width = layer.tileSize.w;
539 height = layer.tileSize.h;
540 t.ok(width == parseInt(width) && height == parseInt(height), "calculated tileSize width/height are integer values");
542 function test_Layer_Grid_getTileBounds(t) {
544 var map = new OpenLayers.Map("map2");
545 var url = "http://octo.metacarta.com/cgi-bin/mapserv";
546 layer = new OpenLayers.Layer.WMS(name, url, params);
548 var newParams = { layers: 'sooper',
549 chickpeas: 'image/png'};
552 map.zoomToMaxExtent();
554 var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200));
555 t.eq(bounds.toBBOX(), "-180,-90,0,90", "get tile bounds returns correct bounds");
556 map.pan(200,0, {animate:false});
557 var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200));
558 t.eq(bounds.toBBOX(), "0,-90,180,90", "get tile bounds returns correct bounds after pan");
561 function test_Layer_Grid_moveTo_buffer_calculation (t) {
564 var map = new OpenLayers.Map( 'map3' ); // odd map size
565 var layer0 = new OpenLayers.Layer.WMS( "0 buffer: OpenLayers WMS",
566 "http://labs.metacarta.com/wms/vmap0",
567 {layers: 'basic'}, {'buffer':0} );
568 map.addLayer(layer0);
570 var layer1 = new OpenLayers.Layer.WMS( "1 buffer: OpenLayers WMS",
571 "http://labs.metacarta.com/wms/vmap0",
572 {layers: 'basic'}, {'buffer':1} );
573 map.addLayer(layer1);
575 var layer2 = new OpenLayers.Layer.WMS( "2 buffer: OpenLayers WMS",
576 "http://labs.metacarta.com/wms/vmap0",
577 {layers: 'basic'}, {'buffer':2} );
578 map.addLayer(layer2);
580 map.setCenter(new OpenLayers.LonLat(0, 0), 4);
581 t.eq( layer0.grid.length, 3, "Grid rows with buffer:0" );
582 map.setBaseLayer(layer1);
583 t.eq( layer1.grid.length, 4, "Grid rows with buffer:1" );
584 map.setBaseLayer(layer2);
585 t.eq( layer2.grid.length, 6, "Grid rows with buffer:2" );
587 // zooming in on Greenland exercises the bug from pre-r4313
588 map.setCenter(new OpenLayers.LonLat(0, 90), 4);
589 t.eq( layer0.grid.length, 3, "Grid rows with buffer:0" );
590 map.setBaseLayer(layer1);
591 t.eq( layer1.grid.length, 4, "Grid rows with buffer:1" );
592 map.setBaseLayer(layer2);
593 t.eq( layer2.grid.length, 6, "Grid rows with buffer:2" );
596 function test_Layer_Grid_destroy (t) {
600 var map = new OpenLayers.Map('map');
601 layer = new OpenLayers.Layer.Grid(name, url, params);
604 t.eq( layer.grid, null, "layer.grid is null after destroy" );
605 t.eq( layer.tileSize, null, "layer.tileSize is null after destroy" );
608 //test with tile creation
609 layer = new OpenLayers.Layer.WMS(name, url, params);
612 map.setCenter(new OpenLayers.LonLat(0,0), 10);
615 //grab a reference to one of the tiles
616 var tile = layer.grid[1][1];
617 t.eq( tile.imgDiv.className, "olTileImage", "Tile has an image" );
620 t.eq( tile.imgDiv, null, "Tile destroyed" );
622 t.ok( layer.grid == null, "tiles appropriately destroyed")
629 <div id="map" style="width:499px;height:549px;display:none"></div>
630 <div id="map2" style="width:500px;height:550px;display:none"></div>
631 <div id="map3" style="width:594px;height:464px;display:none"></div>