Files
openlayers/tests/Control/WMSGetFeatureInfo.html

316 lines
10 KiB
HTML

<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(5);
var options = {
url: 'http://localhost/wms',
layers: ["foo"],
formatOptions: {
foo: "bar"
}
};
var control = new OpenLayers.Control.WMSGetFeatureInfo(options);
t.ok(control instanceof OpenLayers.Control.WMSGetFeatureInfo,
"new OpenLayers.Control.WMSGetFeatureInfo returns an instance");
t.eq(control.url, 'http://localhost/wms',
"constructor sets url correctly");
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_destroy(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
var click = new OpenLayers.Control.WMSGetFeatureInfo({
url: 'http://localhost/wms',
layers: ["foo"]
});
var hover = new OpenLayers.Control.WMSGetFeatureInfo({
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.WMSGetFeatureInfo();
map.addControl(control);
control.activate();
control.request = function(position) {
t.eq(position.x, 50,
"x position is as expected");
t.eq(position.y, 50,
"y position is as expected");
};
control.getInfoForClick({xy: {x: 50, y: 50}});
control.getInfoForHover({xy: {x: 50, y: 50}});
}
function test_beforegetfeatureinfo_event(t) {
t.plan(2);
var map = new OpenLayers.Map('map');
var xy, mode;
// mock up active control
var control = new OpenLayers.Control.WMSGetFeatureInfo({
request: function(position) {},
eventListeners: {
beforegetfeatureinfo: function(evt) {
t.eq(evt.xy, xy,
"beforegetfeatureinfo listener gets " +
"expected xy (" + mode + ")");
}
}
});
map.addControl(control);
control.activate();
// 1 test
mode = "click";
xy = {x: 50, y: 50};
control.getInfoForClick({xy: xy});
// 1 test
mode = "hover";
xy = {x: 70, y: 70};
control.getInfoForHover({xy: xy});
}
function test_activate(t) {
t.plan(4);
var map = new OpenLayers.Map("map");
var click = new OpenLayers.Control.WMSGetFeatureInfo({
url: 'http://localhost/wms',
featureType: 'type',
featureNS: 'http://localhost/ns',
layers: 'ns:type'
});
var hover = new OpenLayers.Control.WMSGetFeatureInfo({
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.WMSGetFeatureInfo({
url: 'http://localhost/wms',
featureType: 'type',
featureNS: 'http://localhost/ns',
layers: 'ns:type'
});
var hover = new OpenLayers.Control.WMSGetFeatureInfo({
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();
}
// Verify that things work all right when we combine different types for the STYLES and LAYERS
// params in the WMS Layers involved
function test_mixedParams(t) {
t.plan(3);
var map = new OpenLayers.Map("map", {
getExtent: function() {return(new OpenLayers.Bounds(-180,-90,180,90));}
}
);
var a = new OpenLayers.Layer.WMS("dummy","http://localhost/wms", {
layers: "a,b,c,d",
styles: "a,b,c,d"
});
var b = new OpenLayers.Layer.WMS("dummy","http://localhost/wms", {
layers: ["a","b","c","d"],
styles: ["a","b","c","d"]
});
var c = new OpenLayers.Layer.WMS("dummy","http://localhost/wms", {
layers: ["a","b","c","d"]
});
var d = new OpenLayers.Layer.WMS("dummy","http://localhost/wms", {
layers: "a,b,c,d"
});
var click = new OpenLayers.Control.WMSGetFeatureInfo({
featureType: 'type',
featureNS: 'ns',
layers: [a, b, c, d]
});
map.addControl(click);
var log = {};
var _request = OpenLayers.Request.GET;
OpenLayers.Request.GET = function(options) {
log.options = options;
};
click.activate();
click.getInfoForClick({xy: {x: 50, y: 50}});
OpenLayers.Request.GET = _request;
t.eq(
log.options && log.options.url,
"http://localhost/wms",
"url from first layer used"
);
t.eq(
log.options && log.options.params.styles.join(","),
"a,b,c,d,a,b,c,d,,,,,,,,",
"Styles merged correctly"
);
t.eq(
log.options && log.options.params.format,
"image/jpeg",
"Required 'format' parameter included"
);
}
function test_urlMatches(t) {
t.plan(5);
var control = new OpenLayers.Control.WMSGetFeatureInfo({
url: "http://host/wms?one=1&two=2"
});
t.ok(!control.urlMatches("foo"), "doesn't match garbage");
t.ok(control.urlMatches("http://host:80/wms?two=2&one=1"), "matches equivalent url");
// give the control more urls to match from
control.layerUrls = ["http://a.host/wms", "http://b.host/wms"];
t.ok(control.urlMatches("http://host:80/wms?two=2&one=1"), "still matches equivalent url");
t.ok(control.urlMatches("http://a.host:80/wms"), "matches equivalent of first of layerUrls");
t.ok(control.urlMatches("http://b.host:80/wms"), "matches equivalent of second of layerUrls");
}
function test_layerUrls(t) {
t.plan(4);
var map = new OpenLayers.Map({
div: "map",
getExtent: function() {
return new OpenLayers.Bounds(-180,-90,180,90);
}
});
var a = new OpenLayers.Layer.WMS(
null, "http://a.mirror/wms", {layers: "a"}
);
var b = new OpenLayers.Layer.WMS(
null, "http://b.mirror/wms", {layers: "b"}
);
var c = new OpenLayers.Layer.WMS(
null, ["http://c.mirror/wms", "http://d.mirror/wms"], {layers: "c"}
);
var control = new OpenLayers.Control.WMSGetFeatureInfo({
url: "http://host/wms",
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.options = options;
};
// control url doesn't match layer urls, no request issued
log = {};
control.getInfoForClick({xy: {x: 50, y: 50}});
t.ok(!log.options, "no url match, no request issued");
// give control a list of urls to match
log = {};
control.layerUrls = ["http://a.mirror/wms", "http://b.mirror/wms"];
control.getInfoForClick({xy: {x: 50, y: 50}});
t.eq(log.options && log.options.url, "http://host/wms", "some match, request issued");
t.eq(log.options && log.options.params["query_layers"].join(","), "a,b", "selected layers queried");
// show that a layer can be matched if it has a urls array itself (first needs to be matched)
log = {};
control.layerUrls = ["http://c.mirror/wms"];
control.getInfoForClick({xy: {x: 50, y: 50}});
t.eq(log.options && log.options.params["query_layers"].join(","), "c", "layer with urls array can be queried");
// clean up
OpenLayers.Request.GET = _request;
map.destroy();
}
</script>
</head>
<body>
<div id="map" style="width: 400px; height: 250px;"/>
</body>
</html>