3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_Handler_RegularPolygon_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 "regular polygon constructor calls parent with the correct options");
22 var handler = new OpenLayers.Handler.RegularPolygon(control, callbacks, options);
24 OpenLayers.Handler.prototype.initialize = oldInit;
27 function test_Handler_RegularPolygon_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.RegularPolygon(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_RegularPolygon_deactivation(t) {
49 var map = new OpenLayers.Map('map');
50 var control = new OpenLayers.Control();
51 map.addControl(control);
53 var handler = new OpenLayers.Handler.RegularPolygon(control, {foo: 'bar'});
55 handler.layer.destroy();
57 t.eq(handler.layer, null,
58 "deactivate doesn't throw an error if layer was" +
59 " previously destroyed");
63 function test_Handler_RegularPolygon_four_corners(t) {
65 var map = new OpenLayers.Map('map');
66 map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
67 map.zoomToMaxExtent();
68 var control = new OpenLayers.Control();
69 map.addControl(control);
70 var handler = new OpenLayers.Handler.RegularPolygon(control, {});
71 var activated = handler.activate();
72 var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
74 var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1};
76 t.eq(handler.feature.geometry.getBounds().toBBOX(),
77 "-35.15625,-35.15625,35.15625,35.15625",
78 "correct bounds after move");
79 t.eq(handler.feature.geometry.components[0].components.length, 5,
80 "geometry has 5 components");
81 t.eq(handler.feature.geometry.CLASS_NAME,
82 "OpenLayers.Geometry.Polygon",
83 "geometry is a polygon");
84 t.eq(handler.radius, 25*1.40625, "feature radius as set on handler");
85 var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
87 t.eq(handler.feature.geometry.getBounds().toBBOX(),
88 "-35.15625,-35.15625,35.15625,35.15625",
89 "correct bounds after move with a fixed radius");
91 handler.setOptions({radius:2 / Math.sqrt(2)});
92 var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
95 t.eq(handler.feature.geometry.getBounds().toBBOX(),
97 "bounds with manual radius setting");
98 var evt = {xy: new OpenLayers.Pixel(175, 90), which: 1};
100 t.eq(handler.feature.geometry.getBounds().toBBOX(),
101 "34.15625,-22.09375,36.15625,-20.09375",
102 "bounds with manual radius setting and mousemove");
106 function test_Handler_RegularPolygon_circle(t) {
108 var map = new OpenLayers.Map('map');
109 map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
110 map.zoomToMaxExtent();
111 var control = new OpenLayers.Control();
112 map.addControl(control);
113 var handler = new OpenLayers.Handler.RegularPolygon(control, {}, {'sides':40});
114 var activated = handler.activate();
115 var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
117 var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1};
119 t.eq(handler.feature.geometry.getBounds().toBBOX(),
120 "-35.15625,-35.15625,35.15625,35.15625",
121 "correct bounds after move");
122 t.eq(handler.feature.geometry.components[0].components.length, 41,
123 "geometry has correct numbre of components");
124 t.eq(handler.feature.geometry.CLASS_NAME,
125 "OpenLayers.Geometry.Polygon",
126 "geometry is a polygon");
127 t.eq(handler.radius, 25*1.40625, "feature radius as set on handler");
128 var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
130 t.eq(handler.feature.geometry.getBounds().toBBOX(),
131 "-35.823348,-35.823348,35.823348,35.823348",
132 "correct bounds after move with fixed radius");
134 handler.setOptions({radius:1});
135 var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
138 t.eq(handler.feature.geometry.getBounds().toBBOX(),
139 "-0.996917,-0.996917,0.996917,0.996917",
140 "bounds with manual radius setting");
141 var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
143 t.eq(handler.feature.geometry.getBounds().toBBOX(),
144 "34.159333,-8.028167,36.153167,-6.034333",
145 "bounds with manual radius setting and mousemove");
149 function test_Handler_RegularPolygon_irregular(t) {
152 getLonLatFromPixel: function(px) {
153 return {lon: px.x, lat: px.y};
155 getResolution: function() {
160 addFeatures: function() {},
161 drawFeature: function(feature, style) {
162 var ring = feature.geometry.components[0];
163 t.eq(ring.components[0].x, 20, "correct right");
164 t.eq(ring.components[0].y, 10, "correct bottom");
165 t.eq(ring.components[2].x, 10, "correct left");
166 t.eq(ring.components[2].y, 15, "correct top");
176 var handler = new OpenLayers.Handler.RegularPolygon(
177 control, null, options
179 handler.origin = new OpenLayers.Geometry.Point(10, 10);
180 handler.feature = new OpenLayers.Feature.Vector(
181 new OpenLayers.Geometry.Polygon(
182 [new OpenLayers.Geometry.LinearRing()]
185 // should result in a 10 x 5 rectangle
186 handler.move({xy: {x: 20, y: 15}});
189 function test_callbacks(t) {
193 var map = new OpenLayers.Map("map", {
194 getLonLatFromPixel: function(px) {
195 return {lon: px.x, lat: px.y};
199 var control = {"map": map};
201 var done = function(geom) {
203 "done callback called even if no move between down and up");
206 var handler = new OpenLayers.Handler.RegularPolygon(
207 control, {"done": done});
210 var xy = new OpenLayers.Pixel(Math.random(), Math.random());
212 var isLeftClick = OpenLayers.Event.isLeftClick;
213 OpenLayers.Event.isLeftClick = function() { return true; };
216 map.events.triggerEvent("mousedown", {"xy": xy});
217 map.events.triggerEvent("mouseup", {"xy": xy});
220 OpenLayers.Event.isLeftClick = isLeftClick;
226 <div id="map" style="width: 300px; height: 150px;"/>