| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
function test_Handler_Point_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 |
"constructor calls parent with the correct options"); |
|---|
| 21 |
} |
|---|
| 22 |
var handler = new OpenLayers.Handler.Point(control, callbacks, options); |
|---|
| 23 |
|
|---|
| 24 |
OpenLayers.Handler.prototype.initialize = oldInit; |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
function test_Handler_Point_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.Point(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 |
} |
|---|
| 45 |
|
|---|
| 46 |
function test_Handler_Point_events(t) { |
|---|
| 47 |
t.plan(29); |
|---|
| 48 |
|
|---|
| 49 |
var map = new OpenLayers.Map('map'); |
|---|
| 50 |
var control = { |
|---|
| 51 |
map: map |
|---|
| 52 |
}; |
|---|
| 53 |
var handler = new OpenLayers.Handler.Point(control); |
|---|
| 54 |
|
|---|
| 55 |
// list below events that should be handled (events) and those |
|---|
| 56 |
// that should not be handled (nonevents) by the handler |
|---|
| 57 |
var events = ["click", "dblclick", "mousedown", "mouseup", "mousemove"]; |
|---|
| 58 |
var nonevents = ["mouseout", "resize", "focus", "blur"]; |
|---|
| 59 |
map.events.registerPriority = function(type, obj, func) { |
|---|
| 60 |
var r = func(); |
|---|
| 61 |
if(typeof r == "string") { |
|---|
| 62 |
// this is one of the mock handler methods |
|---|
| 63 |
t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, |
|---|
| 64 |
"registered method is not one of the events " + |
|---|
| 65 |
"that should not be handled"); |
|---|
| 66 |
t.ok(OpenLayers.Util.indexOf(events, type) > -1, |
|---|
| 67 |
"activate calls registerPriority with browser event: " + type); |
|---|
| 68 |
t.eq(typeof func, "function", |
|---|
| 69 |
"activate calls registerPriority with a function"); |
|---|
| 70 |
t.eq(func(), type, |
|---|
| 71 |
"activate calls registerPriority with the correct method"); |
|---|
| 72 |
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Point", |
|---|
| 73 |
"activate calls registerPriority with the handler"); |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
// set browser event like properties on the handler |
|---|
| 78 |
for(var i=0; i<events.length; ++i) { |
|---|
| 79 |
setMethod(events[i]); |
|---|
| 80 |
} |
|---|
| 81 |
function setMethod(key) { |
|---|
| 82 |
handler[key] = function() {return key}; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
var activated = handler.activate(); |
|---|
| 86 |
handler.destroy(); |
|---|
| 87 |
|
|---|
| 88 |
// test that click and dblclick are stopped |
|---|
| 89 |
var handler = new OpenLayers.Handler.Point(control); |
|---|
| 90 |
var oldStop = OpenLayers.Event.stop; |
|---|
| 91 |
OpenLayers.Event.stop = function(evt) { |
|---|
| 92 |
t.ok(evt.type == "click" || evt.type == "dblclick", |
|---|
| 93 |
evt.type + " stopped"); |
|---|
| 94 |
} |
|---|
| 95 |
t.eq(handler.click({type: "click"}), false, "click returns false"); |
|---|
| 96 |
t.eq(handler.dblclick({type: "dblclick"}), false, "dblclick returns false"); |
|---|
| 97 |
OpenLayers.Event.stop = oldStop; |
|---|
| 98 |
|
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
function test_Handler_Point_deactivation(t) { |
|---|
| 103 |
t.plan(1); |
|---|
| 104 |
var map = new OpenLayers.Map('map'); |
|---|
| 105 |
var control = new OpenLayers.Control(); |
|---|
| 106 |
map.addControl(control); |
|---|
| 107 |
|
|---|
| 108 |
var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'}); |
|---|
| 109 |
handler.activate(); |
|---|
| 110 |
handler.layer.destroy(); |
|---|
| 111 |
handler.deactivate(); |
|---|
| 112 |
t.eq(handler.layer, null, |
|---|
| 113 |
"deactivate doesn't throw an error if layer was" + |
|---|
| 114 |
" previously destroyed"); |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
function test_Handler_Point_bounds(t) { |
|---|
| 118 |
t.plan(4); |
|---|
| 119 |
var map = new OpenLayers.Map('map'); |
|---|
| 120 |
map.addLayer(new OpenLayers.Layer.WMS("", "", {})); |
|---|
| 121 |
map.zoomToMaxExtent(); |
|---|
| 122 |
var control = new OpenLayers.Control(); |
|---|
| 123 |
map.addControl(control); |
|---|
| 124 |
var handler = new OpenLayers.Handler.Point(control); |
|---|
| 125 |
var activated = handler.activate(); |
|---|
| 126 |
var px = new OpenLayers.Pixel(150, 75); |
|---|
| 127 |
var evt = {xy: px, which: 1}; |
|---|
| 128 |
handler.mousedown(evt); |
|---|
| 129 |
var lonlat = map.getLonLatFromPixel(px); |
|---|
| 130 |
t.eq(handler.point.geometry.x, lonlat.lon, "X is correct"); |
|---|
| 131 |
t.eq(handler.point.geometry.y, lonlat.lat, "Y is correct"); |
|---|
| 132 |
t.ok(handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(lonlat.lon,lonlat.lat,lonlat.lon,lonlat.lat)), "Correct bounds"); |
|---|
| 133 |
var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1}; |
|---|
| 134 |
handler.mousemove(evt); |
|---|
| 135 |
t.ok(!handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(0,0,0,0)), "Bounds changed after moving mouse"); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
function test_Handler_Point_destroy(t) { |
|---|
| 139 |
t.plan(4); |
|---|
| 140 |
var map = new OpenLayers.Map('map'); |
|---|
| 141 |
map.addLayer(new OpenLayers.Layer.WMS("", "", {})); |
|---|
| 142 |
map.zoomToMaxExtent(); |
|---|
| 143 |
var control = new OpenLayers.Control(); |
|---|
| 144 |
map.addControl(control); |
|---|
| 145 |
var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'}); |
|---|
| 146 |
|
|---|
| 147 |
handler.activate(); |
|---|
| 148 |
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; |
|---|
| 149 |
handler.mousedown(evt); |
|---|
| 150 |
|
|---|
| 151 |
t.ok(handler.layer, |
|---|
| 152 |
"handler has a layer prior to destroy"); |
|---|
| 153 |
t.ok(handler.point, |
|---|
| 154 |
"handler has a point prior to destroy"); |
|---|
| 155 |
handler.destroy(); |
|---|
| 156 |
t.eq(handler.layer, null, |
|---|
| 157 |
"handler.layer is null after destroy"); |
|---|
| 158 |
t.eq(handler.point, null, |
|---|
| 159 |
"handler.point is null after destroy"); |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
</script> |
|---|
| 165 |
</head> |
|---|
| 166 |
<body> |
|---|
| 167 |
<div id="map" style="width: 300px; height: 150px;"/> |
|---|
| 168 |
</body> |
|---|
| 169 |
</html> |
|---|