summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Control/WMTSGetFeatureInfo.html
blob: c7be78ca038cf50bac313804148a9984ef6861b5 (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
<html>
<head>
    <script src="../OLLoader.js"></script>
    <script type="text/javascript">

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

        var options = {
            url: "http://localhost/wmts",
            layers: ["foo"],
            formatOptions: {
                foo: "bar"
            }
        };
        var control = new OpenLayers.Control.WMTSGetFeatureInfo(options);
        t.ok(control instanceof OpenLayers.Control.WMTSGetFeatureInfo,
             "new OpenLayers.Control.WMTSGetFeatureInfo returns an instance");
        t.eq(control.layers, ["foo"],
             "constructor layers"
            );
        t.ok(control.format instanceof OpenLayers.Format.WMSGetFeatureInfo, "format created");
        t.eq(control.format.foo, "bar", "format options used")
    }

    function test_clickCallBack_option(t) {
        t.plan(9);

        var control;

        control = new OpenLayers.Control.WMTSGetFeatureInfo({
            hover: true
        });
        t.ok(control.handler instanceof OpenLayers.Handler.Hover,
             'constructor creates hover handler');
        t.ok(control.handler.callbacks["move"] === control.cancelHover,
             'constructor registers proper "move" callback in handler');
        t.ok(control.handler.callbacks["pause"] === control.getInfoForHover,
             'constructor registers proper "pause" callback in handler');

        control = new OpenLayers.Control.WMTSGetFeatureInfo();
        t.ok(control.handler instanceof OpenLayers.Handler.Click,
             'constructor creates click handler');
        t.ok(control.handler.callbacks["click"] === control.getInfoForClick,
             'constructor registers proper "click" callback in handler');

        control = new OpenLayers.Control.WMTSGetFeatureInfo({
            clickCallback: "rightclick"
        });
        t.ok(control.handler.callbacks["rightclick"] === control.getInfoForClick,
             'constructor registers proper "rightclick" callback in handler');

        control = new OpenLayers.Control.WMTSGetFeatureInfo({
            clickCallback: "dblclick",
            handlerOptions: {
                click: {
                    "single": false,
                    "double": true
                }
            }
        });
        t.ok(control.handler.callbacks["dblclick"] === control.getInfoForClick,
             'constructor registers proper "dblclick" callback in handler');
        t.eq(control.handler["single"], false,
             'constructor sets "single" to false in handler');
        t.eq(control.handler["double"], true,
             'constructor sets "double" to true in handler');
     }

    function test_destroy(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var click = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            layers: ["foo"]
        });

        var hover = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            layers: ["foo"],
            hover: true
        });

        click.handler.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on click handler");
        };
        hover.handler.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on hover handler");
        };
        click.destroy();
        hover.destroy();
    }

    function test_click(t) {
        t.plan(4);
        var map = new OpenLayers.Map('map');

        // mock up active control
        var control = new OpenLayers.Control.WMTSGetFeatureInfo();
        map.addControl(control);
        control.activate();

        control.request = function(position) {
            t.eq(position.x, 200,
                 "x position is as expected");
            t.eq(position.y, 125,
                 "y position is as expected");
        };

        control.getInfoForClick({xy: {x: 200, y: 125}});
        control.getInfoForHover({xy: {x: 200, y: 125}});
    }

    function test_beforegetfeatureinfo_event(t) {
        t.plan(2);
        var map = new OpenLayers.Map({
            div: "map",
            allOverlays: true,
            layers: [
                new OpenLayers.Layer.WMTS({
                    name: "Test WMTS 1", 
                    url: "/testwmts/",
                    layer: "test1",
                    style: "",
                    matrixSet: "set-id",
                    isBaseLayer: false
                }),
                new OpenLayers.Layer.WMTS({
                    name: "Test WMTS 2", 
                    url: "/testwmts/",
                    layer: "test2",
                    style: "",
                    matrixSet: "set-id",
                    isBaseLayer: false
                })
            ],
            center: new OpenLayers.LonLat(0, 0),
            zoom: 0
        });

        var log = [];

        // test click
        var click = new OpenLayers.Control.WMTSGetFeatureInfo({
            drillDown: true,
            eventListeners: {
                beforegetfeatureinfo: function(evt) {
                    log.push({xy: evt.xy});
                }
            }
        });
        map.addControl(click);
        click.activate();
        click.getInfoForClick({xy: {x: 200, y: 125}});
        t.eq(log.length, 2, "click: beforegetfeatureinfo triggered twice");
        log = [];
        click.deactivate();

        // test hover
        var hover = new OpenLayers.Control.WMTSGetFeatureInfo({
            hover: true,
            eventListeners: {
                beforegetfeatureinfo: function(evt) {
                    log.push({xy: evt.xy});
                }
            }
        });
        map.addControl(hover);
        hover.activate();
        xy = {x: 70, y: 70};
        hover.getInfoForHover({xy: {x: 70, y: 70}});
        t.eq(log.length, 1, "hover: beforegetfeatureinfo triggered once");
        log = [];
        hover.deactivate();
        
        map.destroy();
    }

    function test_activate(t) {
        t.plan(4);
        var map = new OpenLayers.Map("map");
        var click = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            layers: ['ns:type']
        });
        var hover = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            featureType: 'type',
            featureNS: 'http://localhost/ns',
            layers: 'ns:type',
            hover: true
        });
        map.addControl(click);
        map.addControl(hover);
        t.ok(!click.handler.active,
             "click handler is not active prior to activating control");
        t.ok(!hover.handler.active,
             "hover handler is not active prior to activating control");
        click.activate();
        hover.activate();
        t.ok(click.handler.active,
             "click handler is active after activating control");
        t.ok(hover.handler.active,
             "hover handler is active after activating control");
    }

    function test_deactivate(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var click = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            featureType: 'type',
            featureNS: 'http://localhost/ns',
            layers: 'ns:type'
        });
        var hover = new OpenLayers.Control.WMTSGetFeatureInfo({
            url: 'http://localhost/wms',
            featureType: 'type',
            featureNS: 'http://localhost/ns',
            layers: 'ns:type'
        });
        map.addControl(click);
        map.addControl(hover);
        click.activate();
        hover.activate();

        click.handler.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on click handler");
            OpenLayers.Handler.Click.prototype.deactivate.apply(this, arguments);
        };
        hover.handler.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on hover handler");
            OpenLayers.Handler.Hover.prototype.deactivate.apply(this, arguments);
        };
        click.deactivate();
        hover.deactivate();
    }

    function test_getInfoForClick(t) {

        t.plan(13);
        var map = new OpenLayers.Map({
            div: "map",
            getExtent: function() {
                return new OpenLayers.Bounds(-180,-90,180,90);
            }
        });

        var a = new OpenLayers.Layer.WMTS({
            url: "http://a.example.com/wmts",
            layer: "a",
            matrixSet: "bar",
            style: "default"
        });

        var b = new OpenLayers.Layer.WMTS({
            url: "http://b.example.com/wmts",
            layer: "b",
            matrixSet: "bar",
            style: "default",
            isBaseLayer: false
        });

        var c = new OpenLayers.Layer.WMTS({
            url: ["http://c1.example.com/wmts", "http://c2.example.com"],
            layer: "c",
            matrixSet: "bar",
            style: "default",
            isBaseLayer: false
        });
        map.addLayers([a, b, c]);
        map.zoomToMaxExtent();

        var control = new OpenLayers.Control.WMTSGetFeatureInfo({
            layers: [a, b, c]
        });
        map.addControl(control);
        control.activate();

        // log calls to GET
        var log;
        var _request = OpenLayers.Request.GET;
        OpenLayers.Request.GET = function(options) {
            log.push(options);
        };

        // query first layer (drillDown false)
        log = [];
        control.drillDown = false;
        control.queryVisible = false;
        control.getInfoForClick({xy: {x: 200, y: 125}});
        t.eq(log.length, 1, "one requests issued");
        t.eq(log[0].url, "http://c1.example.com/wmts", "{drillDown: false} correct url");
        t.eq(log[0].params["LAYER"], "c", "{drillDown: false} correct layer parameter");

        // query all layers
        log = [];
        control.drillDown = true;
        control.queryVisible = false;
        control.getInfoForClick({xy: {x: 200, y: 125}});
        t.eq(log.length, 3, "three requests issued");
        t.eq(log[0].url, "http://c1.example.com/wmts", "[c] correct url");
        t.eq(log[0].params["LAYER"], "c", "[c] correct layer parameter");
        t.eq(log[1].url, "http://b.example.com/wmts", "[b] correct url");
        t.eq(log[1].params["LAYER"], "b", "[b] correct layer parameter");
        t.eq(log[2].url, "http://a.example.com/wmts", "[a] correct url");
        t.eq(log[2].params["LAYER"], "a", "[a] correct layer parameter");

        // query only visible layers
        log = [];
        control.drillDown = true;
        control.queryVisible = true;
        b.setVisibility(false);
        control.getInfoForClick({xy: {x: 200, y: 125}});
        t.eq(log.length, 2, "two requests issued");
        t.eq(log[0].url, "http://c1.example.com/wmts", "correct url for second visible layer");
        t.eq(log[1].url, "http://a.example.com/wmts", "correct url for first visible layer");

        // clean up
        OpenLayers.Request.GET = _request;
        map.destroy();

    }

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