Files
openlayers/tests/Control/test_SelectFeature.html

173 lines
6.7 KiB
HTML

<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_Control_SelectFeature_constructor(t) {
t.plan(2);
var options = {
// geometryTypes: "foo"
};
var layer = "bar";
var control = new OpenLayers.Control.SelectFeature(layer, options);
t.ok(control instanceof OpenLayers.Control.SelectFeature,
"new OpenLayers.Control.SelectFeature returns an instance");
t.eq(control.layer, "bar",
"constructor sets layer correctly");
// t.eq(control.featureHandler.geometryTypes, "foo",
// "constructor sets options correctly on feature handler");
}
function test_Control_SelectFeature_destroy(t) {
t.plan(1);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.SelectFeature(layer);
control.handler.destroy = function() {
t.ok(true,
"control.destroy calls destroy on feature handler");
}
// should nullify the layer property here
control.destroy();
}
function test_Control_SelectFeature_select(t) {
t.plan(7);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.SelectFeature(layer);
var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0));
layer.addFeatures(feature);
layer.drawFeature = function() { }
control.select(feature);
t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['select'].strokeColor, "feature style is set to select style");
control.unselect(feature);
t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['default'].strokeColor, "feature style is set back to old style");
// Don't ever overwrite my feature style with undefined properties from the selectStyle
feature.style = {externalGraphic: "foo.png", pointRadius: 39};
control.selectStyle.pointRadius = undefined;
control.select(feature);
t.eq(feature.style.pointRadius, 39, "undefined style property inherited from original feature style");
control.unselect(feature);
// Ok, that one went well. But I'm sure you cannot handle OL.Style.
feature.style = new OpenLayers.Style({externalGraphic: "foo.png", pointRadius: 39});
control.select(feature);
t.eq(feature.style.pointRadius, 39, "undefined style property inherited from original feature style object");
control.unselect(feature);
// Wow, but using OL.Style as selectStyle will break you.
control.selectStyle = new OpenLayers.Style({strokeColor: "green"});
control.select(feature);
t.eq(feature.style.strokeColor, "green", "style correct if both feature.style and selectStyle are OL.Style");
control.unselect(feature);
// Not bad, not bad. And what if I set feature.style back to a style hash?
feature.style = layer.style;
control.select(feature);
t.eq(feature.style.strokeColor, "green", "style still correct with only selectStyle being OL.Style");
control.unselect(feature);
t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style["default"].strokeColor, "style set back to original correctly");
}
function test_Control_SelectFeature_clickFeature(t) {
t.plan(4);
// mock up layer
var layer = {
selectedFeatures: [],
drawFeature: function() {},
events: {
triggerEvent: function() {}
}
};
// mock up active control
var control = new OpenLayers.Control.SelectFeature(layer);
control.handler = {
evt: {}
};
// mock up features
var features = new Array(4);
for(var i=0; i<features.length; ++i) {
features[i] = {
id: Math.random(),
tested: 0,
style: ""
};
}
// test that onSelect gets called properly
control.onSelect = function(feature) {
feature.tested += 1;
t.eq(feature, features[feature.index],
"onSelect called with proper feature (" + feature.index + ")");
t.eq(feature.tested, feature.test,
"onSelect called only once for feature (" + feature.index + ")");
}
// test that onUnselect gets called properly
control.onUnselect = function(feature) {
feature.tested += 1;
t.eq(feature, features[feature.index],
"onUnselect called with proper feature (" + feature.index + ")");
t.eq(feature.tested, feature.test,
"onUnselect called only once for feature (" + feature.index + ")");
}
// mock up first click on first feature (runs 2 tests from onSelect)
var feature = features[0];
feature.index = 0;
feature.test = 1;
control.clickFeature(feature);
// mock up second click on first feature (runs no tests - already selected)
control.toggle = false;
control.clickFeature(feature);
// mock up second click on first feature (runs 2 tests from onUnselect)
control.toggle = true;
feature.test = 2;
control.clickFeature(feature);
}
function test_Control_SelectFeature_activate(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.SelectFeature(layer);
map.addControl(control);
t.ok(!control.handler.active,
"feature handler is not active prior to activating control");
control.activate();
t.ok(control.handler.active,
"feature handler is active after activating control");
}
function test_Control_SelectFeature_deactivate(t) {
t.plan(1);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector();
map.addLayer(layer);
var control = new OpenLayers.Control.SelectFeature(layer);
map.addControl(control);
control.activate();
control.handler.deactivate = function() {
t.ok(true,
"control.deactivate calls deactivate on feature handler");
}
control.deactivate();
}
</script>
</head>
<body>
<div id="map" style="width: 400px; height: 250px;"/>
</body>
</html>