[OpenLayers-Commits] r11078 -
sandbox/elemoine/draw-feature/tests/Handler
commits-20090109 at openlayers.org
commits-20090109 at openlayers.org
Thu Feb 10 05:45:25 EST 2011
Author: erilem
Date: 2011-02-10 02:45:25 -0800 (Thu, 10 Feb 2011)
New Revision: 11078
Modified:
sandbox/elemoine/draw-feature/tests/Handler/Path.html
Log:
path handler - fix and add tests
Modified: sandbox/elemoine/draw-feature/tests/Handler/Path.html
===================================================================
--- sandbox/elemoine/draw-feature/tests/Handler/Path.html 2011-02-10 10:45:19 UTC (rev 11077)
+++ sandbox/elemoine/draw-feature/tests/Handler/Path.html 2011-02-10 10:45:25 UTC (rev 11078)
@@ -25,12 +25,27 @@
}
function test_Handler_Path_activation(t) {
- t.plan(3);
- var map = new OpenLayers.Map('map');
+ t.plan(12);
+ var log = [];
+ var map = new OpenLayers.Map("map", {
+ resolutions: [1]
+ });
+ var layer = new OpenLayers.Layer.Vector("foo", {
+ maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
+ isBaseLayer: true
+ });
+ map.addLayer(layer);
var control = new OpenLayers.Control();
+ var handler = new OpenLayers.Handler.Path(control, {
+ "create": function(g, f) {
+ log.push({geometry: g, feature: f});
+ }
+ });
+ control.handler = handler;
map.addControl(control);
- var handler = new OpenLayers.Handler.Path(control);
+ map.setCenter(new OpenLayers.LonLat(0, 0), 0);
handler.active = true;
+
var activated = handler.activate();
t.ok(!activated,
"activate returns false if the handler was already active");
@@ -38,20 +53,44 @@
activated = handler.activate();
t.ok(activated,
"activate returns true if the handler was not already active");
+ t.ok(handler.layer instanceof OpenLayers.Layer.Vector,
+ "activate creates a vector layer");
+ t.ok(handler.layer.map == map,
+ "activate adds the vector layer to the map");
+ t.ok(handler.point instanceof OpenLayers.Feature.Vector,
+ "activate creates a point feature");
+ t.ok(handler.point.layer == handler.layer,
+ "activate adds the point feature to the layer");
+ t.ok(handler.line instanceof OpenLayers.Feature.Vector,
+ "acttivates creates a line feature");
+ t.ok(handler.line.layer == handler.layer,
+ "activate adds the line feature to the layer");
+ t.eq(log.length, 1,
+ "activate calls \"create\" once");
+ t.geom_eq(log[0].geometry, handler.point.geometry,
+ "\"create\" called with expected geometry");
+ t.ok(log[0].feature == handler.line,
+ "\"create\" called with expected feature");
activated = handler.deactivate();
t.ok(activated,
"deactivate returns true if the handler was active already");
- map.destroy();
+
+ map.destroy();
}
function test_Handler_Path_bounds(t) {
t.plan(2);
+ var geometry;
var map = new OpenLayers.Map('map');
map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
map.zoomToMaxExtent();
var control = new OpenLayers.Control();
map.addControl(control);
- var handler = new OpenLayers.Handler.Path(control, {});
+ var handler = new OpenLayers.Handler.Path(control, {
+ modify: function(g, f) {
+ geometry = f.geometry;
+ }
+ });
var activated = handler.activate();
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
handler.mousedown(evt);
@@ -60,17 +99,23 @@
handler.mousemove(evt);
handler.mousedown(evt);
handler.mouseup(evt);
- t.ok(handler.line.geometry.getBounds().equals(new OpenLayers.Bounds(0,-35.15625,35.15625,0)), "Correct bounds");
+ t.ok(geometry.getBounds().equals(
+ new OpenLayers.Bounds(0,-35.15625,35.15625,0)),
+ "Correct bounds");
var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1};
handler.mousedown(evt);
var evt = {xy: new OpenLayers.Pixel(125, 100), which: 1};
handler.mousemove(evt);
- t.ok(!handler.line.geometry.getBounds().equals(new OpenLayers.Bounds(0,-35.15625,35.15625,0)), "Correct bounds after dragging without letting go. (Came out as "+handler.line.geometry.getBounds().toBBOX() + ".)");
+ t.ok(!handler.line.geometry.getBounds().equals(
+ new OpenLayers.Bounds(0,-35.15625,35.15625,0)),
+ "Correct bounds after dragging without letting go. " +
+ "(Came out as " + handler.line.geometry.getBounds().toBBOX() +
+ ".)");
map.destroy();
}
function test_callbacks(t) {
- t.plan(15);
+ t.plan(36);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
@@ -79,71 +124,124 @@
isBaseLayer: true
});
map.addLayer(layer);
- var control = new OpenLayers.Control({
- });
- var log = {};
+ var control = new OpenLayers.Control({});
+ var log = [];
var handler = new OpenLayers.Handler.Path(control, {
create: function() {
- log.type = "create",
- log.args = arguments
+ log.push({type: "create", args: arguments});
},
+ point: function() {
+ log.push({type: "point", args: arguments});
+ },
modify: function() {
- log.type = "modify",
- log.args = arguments
+ log.push({type: "modify", args: arguments});
},
done: function() {
- log.type = "done",
- log.args = arguments
+ log.push({type: "done", args: arguments});
},
cancel: function() {
- log.type = "cancel",
- log.args = arguments
+ log.push({type: "cancel", args: arguments});
}
});
control.handler = handler;
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
- // mock up feature drawing
+ // create line
handler.activate();
- handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
- t.eq(log.type, "create", "[mousedown] create called");
- t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct vertex");
- t.ok(log.args[1] === handler.line, "[mousedown] correct sketch feature");
+ t.eq(log.length, 1, "[activate] called back");
+ t.eq(log[0].type, "create", "[activate] create called");
+ t.geom_eq(log[0].args[0], new OpenLayers.Geometry.Point(-200, 125),
+ "[activate] correct point");
+ t.ok(log[0].args[1] == handler.line,
+ "[activate] correct feature");
+ // mouse down
+ handler.mousedown(
+ {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
+ t.eq(log.length, 2, "[mousedown] called back");
+ t.eq(log[1].type, "modify", "[mousedown] modify called");
+ t.geom_eq(log[1].args[0], new OpenLayers.Geometry.Point(-150, 75),
+ "[mousedown] correct point");
+ t.ok(log[1].args[1] == handler.line,
+ "[mousedown] correct feature");
+ // mouse up
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
- t.eq(log.type, "modify", "[mouseup] modify called");
- t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mouseup] correct vertex");
- t.ok(log.args[1] === handler.line, "[mouseup] correct sketch feature");
- handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 1)});
- t.eq(log.type, "modify", "[mousemove] modify called");
- t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 74), "[mousemove] correct vertex");
- t.ok(log.args[1] === handler.line, "[mousemove] correct sketch feature");
- handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(10, 10)});
- t.eq(log.type, "modify", "[mousemove] modify called");
- t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-140, 65), "[mousemove] correct vertex");
- t.ok(log.args[1] === handler.line, "[mousemove] correct sketch feature");
- handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(10, 10)});
- handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(10, 10)});
- handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(10, 10)});
- handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(10, 10)});
- handler.dblclick({type: "dblclick", xy: new OpenLayers.Pixel(10, 10)});
- t.eq(log.type, "done", "[dblclick] done called");
- t.geom_eq(
- log.args[0],
+ t.eq(log.length, 4, "[mouseup] called back twice");
+ t.eq(log[2].type, "point", "[mouseup] point called");
+ t.geom_eq(log[2].args[0], new OpenLayers.Geometry.Point(-150, 75),
+ "[mouseup] correct point");
+ t.geom_eq(log[2].args[1],
+ new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(-150, 75),
+ new OpenLayers.Geometry.Point(-150, 75)
+ ]), "[mouseup] correct line");
+ t.eq(log[3].type, "modify", "[mouseup] modify called");
+ t.geom_eq(log[3].args[0], new OpenLayers.Geometry.Point(-150, 75),
+ "[mouseup] correct point");
+ t.ok(log[3].args[1] == handler.line,
+ "[mouseup] correct feature");
+ // mouse move
+ handler.mousemove({type: "mousemove",
+ xy: new OpenLayers.Pixel(1, 1)});
+ t.eq(log.length, 5, "[mousemove] called back");
+ t.eq(log[4].type, "modify", "[mousemove] modify called");
+ t.geom_eq(log[4].args[0], new OpenLayers.Geometry.Point(-149, 74),
+ "[mousemove] correct point");
+ t.ok(log[4].args[1] === handler.line,
+ "[mousemove] correct feature");
+ // mouse move
+ handler.mousemove({type: "mousemove",
+ xy: new OpenLayers.Pixel(10, 10)});
+ t.eq(log.length, 6, "[mousemove] called back");
+ t.eq(log[5].type, "modify", "[mousemove] modify called");
+ t.geom_eq(log[5].args[0], new OpenLayers.Geometry.Point(-140, 65),
+ "[mousemove] correct point");
+ t.ok(log[5].args[1] === handler.line,
+ "[mousemove] correct feature");
+ // mouse down ("modify")
+ handler.mousedown({type: "mousedown",
+ xy: new OpenLayers.Pixel(10, 10)});
+ t.eq(log.length, 7, "[mousedown] called back");
+ // mouse up ("point", "modify")
+ handler.mouseup({type: "mouseup",
+ xy: new OpenLayers.Pixel(10, 10)});
+ t.eq(log.length, 9, "[mouseup] called back twice");
+ // mouse down ("modify")
+ handler.mousedown({type: "mousedown",
+ xy: new OpenLayers.Pixel(10, 10)});
+ t.eq(log.length, 10, "[mousedown] called back");
+ // mouse up ("point", "modify")
+ handler.mouseup({type: "mouseup",
+ xy: new OpenLayers.Pixel(10, 10)});
+ t.eq(log.length, 10, "[mouseup] not called back");
+ // double click
+ handler.dblclick({type: "dblclick",
+ xy: new OpenLayers.Pixel(10, 10)});
+ t.eq(log.length, 12, "[dblclick] called back");
+ t.eq(log[10].type, "done", "[dblclick] done called");
+ t.geom_eq(log[10].args[0],
new OpenLayers.Geometry.LineString([
new OpenLayers.Geometry.Point(-150, 75),
new OpenLayers.Geometry.Point(-140, 65)
]),
"[dblclick] correct linestring"
);
+ t.eq(log[11].type, "create", "[dblclick] create called");
+ t.geom_eq(log[11].args[0], new OpenLayers.Geometry.Point(-200, 125),
+ "[dblclick] correct point");
+ t.ok(log[11].args[1] == handler.line,
+ "[dblclick] correct feature");
+ // cancel
+ handler.cancel();
+ t.eq(log.length, 13, "[cancel] called back");
+ t.eq(log[12].type, "cancel", "[cancel] canced called");
+ t.geom_eq(log[12].args[0],
+ new OpenLayers.Geometry.LineString([
+ new OpenLayers.Geometry.Point(-200, 125)
+ ]),
+ "[cancel] correct linestring"
+ );
- // mock up sketch cancel
- handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
- handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
- handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 1)});
- handler.deactivate();
- t.eq(log.type, "cancel", "[deactivate while drawing] cancel called");
-
map.destroy();
}
More information about the Commits
mailing list