3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_Handler_Polygon_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.Polygon(control, callbacks, options);
24 OpenLayers.Handler.prototype.initialize = oldInit;
27 function test_Handler_Polygon_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.Polygon(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_Polygon_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.Polygon(control, {});
55 var activated = handler.activate();
57 var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
58 handler.mousedown(evt);
60 var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1};
61 handler.mousemove(evt);
62 handler.mousedown(evt);
64 t.ok(handler.line.geometry.getBounds().equals(new OpenLayers.Bounds(0,-35.15625,35.15625,0)), "Correct bounds");
65 var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1};
66 handler.mousedown(evt);
67 var evt = {xy: new OpenLayers.Pixel(125, 100), which: 1};
68 handler.mousemove(evt);
69 t.ok(!handler.polygon.geometry.getBounds().equals(new OpenLayers.Bounds(0,-35.15625,35.15625,0)),
70 "Correct bounds after dragging without letting go. (Came out as "+handler.line.geometry.getBounds().toBBOX() + ".)");
74 function test_callbacks(t) {
76 var map = new OpenLayers.Map("map", {
79 var layer = new OpenLayers.Layer.Vector("foo", {
80 maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
84 var control = new OpenLayers.Control({
87 var handler = new OpenLayers.Handler.Polygon(control, {
105 control.handler = handler;
106 map.addControl(control);
107 map.setCenter(new OpenLayers.LonLat(0, 0), 0);
109 // mock up feature drawing
112 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
113 t.eq(log.type, "create", "[mousedown] create called");
114 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct vertex");
115 t.ok(log.args[1] === handler.polygon, "[mousedown] correct sketch feature");
116 handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
117 t.eq(log.type, "modify", "[mouseup] modify called");
118 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mouseup] correct vertex");
119 t.ok(log.args[1] === handler.polygon, "[mouseup] correct sketch feature");
120 // move to 10, 10 and click
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.polygon, "[mouseup] 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 // move to 0, 10 and double click
128 handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(0, 10)});
129 t.eq(log.type, "modify", "[mousemove] modify called");
130 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 65), "[mousemove] correct vertex");
131 t.ok(log.args[1] === handler.polygon, "[mouseup] correct sketch feature");
132 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 10)});
133 handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 10)});
134 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 10)});
135 handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 10)});
136 handler.dblclick({type: "dblclick", xy: new OpenLayers.Pixel(0, 10)});
137 t.eq(log.type, "done", "[dblclick] done called");
140 new OpenLayers.Geometry.Polygon([
141 new OpenLayers.Geometry.LinearRing([
142 new OpenLayers.Geometry.Point(-150, 75),
143 new OpenLayers.Geometry.Point(-140, 65),
144 new OpenLayers.Geometry.Point(-150, 65),
145 new OpenLayers.Geometry.Point(-150, 75)
148 "[dblclick] correct polygon"
151 // mock up sketch cancel
152 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
153 handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
154 handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 1)});
155 handler.deactivate();
156 t.eq(log.type, "cancel", "[deactivate while drawing] cancel called");
161 function test_Handler_Polygon_destroy(t) {
163 var map = new OpenLayers.Map('map');
164 map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
165 map.zoomToMaxExtent();
166 var control = new OpenLayers.Control();
167 map.addControl(control);
168 var handler = new OpenLayers.Handler.Polygon(control, {foo: 'bar'});
171 var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
172 handler.mousedown(evt);
175 "handler has a layer prior to destroy");
177 "handler has a point prior to destroy");
179 "handler has a line prior to destroy");
180 t.ok(handler.polygon,
181 "handler has a polygon prior to destroy");
183 t.eq(handler.layer, null,
184 "handler.layer is null after destroy");
185 t.eq(handler.point, null,
186 "handler.point is null after destroy");
187 t.eq(handler.line, null,
188 "handler.line is null after destroy");
189 t.eq(handler.polygon, null,
190 "handler.polygon is null after destroy");
199 <div id="map" style="width: 300px; height: 150px;"/>