summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Control/LayerSwitcher.html
blob: c81a77934ac7bacaeca7c0baec24085682d15475 (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
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var map; 
    OpenLayers.Lang.setCode('en');

    function test_Control_LayerSwitcher_constructor (t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.LayerSwitcher();
        t.ok( control instanceof OpenLayers.Control.LayerSwitcher, "new OpenLayers.Control.LayerSwitcher returns object" );
        t.eq( control.displayClass,  "olControlLayerSwitcher", "displayClass is correct" );
    }

    function test_Control_LayerSwitcher_draw (t) {
        t.plan( 2 );

        map = new OpenLayers.Map('map');
        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);

        var div = control.draw();
        t.ok( control.div != null, "draw makes a div" );
        t.ok( div != null, "draw returns its div" );
    }
    function test_Control_LayerSwitcher_outsideViewport (t) {
        t.plan( 4 );

        map = new OpenLayers.Map('map');
        control = new OpenLayers.Control.LayerSwitcher({'div':OpenLayers.Util.getElement('layerswitcher')});
        map.addControl(control);
        t.eq(control.div.style.width, "250px", "Div is not minimized when added.");
        t.ok(control.events.element && control.events.listeners.buttonclick, "[outside] Events instance attached to div and has buttonclick event");
        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);
        t.eq(control.div.style.width, "0px", "Div is minimized when added.");
        t.ok(!control.events.element && map.events.listeners.buttonclick, "[inside] Events instance not attached to div and buttonclick event registered on map");
    }
 
    function test_Control_LayerSwitcher_loadContents(t) {

        t.plan( 10 );

        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        markers = new OpenLayers.Layer.Markers("markers");
        map.addLayer(markers);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);

        t.ok(control.layersDiv != null, "correctly makes layers div");
        t.ok(OpenLayers.Element.hasClass(control.layersDiv, "layersDiv"),
             "layers div has class layersDiv");
        t.ok(control.baseLayersDiv != null, "correctly makes layers div");
        t.ok(OpenLayers.Element.hasClass(control.baseLayersDiv, "baseLayersDiv"),
             "base layers div has class baseLayersDiv");
        t.ok(control.dataLayersDiv != null, "correctly makes layers div");
        t.ok(OpenLayers.Element.hasClass(control.dataLayersDiv, "dataLayersDiv"),
             "data layers div has class dataLayersDiv");
        t.ok(control.maximizeDiv != null, "correctly makes resize div");
        t.ok(OpenLayers.Element.hasClass(control.maximizeDiv, "maximizeDiv"),
             "maximize div has class maximizeDiv");
        t.ok(control.minimizeDiv != null, "correctly makes resize div");
        t.ok(OpenLayers.Element.hasClass(control.minimizeDiv, "minimizeDiv"),
             "minimize div has class minmizeDiv");
    }


    function test_Control_LayerSwitcher_redraw (t) {

        t.plan( (OpenLayers.BROWSER_NAME == "opera" ? 9 : 19 ) );

        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        markers = new OpenLayers.Layer.Markers("markers");
        map.addLayer(markers);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);

        var wmsInput = control.div.childNodes[0].childNodes[1].childNodes[0];
        t.ok(wmsInput != null, "correctly makes an input for wms layer");
        t.eq(wmsInput.type, "radio", "wms correctly made a radio button");
        t.eq(wmsInput.name, control.id + "_baseLayers", "wms correctly named");
        t.eq(wmsInput.value, layer.name, "wms correctly valued");
        
        var markersInput = control.div.childNodes[0].childNodes[3].childNodes[0];
        t.ok(markersInput != null, "correctly makes an input for markers layer");
        t.eq(markersInput.type, "checkbox", "wms correctly made a radio button");
        t.eq(markersInput.name, markers.name, "wms correctly named");
        t.eq(markersInput.value, markers.name, "wms correctly valued");

        t.eq(false, control.checkRedraw(), "check redraw is false");
        if (OpenLayers.BROWSER_NAME != "opera") { 
            control = new OpenLayers.Control.LayerSwitcher();
            var myredraw = control.redraw;
            control.redraw = function() { 
                t.ok(true, "redraw called when setting vis");
            }
            map.addControl(control); 
            var func = OpenLayers.Function.bind(myredraw, control);
            func();
            markers.setVisibility(false); 
            t.eq(control.checkRedraw(), true, "check redraw is true after changing layer and not letting redraw happen.");
            map.removeControl(control);
            
            control = new OpenLayers.Control.LayerSwitcher();
            var myredraw = control.redraw;
            control.redraw = function() { 
                t.ok(true, "redraw called when setting inRange");
            }
            map.addControl(control); 
            var func = OpenLayers.Function.bind(myredraw, control);
            func();
            markers.inRange = false;
            t.eq(control.checkRedraw(), true, "check redraw is true after changing layer.inRange and not letting redraw happen.");
            map.removeControl(control);
            
            control = new OpenLayers.Control.LayerSwitcher();
            var myredraw = control.redraw;
            control.redraw = function() { 
                t.ok(true, "redraw called when raising base layer ");
            }
            
            map.addControl(control); 
            var func = OpenLayers.Function.bind(myredraw, control);
            func();
            map.raiseLayer(layer, 1);
            t.eq(control.checkRedraw(), true, "check redraw is true after changing layer.inRange and not letting redraw happen.");
            map.removeControl(control);
        } else {
            t.debug_print("FIXME: Some LayerSwitcher tests fail in Opera.");
        }    

    }
    function test_Control_LayerSwitcher_ascending (t) {

        t.plan( 4 );

        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        markers = new OpenLayers.Layer.Markers("markers");
        map.addLayer(markers);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);
        control2 = new OpenLayers.Control.LayerSwitcher({'ascending':false});
        map.addControl(control2);
        t.ok(control.div.childNodes[0].childNodes[0].innerHTML.match("Base Layer"), "Base Layers first in LayerSwitcher with ascending true");
        t.ok(control.div.childNodes[0].childNodes[2].innerHTML.match("Overlays"), "Overlays in LayerSwitcher with ascending true");
        t.ok(control2.div.childNodes[0].childNodes[2].innerHTML.match("Base Layer"), "Base Layers last in LayerSwitcher with ascending false");
        t.ok(control2.div.childNodes[0].childNodes[0].innerHTML.match("Overlays"), "Base Layers last in LayerSwitcher with ascending false");
    }
    
    function test_Control_LayerSwitcher_displayInLayerSwitcher (t) {

        t.plan( 2 );

        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"}, {'displayInLayerSwitcher': false});
        map.addLayer(layer);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);
        t.eq(control.div.childNodes[0].childNodes[0].style.display, "none" , "Base layer display off when no visble base layer");
        
        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("WMS", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        control = new OpenLayers.Control.LayerSwitcher();
        map.addControl(control);
        t.eq(control.div.childNodes[0].childNodes[0].style.display, "" , "Base layer display on when visble base layer");
    }

    // See e.g. https://github.com/openlayers/openlayers/issues/866
    function test_Control_LayerSwitcher_validIds(t){
        t.plan(2);

        // setup
        var layername = "Name with spaces & illegal characters * + ~ ` ' ? )",
            map = new OpenLayers.Map("map", {
                controls: [
                    new OpenLayers.Control.LayerSwitcher()
                ],
                layers: [
                    new OpenLayers.Layer.WMS(
                        layername,
                        "../../img/blank.gif"
                    ),
                    // add another layer with the same name, the generated id
                    // must be different
                    new OpenLayers.Layer.WMS(
                        layername,
                        "../../img/blank.gif"
                    )
                ]
            });

        var baselayerDiv = map.controls[0].div.childNodes[0].childNodes[1],
            firstGeneratedInputId = baselayerDiv.childNodes[0].id,
            secondGeneratedInputId = baselayerDiv.childNodes[1].id,
            // legal ids start with a letter and are followed only by word
            // characters (letters, digits, and underscores) plus the dash (-)
            // This is only a subset of all allowed charcters inside of ids.
            allowedIdChars = (/^[a-zA-Z]{1}[\w-]*$/g);

        // tests
        // validity
        t.ok(
            allowedIdChars.test(firstGeneratedInputId),
            "id only contains letters, digits, underscores and dashes. It " +
                "starts with a letter."
        );
        // uniqueness
        t.ok(
            firstGeneratedInputId !== secondGeneratedInputId,
            "generated ids are different even for equal layernames"
        );

        // teardown
        map.destroy();
    }

  </script>
</head>
<body>
    <div id="map" style="width: 1024px; height: 512px;"/>
    <div id="layerswitcher" style="width:250px; height:256px;" />
</body>
</html>