| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
function test_Handler_RegularPolygon_constructor(t) { |
|---|
| 6 |
t.plan(3); |
|---|
| 7 |
var control = new OpenLayers.Control(); |
|---|
| 8 |
control.id = Math.random(); |
|---|
| 9 |
var callbacks = {foo: "bar"}; |
|---|
| 10 |
var options = {bar: "foo"}; |
|---|
| 11 |
|
|---|
| 12 |
var oldInit = OpenLayers.Handler.prototype.initialize; |
|---|
| 13 |
|
|---|
| 14 |
OpenLayers.Handler.prototype.initialize = function(con, call, opt) { |
|---|
| 15 |
t.eq(con.id, control.id, |
|---|
| 16 |
"constructor calls parent with the correct control"); |
|---|
| 17 |
t.eq(call, callbacks, |
|---|
| 18 |
"constructor calls parent with the correct callbacks"); |
|---|
| 19 |
t.eq(opt, options, |
|---|
| 20 |
"regular polygon constructor calls parent with the correct options"); |
|---|
| 21 |
} |
|---|
| 22 |
var handler = new OpenLayers.Handler.RegularPolygon(control, callbacks, options); |
|---|
| 23 |
|
|---|
| 24 |
OpenLayers.Handler.prototype.initialize = oldInit; |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
function test_Handler_RegularPolygon_activation(t) { |
|---|
| 28 |
t.plan(3); |
|---|
| 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(); |
|---|
| 35 |
t.ok(!activated, |
|---|
| 36 |
"activate returns false if the handler was already active"); |
|---|
| 37 |
handler.active = false; |
|---|
| 38 |
activated = handler.activate(); |
|---|
| 39 |
t.ok(activated, |
|---|
| 40 |
"activate returns true if the handler was not already active"); |
|---|
| 41 |
activated = handler.deactivate(); |
|---|
| 42 |
t.ok(activated, |
|---|
| 43 |
"deactivate returns true if the handler was active already"); |
|---|
| 44 |
map.destroy(); |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
function test_Handler_RegularPolygon_deactivation(t) { |
|---|
| 48 |
t.plan(1); |
|---|
| 49 |
var map = new OpenLayers.Map('map'); |
|---|
| 50 |
var control = new OpenLayers.Control(); |
|---|
| 51 |
map.addControl(control); |
|---|
| 52 |
|
|---|
| 53 |
var handler = new OpenLayers.Handler.RegularPolygon(control, {foo: 'bar'}); |
|---|
| 54 |
handler.activate(); |
|---|
| 55 |
handler.layer.destroy(); |
|---|
| 56 |
handler.deactivate(); |
|---|
| 57 |
t.eq(handler.layer, null, |
|---|
| 58 |
"deactivate doesn't throw an error if layer was" + |
|---|
| 59 |
" previously destroyed"); |
|---|
| 60 |
map.destroy(); |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
function test_Handler_RegularPolygon_four_corners(t) { |
|---|
| 64 |
t.plan(7); |
|---|
| 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}; |
|---|
| 73 |
handler.down(evt); |
|---|
| 74 |
var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1}; |
|---|
| 75 |
handler.move(evt); |
|---|
| 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}; |
|---|
| 86 |
handler.move(evt); |
|---|
| 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"); |
|---|
| 90 |
handler.cancel(); |
|---|
| 91 |
handler.setOptions({radius:2 / Math.sqrt(2)}); |
|---|
| 92 |
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; |
|---|
| 93 |
handler.down(evt); |
|---|
| 94 |
|
|---|
| 95 |
t.eq(handler.feature.geometry.getBounds().toBBOX(), |
|---|
| 96 |
"-1,-1,1,1", |
|---|
| 97 |
"bounds with manual radius setting"); |
|---|
| 98 |
var evt = {xy: new OpenLayers.Pixel(175, 90), which: 1}; |
|---|
| 99 |
handler.move(evt); |
|---|
| 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"); |
|---|
| 103 |
map.destroy(); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
function test_Handler_RegularPolygon_circle(t) { |
|---|
| 107 |
t.plan(7); |
|---|
| 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}; |
|---|
| 116 |
handler.down(evt); |
|---|
| 117 |
var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1}; |
|---|
| 118 |
handler.move(evt); |
|---|
| 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}; |
|---|
| 129 |
handler.move(evt); |
|---|
| 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"); |
|---|
| 133 |
handler.cancel(); |
|---|
| 134 |
handler.setOptions({radius:1}); |
|---|
| 135 |
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; |
|---|
| 136 |
handler.down(evt); |
|---|
| 137 |
|
|---|
| 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}; |
|---|
| 142 |
handler.move(evt); |
|---|
| 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"); |
|---|
| 146 |
map.destroy(); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
function test_Handler_RegularPolygon_irregular(t) { |
|---|
| 150 |
t.plan(4); |
|---|
| 151 |
var map = { |
|---|
| 152 |
getLonLatFromPixel: function(px) { |
|---|
| 153 |
return {lon: px.x, lat: px.y}; |
|---|
| 154 |
}, |
|---|
| 155 |
getResolution: function() { |
|---|
| 156 |
return 1; |
|---|
| 157 |
} |
|---|
| 158 |
}; |
|---|
| 159 |
var layer = { |
|---|
| 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"); |
|---|
| 167 |
} |
|---|
| 168 |
}; |
|---|
| 169 |
var control = {}; |
|---|
| 170 |
var options = { |
|---|
| 171 |
sides: 4, |
|---|
| 172 |
irregular: true, |
|---|
| 173 |
layer: layer, |
|---|
| 174 |
map: map |
|---|
| 175 |
}; |
|---|
| 176 |
var handler = new OpenLayers.Handler.RegularPolygon( |
|---|
| 177 |
control, null, options |
|---|
| 178 |
); |
|---|
| 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()] |
|---|
| 183 |
) |
|---|
| 184 |
); |
|---|
| 185 |
// should result in a 10 x 5 rectangle |
|---|
| 186 |
handler.move({xy: {x: 20, y: 15}}); |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
function test_callbacks(t) { |
|---|
| 190 |
t.plan(1); |
|---|
| 191 |
|
|---|
| 192 |
// setup |
|---|
| 193 |
var map = new OpenLayers.Map("map", { |
|---|
| 194 |
getLonLatFromPixel: function(px) { |
|---|
| 195 |
return {lon: px.x, lat: px.y}; |
|---|
| 196 |
} |
|---|
| 197 |
}); |
|---|
| 198 |
|
|---|
| 199 |
var control = {"map": map}; |
|---|
| 200 |
|
|---|
| 201 |
var done = function(geom) { |
|---|
| 202 |
t.ok(true, |
|---|
| 203 |
"done callback called even if no move between down and up"); |
|---|
| 204 |
}; |
|---|
| 205 |
|
|---|
| 206 |
var handler = new OpenLayers.Handler.RegularPolygon( |
|---|
| 207 |
control, {"done": done}); |
|---|
| 208 |
handler.activate(); |
|---|
| 209 |
|
|---|
| 210 |
var xy = new OpenLayers.Pixel(Math.random(), Math.random()); |
|---|
| 211 |
|
|---|
| 212 |
var isLeftClick = OpenLayers.Event.isLeftClick; |
|---|
| 213 |
OpenLayers.Event.isLeftClick = function() { return true; }; |
|---|
| 214 |
|
|---|
| 215 |
// test |
|---|
| 216 |
map.events.triggerEvent("mousedown", {"xy": xy}); |
|---|
| 217 |
map.events.triggerEvent("mouseup", {"xy": xy}); |
|---|
| 218 |
|
|---|
| 219 |
// tear down |
|---|
| 220 |
OpenLayers.Event.isLeftClick = isLeftClick; |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
</script> |
|---|
| 224 |
</head> |
|---|
| 225 |
<body> |
|---|
| 226 |
<div id="map" style="width: 300px; height: 150px;"/> |
|---|
| 227 |
</body> |
|---|
| 228 |
</html> |
|---|