summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Layer.html
blob: 954a363155d14fcbb52e06f23553dfa30a633697 (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
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
<html>
<head>
  <script src="OLLoader.js"></script>
  <script type="text/javascript">
    var layer; 

    function test_Layer_constructor (t) {
        t.plan( 15 );
        
        var options = { chicken: 151, foo: "bar", projection: "none" };
        var layer = new OpenLayers.Layer('Test Layer', options);
        
        t.ok( layer instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" );
        t.eq( layer.CLASS_NAME, "OpenLayers.Layer", "CLASS_NAME variable set correctly");

        t.eq( layer.name, "Test Layer", "layer.name is correct" );
        t.ok( layer.id != null, "Layer is given an id");
        t.ok( layer.projection, "none", "default layer projection correctly set");
        t.ok( ((layer.chicken == 151) && (layer.foo == "bar")), "layer.options correctly set to Layer Object" );
        t.ok( ((layer.options["chicken"] == 151) && (layer.options["foo"] == "bar")), "layer.options correctly backed up" );

        t.ok( typeof layer.div == "object" , "layer.div is created" );
        t.eq( layer.div.id, layer.id, "layer.div.id is correct" );

        options.chicken = 552;
        
        t.eq( layer.options["chicken"], 151 , "layer.options correctly made fresh copy" );
        
        t.eq( layer.isBaseLayer, false, "Default layer is not base layer" );

        layer = new OpenLayers.Layer('Test Layer');
        t.ok( layer instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" );
        t.eq( layer.name, "Test Layer", "layer.name is correct" );
        t.ok( layer.projection == null, "default layer projection correctly set");
        t.ok( layer.options instanceof Object, "layer.options correctly initialized as a non-null Object" );
    }


    function test_Layer_clone (t) {
        t.plan( 7 );
        
        var mapone = new OpenLayers.Map('map'); 
        var options = { chicken: 151, foo: "bar", maxResolution: "auto", visibility: false };
        var layer = new OpenLayers.Layer('Test Layer', options);
        mapone.addLayer(layer);
        layer.setVisibility(true);

        // randomly assigned property
        layer.chocolate = 5;

        var clone = layer.clone();

        t.ok( clone.map == null, "cloned layer has map property set to null")

        var maptwo = new OpenLayers.Map('map2'); 
        maptwo.addLayer(clone);

        t.ok( clone instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" );
        t.eq( clone.name, "Test Layer", "default clone.name is correct" );
        t.ok( ((clone.options["chicken"] == 151) && (clone.options["foo"] == "bar")), "clone.options correctly set" );
        t.eq(clone.chocolate, 5, "correctly copied randomly assigned property");

        t.eq(clone.visibility, true, "visibility correctly cloned");
        
        layer.addOptions({chicken:152});
        t.eq(clone.options["chicken"], 151, "made a clean copy of options");        

        mapone.destroy();
        maptwo.destroy();
    }

    function test_Layer_setName (t) {
        
        t.plan( 1 );

        layer = new OpenLayers.Layer('Test Layer');
        layer.setName("chicken");
        
        t.eq(layer.name, "chicken", "setName() works")
        
    }

    function test_Layer_addOptions (t) {
        
        t.plan( 20 );

        var map = new OpenLayers.Map("map", {allOverlays: true});
        var options = { chicken: 151, foo: "bar" };
        var layer = new OpenLayers.Layer('Test Layer', options);
        map.addLayer(layer);

        layer.addOptions({bark:55, chicken: 171});
        t.eq(layer.bark, 55, "addOptions() assigns new option correctly to Layer");
        t.eq(layer.options.bark, 55, "addOptions() adds new option correctly to backup");

        t.eq(layer.chicken, 171, "addOptions() overwrites option correctly to Layer");
        t.eq(layer.options.chicken, 171, "addOptions() overwrites option correctly to backup");

        var log;
        layer.initResolutions = function() {
            log++;
        };
        log = 0;
        layer.addOptions({bark: 56});
        t.eq(log, 0, "addOptions doesn't call initResolutions when not given a resolution option");

        log = 0;
        layer.addOptions({scales: [1, 2]});
        t.eq(log, 1, "addOptions calls initResolutions when given scales");

        log = 0;
        layer.addOptions({resolutions: [1, 2]});
        t.eq(log, 1, "addOptions calls initResolutions when given resolutions");

        log = 0;
        layer.addOptions({minScale: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given minScale");

        log = 0;
        layer.addOptions({maxScale: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given maxScale");

        log = 0;
        layer.addOptions({minResolution: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given minResolution");

        log = 0;
        layer.addOptions({maxResolution: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given maxResolution");

        log = 0;
        layer.addOptions({numZoomLevels: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given numZoomLevels");

        log = 0;
        layer.addOptions({maxZoomLevel: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given maxZoomLevel");

        log = 0;
        layer.addOptions({projection: new OpenLayers.Projection("EPSG:900913")});
        t.eq(log, 1, "addOptions calls initResolutions when given projection");

        log = 0;
        layer.addOptions({units: "m"});
        t.eq(log, 1, "addOptions calls initResolutions when given units");

        log = 0;
        layer.addOptions({minExtent: new OpenLayers.Bounds(0, 0, 0, 0)});
        t.eq(log, 1, "addOptions calls initResolutions when given minExtent");

        log = 0;
        layer.addOptions({maxExtent: new OpenLayers.Bounds(0, 0, 0, 0)});
        t.eq(log, 1, "addOptions calls initResolutions when given maxExtent");

        layer.projection = null;
        layer.addOptions({projection: "EPSG:900913"});
        t.ok(layer.projection instanceof OpenLayers.Projection,
             "addOptions creates a Projection object when given a projection string");
        
        log = null;
        // adding a 2nd layer to see if it gets reinitialized properly
        var layer2 = new OpenLayers.Layer(null, {
            moveTo: function(bounds) {
                log = bounds;
            }
        });
        map.addLayer(layer2);
        layer.addOptions({maxResolution: 0.00034332275390625}, true);
        t.eq(log.toBBOX(), map.getExtent().toBBOX(), "when reinitialize is set to true, changing base layer's resolution property reinitializes all layers.");

        map.removeLayer(layer);
        log = 0;
        layer.addOptions({minExtent: new OpenLayers.Bounds(0, 0, 0, 0)});
        t.eq(log, 0, "addOptions doesn't call initResolutions when layer is not in map");
    }
    
    function test_addOptionsScale(t) {
        t.plan(1);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.WMS();
        map.addLayer(layer);
        map.zoomToMaxExtent();
        layer.addOptions({maxResolution: 0.5, numZoomLevels: 15});
        t.eq(layer.alwaysInRange, false, "alwaysInRange should not be true anymore");
    }
    
    function test_Layer_StandardOptionsAccessors (t) {

        t.plan( 4 );

        var projection = "EPSG:4326";
        var maxExtent = new OpenLayers.Bounds(50,50,100,100);
        var maxResolution = 1.5726;
        var numZoomLevels = 11;

        var options = { projection: projection, 
                        maxExtent: maxExtent,
                        maxResolution: maxResolution,
                        numZoomLevels: numZoomLevels
                        };

        var layer = new OpenLayers.Layer('Test Layer', options);

        t.eq(layer.projection.getCode(), projection, "projection set correctly");
        t.ok(layer.maxExtent.equals(maxExtent), "maxExtent set correctly");
        t.eq(layer.maxResolution, maxResolution, "maxResolution set correctly");
        t.eq(layer.numZoomLevels, numZoomLevels, "numZoomLevels set correctly");
    }
    
    function test_maxExtent(t) {
        t.plan(5);
        
        var layer = new OpenLayers.Layer(
            null, {maxExtent: [-180, 0, 0, 90]}
        );
        
        t.ok(layer.maxExtent instanceof OpenLayers.Bounds, "(array) bounds instance");
        t.eq(layer.maxExtent.left, -180, "(array) bounds left");
        t.eq(layer.maxExtent.bottom, 0, "(array) bounds left");
        t.eq(layer.maxExtent.right, 0, "(array) bounds right");
        t.eq(layer.maxExtent.top, 90, "(array) bounds top");
        
        layer.destroy();
    }

    function test_minExtent(t) {
        t.plan(5);
        
        var layer = new OpenLayers.Layer(
            null, {minExtent: [-180, 0, 0, 90]}
        );
        
        t.ok(layer.minExtent instanceof OpenLayers.Bounds, "(array) bounds instance");
        t.eq(layer.minExtent.left, -180, "(array) bounds left");
        t.eq(layer.minExtent.bottom, 0, "(array) bounds left");
        t.eq(layer.minExtent.right, 0, "(array) bounds right");
        t.eq(layer.minExtent.top, 90, "(array) bounds top");
        
        layer.destroy();
    }


    function test_eventListeners(t) {
        t.plan(1);
        
        var method = OpenLayers.Events.prototype.on;
        // test that events.on is called at layer construction
        var options = {
            eventListeners: {foo: "bar"}
        };
        OpenLayers.Events.prototype.on = function(obj) {
            t.eq(obj, options.eventListeners, "events.on called with eventListeners");
        }
        var layer = new OpenLayers.Layer("test", options);
        OpenLayers.Events.prototype.on = method;
        layer.destroy();
        
        // if events.on is called again, this will fail due to an extra test
        // test layer without eventListeners
        OpenLayers.Events.prototype.on = function(obj) {
            t.fail("events.on called without eventListeners");
        }
        var layer2 = new OpenLayers.Layer("test");
        OpenLayers.Events.prototype.on = method;
        layer2.destroy();
    }

    function test_initResolutions_alwaysInRange(t) {
        t.plan(3);

        var map, layer;

        map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer("test", {maxResolution: 80, minResolution: 10});
        map.addLayer(layer);
        t.eq(layer.alwaysInRange, false,
             "alwaysInRange set to false due to passed options");
        map.destroy();

        map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer("test", {projection: "unknown"});
        map.addLayer(layer);
        t.eq(layer.alwaysInRange, true,
             "alwaysInRange true if unknown projection is set.");
        map.destroy();

        map = new OpenLayers.Map("map");
        OpenLayers.Layer.prototype.alwaysInRange = false;
        layer = new OpenLayers.Layer("test", {'projection': 'EPSG:4326'});
        map.addLayer(layer);
        t.eq(layer.alwaysInRange, false,
             "alwaysInRange true if overridden on prototype.");
        OpenLayers.Layer.prototype.alwaysInRange = null;
        map.destroy();
    }

    function test_initResolutions_resolutions(t) {

        function initResolutionsTest(
            id, mapOptions, layerOptions,
            expectedResolutions, expectedMinResolution, expectedMaxResolution) {

            // setup
            var map = new OpenLayers.Map("map", mapOptions);
            var layer = new OpenLayers.Layer(null, layerOptions);
            map.addLayer(layer);

            // make resolution assertions
            t.eq(
                layer.resolutions.length, expectedResolutions.length,
                id + ": correct resolutions length"
            );
            // allow for floating point imprecision
            var got, exp, same = true;
            for (var i=0; i<expectedResolutions.length; ++i) {
                got = layer.resolutions[i];
                exp = expectedResolutions[i];
                if (got.toFixed(10) !== exp.toFixed(10)) {
                    t.fail(id + ": bad resolutions - index " + i + " got " + got + " but expected " + exp);
                    same = false;
                    break;
                }
            }
            if (same) {
                t.ok(true, id + ": correct resolutions");
            }
            t.eq(
                layer.maxResolution, expectedMaxResolution,
                id + ": maxResolution set"
            );
            t.eq(
                layer.minResolution, expectedMinResolution,
                id + ": minResolution set"
            );

            // teardown
            map.destroy();
        }

        // each case is an array of id, map options, layer options, expected resolutions,
        // expected min resolution, and expected max resolution
        var cases = [[

        /*
         * Batch 1: map defaults and sensible layer options
         */

            "1.0",  null, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "1.1",  null, {resolutions: [400, 200, 100], minResolution: 150, maxResolution: 300},
            [400, 200, 100], 150, 300
        ], [
            "1.2",  null, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "1.3",  null, {maxResolution: 4000, maxZoomLevel: 2},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "1.4",  null, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "1.5",  null, {minResolution: 40, maxZoomLevel: 2},
            [160, 80, 40], 40, 160
        ], [
            "1.6", null, {minResolution: 10, maxResolution: 40},
            [40, 20, 10], 10, 40
        ], [
            "1.7", null, {minResolution: 10, maxResolution: 40, numZoomLevels: 3},
            [40, 20, 10], 10, 40
        ], [
            "1.8", null, {minResolution: 10, maxResolution: 40, maxZoomLevel: 2},
            [40, 20, 10], 10, 40
        ], [
            "1.9",  null, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.10",  null, {scales: [400000, 200000, 100000], minScale: 400000, maxScale: 100000},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.11",  null, {minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.12",  null, {minScale: 400000, maxZoomLevel: 2},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.13",  null, {maxScale: 100000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.14",  null, {maxScale: 100000, maxZoomLevel: 2},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.15",  null, {maxScale: 100000, minScale: 400000},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.16",  null, {maxScale: 100000, minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.17",  null, {maxScale: 100000, minScale: 400000, maxZoomLevel: 2},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.18",  null, {scales: [400000, 200000, 100000], units: "m"},
            [141.11139333389778, 70.55569666694889, 35.277848333474445], 35.277848333474445, 141.11139333389778
        ], [
            "1.19",  null, {minScale: 400000, numZoomLevels: 3, units: "m"},
            [141.11139333389778, 70.55569666694889, 35.277848333474445], 35.277848333474445, 141.11139333389778
        ], [
            "1.20",  null, {maxScale: 100000, numZoomLevels: 3, units: "m"},
            [141.11139333389778, 70.55569666694889, 35.277848333474445], 35.277848333474445, 141.11139333389778
        ], [
            "1.21", null, {numZoomLevels: 2},  // maxResolution calculated based on the projection's maxExtent here
           [1.40625, 0.703125], 0.703125, 1.40625
        ], [

        /*
         * Batch 2: custom map options map and sensible layer options
         */

        /*
         * Batch 2.1: resolutions set in the layer options
         */
            "2.1.0",  {resolutions: [300, 150]}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.1",  {numZoomLevels: 4}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.2",  {maxResolution: 300}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.3",  {minResolution: 300}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.4",  {scales: [4, 2, 1]}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.5",  {minScale: 4}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.6",  {maxScale: 4}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
        /*
         * Batch 2.2: minResolution and maxResolution set in the layer options
         */
            "2.2.0",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: 0},
            [80, 40, 20, 10], 12, 48
        ], [
            "2.2.1",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: -1},
            [80, 40, 20, 10], 12, 48
        ], [
            "2.2.2",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: null},
            [80, 40, 20, 10], 12, 48
        ], [
            "2.2.3",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: undefined},
            [48, 24, 12], 12, 48
        ], [
            "2.2.4",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: 3},
            [48, 24, 12], 12, 48
        ], [
            "2.2.5",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48},
            [48, 24, 12], 12, 48
        ], [
        /*
         * Batch 2.3: maxResolution set in the layer options
         */
            "2.3.0",  {resolutions: [300, 150]}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.1",  {numZoomLevels: 2}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.2",  {maxResolution: 50}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.3",  {scales: [4, 2, 1]}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.4",  {minScale: 4}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.5",  {resolutions: [300, 150]}, {maxResolution: 250},
            [300, 150], 150, 250
        ], [
        /*
         * Batch 2.4: minResolution set in the layer options
         */
            "2.4.0",  {resolutions: [300, 150]}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.1",  {numZoomLevels: 2}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.2",  {minResolution: 50}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.3",  {scales: [4, 2, 1]}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.4",  {maxScale: 1}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.5",  {resolutions: [300, 150]}, {minResolution: 250},
            [300, 150], 250, 300
        ], [
        /*
         * Batch 2.5: scales set in the layer options
         */
            "2.5.0",  {resolutions: [4, 2, 1]}, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.5.1",  {numZoomLevels: 2}, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.5.2",  {maxResolution: 4}, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.5.3",  {minResolution: 1}, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.5.4",  {units: "m"}, {scales: [400000, 200000, 100000]},
            [141.11139333389778, 70.55569666694889, 35.277848333474445], 35.277848333474445, 141.11139333389778
        ], [
        /*
         * Batch 2.6: minScale set in the layer options
         */
            "2.6.0",  {resolutions: [4, 2, 1]}, {minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.6.1",  {numZoomLevels: 2}, {minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.6.2",  {maxResolution: 4}, {minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.6.3", {scales: [400000, 200000, 100000]}, {minScale: 200000},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0006349563376084181
        ], [
        /*
         * Batch 2.7: maxScale set in the layer options
         */
            "2.7.0",  {resolutions: [4, 2, 1]}, {maxScale: 100000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.7.1",  {numZoomLevels: 2}, {maxScale: 100000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.7.2",  {minResolution: 1}, {maxScale: 100000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.7.3", {scales: [400000, 200000, 100000]}, {maxScale: 200000},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.0006349563376084181, 0.0012699126752168362
        ], [
        /*
         * Batch 2.8: numZoomLevels set in the layer options
         */
            "2.8.0",  {maxResolution: 80}, {numZoomLevels: 4},  // maxResolution calculated based on the projection's maxExtent here
            [1.40625, 0.703125, 0.3515625, 0.17578125], 0.17578125, 1.40625
        ], [
            "2.8.1", {maxResolution: 80, numZoomLevels: 4}, {numZoomLevels: null},
            [80, 40, 20, 10], 10, 80
        ], [
            "2.8.2", {maxResolution: 80, numZoomLevels: 4}, {numZoomLevels: undefined},
            [80, 40, 20, 10], 10, 80
        ]];

        // run all cases (4 tests each)
        var i, num = cases.length, c;
        t.plan(num * 4);
        for (i=0; i<num; ++i) {
            c = cases[i];
            initResolutionsTest(c[0], c[1], c[2], c[3], c[4], c[5]);
        }

    }

    function test_Layer_visibility(t) {

        t.plan(7);

        var layer = new OpenLayers.Layer('Test Layer');
    
        t.eq(layer.getVisibility(), true, "default for layer creation is visible");
        
        layer.setVisibility(false);
        t.eq(layer.getVisibility(), false, "setVisibility false works");

        layer.setVisibility(true);
        t.eq(layer.getVisibility(), true, "setVisibility true works");
       
        // Need a map in order to have moveTo called.
        // Tests added for #654.
        var layer = new OpenLayers.Layer.WMS('Test Layer','http://example.com');
        var m = new OpenLayers.Map('map');
        m.addLayer(layer);
        m.zoomToMaxExtent();
        
        layermoved = false; 
        layer.moveTo = function() { layermoved = true; }
        
        layer.events.register('visibilitychanged', t, function() {
            this.ok(true, "Visibility changed calls layer event.");
        });    
        
        layer.setVisibility(false);
        t.eq(layermoved, false, "Layer didn't move when calling setvis false");
        
        layer.setVisibility(true);
        t.eq(layermoved, true, "Layer moved when calling setvis true.");
    
    }


    function test_Layer_getZoomForResolution(t) {

        t.plan(12);

        var layer = new OpenLayers.Layer('Test Layer');
        layer.map = {};
        
        //make some dummy resolutions
        layer.resolutions = [128, 64, 32, 16, 8, 4, 2];
        
        t.eq(layer.getZoomForResolution(200), 0, "zoom all the way out");
        t.eq(layer.getZoomForResolution(25), 2, "zoom in middle");
        t.eq(layer.getZoomForResolution(3), 5, "zoom allmost all the way in");
        t.eq(layer.getZoomForResolution(1), 6, "zoom  all the way in");

        t.eq(layer.getZoomForResolution(65), 0, "smallest containing res");
        t.eq(layer.getZoomForResolution(63), 1, "smallest containing res");

        t.eq(layer.getZoomForResolution(65, true), 1, "closest res");
        t.eq(layer.getZoomForResolution(63, true), 1, "closest res");
        
        layer.map.fractionalZoom = true;
        t.eq(layer.getZoomForResolution(64), 1,
             "(fractionalZoom) correct zoom for res in array");
        t.eq(layer.getZoomForResolution(48).toPrecision(6), (1.5).toPrecision(6),
             "(fractionalZoom) linear scaling for res between entries");
        t.eq(layer.getZoomForResolution(200).toPrecision(6), (0).toPrecision(6),
             "(fractionalZoom) doesn't return zoom below zero");
        t.eq(layer.getZoomForResolution(1).toPrecision(6), (layer.resolutions.length - 1).toPrecision(6),
             "(fractionalZoom) doesn't return zoom above highest index");
    }
    
    function test_Layer_redraw(t) {
        t.plan(11)

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

        var layer = new OpenLayers.Layer.WMS(name, url, params);
        
        t.ok(!layer.redraw(),
             "redraw on an orphan layer returns false");
        
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);

        t.ok(!layer.redraw(),
             "redraw returns false if map does not yet have a center");
        map.zoomToMaxExtent();
        
        t.ok(layer.redraw(),
             "redraw returns true after map has a center");
        
        layer.setVisibility(false);
        t.ok(!layer.redraw(),
             "redraw returns false if a layer is not visible");
        
        layer.setVisibility(true);
        t.ok(layer.redraw(),
                "redraw returns true even if extent has not changed");
        
        var log = {};
        var onMoveend = function(e) {
            log.event = e;
        };
        layer.events.on({"moveend": onMoveend});
        layer.redraw();
        layer.events.un({"moveend": onMoveend});
        // test that the moveend event was triggered
        t.ok(log.event, "an event was logged");
        t.eq(log.event.type, "moveend", "moveend was triggered");
        t.eq(log.event.zoomChanged, true, "event says zoomChanged true - poor name");

        layer.moveTo = function(bounds, zoomChanged, dragging) {
            var extent = layer.map.getExtent();
            t.ok(bounds.equals(extent),
                 "redraw calls moveTo with the map extent");
            t.ok(zoomChanged,
                 "redraw calls moveTo with zoomChanged true");
            t.ok(!dragging,
                 "redraw calls moveTo with dragging false");
        }
        layer.redraw();
    }
      
    function test_layer_setIsBaseLayer(t) {
        t.plan(2);
        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer();

        map.events.register("changebaselayer", t, function() {
          this.ok(true, "setIsBaseLayer() trig changebaselayer event.")
        });

        map.addLayer(layer);
        layer.setIsBaseLayer(true);
        t.ok(layer.isBaseLayer, "setIsBaseLayer() change isBaseLayer property.");
    }
      
    function test_layer_setTileSize(t) {
        t.plan(4);

        layer = new OpenLayers.Layer();
        
        g_MapTileSize = new OpenLayers.Size(25,67);        
        layer.map = {
            getTileSize: function() {
                return g_MapTileSize;
            }
        };

        var layerTileSize = new OpenLayers.Size(1,1);

    //TILE SIZE
        layer.tileSize = layerTileSize;
      
      //parameter
        var size = new OpenLayers.Size(2,2);
        layer.setTileSize(size);
        t.ok(layer.tileSize.equals(size), "size paramater set correctly to layer's tile size");
      
      //set on layer
        layer.tileSize = layerTileSize;
        layer.setTileSize();
        t.ok(layer.tileSize.equals(layerTileSize), "layer's tileSize property preserved if no parameter sent in");
      
      //take it from map
        layer.tileSize = null;
        layer.setTileSize();
        t.ok(layer.tileSize.equals(g_MapTileSize), "layer's tileSize property is null and so correctly taken from the map");
        
        

    //GUTTERS
        layer.gutter = 15;
        size = new OpenLayers.Size(10,100);
        layer.setTileSize(size);

        var desiredImageSize = new OpenLayers.Size(40, 130); 

        t.ok(layer.imageSize.equals(desiredImageSize), "image size correctly calculated");
    }
    
    function test_Layer_getResolution(t) {
        t.plan(1);
        var layer = new OpenLayers.Layer("test");
        layer.map = {
            getZoom: function() {return "foo";}
        };
        layer.getResolutionForZoom = function(zoom) {
            t.eq(zoom, "foo", "getResolution calls getResolutionForZoom");
        }
        layer.getResolution();
        layer.map = null;
        layer.destroy();
    }
    
    function test_Layer_getResolutionForZoom(t) {
        t.plan(8);
        var layer = new OpenLayers.Layer("test");
        layer.map = {fractionalZoom: false};
        layer.resolutions = ["zero", "one", "two"];
        t.eq(layer.getResolutionForZoom(0), "zero",
             "(fractionalZoom false) returns resolution for given index");
        t.eq(layer.getResolutionForZoom(0.9), "one",
             "(fractionalZoom false) returns resolution for float index");
        
        layer.resolutions = [2, 4, 6, 8];
        layer.map.fractionalZoom = true;
        t.eq(layer.getResolutionForZoom(1).toPrecision(6), (4).toPrecision(6),
             "(fractionalZoom true) returns resolution for integer zoom");

        t.eq(layer.getResolutionForZoom(1.3).toPrecision(6), (4.6).toPrecision(6),
             "(fractionalZoom true) for zoom 1.3 should be 4.6");

        t.eq(layer.getResolutionForZoom(1.6).toPrecision(6), (5.2).toPrecision(6),
             "(fractionalZoom true) for zoom 1.6 should be 5.2");

        t.eq(layer.getResolutionForZoom(1.8).toPrecision(6), (5.6).toPrecision(6),
             "(fractionalZoom true) for zoom 1.8 should be 5.6");

        t.eq(layer.getResolutionForZoom(1.5).toPrecision(6), (5).toPrecision(6),
             "(fractionalZoom true) returns resolution for float zoom");             
        t.eq(layer.getResolutionForZoom(3.5).toPrecision(6), (8).toPrecision(6),
             "(fractionalZoom true) returns resolution for zoom beyond res length - 1");
        
    }
    
    function test_afterAdd(t) {
        
        t.plan(4);
        
        var log = [];
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer(null, {
            isBaseLayer: true,
            eventListeners: {
                "added": function(evt) {
                    log.push(evt);
                }
            }
        });
        var hasBase = false;
        layer.afterAdd = function() {
            hasBase = !!(layer.map && layer.map.baseLayer);
        }
        map.addLayer(layer);
        t.eq(hasBase, true, "when afterAdd is called, map has a base layer");
        t.eq(log.length, 1, "added event triggered");
        t.eq(log[0].map.id, map.id, "added listener argument with correct map");
        t.eq(log[0].layer.id, layer.id, "added listener argument with correct layer");
        
    }
    
    function test_setOpacity(t) {
        t.plan(5);

        var map, layer, log;
        
        map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer("");
        map.addLayer(layer);

        log = [];
        map.events.register('changelayer', t, function(event) {
            log.push({layer: event.layer, property: event.property});
        });
        layer.setOpacity(0.42);
        t.eq(layer.opacity, 0.42,
             "setOpacity() set layer.opacity to correct value");
        t.eq(log.length, 1,
             "setOpacity() triggers changelayer once");
        t.ok(log[0].layer == layer,
             "changelayer listener called with expected layer");
        t.eq(log[0].property, "opacity",
             "changelayer listener called with expected property");

        // This call must not trig the event because the opacity value is the same.
        log = [];
        layer.setOpacity(0.42);
        t.eq(log.length, 0,
             "setOpacity() does not trigger changelayer if the opacity value is the same");
    }


/******
 * 
 * 
 * HERE IS WHERE SOME TESTS SHOULD BE PUT TO CHECK ON THE LONLAT-PX TRANSLATION
 * FUNCTIONS AND RESOLUTION AND GETEXTENT GETZOOMLEVEL, ETC
 * 
 * 
 */


    function test_Layer_destroy (t) {
        t.plan( 8 );    

        var log = [];
        var map = new OpenLayers.Map('map');
        
        layer = new OpenLayers.Layer('Test Layer', {
            eventListeners: {
                "removed": function(evt) {
                    log.push(evt);
                }
            }
        });

        map.addLayer(layer);

        layer.destroy();

        t.eq( layer.name, null, "layer.name is null after destroy" );
        t.eq( layer.div, null, "layer.div is null after destroy" );
        t.eq( layer.map, null, "layer.map is null after destroy" );
        t.eq( layer.options, null, "layer.options is null after destroy" );

        t.eq(map.layers.length, 0, "layer removed from map");
        t.eq(log.length, 1, "removed event triggered");
        t.eq(log[0].map.id, map.id, "removed listener argument with correct map");
        t.eq(log[0].layer.id, layer.id, "removed listener argument with correct layer");
        
        map.destroy();

    }

  </script>
</head>
<body>
  <div id="map" style="width:500px;height:500px"></div>
  <div id="map2" style="width:100px;height:100px"></div>
</body>
</html>