summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Layer/Markers.html
blob: 07f699fedc1701096a658c77a6742ec229af0b6e (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
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var layer; 

    function test_initialize(t) {
        t.plan( 2 );
        
        layer = new OpenLayers.Layer.Markers('Test Layer');
        t.ok( layer instanceof OpenLayers.Layer.Markers, "new OpenLayers.Layer.Markers returns object" );
        t.eq( layer.name, "Test Layer", "layer.name is correct" );
    }
    function test_addlayer (t) {
        t.plan( 3 );
        
        layer = new OpenLayers.Layer.Markers('Test Layer');
        t.ok( layer instanceof OpenLayers.Layer.Markers, "new OpenLayers.Layer.Markers returns object" );
        t.eq( layer.name, "Test Layer", "layer.name is correct" );
        layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0), 
                                              new OpenLayers.Icon())
                       );
        t.eq( layer.markers.length, 1, "addLayer adds marker to layer." );
    }
    function test_addMarker_removeMarker (t) {
        t.plan( 6 );

        var map = new OpenLayers.Map('map');
        var baseLayer = new OpenLayers.Layer.WMS("Test Layer", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(baseLayer);
        map.zoomToMaxExtent();
        layer = new OpenLayers.Layer.Markers('Test Layer');
        map.addLayer(layer);
        var marker = new OpenLayers.Marker(new OpenLayers.LonLat(5,40));
        layer.addMarker(marker);
        t.ok(  marker.icon.imageDiv.parentNode == layer.div, "addMarker adds marker image node into layer node." ); 
        layer.removeMarker(marker);
        t.ok(  marker.icon.imageDiv.parentNode != layer.div, "removeMarker removes marker image node from layer node." ); 
        layer.removeMarker(marker);
        t.ok(true, "Removing marker twice does not fail.");
        layer.addMarker(marker);
        t.ok(  marker.icon.imageDiv.parentNode == layer.div, "addMarker adds marker image node into layer node." ); 

        layer.markers = null;
        layer.removeMarker(marker);
        t.ok(true, "removing marker when no markers present does not script error");

        var l = new OpenLayers.Layer.Markers();
        var marker = new OpenLayers.Marker(new OpenLayers.LonLat(5,40));
        l.addMarker(marker);
        l.removeMarker(marker);
        t.ok(true, "Removing marker when layer not added to map does not fail.");

    }
    
    function test_markerMovement(t) {
        
        t.plan(6);
        
        var map = new OpenLayers.Map("map", {zoomMethod: null});
        var layer = new OpenLayers.Layer.Markers("Base", {isBaseLayer: true});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0, 0), 1);
        
        var size = new OpenLayers.Size(10, 10);
        var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
        var icon = new OpenLayers.Icon("foo", size, offset);
        var marker = new OpenLayers.Marker(new OpenLayers.LonLat(10, -10), icon)
        layer.addMarker(marker);
        
        t.eq(marker.icon.px.x, 554, "marker icon is placed at 554 px on x-axis");
        t.eq(marker.icon.px.y, 314, "marker icon is placed at 314 px on y-axis");
        
        map.zoomTo(2);
        
        t.eq(marker.icon.px.x, 568, "marker icon moved to 568 px on x-axis");
        t.eq(marker.icon.px.y, 328, "marker icon moved to 328 px on y-axis");
        
        map.zoomTo(1);

        t.eq(marker.icon.px.x, 554, "marker icon moved back to 554 px on x-axis");
        t.eq(marker.icon.px.y, 314, "marker icon moved back to 314 px on y-axis");
        
    }
    
    function test_destroy (t) {
        t.plan( 1 );    
        layer = new OpenLayers.Layer.Markers('Test Layer');
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);
        layer.destroy();
        t.eq( layer.map, null, "layer.map is null after destroy" );
    }

    function test_getDataExtent(t) {
        t.plan( 4 );

        var layer = {};
        var ret = OpenLayers.Layer.Markers.prototype.getDataExtent.apply(layer, []);
        t.eq(ret, null, "does not crash, returns null on layer with null 'this.markers'");

        layer.markers = [];
        ret = OpenLayers.Layer.Markers.prototype.getDataExtent.apply(layer, []);
        t.eq(ret, null, "returns null on layer with empty 'this.markers'");
        
        layer.markers.push({
            'lonlat': new OpenLayers.LonLat(4,5)
        });
        var expectedBounds = new OpenLayers.Bounds(4,5,4,5);
        ret = OpenLayers.Layer.Markers.prototype.getDataExtent.apply(layer, []);
        t.ok(ret.equals(expectedBounds), "returns expected bounds with only one marker");

        layer.markers.push({
            'lonlat': new OpenLayers.LonLat(1,2)
        });
        var expectedBounds = new OpenLayers.Bounds(1,2,4,5);
        ret = OpenLayers.Layer.Markers.prototype.getDataExtent.apply(layer, []);
        t.ok(ret.equals(expectedBounds), "returns expected bounds with multiple markers");

    }

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

        layer = new OpenLayers.Layer.Markers('Test Layer');

        var opacity = 0.1234;
        
        for (var i = 0; i < 12; i++) {
            layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0), new OpenLayers.Icon()));
        }

        layer.setOpacity(opacity);

        for (var i = 0; i < 4; i++) {
            layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0), new OpenLayers.Icon()));
        }
        
        var itWorks = false;
        for (var i = 0; i < layer.markers.length; i++) {
            itWorks = parseFloat(layer.markers[i].icon.imageDiv.style.opacity) == opacity;
            if (!itWorks) {
                break;
            }
        }
        t.ok(itWorks, "setOpacity change markers opacity");
    }

  </script>
</head>
<body>
  <div id="map" style="width: 1080px; height: 600px;"/>
</body>
</html>