3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_Handler_Path_constructor(t) {
7 var control = new OpenLayers.Control();
8 control.id = Math.random();
9 var callbacks = {foo: "bar"};
10 var options = {bar: "foo"};
12 var oldInit = OpenLayers.Handler.prototype.initialize;
14 OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
15 t.eq(con.id, control.id,
16 "constructor calls parent with the correct control");
18 "constructor calls parent with the correct callbacks");
20 "constructor calls parent with the correct options");
22 var handler = new OpenLayers.Handler.Path(control, callbacks, options);
24 OpenLayers.Handler.prototype.initialize = oldInit;
27 function test_Handler_Path_activation(t) {
29 var map = new OpenLayers.Map('map');
30 var control = new OpenLayers.Control();
31 map.addControl(control);
32 var handler = new OpenLayers.Handler.Path(control);
33 handler.active = true;
34 var activated = handler.activate();
36 "activate returns false if the handler was already active");
37 handler.active = false;
38 activated = handler.activate();
40 "activate returns true if the handler was not already active");
41 activated = handler.deactivate();
43 "deactivate returns true if the handler was active already");
47 function test_Handler_Path_bounds(t) {
49 var map = new OpenLayers.Map('map');
50 map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
51 map.zoomToMaxExtent();
52 var control = new OpenLayers.Control();
53 map.addControl(control);
54 var handler = new OpenLayers.Handler.Path(control, {});
55 var activated = handler.activate();
56 var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
57 handler.mousedown(evt);
59 var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1};
60 handler.mousemove(evt);
61 handler.mousedown(evt);
63 t.ok(handler.line.geometry.getBounds().equals(new OpenLayers.Bounds(0,-35.15625,35.15625,0)), "Correct bounds");
64 var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1};
65 handler.mousedown(evt);
66 var evt = {xy: new OpenLayers.Pixel(125, 100), which: 1};
67 handler.mousemove(evt);
68 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() + ".)");
72 function test_callbacks(t) {
74 var map = new OpenLayers.Map("map", {
77 var layer = new OpenLayers.Layer.Vector("foo", {
78 maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
82 var control = new OpenLayers.Control({
85 var handler = new OpenLayers.Handler.Path(control, {
103 control.handler = handler;
104 map.addControl(control);
105 map.setCenter(new OpenLayers.LonLat(0, 0), 0);
107 // mock up feature drawing
109 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
110 t.eq(log.type, "create", "[mousedown] create called");
111 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct vertex");
112 t.ok(log.args[1] === handler.line, "[mousedown] correct sketch feature");
113 handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
114 t.eq(log.type, "modify", "[mouseup] modify called");
115 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mouseup] correct vertex");
116 t.ok(log.args[1] === handler.line, "[mouseup] correct sketch feature");
117 handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 1)});
118 t.eq(log.type, "modify", "[mousemove] modify called");
119 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 74), "[mousemove] correct vertex");
120 t.ok(log.args[1] === handler.line, "[mousemove] correct sketch feature");
121 handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(10, 10)});
122 t.eq(log.type, "modify", "[mousemove] modify called");
123 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-140, 65), "[mousemove] correct vertex");
124 t.ok(log.args[1] === handler.line, "[mousemove] correct sketch feature");
125 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(10, 10)});
126 handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(10, 10)});
127 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(10, 10)});
128 handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(10, 10)});
129 handler.dblclick({type: "dblclick", xy: new OpenLayers.Pixel(10, 10)});
130 t.eq(log.type, "done", "[dblclick] done called");
133 new OpenLayers.Geometry.LineString([
134 new OpenLayers.Geometry.Point(-150, 75),
135 new OpenLayers.Geometry.Point(-140, 65)
137 "[dblclick] correct linestring"
140 // mock up sketch cancel
141 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
142 handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
143 handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 1)});
144 handler.deactivate();
145 t.eq(log.type, "cancel", "[deactivate while drawing] cancel called");
150 function test_Handler_Path_destroy(t) {
152 var map = new OpenLayers.Map('map');
153 map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
154 map.zoomToMaxExtent();
155 var control = new OpenLayers.Control();
156 map.addControl(control);
157 var handler = new OpenLayers.Handler.Path(control, {foo: 'bar'});
160 var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
161 handler.mousedown(evt);
164 "handler has a layer prior to destroy");
166 "handler has a point prior to destroy");
168 "handler has a line prior to destroy");
170 t.eq(handler.layer, null,
171 "handler.layer is null after destroy");
172 t.eq(handler.point, null,
173 "handler.point is null after destroy");
174 t.eq(handler.line, null,
175 "handler.line is null after destroy");
184 <div id="map" style="width: 300px; height: 150px;"/>