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
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
var layer;
var name = 'Test Layer';
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
var params = { map: '/mapdata/vmap_wms.map',
layers: 'basic',
format: 'image/png'};
/**
* NOTE TO READER:
*
* Some of the tests on the Grid class actually use the WMS class.
* This is because WMS is a subclass of Grid and it implements the
* core functions which are necessary to test the tile-generation
* mechanism.
*
*/
function test_Layer_Grid_constructor (t) {
t.plan( 8 );
layer = new OpenLayers.Layer.Grid(name, url, params, null);
t.ok( layer instanceof OpenLayers.Layer.Grid, "returns OpenLayers.Layer.Grid object" );
t.eq( layer.buffer, 0, "buffer default is 0");
t.eq( layer.ratio, 1.5, "ratio default is 1.5");
t.eq( layer.numLoadingTiles, 0, "numLoadingTiles starts at 0");
t.ok( layer.events.listeners["tileloaded"] != null, "'tileloaded' event added to layer's event types");
t.ok( OpenLayers.Util.indexOf(layer.events.eventTypes, 'tileloaded') != -1 ,"'tileloaded' event added to layer's event types");
//regression testing for #1502
t.ok( layer.events.listeners['tileloaded'].length == 0, "no listeners for tileloaded preregister");
var obj = {};
var func = function() {};
layer.events.register('tileloaded', obj, func);
t.ok( layer.events.listeners['tileloaded'].length == 1, "one listener for tileloaded after register");
}
function test_Layer_Grid_inittiles (t) {
t.plan( 2 );
var map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.WMS(name, url, params, {buffer:2});
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0,0),5);
t.eq( layer.grid.length, 7, "Grid rows is correct." );
t.eq( layer.grid[0].length, 6, "Grid cols is correct." );
}
function test_Layer_Grid_clearTiles (t) {
t.plan(3);
var map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.WMS(name, url, params);
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0,0));
var numTiles = layer.grid.length * layer.grid[0].length;
//our count of how many times tile.destroy() is called
tilesDeleted = 0;
//this will get set to false if we try to destroy a tile that has
// not been unhookedv
allTilesUnhooked = true;
OpenLayers.Tile.Image.prototype._destroy =
OpenLayers.Tile.Image.prototype.destroy;
OpenLayers.Tile.Image.prototype.destroy = function() {
if (!this.unhooked) {
allTilesUnhooked = false;
}
tilesDeleted++;
}
layer.removeTileMonitoringHooks = function(tile) {
tile.unhooked = true;
}
layer.clearGrid();
t.ok( layer.grid != null, "layer.grid does not get nullified" );
t.eq(tilesDeleted, numTiles, "all tiles destroy()ed properly");
t.ok(allTilesUnhooked, "all tiles unhooked before being destroyed");
OpenLayers.Tile.Image.prototype.destroy =
OpenLayers.Tile.Image.prototype._destroy;
}
function test_Layer_Grid_getTilesBounds(t) {
t.plan( 4 );
layer = new OpenLayers.Layer.WMS(name, url, params);
//normal grid
var bl = { bounds: new OpenLayers.Bounds(1,2,0,0)};
var tr = { bounds: new OpenLayers.Bounds(0,0,3,4)};
layer.grid = [ [6, tr],
[bl, 7]];
var bounds = layer.getTilesBounds();
var testBounds = new OpenLayers.Bounds(1,2,3,4);
t.ok( bounds.equals(testBounds), "getTilesBounds() returns correct bounds");
var bounds = layer.getGridBounds();
t.ok( bounds.equals(testBounds), "getGridBounds() wrapper works the same as getTilesBounds.");
//no tiles
layer.grid = [];
bounds = layer.getTilesBounds();
t.ok(bounds == null, "getTilesBounds() on a tile-less grid returns null");
//singleTile
var singleTile = { bounds: new OpenLayers.Bounds(1,2,3,4)};
layer.grid = [ [ singleTile ] ];
bounds = layer.getTilesBounds();
t.ok( bounds.equals(testBounds), "getTilesBounds() returns correct bounds");
}
function test_Layer_Grid_getResolution(t) {
t.plan( 1 );
var map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.WMS(name, url, params);
map.addLayer(layer);
map.zoom = 5;
t.eq( layer.getResolution(), 0.0439453125, "getResolution() returns correct value");
}
function test_Layer_Grid_getZoomForExtent(t) {
t.plan( 2 );
var bounds, zoom;
var map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.WMS(name, url, params);
map.addLayer(layer);
bounds = new OpenLayers.Bounds(10,10,12,12);
zoom = layer.getZoomForExtent(bounds);
t.eq( zoom, 8, "getZoomForExtent() returns correct value");
bounds = new OpenLayers.Bounds(10,10,100,100);
zoom = layer.getZoomForExtent(bounds);
t.eq( zoom, 2, "getZoomForExtent() returns correct value");
}
function test_Layer_Grid_moveTo(t) {
t.plan(14);
var map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.WMS(name, url, params);
layer.destroy = function() {}; //we're going to do funky things with the grid
map.addLayer(layer);
//make sure null bounds doesnt cause script error.
// no test necessary, just action
map.getExtent = function() { return null; }
layer.singleTile = false;
layer.moveTo(); //checks to make sure null bounds doesnt break us
//observing globals
layer.initSingleTile = function(bounds) {
g_WhichFunc = "InitSingle";
g_Bounds = bounds;
};
layer.initGriddedTiles = function(bounds) {
g_WhichFunc = "InitGridded";
g_Bounds = bounds;
};
layer._moveGriddedTiles = function() {
g_WhichFunc = "MoveGridded";
g_Bounds = layer.map.getExtent();
};
var clearTestBounds = function() {
g_WhichFunc = null;
g_Bounds = null;
};
//default map extent (tested every time below)
b = new OpenLayers.Bounds(0,0,100,100);
map.getExtent = function() {
return b;
};
var tilesBounds = null;
layer.getTilesBounds = function() {
return tilesBounds;
}
//FORCE
//empty grid
layer.grid = [];
//grid
clearTestBounds();
layer.singleTile = false;
layer.moveTo()
t.ok(g_Bounds.equals(b), "if grid is empty, initGridded called");
//singletile
clearTestBounds();
layer.singleTile = true;
layer.moveTo()
t.ok(g_Bounds.equals(b), "if grid is empty, initSingleTile called");
//zoomChanged
zoomChanged = true;
layer.grid = [ [ {} ] ];
//grid
clearTestBounds();
layer.singleTile = false;
layer.moveTo(null, zoomChanged);
t.ok(g_Bounds.equals(b), "if layer has grid but zoomChanged is called, initGridded called");
//singletile
clearTestBounds();
layer.singleTile = true;
layer.moveTo(null, zoomChanged);
t.ok(g_Bounds.equals(b), "if layer has grid but zoomChanged is called, initSingleTile called");
//NO FORCE
zoomChanged = false;
layer.grid = [ [ {} ] ];
//single tile
layer.singleTile = true;
//DRAGGING
var dragging = true;
//in bounds
clearTestBounds();
tilesBounds = new OpenLayers.Bounds(-10,-10,110,110);
layer.moveTo(null, zoomChanged, dragging);
t.ok(g_Bounds == null, "if dragging and tile in bounds, no init()");
//out bounds
clearTestBounds();
tilesBounds = new OpenLayers.Bounds(10,10,120,120);
layer.moveTo(null, zoomChanged, dragging);
t.ok(g_Bounds == null, "if dragging and tile out of bounds, no init()");
//NOT DRAGGING
dragging = false;
//in bounds
clearTestBounds();
tilesBounds = new OpenLayers.Bounds(-10,-10,110,110);
layer.moveTo(null, zoomChanged, dragging);
t.ok(g_Bounds == null, "if dragging and tile in bounds, no init()");
//out bounds
clearTestBounds();
tilesBounds = new OpenLayers.Bounds(10,10,120,120);
layer.moveTo(null, zoomChanged, dragging);
t.ok(g_WhichFunc == "InitSingle", "if not dragging and tile out of bounds, we call initSingleTile()");
t.ok(g_Bounds.equals(b), "if not dragging and tile out of bounds, we call initSingleTile() with correct bounds");
//gridded
layer.grid = [ [ {} ] ];
layer.singleTile = false;
// drastic pan
clearTestBounds();
tilesBounds = new OpenLayers.Bounds(-150,-150,-120,-120);
layer.moveTo(null, zoomChanged);
t.ok(g_WhichFunc == "InitGridded", "if tiles drastically out of bounds, we call initGriddedTile()");
t.ok(g_Bounds.equals(b), "if tiles drastically out of bounds, we call initGriddedTile() with correct bounds");
//regular move
clearTestBounds();
tilesBounds = new OpenLayers.Bounds(10,10,120,120);
g_WhichFunc = null;
layer.moveTo(null, zoomChanged);
t.eq(g_WhichFunc, null, "moveGriddedTiles is delayed - not called yet");
t.delay_call(0.2, function() {
t.ok(g_WhichFunc == "MoveGridded", "if tiles not drastically out of bounds, we call moveGriddedTile()");
t.ok(g_Bounds.equals(b), "if tiles not drastically out of bounds, we call moveGriddedTile() with correct bounds");
});
}
/** THIS WOULD BE WHERE THE TESTS WOULD GO FOR
*
* -insertColumn
* -insertRow
*
function 08_Layer_Grid_insertColumn(t) {
}
function 09_Layer_Grid_insertRow(t) {
}
*
*/
function test_Layer_Grid_clone(t) {
t.plan(5);
var options = {tileSize: new OpenLayers.Size(500,50)};
var map = new OpenLayers.Map('map', options);
layer = new OpenLayers.Layer.Grid(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.grid.length == 0, "clone creates a new array instead");
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;
}
function test_Layer_Grid_setTileSize(t) {
t.plan(1);
OpenLayers.Layer.HTTPRequest.prototype._setTileSize =
OpenLayers.Layer.HTTPRequest.prototype.setTileSize;
OpenLayers.Layer.HTTPRequest.prototype.setTileSize = function(size) {
g_Size = size;
};
layer = new OpenLayers.Layer.Grid(name, url, params, {
singleTile: true
});
mapSize = new OpenLayers.Size(100,1000);
layer.map = {
getSize: function() { return mapSize; }
}
g_Size = null;
layer.setTileSize();
var idealSize = new OpenLayers.Size(150,1500);
t.ok( g_Size && g_Size.equals(idealSize), "correctly calculated tile size passed to superclass setTileSize() function");
OpenLayers.Layer.HTTPRequest.prototype.setTileSize =
OpenLayers.Layer.HTTPRequest.prototype._setTileSize;
}
function test_Layer_Grid_initSingleTile(t) {
t.plan( 11 );
layer = new OpenLayers.Layer.Grid(name, url, params, {
singleTile: true,
ratio: 2
});
var bounds = new OpenLayers.Bounds(-10,10,50,100);
var desiredTileBounds = new OpenLayers.Bounds(-40,-35,80,145);
var desiredUL = new OpenLayers.LonLat(-40,145);
translatedPX = {};
layer.map = {
getLayerPxFromLonLat: function(ul) {
t.ok(ul.equals(desiredUL), "correct ul passed to translation");
return translatedPX;
}
}
var newTile = {
draw: function() {
t.ok(true, "newly created tile has been drawn");
}
};
layer.addTile = function(tileBounds, px) {
t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to addTile to create new tile");
t.ok(px == translatedPX, "correct tile px passed to addTile to create new tile");
return newTile;
};
layer.addTileMonitoringHooks = function(tile) {
t.ok(tile == newTile, "adding monitoring hooks to the newly added tile");
};
layer.removeExcessTiles = function(x,y) {
t.ok(x == 1 && y == 1, "removeExcessTiles called")
};
layer.grid = [];
layer.initSingleTile(bounds);
t.ok(layer.grid[0][0] == newTile, "grid's 0,0 is set to the newly created tile");
var tile = {
moveTo: function(tileBounds, px) {
t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to tile.moveTo()");
t.ok(px == translatedPX, "correct tile px passed to tile.moveTo()");
}
};
layer.grid = [[ tile ]];
layer.initSingleTile(bounds);
}
function test_Layer_Grid_addTileMonitoringHooks(t) {
t.plan(14);
layer = new OpenLayers.Layer.Grid();
layer.events = {
'triggerEvent': function(str) {
g_events.push(str);
}
}
var tile = {
events: {
register: function(name, obj, func) {
g_registered[name] = [obj, func];
}
}
}
g_registered = {};
g_events = [];
layer.addTileMonitoringHooks(tile);
//loadstart
t.ok(tile.onLoadStart != null, "onLoadStart function created and added to tile");
entry = g_registered["loadstart"];
t.ok( entry && entry[0] == layer && entry[1] == tile.onLoadStart, "loadstart correctly registered");
layer.numLoadingTiles = 0;
g_events = [];
tile.onLoadStart.apply(layer);
t.eq(g_events[0], "loadstart", "loadstart event triggered when numLoadingTiles is 0");
t.eq(layer.numLoadingTiles, 1, "numLoadingTiles incremented");
g_events = [];
tile.onLoadStart.apply(layer);
t.eq(g_events.length, 0, "loadstart event not triggered when numLoadingTiles is not 0");
t.eq(layer.numLoadingTiles, 2, "numLoadingTiles incremented");
//loadend
t.ok(tile.onLoadEnd != null, "onLoadEnd function created and added to tile");
entry = g_registered["loadend"];
t.ok( entry && entry[0] == layer && entry[1] == tile.onLoadEnd, "loadend correctly registered");
layer.numLoadingTiles = 2;
g_events = [];
tile.onLoadEnd.apply(layer);
t.eq(g_events[0], "tileloaded", "tileloaded triggered when numLoadingTiles is > 0");
t.eq(g_events.length, 1, "loadend event not triggered when numLoadingTiles is > 0");
t.eq(layer.numLoadingTiles, 1, "numLoadingTiles decremented");
g_events = [];
tile.onLoadEnd.apply(layer);
t.eq(g_events[0], "tileloaded", "tileloaded triggered when numLoadingTiles is 0");
t.eq(g_events[1], "loadend", "loadend event triggered when numLoadingTiles is 0");
t.eq(layer.numLoadingTiles, 0, "numLoadingTiles decremented");
}
function test_Layer_Grid_removeTileMonitoringHooks(t) {
t.plan(2);
layer = new OpenLayers.Layer.Grid();
var tile = {
onLoadStart: {},
onLoadEnd: {},
unload: function() {},
events: {
unregister: function(name, obj, func) {
g_unregistered[name] = [obj, func];
},
un: OpenLayers.Events.prototype.un
}
}
g_unregistered = {};
layer.removeTileMonitoringHooks(tile);
entry = g_unregistered["loadstart"];
t.ok( entry && entry[0] == layer && entry[1] == tile.onLoadStart, "loadstart correctly unregistered");
entry = g_unregistered["loadend"];
t.ok( entry && entry[0] == layer && entry[1] == tile.onLoadEnd, "loadend correctly unregistered");
}
function test_Layer_Grid_tileSizeIsInteger(t) {
t.plan(1);
var map = new OpenLayers.Map('map');
var layer = new OpenLayers.Layer.Grid(name, url, params, {
singleTile: true,
ratio: 1.5
});
map.addLayers([layer]);
width = layer.tileSize.w;
height = layer.tileSize.h;
t.ok(width == parseInt(width) && height == parseInt(height), "calculated tileSize width/height are integer values");
}
function test_Layer_Grid_getTileBounds(t) {
t.plan(2);
var map = new OpenLayers.Map("map2");
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.WMS(name, url, params);
var newParams = { layers: 'sooper',
chickpeas: 'image/png'};
map.addLayer(layer);
map.zoomToMaxExtent();
map.zoomIn();
var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200));
t.eq(bounds.toBBOX(), "-180,-90,0,90", "get tile bounds returns correct bounds");
map.pan(200,0, {animate:false});
var bounds = layer.getTileBounds(new OpenLayers.Pixel(200,200));
t.eq(bounds.toBBOX(), "0,-90,180,90", "get tile bounds returns correct bounds after pan");
}
function test_Layer_Grid_moveTo_buffer_calculation (t) {
t.plan(6);
var map = new OpenLayers.Map( 'map3' ); // odd map size
var layer0 = new OpenLayers.Layer.WMS( "0 buffer: OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}, {'buffer':0} );
map.addLayer(layer0);
var layer1 = new OpenLayers.Layer.WMS( "1 buffer: OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}, {'buffer':1} );
map.addLayer(layer1);
var layer2 = new OpenLayers.Layer.WMS( "2 buffer: OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'}, {'buffer':2} );
map.addLayer(layer2);
map.setCenter(new OpenLayers.LonLat(0, 0), 4);
t.eq( layer0.grid.length, 3, "Grid rows with buffer:0" );
map.setBaseLayer(layer1);
t.eq( layer1.grid.length, 4, "Grid rows with buffer:1" );
map.setBaseLayer(layer2);
t.eq( layer2.grid.length, 6, "Grid rows with buffer:2" );
// zooming in on Greenland exercises the bug from pre-r4313
map.setCenter(new OpenLayers.LonLat(0, 90), 4);
t.eq( layer0.grid.length, 3, "Grid rows with buffer:0" );
map.setBaseLayer(layer1);
t.eq( layer1.grid.length, 4, "Grid rows with buffer:1" );
map.setBaseLayer(layer2);
t.eq( layer2.grid.length, 6, "Grid rows with buffer:2" );
}
function test_Layer_Grid_destroy (t) {
t.plan( 8 );
var map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.Grid(name, url, params);
map.addLayer(layer);
layer.destroy();
t.eq( layer.grid, null, "layer.grid is null after destroy" );
t.eq( layer.tileSize, null, "layer.tileSize is null after destroy" );
//test with tile creation
layer = new OpenLayers.Layer.WMS(name, url, params);
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0,0), 10);
map.setCenter(new OpenLayers.LonLat(1,1));
//grab a reference to one of the tiles
var tile = layer.grid[1][1];
t.eq( tile.imgDiv.className, "olTileImage", "Tile has an image" );
layer.destroy();
t.eq( tile.imgDiv, null, "Tile destroyed" );
t.eq( layer.timerId, null, "Tile loading timeout cleared");
t.ok( layer.grid == null, "tiles appropriately destroyed")
// destroy after remove from map
layer = new OpenLayers.Layer.WMS(name, url, params);
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0,0), 10);
map.removeLayer(layer);
layer.destroy();
t.eq( layer.grid, null, "layer.grid is null after destroy" );
t.eq( layer.tileSize, null, "layer.tileSize is null after destroy" );
}
</script>
</head>
<body>
<div id="map" style="width:499px;height:549px;display:none"></div>
<div id="map2" style="width:500px;height:550px;display:none"></div>
<div id="map3" style="width:594px;height:464px;display:none"></div>
</body>
</html>
|