bring back r6710 now that popup changes are in
git-svn-id: http://svn.openlayers.org/trunk/openlayers@6719 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
455
tests/Control/ModifyFeature.html
Normal file
455
tests/Control/ModifyFeature.html
Normal file
@@ -0,0 +1,455 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="../../lib/OpenLayers.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
function test_initalize(t) {
|
||||
t.plan(3);
|
||||
var layer = {
|
||||
styleMap: {createSymbolizer: function(){}},
|
||||
events: {
|
||||
on: function() {},
|
||||
un: function() {}
|
||||
}
|
||||
};
|
||||
var options = {
|
||||
geometryTypes: "bar"
|
||||
};
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer, options);
|
||||
|
||||
t.ok(control.layer == layer,
|
||||
"constructor sets layer correctly");
|
||||
t.eq(control.selectControl.geometryTypes, "bar",
|
||||
"constructor sets options correctly on feature handler");
|
||||
t.eq(control.mode, OpenLayers.Control.ModifyFeature.RESHAPE,
|
||||
"constructor initializes modification mode correctly");
|
||||
control.destroy();
|
||||
}
|
||||
|
||||
function test_destroy(t) {
|
||||
t.plan(2);
|
||||
var map = new OpenLayers.Map("map");
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
map.addLayer(layer);
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
control.selectControl.destroy = function() {
|
||||
t.ok(true,
|
||||
"control.destroy calls destroy on select control");
|
||||
}
|
||||
control.dragControl.destroy = function() {
|
||||
t.ok(true,
|
||||
"control.destroy calls destroy on feature handler");
|
||||
}
|
||||
control.destroy();
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
function test_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.ModifyFeature(layer);
|
||||
map.addControl(control);
|
||||
t.ok(!control.selectControl.active,
|
||||
"select control is not active prior to activating control");
|
||||
control.activate();
|
||||
t.ok(control.selectControl.active,
|
||||
"select control is active after activating control");
|
||||
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
function test_initDeleteCodes(t) {
|
||||
t.plan(3);
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer, {'deleteCodes': 46});
|
||||
t.eq(control.deleteCodes[0], 46, "Delete code properly turned into an array.");
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
t.eq(control.deleteCodes[0], 46, "Default deleteCodes include delete");
|
||||
t.eq(control.deleteCodes[1], 100, "Default deleteCodes include 'd'");
|
||||
|
||||
control.destroy();
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
function test_handleKeypress(t) {
|
||||
t.plan(10);
|
||||
|
||||
/**
|
||||
* There are two things that we want to test here
|
||||
* 1) test that control.deleteCodes are respected
|
||||
* 3) test that a vertex is properly deleted
|
||||
*
|
||||
* In the future, feature deletion may be added to the control.
|
||||
*/
|
||||
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
var delKey = 46;
|
||||
var dKey = 100;
|
||||
control.deleteCodes = [delKey, dKey];
|
||||
|
||||
// test that a polygon vertex is deleted for all delete codes
|
||||
var point = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Point()
|
||||
);
|
||||
var poly = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Polygon()
|
||||
);
|
||||
|
||||
// mock up vertex deletion
|
||||
control.dragControl.feature = point;
|
||||
control.feature = poly;
|
||||
control.vertices = [point];
|
||||
point.geometry.parent = {
|
||||
removeComponent: function(geometry) {
|
||||
t.eq(geometry.id, point.geometry.id,
|
||||
"vertex deletion: removeComponent called on parent with proper geometry");
|
||||
}
|
||||
};
|
||||
layer.events.on({"featuremodified": function(event) {
|
||||
t.eq(event.feature.id, poly.id, "vertex deletion: featuremodifed triggered");
|
||||
}});
|
||||
layer.drawFeature = function(feature) {
|
||||
t.eq(feature.id, poly.id,
|
||||
"vertex deletion: drawFeature called with the proper feature");
|
||||
};
|
||||
control.resetVertices = function() {
|
||||
t.ok(true, "vertex deletion: resetVertices called");
|
||||
};
|
||||
control.onModification = function(feature) {
|
||||
t.eq(feature.id, poly.id,
|
||||
"vertex deletion: onModification called with the proper feature");
|
||||
};
|
||||
// run the above four tests twice
|
||||
control.handleKeypress(delKey);
|
||||
control.handleKeypress(dKey);
|
||||
|
||||
// now make sure nothing happens if the vertex is mid-drag
|
||||
control.dragControl.handlers.drag.dragging = true;
|
||||
control.handleKeypress(delKey);
|
||||
|
||||
// clean up
|
||||
control.destroy();
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
|
||||
function test_onUnSelect(t) {
|
||||
t.plan(6);
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
var fakeFeature = {'id':'myid'};
|
||||
control.vertices = 'a';
|
||||
control.virtualVertices = 'b';
|
||||
control.features = true;
|
||||
layer.events.on({"afterfeaturemodified": function(event) {
|
||||
t.eq(event.feature, fakeFeature, "afterfeaturemodified triggered");
|
||||
}});
|
||||
control.dragControl.deactivate = function() { t.ok(true, "Deactivate called on drag control"); }
|
||||
control.onModificationEnd = function (feature) { t.eq(feature.id, fakeFeature.id, "onModificationEnd got feature.") }
|
||||
layer.removeFeatures = function(verts) {
|
||||
t.ok(verts == 'a', "Normal verts removed correctly");
|
||||
}
|
||||
layer.destroyFeatures = function(verts) {
|
||||
t.ok(verts == 'b', "Virtual verts destroyed correctly");
|
||||
}
|
||||
control.unselectFeature({feature: fakeFeature});
|
||||
t.eq(control.feature, null, "feature is set to null");
|
||||
|
||||
layer.destroyFeatures = function() {};
|
||||
control.destroy();
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
function test_selectFeature(t) {
|
||||
t.plan(15);
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
control.vertices = [];
|
||||
control.virtualVertices = [];
|
||||
layer.events.on({"beforefeaturemodified": function(event) {
|
||||
t.eq(event.feature, fakeFeature, "beforefeaturemodified triggered");
|
||||
}});
|
||||
control.dragControl.activate = function() { t.ok(true, "drag Control activated"); }
|
||||
control.onModificationStart = function(feature) { t.eq(feature.id, fakeFeature.id, "On Modification Start called with correct feature."); }
|
||||
|
||||
|
||||
// Start of testing
|
||||
|
||||
control.collectVertices = function() { t.fail("Collect vertices called when geom is a point"); }
|
||||
var fakeFeature = {'id':'myFakeFeature','geometry':{'CLASS_NAME':'OpenLayers.Geometry.Point'}};
|
||||
|
||||
// Points don't call collectVertices
|
||||
control.selectFeature({feature: fakeFeature});
|
||||
|
||||
control.collectVertices = function() {
|
||||
t.ok(true, "collectVertices called");
|
||||
this.vertices = 'a';
|
||||
this.virtualVertices = 'd';
|
||||
layer.addFeatures(this.vertices);
|
||||
layer.addFeatures(this.virtualVertices);
|
||||
}
|
||||
|
||||
layer.addFeatures = function(features) {
|
||||
t.ok(features == 'a' || features == 'd', "features passed correctly");
|
||||
}
|
||||
fakeFeature.geometry.CLASS_NAME='OpenLayers.Geometry.Polygon';
|
||||
|
||||
// OnSelect calls collectVertices and passes features to layer
|
||||
control.selectFeature({feature: fakeFeature});
|
||||
|
||||
control.vertices = ['a'];
|
||||
control.virtualVertices = ['b'];
|
||||
|
||||
layer.addFeatures = function(features) {}
|
||||
|
||||
layer.removeFeatures = function(features) {
|
||||
t.eq(features.length, 1, "Correct feature length passed in");
|
||||
}
|
||||
|
||||
// Features are removed whenever they exist
|
||||
control.selectFeature({feature: fakeFeature});
|
||||
|
||||
control.destroy();
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
function test_resetVertices(t) {
|
||||
t.plan(18);
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
var point = new OpenLayers.Geometry.Point(5,6);
|
||||
var point2 = new OpenLayers.Geometry.Point(7,8);
|
||||
var point3 = new OpenLayers.Geometry.Point(9,10);
|
||||
|
||||
control.feature = new OpenLayers.Feature.Vector(point);
|
||||
control.resetVertices();
|
||||
t.eq(control.vertices.length, 0, "Correct vertices length");
|
||||
t.eq(control.virtualVertices.length, 0, "Correct virtual vertices length.");
|
||||
|
||||
var multiPoint = new OpenLayers.Geometry.MultiPoint([point, point2]);
|
||||
control.feature = new OpenLayers.Feature.Vector(multiPoint);
|
||||
control.resetVertices();
|
||||
t.eq(control.vertices.length, 2, "Correct vertices length with multipoint");
|
||||
t.eq(control.virtualVertices.length, 0, "Correct virtual vertices length (multipoint).");
|
||||
|
||||
var line = new OpenLayers.Geometry.LineString([point, point2]);
|
||||
control.feature = new OpenLayers.Feature.Vector(line);
|
||||
control.resetVertices();
|
||||
t.eq(control.vertices.length, 2, "Correct vertices length with line");
|
||||
t.eq(control.virtualVertices.length, 1, "Correct virtual vertices length (linestring).");
|
||||
|
||||
var polygon = new OpenLayers.Geometry.Polygon([new OpenLayers.Geometry.LinearRing([point, point2, point3])]);
|
||||
control.feature = new OpenLayers.Feature.Vector(polygon);
|
||||
control.resetVertices();
|
||||
t.eq(control.vertices.length, 3, "Correct vertices length with polygon");
|
||||
t.eq(control.virtualVertices.length, 3, "Correct virtual vertices length (polygon).");
|
||||
|
||||
control.mode = OpenLayers.Control.ModifyFeature.DRAG;
|
||||
control.resetVertices();
|
||||
t.ok(control.dragHandle != null, "Drag handle is set");
|
||||
t.eq(control.vertices.length, 0, "Correct vertices length with polygon (DRAG)");
|
||||
|
||||
control.mode = OpenLayers.Control.ModifyFeature.ROTATE;
|
||||
control.resetVertices();
|
||||
t.ok(control.radiusHandle != null, "Radius handle is set");
|
||||
t.eq(control.vertices.length, 0, "Correct vertices length with polygon (ROTATE)");
|
||||
|
||||
control.mode = OpenLayers.Control.ModifyFeature.RESIZE;
|
||||
control.resetVertices();
|
||||
t.ok(control.radiusHandle != null, "Radius handle is set");
|
||||
t.eq(control.vertices.length, 0, "Correct vertices length with polygon (RESIZE)");
|
||||
|
||||
control.mode = OpenLayers.Control.ModifyFeature.RESHAPE | OpenLayers.Control.ModifyFeature.RESIZE;
|
||||
control.resetVertices();
|
||||
t.ok(control.radiusHandle != null, "Radius handle is set");
|
||||
t.eq(control.vertices.length, 3, "Correct vertices length with polygon (RESHAPE | RESIZE)");
|
||||
t.eq(control.virtualVertices.length, 3, "Correct virtual vertices length (RESHAPE | RESIZE)");
|
||||
|
||||
control.dragControl.feature = new OpenLayers.Feature.Vector(polygon);
|
||||
control.dragControl.map = {};
|
||||
control.dragControl.map.div = {};
|
||||
control.dragControl.map.div.style = {};
|
||||
control.dragControl.map.div.cursor = "foo";
|
||||
control.dragControl.handlers.drag.deactivate = function() {
|
||||
this.active = false;
|
||||
}
|
||||
control.resetVertices();
|
||||
t.ok(!control.dragControl.handlers.drag.active, "resetVertices deactivates drag handler");
|
||||
control.dragControl.map = null;
|
||||
|
||||
control.destroy();
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
function test_onDrag(t) {
|
||||
t.plan(1);
|
||||
t.ok(true, "onDrag not tested yet.");
|
||||
}
|
||||
|
||||
function test_dragComplete(t) {
|
||||
t.plan(7);
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
|
||||
var fakeFeature = {
|
||||
'geometry': { 'id':'myGeom'},
|
||||
'id': 'fakeFeature'
|
||||
};
|
||||
layer.addFeatures = function (verts) {
|
||||
t.ok(verts == 'virtual' || verts == 'normal', verts + " verts correct");
|
||||
}
|
||||
layer.removeFeatures = function (verts) {
|
||||
t.ok(verts == 'previous virtual' || verts == 'previous normal', verts + " verts correct");
|
||||
}
|
||||
layer.events.on({"featuremodified": function(event) {
|
||||
t.eq(event.feature, fakeFeature, "featuremodified triggered");
|
||||
}});
|
||||
control.onModification = function(feat) {
|
||||
t.eq(feat.id, fakeFeature.id, "onModification gets correct feat");
|
||||
}
|
||||
control.collectVertices = function() {
|
||||
t.ok(true, "collectVertices called");
|
||||
this.vertices = 'normal';
|
||||
this.virtualVertices = 'virtual';
|
||||
layer.addFeatures(this.vertices);
|
||||
layer.addFeatures(this.virtualVertices);
|
||||
}
|
||||
control.feature = fakeFeature;
|
||||
control.vertices = 'previous normal';
|
||||
control.virtualVertices = 'previous virtual';
|
||||
control.dragComplete();
|
||||
|
||||
control.destroy();
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
function test_deactivate(t) {
|
||||
t.plan(2);
|
||||
var map = new OpenLayers.Map("map");
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
map.addLayer(layer);
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
map.addControl(control);
|
||||
|
||||
control.selectControl.deactivate = function() {
|
||||
t.ok(true,
|
||||
"control.deactivate calls deactivate on select control");
|
||||
}
|
||||
control.dragControl.deactivate = function() {
|
||||
t.ok(true,
|
||||
"control.deactivate calls deactivate on drag control");
|
||||
}
|
||||
control.active = true;
|
||||
control.deactivate();
|
||||
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
function test_onModificationStart(t) {
|
||||
t.plan(2);
|
||||
var map = new OpenLayers.Map("map");
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
map.addLayer(layer);
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
map.addControl(control);
|
||||
control.activate();
|
||||
|
||||
// test that beforefeaturemodified is triggered
|
||||
layer.events.on({"beforefeaturemodified": function(event) {
|
||||
t.eq(event.feature.id, testFeature.id,
|
||||
"beforefeaturemodified is triggered with correct feature");
|
||||
}});
|
||||
|
||||
// make sure onModificationStart is called on feature selection
|
||||
var testFeature = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Point(Math.random(), Math.random())
|
||||
);
|
||||
control.onModificationStart = function(feature) {
|
||||
t.eq(feature.id, testFeature.id,
|
||||
"onModificationStart called with the right feature");
|
||||
};
|
||||
control.selectFeature({feature: testFeature});
|
||||
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
function test_onModification(t) {
|
||||
t.plan(3);
|
||||
var map = new OpenLayers.Map("map");
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
map.addLayer(layer);
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
map.addControl(control);
|
||||
control.activate();
|
||||
|
||||
// make sure onModification is called on drag complete
|
||||
var point = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Point(Math.random(), Math.random())
|
||||
);
|
||||
control.feature = point;
|
||||
control.onModification = function(feature) {
|
||||
t.eq(feature.id, point.id,
|
||||
"onModification called with the right feature on drag complete");
|
||||
};
|
||||
control.dragComplete();
|
||||
|
||||
// make sure onModification is called on vertex deletion
|
||||
var poly = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Polygon()
|
||||
);
|
||||
var oldDraw = layer.drawFeature;
|
||||
layer.drawFeature = function() {
|
||||
return;
|
||||
};
|
||||
control.feature = poly;
|
||||
control.vertices = [point];
|
||||
layer.events.on({"featuremodified": function(event) {
|
||||
t.eq(event.feature.id, poly.id, "featuremodified triggered");
|
||||
}});
|
||||
control.onModification = function(feature) {
|
||||
t.eq(feature.id, poly.id,
|
||||
"onModification called with the right feature on vertex delete");
|
||||
};
|
||||
point.geometry.parent = poly.geometry;
|
||||
control.dragControl.feature = point;
|
||||
control.handleKeypress(46);
|
||||
layer.drawFeature = oldDraw;
|
||||
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
function test_onModificationEnd(t) {
|
||||
t.plan(2);
|
||||
var map = new OpenLayers.Map("map");
|
||||
var layer = new OpenLayers.Layer.Vector();
|
||||
map.addLayer(layer);
|
||||
var control = new OpenLayers.Control.ModifyFeature(layer);
|
||||
map.addControl(control);
|
||||
control.activate();
|
||||
|
||||
// make sure onModificationEnd is called on unselect feature
|
||||
var testFeature = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Point(Math.random(), Math.random())
|
||||
);
|
||||
layer.events.on({"afterfeaturemodified": function(event) {
|
||||
t.eq(event.feature.id, testFeature.id, "afterfeaturemodified triggered");
|
||||
}});
|
||||
control.onModificationEnd = function(feature) {
|
||||
t.eq(feature.id, testFeature.id,
|
||||
"onModificationEnd called with the right feature");
|
||||
};
|
||||
control.unselectFeature({feature: testFeature});
|
||||
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map" style="width: 400px; height: 250px;"/>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user