]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Control/WMSGetFeatureInfo.html
initial commit
[syp.git] / openlayers / tests / Control / WMSGetFeatureInfo.html
1 <html>
2 <head>
3     <script src="../../lib/OpenLayers.js"></script>
4     <script type="text/javascript">
5
6     function test_initialize(t) {
7         t.plan(5);
8         
9         var options = {
10             url: 'http://localhost/wms',
11             layers: ["foo"],
12             formatOptions: {
13                 foo: "bar"
14             }
15         };
16         var control = new OpenLayers.Control.WMSGetFeatureInfo(options);
17         t.ok(control instanceof OpenLayers.Control.WMSGetFeatureInfo,
18              "new OpenLayers.Control.WMSGetFeatureInfo returns an instance");
19         t.eq(control.url, 'http://localhost/wms',
20              "constructor sets url correctly");        
21         t.eq(control.layers, ["foo"],
22              "constructor layers"
23             );
24         t.ok(control.format instanceof OpenLayers.Format.WMSGetFeatureInfo, "format created");
25         t.eq(control.format.foo, "bar", "format options used")
26     }
27     
28     function test_destroy(t) {
29         t.plan(2);
30         var map = new OpenLayers.Map("map");
31         var click = new OpenLayers.Control.WMSGetFeatureInfo({
32             url: 'http://localhost/wms',
33             layers: ["foo"]
34         });
35
36         var hover = new OpenLayers.Control.WMSGetFeatureInfo({
37             url: 'http://localhost/wms',
38             layers: ["foo"],
39             hover: true
40         });
41
42         click.handler.deactivate = function() {
43             t.ok(true,
44                  "control.deactivate calls deactivate on click handler");
45         }
46         hover.handler.deactivate = function() {
47             t.ok(true,
48                  "control.deactivate calls deactivate on hover handler");
49         }
50         click.destroy();
51         hover.destroy();
52     }
53     
54     function test_click(t) {
55         t.plan(4);
56         var map = new OpenLayers.Map('map');
57
58         // mock up active control
59         var control = new OpenLayers.Control.WMSGetFeatureInfo();
60         map.addControl(control);
61         control.activate();
62        
63         control.request = function(position) {
64             t.eq(position.x, 50,
65                  "x position is as expected");
66             t.eq(position.y, 50,
67                  "y position is as expected");
68         }
69
70         control.getInfoForClick({xy: {x: 50, y: 50}});
71         control.getInfoForHover({xy: {x: 50, y: 50}});
72     }
73    
74     function test_activate(t) {
75         t.plan(4);
76         var map = new OpenLayers.Map("map");
77         var click = new OpenLayers.Control.WMSGetFeatureInfo({
78             url: 'http://localhost/wms',
79             featureType: 'type',
80             featureNS: 'http://localhost/ns',
81             layers: 'ns:type'
82         });
83         var hover = new OpenLayers.Control.WMSGetFeatureInfo({
84             url: 'http://localhost/wms',
85             featureType: 'type',
86             featureNS: 'http://localhost/ns',
87             layers: 'ns:type',
88             hover: true
89         });
90         map.addControl(click);
91         map.addControl(hover);
92         t.ok(!click.handler.active,
93              "click handler is not active prior to activating control");
94         t.ok(!hover.handler.active,
95              "hover handler is not active prior to activating control");
96         click.activate();
97         hover.activate();
98         t.ok(click.handler.active,
99              "click handler is active after activating control");
100         t.ok(hover.handler.active,
101              "hover handler is active after activating control");
102     }
103
104     function test_deactivate(t) {
105         t.plan(2);
106         var map = new OpenLayers.Map("map");
107         var click = new OpenLayers.Control.WMSGetFeatureInfo({
108             url: 'http://localhost/wms',
109             featureType: 'type',
110             featureNS: 'http://localhost/ns',
111             layers: 'ns:type'
112         });
113         var hover = new OpenLayers.Control.WMSGetFeatureInfo({
114             url: 'http://localhost/wms',
115             featureType: 'type',
116             featureNS: 'http://localhost/ns',
117             layers: 'ns:type'
118         });
119         map.addControl(click);
120         map.addControl(hover);
121         click.activate();
122         hover.activate();
123
124         click.handler.deactivate = function() {
125             t.ok(true,
126                  "control.deactivate calls deactivate on click handler");
127             OpenLayers.Handler.Click.prototype.deactivate.apply(this, arguments);
128         }
129         hover.handler.deactivate = function() {
130             t.ok(true,
131                  "control.deactivate calls deactivate on hover handler");
132             OpenLayers.Handler.Hover.prototype.deactivate.apply(this, arguments);
133         }
134         click.deactivate();
135         hover.deactivate();
136     }
137
138     // Verify that things work all right when we combine different types for the STYLES and LAYERS 
139     // params in the WMS Layers involved
140     function test_mixedParams(t) {
141         t.plan(2);
142         var map = new OpenLayers.Map("map", {
143             getExtent: function() {return(new OpenLayers.Bounds(-180,-90,180,90));}
144             }
145         );
146
147         var a = new OpenLayers.Layer.WMS("dummy","http://localhost/wms", {
148             layers: "a,b,c,d",
149             styles: "a,b,c,d"
150         });
151
152         var b = new OpenLayers.Layer.WMS("dummy","http://localhost/wms", {
153             layers: ["a","b","c","d"],
154             styles: ["a","b","c","d"]
155         });
156
157         var c = new OpenLayers.Layer.WMS("dummy","http://localhost/wms", {
158             layers: ["a","b","c","d"]
159         });
160
161         var d = new OpenLayers.Layer.WMS("dummy","http://localhost/wms", {
162             layers: "a,b,c,d"
163         });
164
165         var click = new OpenLayers.Control.WMSGetFeatureInfo({
166             featureType: 'type',
167             featureNS: 'ns',
168             layers: [a, b, c, d]
169         });
170
171         map.addControl(click);
172
173         var log = {};
174         var _request = OpenLayers.Request.GET;
175         OpenLayers.Request.GET = function(options) {
176             log.options = options;
177         };
178         click.activate();
179         click.getInfoForClick({xy: {x: 50, y: 50}});
180         OpenLayers.Request.GET = _request;
181         
182         t.eq(
183             log.options && log.options.url,
184             "http://localhost/wms",
185             "url from first layer used"
186         );
187         t.eq(
188             log.options && log.options.params.styles.join(","),
189             "a,b,c,d,a,b,c,d,,,,,,,,",
190             "Styles merged correctly"
191         );        
192         
193     }
194     
195     function test_urlMatches(t) {
196         
197         t.plan(5);
198
199         var control = new OpenLayers.Control.WMSGetFeatureInfo({
200             url: "http://host/wms?one=1&two=2"
201         });
202         
203         t.ok(!control.urlMatches("foo"), "doesn't match garbage");
204         t.ok(control.urlMatches("http://host:80/wms?two=2&one=1"), "matches equivalent url");
205         
206         // give the control more urls to match from
207         control.layerUrls = ["http://a.host/wms", "http://b.host/wms"];
208         
209         t.ok(control.urlMatches("http://host:80/wms?two=2&one=1"), "still matches equivalent url");
210         t.ok(control.urlMatches("http://a.host:80/wms"), "matches equivalent of first of layerUrls");
211         t.ok(control.urlMatches("http://b.host:80/wms"), "matches equivalent of second of layerUrls");
212
213     }
214     
215     function test_layerUrls(t) {
216
217         t.plan(4);
218         var map = new OpenLayers.Map({
219             div: "map", 
220             getExtent: function() {
221                 return new OpenLayers.Bounds(-180,-90,180,90);
222             }
223         });
224
225         var a = new OpenLayers.Layer.WMS(
226             null, "http://a.mirror/wms", {layers: "a"}
227         );
228         var b = new OpenLayers.Layer.WMS(
229             null, "http://b.mirror/wms", {layers: "b"}
230         );
231         var c = new OpenLayers.Layer.WMS(
232             null, ["http://c.mirror/wms", "http://d.mirror/wms"], {layers: "c"}
233         );
234         
235         var control = new OpenLayers.Control.WMSGetFeatureInfo({
236             url: "http://host/wms",
237             layers: [a, b, c]
238         });
239         map.addControl(control);
240         control.activate();
241         
242         // log calls to GET
243         var log;
244         var _request = OpenLayers.Request.GET;
245         OpenLayers.Request.GET = function(options) {
246             log.options = options;
247         };
248         
249         // control url doesn't match layer urls, no request issued
250         log = {};
251         control.getInfoForClick({xy: {x: 50, y: 50}});
252         t.ok(!log.options, "no url match, no request issued");
253         
254         // give control a list of urls to match
255         log = {};
256         control.layerUrls = ["http://a.mirror/wms", "http://b.mirror/wms"];
257         control.getInfoForClick({xy: {x: 50, y: 50}});
258         t.eq(log.options && log.options.url, "http://host/wms", "some match, request issued");
259         t.eq(log.options && log.options.params["query_layers"].join(","), "a,b", "selected layers queried");
260         
261         // show that a layer can be matched if it has a urls array itself (first needs to be matched)
262         log = {};
263         control.layerUrls = ["http://c.mirror/wms"];
264         control.getInfoForClick({xy: {x: 50, y: 50}});
265         t.eq(log.options && log.options.params["query_layers"].join(","), "c", "layer with urls array can be queried");
266         
267         // clean up
268         OpenLayers.Request.GET = _request;
269         map.destroy();
270
271     }
272
273     </script>
274 </head>
275 <body>
276     <div id="map" style="width: 400px; height: 250px;"/>
277 </body>
278 </html>