3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_Control_SelectFeature_constructor(t) {
8 // geometryTypes: "foo"
11 var control = new OpenLayers.Control.SelectFeature([layer], options);
12 t.ok(control instanceof OpenLayers.Control.SelectFeature,
13 "new OpenLayers.Control.SelectFeature returns an instance");
14 t.eq(control.layers[0], "bar",
15 "constructor with array of layers sets layer correctly");
16 // t.eq(control.handlers.feature.geometryTypes, "foo",
17 // "constructor sets options correctly on feature handler");
18 t.ok(control.layer instanceof OpenLayers.Layer.Vector.RootContainer, "control has a root container layer if constructor was called with an array of layers");
20 control = new OpenLayers.Control.SelectFeature(layer, options);
21 t.eq(control.layers, null, "this.layers is null if constructor called with a single layer");
22 t.eq(control.layer, layer, "this.layer holds the layer that was passed with the constructor if called with a single layer")
25 function test_Control_SelectFeature_destroy(t) {
27 var map = new OpenLayers.Map("map");
28 var layer = new OpenLayers.Layer.Vector();
30 var control = new OpenLayers.Control.SelectFeature(layer, {box: true});
31 control.handlers.feature.deactivate = function() {
33 "control.deactivate calls deactivate on feature handler");
35 control.handlers.box.deactivate = function() {
37 "control.deactivate calls deactivate on box handler");
39 // should nullify the layer property here
44 function test_Control_SelectFeature_select(t) {
46 var map = new OpenLayers.Map("map");
47 var layer1 = new OpenLayers.Layer.Vector();
48 var layer2 = new OpenLayers.Layer.Vector();
49 map.addLayers([layer1, layer2]);
50 var control = new OpenLayers.Control.SelectFeature([layer1, layer2]);
51 var feature1 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,1));
52 var feature2 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(1,0));
53 layer1.addFeatures(feature1);
54 layer2.addFeatures(feature2);
55 var drawFeature = function(feature, style) {
56 feature.layer.styleMap.createSymbolizer(feature, style);
58 layer1.drawFeature = drawFeature;
59 layer2.drawFeature = drawFeature;
60 control.select(feature1);
61 t.eq(feature1.renderIntent, "select", "render intent is set to select");
62 control.select(feature2);
63 t.eq(feature2.renderIntent, "select", "render intent is set to select");
64 control.unselect(feature1);
65 t.eq(feature1.renderIntent, "default", "render intent is set back to default");
66 control.unselect(feature2);
67 t.eq(feature2.renderIntent, "default", "render intent is set back to default");
70 function test_Control_SelectFeature_clickFeature(t) {
75 drawFeature: function() {},
77 triggerEvent: function() {}
80 // mock up active control
81 var control = new OpenLayers.Control.SelectFeature(layer);
82 control.handlers.feature = {
86 var features = new Array(4);
87 for(var i=0; i<features.length; ++i) {
96 // test that onSelect gets called properly
97 control.onSelect = function(feature) {
99 t.eq(feature.id, features[feature.index].id,
100 "onSelect called with proper feature (" + feature.index + ")");
101 t.eq(feature.tested, feature.test,
102 "onSelect called only once for feature (" + feature.index + ")");
103 t.ok(this == control, "onSelect called in the scope of the control if control.scope is not provided");
106 // test that onUnselect gets called properly
107 control.onUnselect = function(feature) {
109 t.eq(feature.id, features[feature.index].id,
110 "onUnselect called with proper feature (" + feature.index + ")");
111 t.eq(feature.tested, feature.test,
112 "onUnselect called only once for feature (" + feature.index + ")");
113 t.ok(this == control, "onUnselect called in the scope of the control if control.scope is not provided");
116 // mock up first click on first feature (runs 3 tests from onSelect)
117 var feature = features[0];
120 control.clickFeature(feature);
122 // mock up second click on first feature (runs no tests - already selected)
123 control.toggle = false;
124 control.clickFeature(feature);
126 // mock up second click on first feature (runs 3 tests from onUnselect)
127 control.toggle = true;
129 control.clickFeature(feature);
134 function test_box(t) {
136 var map = new OpenLayers.Map("map");
137 var layer = new OpenLayers.Layer.Vector();
139 map.setBaseLayer(layer);
140 map.setCenter(new OpenLayers.LonLat(1,1));
141 var control = new OpenLayers.Control.SelectFeature(layer, {'multiple': true, box: true });
142 var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0));
143 var feature2 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(1,1));
144 var feature3 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-2,-2));
145 var feature4 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString([
146 new OpenLayers.Geometry.Point(0, 0), new OpenLayers.Geometry.Point(1, 1)
148 layer.addFeatures([feature, feature2, feature3, feature4]);
150 map.getLonLatFromPixel = function(arg) {
151 return new OpenLayers.LonLat(arg.x, arg.y);
153 control.selectBox(new OpenLayers.Bounds(-1, -1, 2, 2));
154 t.eq(layer.selectedFeatures.length, 3, "box around all features selects 3 features");
156 control.selectBox(new OpenLayers.Bounds(-3, -3, -1, -1));
157 t.eq(layer.selectedFeatures.length, 4, "box around other features doesn't turn off already selected features.");
159 control.multipleSelect = function() {
162 control.selectBox(new OpenLayers.Bounds(-3, -3, -1, -1));
163 t.eq(layer.selectedFeatures.length, 1, "box around other features correctly turns off already selected features.");
165 control.geometryTypes = null;
166 control.selectBox(new OpenLayers.Bounds(-100, -100, 100, 100));
167 t.eq(layer.selectedFeatures.length, layer.features.length, "all features selected with no geometryTypes filter");
169 control.geometryTypes = ["OpenLayers.Geometry.Point"];
170 control.selectBox(new OpenLayers.Bounds(-100, -100, 100, 100));
171 t.eq(layer.selectedFeatures.length, 3, "3 features selected with geometryTypes filter");
176 function test_Control_SelectFeature_activate(t) {
178 var map = new OpenLayers.Map("map");
179 var layer = new OpenLayers.Layer.Vector();
181 var control = new OpenLayers.Control.SelectFeature(layer, {box: true});
182 map.addControl(control);
183 t.ok(!control.handlers.feature.active,
184 "feature handler is not active prior to activating control");
185 t.ok(!control.handlers.box.active,
186 "box handler is not active prior to activating control");
188 t.ok(control.handlers.feature.active,
189 "feature handler is active after activating control");
190 t.ok(control.handlers.box.active,
191 "box handler is active after activating control");
194 function test_Control_SelectFeature_deactivate(t) {
196 var map = new OpenLayers.Map("map");
197 var layer = new OpenLayers.Layer.Vector();
199 var control = new OpenLayers.Control.SelectFeature(layer, {box: true});
200 map.addControl(control);
203 control.handlers.feature.deactivate = function() {
205 "control.deactivate calls deactivate on feature handler");
206 OpenLayers.Handler.Feature.prototype.deactivate.apply(this, arguments);
208 control.handlers.box.deactivate = function() {
210 "control.deactivate calls deactivate on box handler");
212 control.deactivate();
215 function test_highlighyOnly(t) {
222 var map, layer, ctrl1, ctrl2, _feature, feature, evt, _style;
224 map = new OpenLayers.Map("map");
225 layer = new OpenLayers.Layer.Vector("name", {isBaseLayer: true});
228 ctrl1 = new OpenLayers.Control.SelectFeature(layer, {
229 highlightOnly: false,
232 map.addControl(ctrl1);
234 ctrl2 = new OpenLayers.Control.SelectFeature(layer, {
238 map.addControl(ctrl2);
243 feature = new OpenLayers.Feature.Vector();
244 feature.layer = layer;
246 // override the layer's getFeatureFromEvent func so that it always
247 // returns the feature referenced to by _feature
248 layer.getFeatureFromEvent = function(evt) { return _feature; };
250 evt = {xy: new OpenLayers.Pixel(Math.random(), Math.random())};
252 map.zoomToMaxExtent();
260 ctrl1.renderIntent = "select";
261 ctrl2.renderIntent = "temporary";
263 // mouse over feature, feature is drawn with "temporary"
265 evt.type = "mousemove";
266 map.events.triggerEvent("mousemove", evt);
267 t.eq(feature.renderIntent, "temporary",
268 "feature drawn with expected render intent after \"mouseover\"");
269 t.eq(feature._lastHighlighter, ctrl2.id,
270 "feature._lastHighlighter properly set after \"mouseover\"");
271 t.eq(feature._prevHighlighter, undefined,
272 "feature._prevHighlighter properly set after \"mouseover\"");
274 // click in feature, feature is drawn with "select"
277 map.events.triggerEvent("click", evt);
278 t.eq(feature.renderIntent, "select",
279 "feature drawn with expected render intent after \"clickin\"");
280 t.eq(feature._lastHighlighter, ctrl1.id,
281 "feature._lastHighlighter properly set after \"clickin\"");
282 t.eq(feature._prevHighlighter, ctrl2.id,
283 "feature._prevHighlighter properly set after \"clickin\"");
285 // mouse out of feature, feature is still drawn with "select"
287 evt.type = "mousemove";
288 map.events.triggerEvent("mousemove", evt);
289 t.eq(feature.renderIntent, "select",
290 "feature drawn with expected render intent after \"mouseout\"");
291 t.eq(feature._lastHighlighter, ctrl1.id,
292 "feature._lastHighlighter properly set after \"nouseout\"");
293 t.ok(feature._prevHighlighter, ctrl2.id,
294 "feature._prevHighlighter properly set after \"mouseout\"");
296 // mouse over feature again, feature is drawn with "temporary"
298 evt.type = "mousemove";
299 map.events.triggerEvent("mousemove", evt);
300 t.eq(feature.renderIntent, "temporary",
301 "feature drawn with expected render intent after \"mouseover\"");
302 t.eq(feature._lastHighlighter, ctrl2.id,
303 "feature._lastHighlighter properly set after \"mouseover\"");
304 t.eq(feature._prevHighlighter, ctrl1.id,
305 "feature._prevHighlighter properly set after \"mouseover\"");
307 // mouve out of feature again, feature is still drawn with "select"
309 evt.type = "mousemove";
310 map.events.triggerEvent("mousemove", evt);
311 t.eq(feature.renderIntent, "select",
312 "feature drawn with expected render intent after \"mouseout\"");
313 t.eq(feature._lastHighlighter, ctrl1.id,
314 "feature._lastHighlighter properly set after \"mouseout\"");
315 t.eq(feature._prevHighlighter, undefined,
316 "feature._prevHighlighter properly set after \"mouseout\"");
318 // click out of feature, feature is drawn with "default"
321 map.events.triggerEvent("click", evt);
322 t.eq(feature.renderIntent, "default",
323 "feature drawn with expected render intent after \"clickout\"");
324 t.eq(feature._lastHighlighter, undefined,
325 "feature._lastHighlighter properly set after \"clickout\"");
326 t.eq(feature._prevHighlighter, undefined,
327 "feature._prevHighlighter properly set after \"clickout\"");
331 ctrl1.selectStyle = OpenLayers.Feature.Vector.style["select"];
332 ctrl2.selectStyle = OpenLayers.Feature.Vector.style["temporary"];
334 layer.renderer.drawFeature = function(f, s) {
335 var style = OpenLayers.Feature.Vector.style[_style];
336 t.eq(s, style, "renderer drawFeature called with expected style obj (" + _style + ")");
339 // mouse over feature, feature is drawn with "temporary"
341 _style = "temporary";
342 evt.type = "mousemove";
343 map.events.triggerEvent("mousemove", evt);
345 // click in feature, feature is drawn with "select"
349 map.events.triggerEvent("click", evt);
351 // mouse out of feature, feature is still drawn with "select" and
352 // the renderer drawFeature method should not be called
354 evt.type = "mousemove";
355 map.events.triggerEvent("mousemove", evt);
357 // mouse over feature again, feature is drawn with "temporary"
359 _style = "temporary";
360 evt.type = "mousemove";
361 map.events.triggerEvent("mousemove", evt);
363 // mouve out of feature again, feature is still drawn with "select"
366 evt.type = "mousemove";
367 map.events.triggerEvent("mousemove", evt);
369 // click out of feature, feature is drawn with "default"
373 map.events.triggerEvent("click", evt);
379 <div id="map" style="width: 400px; height: 250px;"/>