3 <script src="../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
7 function test_Layer_constructor (t) {
10 var options = { chicken: 151, foo: "bar", projection: "none" };
11 var layer = new OpenLayers.Layer('Test Layer', options);
13 t.ok( layer instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" );
14 t.eq( layer.CLASS_NAME, "OpenLayers.Layer", "CLASS_NAME variable set correctly");
16 t.eq( layer.name, "Test Layer", "layer.name is correct" );
17 t.ok( layer.id != null, "Layer is given an id");
18 t.ok( layer.projection, "none", "default layer projection correctly set");
19 t.ok( ((layer.chicken == 151) && (layer.foo == "bar")), "layer.options correctly set to Layer Object" );
20 t.ok( ((layer.options["chicken"] == 151) && (layer.options["foo"] == "bar")), "layer.options correctly backed up" );
22 t.ok( typeof layer.div == "object" , "layer.div is created" );
23 t.eq( layer.div.id, layer.id, "layer.div.id is correct" );
25 options.chicken = 552;
27 t.eq( layer.options["chicken"], 151 , "layer.options correctly made fresh copy" );
29 t.eq( layer.isBaseLayer, false, "Default layer is not base layer" );
31 layer = new OpenLayers.Layer('Test Layer');
32 t.ok( layer instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" );
33 t.eq( layer.name, "Test Layer", "layer.name is correct" );
34 t.ok( layer.projection == null, "default layer projection correctly set");
35 t.ok( layer.options instanceof Object, "layer.options correctly initialized as a non-null Object" );
38 function test_Layer_clone (t) {
41 var mapone = new OpenLayers.Map('map');
42 var options = { chicken: 151, foo: "bar", maxResolution: "auto" };
43 var layer = new OpenLayers.Layer('Test Layer', options);
44 mapone.addLayer(layer);
46 // randomly assigned property
49 var clone = layer.clone();
51 t.ok( clone.map == null, "cloned layer has map property set to null")
53 var maptwo = new OpenLayers.Map('map2');
54 maptwo.addLayer(clone);
56 t.ok( clone instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" );
57 t.eq( clone.name, "Test Layer", "default clone.name is correct" );
58 t.ok( ((clone.options["chicken"] == 151) && (clone.options["foo"] == "bar")), "clone.options correctly set" );
59 t.eq(clone.chocolate, 5, "correctly copied randomly assigned property");
61 layer.addOptions({chicken:152});
62 t.eq(clone.options["chicken"], 151, "made a clean copy of options");
64 t.ok( (layer.maxResolution != clone.maxResolution), "maxresolution of clone reset to new map div");
65 t.ok( (layer.minResolution != clone.minResolution), "minresolution of clone reset to new map div");
71 function test_Layer_setName (t) {
75 layer = new OpenLayers.Layer('Test Layer');
76 layer.setName("chicken");
78 t.eq(layer.name, "chicken", "setName() works")
82 function test_Layer_addOptions (t) {
86 var options = { chicken: 151, foo: "bar" };
87 var layer = new OpenLayers.Layer('Test Layer', options);
89 layer.addOptions({bark:55, chicken: 171});
91 t.eq(layer.bark, 55, "addOptions() assigns new option correctly to Layer");
92 t.eq(layer.options.bark, 55, "addOptions() adds new option correctly to backup");
94 t.eq(layer.chicken, 171, "addOptions() overwrites option correctly to Layer");
95 t.eq(layer.options.chicken, 171, "addOptions() overwrites option correctly to backup");
99 function test_Layer_StandardOptionsAccessors (t) {
103 var projection = "chicken";
104 var maxExtent = new OpenLayers.Bounds(50,50,100,100);
105 var maxResolution = 1.5726;
106 var numZoomLevels = 11;
108 var options = { projection: projection,
109 maxExtent: maxExtent,
110 maxResolution: maxResolution,
111 numZoomLevels: numZoomLevels
114 var layer = new OpenLayers.Layer('Test Layer', options);
116 t.eq(layer.projection, projection, "projection set correctly");
117 t.ok(layer.maxExtent.equals(maxExtent), "maxExtent set correctly");
118 t.eq(layer.maxResolution, maxResolution, "maxResolution set correctly");
119 t.eq(layer.numZoomLevels, numZoomLevels, "numZoomLevels set correctly");
122 function test_eventListeners(t) {
125 var method = OpenLayers.Events.prototype.on;
126 // test that events.on is called at layer construction
128 eventListeners: {foo: "bar"}
130 OpenLayers.Events.prototype.on = function(obj) {
131 t.eq(obj, options.eventListeners, "events.on called with eventListeners");
133 var layer = new OpenLayers.Layer("test", options);
134 OpenLayers.Events.prototype.on = method;
137 // if events.on is called again, this will fail due to an extra test
138 // test layer without eventListeners
139 OpenLayers.Events.prototype.on = function(obj) {
140 t.fail("events.on called without eventListeners");
142 var layer2 = new OpenLayers.Layer("test");
143 OpenLayers.Events.prototype.on = method;
147 function test_Layer_initResolutions(t) {
149 var map = new OpenLayers.Map("map");
152 // tests for minResolution, maxResolution, and numZoomLevels
159 layer = new OpenLayers.Layer("test", options);
160 layer.initResolutions();
161 t.eq(layer.minResolution.toPrecision(6), (1.5).toPrecision(6),
162 "(with numZoomLevels) layer minResolution preserved");
163 t.eq(layer.maxResolution.toPrecision(6), (10.5).toPrecision(6),
164 "(with numZoomLevels) layer maxResolution preserved");
165 t.eq(layer.numZoomLevels, 5, "(with numZoomLevels) layer numZoomLevels preserved");
166 t.eq(layer.alwaysInRange, false, "Always in range is set to false due to passed options.")
168 // three tests for minResolution, and maxResolution
174 layer = new OpenLayers.Layer("test", options);
175 layer.initResolutions();
176 t.eq(layer.minResolution.toPrecision(6), (1.5).toPrecision(6),
177 "(without numZoomLevels) layer minResolution preserved");
178 t.eq(layer.maxResolution.toPrecision(6), (10.5).toPrecision(6),
179 "(without numZoomLevels) layer maxResolution preserved");
180 t.eq(layer.numZoomLevels, 3, "(without numZoomLevels) layer numZoomLevels calculated");
182 // three tests for minScale, maxScale, and numZoomLevels
189 layer = new OpenLayers.Layer("test", options);
190 layer.initResolutions();
191 t.eq(layer.minScale.toPrecision(6), (105).toPrecision(6),
192 "(with numZoomLevels) layer minScale preserved");
193 t.eq(layer.maxScale.toPrecision(6), (15).toPrecision(6),
194 "(with numZoomLevels) layer maxScale preserved");
195 t.eq(layer.numZoomLevels, 10, "(with numZoomLevels) layer numZoomLevels preserved");
197 // three tests for minScale, and maxScale
203 layer = new OpenLayers.Layer("test", options);
204 layer.initResolutions();
205 t.eq(layer.minScale.toPrecision(6), (1555).toPrecision(6),
206 "(without numZoomLevels) layer minScale preserved");
207 t.eq(layer.maxScale.toPrecision(6), (155).toPrecision(6),
208 "(without numZoomLevels) layer maxScale preserved");
209 t.eq(layer.numZoomLevels, 4, "(without numZoomLevels) layer numZoomLevels calculated");
211 layer = new OpenLayers.Layer("test", {'projection': 'EPSG:4326', 'map': map});
212 layer.initResolutions();
213 t.eq(layer.alwaysInRange, true, "always in range true if only get projection.");
215 OpenLayers.Layer.prototype.alwaysInRange = false;
216 layer = new OpenLayers.Layer("test", {'projection': 'EPSG:4326', 'map': map});
217 layer.initResolutions();
218 t.eq(layer.alwaysInRange, false, "always in range true if overridden on prototype.");
219 OpenLayers.Layer.prototype.alwaysInRange = null;
226 function test_Layer_visibility(t) {
230 var layer = new OpenLayers.Layer('Test Layer');
232 t.eq(layer.getVisibility(), true, "default for layer creation is visible");
234 layer.setVisibility(false);
235 t.eq(layer.getVisibility(), false, "setVisibility false works");
237 layer.setVisibility(true);
238 t.eq(layer.getVisibility(), true, "setVisibility true works");
240 // Need a map in order to have moveTo called.
241 // Tests added for #654.
242 var layer = new OpenLayers.Layer.WMS('Test Layer','http://example.com');
243 var m = new OpenLayers.Map('map');
248 layer.moveTo = function() { layermoved = true; }
250 layer.events.register('visibilitychanged', t, function() {
251 this.ok(true, "Visibility changed calls layer event.");
254 layer.setVisibility(false);
255 t.eq(layermoved, false, "Layer didn't move when calling setvis false");
257 layer.setVisibility(true);
258 t.eq(layermoved, true, "Layer moved when calling setvis true.");
263 function test_Layer_getZoomForResolution(t) {
267 var layer = new OpenLayers.Layer('Test Layer');
270 //make some dummy resolutions
271 layer.resolutions = [128, 64, 32, 16, 8, 4, 2];
273 t.eq(layer.getZoomForResolution(200), 0, "zoom all the way out");
274 t.eq(layer.getZoomForResolution(25), 2, "zoom in middle");
275 t.eq(layer.getZoomForResolution(3), 5, "zoom allmost all the way in");
276 t.eq(layer.getZoomForResolution(1), 6, "zoom all the way in");
278 t.eq(layer.getZoomForResolution(65), 0, "smallest containing res");
279 t.eq(layer.getZoomForResolution(63), 1, "smallest containing res");
281 t.eq(layer.getZoomForResolution(65, true), 1, "closest res");
282 t.eq(layer.getZoomForResolution(63, true), 1, "closest res");
284 layer.map.fractionalZoom = true;
285 t.eq(layer.getZoomForResolution(64), 1,
286 "(fractionalZoom) correct zoom for res in array");
287 t.eq(layer.getZoomForResolution(48).toPrecision(6), (1.5).toPrecision(6),
288 "(fractionalZoom) linear scaling for res between entries");
289 t.eq(layer.getZoomForResolution(200).toPrecision(6), (0).toPrecision(6),
290 "(fractionalZoom) doesn't return zoom below zero");
291 t.eq(layer.getZoomForResolution(1).toPrecision(6), (layer.resolutions.length - 1).toPrecision(6),
292 "(fractionalZoom) doesn't return zoom above highest index");
296 function test_Layer_redraw(t) {
299 var name = 'Test Layer';
300 var url = "http://octo.metacarta.com/cgi-bin/mapserv";
301 var params = { map: '/mapdata/vmap_wms.map',
303 format: 'image/jpeg'};
305 var layer = new OpenLayers.Layer.WMS(name, url, params);
307 t.ok(!layer.redraw(),
308 "redraw on an orphan layer returns false");
310 var map = new OpenLayers.Map('map');
313 t.ok(!layer.redraw(),
314 "redraw returns false if map does not yet have a center");
315 map.zoomToMaxExtent();
318 "redraw returns true after map has a center");
320 layer.setVisibility(false);
321 t.ok(!layer.redraw(),
322 "redraw returns false if a layer is not visible");
324 layer.setVisibility(true);
326 "redraw returns true even if extent has not changed");
329 var onMoveend = function(e) {
332 layer.events.on({"moveend": onMoveend});
334 layer.events.un({"moveend": onMoveend});
335 // test that the moveend event was triggered
336 t.ok(log.event, "an event was logged");
337 t.eq(log.event.type, "moveend", "moveend was triggered");
338 t.eq(log.event.zoomChanged, true, "event says zoomChanged true - poor name");
340 layer.moveTo = function(bounds, zoomChanged, dragging) {
341 var extent = layer.map.getExtent();
342 t.ok(bounds.equals(extent),
343 "redraw calls moveTo with the map extent");
345 "redraw calls moveTo with zoomChanged true");
347 "redraw calls moveTo with dragging false");
352 function test_layer_setIsBaseLayer(t) {
354 var map = new OpenLayers.Map('map');
355 layer = new OpenLayers.Layer();
357 map.events.register("changebaselayer", t, function() {
358 this.ok(true, "setIsBaseLayer() trig changebaselayer event.")
362 layer.setIsBaseLayer(true);
363 t.ok(layer.isBaseLayer, "setIsBaseLayer() change isBaseLayer property.");
366 function test_layer_setTileSize(t) {
369 layer = new OpenLayers.Layer();
371 g_MapTileSize = new OpenLayers.Size(25,67);
373 getTileSize: function() {
374 return g_MapTileSize;
378 var layerTileSize = new OpenLayers.Size(1,1);
381 layer.tileSize = layerTileSize;
384 var size = new OpenLayers.Size(2,2);
385 layer.setTileSize(size);
386 t.ok(layer.tileSize.equals(size), "size paramater set correctly to layer's tile size");
387 t.ok(layer.imageOffset == null, "imageOffset and imageSize null when no gutters")
390 layer.tileSize = layerTileSize;
392 t.ok(layer.tileSize.equals(layerTileSize), "layer's tileSize property preserved if no parameter sent in");
395 layer.tileSize = null;
397 t.ok(layer.tileSize.equals(g_MapTileSize), "layer's tileSize property is null and so correctly taken from the map");
403 size = new OpenLayers.Size(10,100);
404 layer.setTileSize(size);
406 var desiredImageOffset = new OpenLayers.Pixel(-15, -15);
407 var desiredImageSize = new OpenLayers.Size(40, 130);
409 t.ok(layer.imageOffset.equals(desiredImageOffset), "image offset correctly calculated");
410 t.ok(layer.imageSize.equals(desiredImageSize), "image size correctly calculated");
413 function test_Layer_getResolution(t) {
415 var layer = new OpenLayers.Layer("test");
417 getZoom: function() {return "foo";}
419 layer.getResolutionForZoom = function(zoom) {
420 t.eq(zoom, "foo", "getResolution calls getResolutionForZoom");
422 layer.getResolution();
427 function test_Layer_getResolutionForZoom(t) {
429 var layer = new OpenLayers.Layer("test");
430 layer.map = {fractionalZoom: false};
431 layer.resolutions = ["zero", "one", "two"];
432 t.eq(layer.getResolutionForZoom(0), "zero",
433 "(fractionalZoom false) returns resolution for given index");
434 t.eq(layer.getResolutionForZoom(0.9), "one",
435 "(fractionalZoom false) returns resolution for float index");
437 layer.resolutions = [2, 4, 6, 8];
438 layer.map.fractionalZoom = true;
439 t.eq(layer.getResolutionForZoom(1).toPrecision(6), (4).toPrecision(6),
440 "(fractionalZoom true) returns resolution for integer zoom");
442 t.eq(layer.getResolutionForZoom(1.3).toPrecision(6), (4.6).toPrecision(6),
443 "(fractionalZoom true) for zoom 1.3 should be 4.6");
445 t.eq(layer.getResolutionForZoom(1.6).toPrecision(6), (5.2).toPrecision(6),
446 "(fractionalZoom true) for zoom 1.6 should be 5.2");
448 t.eq(layer.getResolutionForZoom(1.8).toPrecision(6), (5.6).toPrecision(6),
449 "(fractionalZoom true) for zoom 1.8 should be 5.6");
451 t.eq(layer.getResolutionForZoom(1.5).toPrecision(6), (5).toPrecision(6),
452 "(fractionalZoom true) returns resolution for float zoom");
453 t.eq(layer.getResolutionForZoom(3.5).toPrecision(6), (8).toPrecision(6),
454 "(fractionalZoom true) returns resolution for zoom beyond res length - 1");
458 function test_afterAdd(t) {
462 var map = new OpenLayers.Map("map");
463 var layer = new OpenLayers.Layer(null, {isBaseLayer: true});
465 layer.afterAdd = function() {
466 hasBase = !!(layer.map && layer.map.baseLayer);
469 t.eq(hasBase, true, "when afterAdd is called, map has a base layer");
478 * HERE IS WHERE SOME TESTS SHOULD BE PUT TO CHECK ON THE LONLAT-PX TRANSLATION
479 * FUNCTIONS AND RESOLUTION AND GETEXTENT GETZOOMLEVEL, ETC
485 function test_Layer_destroy (t) {
488 var map = new OpenLayers.Map('map');
490 layer = new OpenLayers.Layer('Test Layer');
496 t.eq( layer.name, null, "layer.name is null after destroy" );
497 t.eq( layer.div, null, "layer.div is null after destroy" );
498 t.eq( layer.map, null, "layer.map is null after destroy" );
499 t.eq( layer.options, null, "layer.options is null after destroy" );
501 t.eq(map.layers.length, 0, "layer removed from map");
510 <div id="map" style="width:500px;height:500px"></div>
511 <div id="map2" style="width:100px;height:100px"></div>