Files
openlayers/tests/test_Map.html
euzuro 06cbeb2f11 fix for #60
conversions to/from lonlat/px need to take into account
the offset of the layersContainerDiv. 


I have introduced the following functions for converting
between layer and screen pixel values:

getLayerPxFromScreenPx() and getScreenPxFromLayerPx()

they are pretty self-explanitory.


I then renamed:

getPixelFromLonLat() and getLonLatFromPixel()

to:

getScreenPxFromLonLat() and getLonLatFroScreenmPx()

and added:

getLayerPxFromLonLat() and getLonLatFromLayerPx()


updates were made throughout the code, demos, and tests
so everything should still run smoothly.

-e-

git-svn-id: http://svn.openlayers.org/trunk/openlayers@329 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2006-05-24 18:45:50 +00:00

155 lines
6.8 KiB
HTML

<html>
<head>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript"><!--
var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
var map;
function test_01_Map_constructor (t) {
t.plan( 10 );
map = new OpenLayers.Map('map'); // no longer need to call $(), constructor does it
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.ok( map.events instanceof OpenLayers.Events, "map.events is an OpenLayers.Events" );
t.ok( map.maxExtent instanceof OpenLayers.Bounds, "map.maxExtent is an OpenLayers.Bounds" );
t.ok( map.maxZoomLevel > 0, "map.maxZoomLevel is set" );
t.ok( map.maxResolution > 0, "map.maxResolution is set" );
}
function test_02_Map_center(t) {
t.plan(5);
map = new OpenLayers.Map($('map'));
map.setCenter(new OpenLayers.LonLat(2,1), 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.eq( map.getResolution(), map.maxResolution, "map.getResolution() == map.maxResolution");
t.eq( map.getCenter().lon, 2, "map center lon is correct after calling setCenter");
t.eq( map.getCenter().lat, 1, "map center lat is correct after calling setCenter");
}
function test_03_Map_add_layers(t) {
t.plan(5);
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['Layer'],
"layer1 zIndex is set" );
}
function test_04_Map_options(t) {
t.plan(2);
map = new OpenLayers.Map($('map'), {maxZoomLevel: 5, maxResolution: 3.14159});
t.eq( map.maxZoomLevel, 5, "map.maxZoomLevel set correctly via options hash" );
t.eq( map.maxResolution, 3.14159, "map.maxResolution set correctly via options hash" );
}
function test_05_Map_center(t) {
t.plan(4);
map = new OpenLayers.Map($('map'));
map.setCenter(new OpenLayers.LonLat(2, 1), 0);
map.zoomIn();
t.eq( map.getZoom(), 1, "map.zoom is correct after calling setCenter,zoom in");
t.eq( map.getCenter().lon, 2, "map center lon is correct after calling setCenter, zoom in");
t.eq( map.getCenter().lat, 1, "map center lat 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");
}
function test_06_Map_zoomend_event (t) {
t.plan(3);
map = new OpenLayers.Map('map');
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();
}
function test_07_Map_add_remove_popup (t) {
t.plan(4);
map = new OpenLayers.Map('map');
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);
t.eq(map.popups.indexOf(popup), 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);
t.eq(map.popups.indexOf(popup), -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");
}
function test_08_Map_px_lonlat_translation (t) {
t.plan( 3 );
map = new OpenLayers.Map($('map'));
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
var pixel = new OpenLayers.Pixel(50,150);
var lonlat = map.getLonLatFromScreenPx(pixel);
t.ok( lonlat instanceof OpenLayers.LonLat, "getLonLatFromScreenPx returns valid OpenLayers.LonLat" );
var newPixel = map.getScreenPxFromLonLat(lonlat);
t.ok( newPixel instanceof OpenLayers.Pixel, "getScreenPxFromLonLat 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");
}
function test_99_Map_destroy (t) {
t.plan( 2 );
map = new OpenLayers.Map($('map'));
map.destroy();
t.eq( map.layers, null, "map.layers is null after destroy" );
t.eq( map.controls, null, "map.controls is null after destroy" );
}
// -->
</script>
</head>
<body>
<div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>