| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
function test_Handler_constructor(t) { |
|---|
| 6 |
t.plan(4); |
|---|
| 7 |
var map = new OpenLayers.Map('map'); |
|---|
| 8 |
var control = new OpenLayers.Control(); |
|---|
| 9 |
map.addControl(control); |
|---|
| 10 |
|
|---|
| 11 |
var callbacks = {foo: "bar"}; |
|---|
| 12 |
var options = {bar: "foo"}; |
|---|
| 13 |
var handler = new OpenLayers.Handler(control, callbacks, options); |
|---|
| 14 |
t.ok(handler instanceof OpenLayers.Handler, |
|---|
| 15 |
"new OpenLayers.Handler returns object"); |
|---|
| 16 |
t.eq(handler.map.id, map.id, |
|---|
| 17 |
"constructing a handler with a map sets the map on the handler"); |
|---|
| 18 |
t.eq(handler.callbacks.foo, callbacks.foo, |
|---|
| 19 |
"constructor correctly sets callbacks"); |
|---|
| 20 |
t.eq(handler.bar, options.bar, |
|---|
| 21 |
"constructor correctly extends handler with options"); |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
function test_Handler_activate(t) { |
|---|
| 25 |
t.plan(52); |
|---|
| 26 |
var map = new OpenLayers.Map('map'); |
|---|
| 27 |
var control = new OpenLayers.Control(); |
|---|
| 28 |
map.addControl(control); |
|---|
| 29 |
|
|---|
| 30 |
var events = ["mouseover", "mouseout", "mousedown", |
|---|
| 31 |
"mouseup", "mousemove", "click", |
|---|
| 32 |
"dblclick", "resize", "focus", "blur"]; |
|---|
| 33 |
|
|---|
| 34 |
var handler = new OpenLayers.Handler(control); |
|---|
| 35 |
handler.active = true; |
|---|
| 36 |
var activated = handler.activate(); |
|---|
| 37 |
t.ok(!activated, |
|---|
| 38 |
"activate returns false if the handler is already active"); |
|---|
| 39 |
|
|---|
| 40 |
handler.active = false; |
|---|
| 41 |
map.events.registerPriority = function(type, obj, func) { |
|---|
| 42 |
var r = func(); |
|---|
| 43 |
if(typeof r == "string") { |
|---|
| 44 |
|
|---|
| 45 |
t.ok(OpenLayers.Util.indexOf(events, type) > -1, |
|---|
| 46 |
"activate calls registerPriority with browser event: " + type); |
|---|
| 47 |
t.eq(typeof func, "function", |
|---|
| 48 |
"activate calls registerPriority with a function"); |
|---|
| 49 |
t.eq(r, type, |
|---|
| 50 |
"activate calls registerPriority with the correct method"); |
|---|
| 51 |
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", |
|---|
| 52 |
"activate calls registerPriority with the handler"); |
|---|
| 53 |
} else { |
|---|
| 54 |
|
|---|
| 55 |
t.ok(r, "activate calls registerPriority with handler.setEvent"); |
|---|
| 56 |
} |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
for(var i=0; i<events.length; ++i) { |
|---|
| 61 |
setMethod(events[i]); |
|---|
| 62 |
} |
|---|
| 63 |
function setMethod(key) { |
|---|
| 64 |
handler[key] = function() {return key}; |
|---|
| 65 |
} |
|---|
| 66 |
activated = handler.activate(); |
|---|
| 67 |
t.ok(activated, |
|---|
| 68 |
"activated returns true if the handler is not already active"); |
|---|
| 69 |
|
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
function test_Handler_deactivate(t) { |
|---|
| 73 |
t.plan(52); |
|---|
| 74 |
var map = new OpenLayers.Map('map'); |
|---|
| 75 |
var control = new OpenLayers.Control(); |
|---|
| 76 |
map.addControl(control); |
|---|
| 77 |
|
|---|
| 78 |
var events = ["mouseover", "mouseout", "mousedown", |
|---|
| 79 |
"mouseup", "mousemove", "click", |
|---|
| 80 |
"dblclick", "resize", "focus", "blur"]; |
|---|
| 81 |
|
|---|
| 82 |
var handler = new OpenLayers.Handler(control); |
|---|
| 83 |
handler.active = false; |
|---|
| 84 |
var deactivated = handler.deactivate(); |
|---|
| 85 |
t.ok(!deactivated, |
|---|
| 86 |
"deactivate returns false if the handler is already deactive"); |
|---|
| 87 |
|
|---|
| 88 |
handler.activate(); |
|---|
| 89 |
map.events.unregister = function(type, obj, func) { |
|---|
| 90 |
var r = func(); |
|---|
| 91 |
if(typeof r == "string") { |
|---|
| 92 |
|
|---|
| 93 |
t.ok(OpenLayers.Util.indexOf(events, type) > -1, |
|---|
| 94 |
"deactivate calls unregister with browser event: " + type); |
|---|
| 95 |
t.eq(typeof func, "function", |
|---|
| 96 |
"activate calls unregister with a function"); |
|---|
| 97 |
t.eq(func(), type, |
|---|
| 98 |
"activate calls unregister with the correct method"); |
|---|
| 99 |
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", |
|---|
| 100 |
"activate calls unregister with the handler"); |
|---|
| 101 |
} else { |
|---|
| 102 |
|
|---|
| 103 |
t.ok(r, "activate calls registerPriority with handler.setEvent"); |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
for(var i=0; i<events.length; ++i) { |
|---|
| 109 |
|
|---|
| 110 |
(function(key) { |
|---|
| 111 |
handler[key] = function() {return key}; |
|---|
| 112 |
})(events[i]); |
|---|
| 113 |
} |
|---|
| 114 |
deactivated = handler.deactivate(); |
|---|
| 115 |
t.ok(deactivated, |
|---|
| 116 |
"deactivated returns true if the handler is already active"); |
|---|
| 117 |
|
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
function test_Handler_setEvent(t) { |
|---|
| 121 |
t.plan(4); |
|---|
| 122 |
var map = new OpenLayers.Map('map'); |
|---|
| 123 |
var control = new OpenLayers.Control(); |
|---|
| 124 |
map.addControl(control); |
|---|
| 125 |
var handler = new OpenLayers.Handler(control); |
|---|
| 126 |
handler.click = function(evt) { |
|---|
| 127 |
} |
|---|
| 128 |
handler.activate(); |
|---|
| 129 |
var testEvent = { |
|---|
| 130 |
xy: new OpenLayers.Pixel(Math.random(), Math.random()), |
|---|
| 131 |
altKey: (Math.random() > 0.5), |
|---|
| 132 |
shiftKey: (Math.random() > 0.5), |
|---|
| 133 |
ctrlKey: (Math.random() > 0.5) |
|---|
| 134 |
} |
|---|
| 135 |
map.events.triggerEvent("click", testEvent); |
|---|
| 136 |
t.ok(handler.evt.xy.x == testEvent.xy.x && |
|---|
| 137 |
handler.evt.xy.y == testEvent.xy.y, |
|---|
| 138 |
"handler.evt has proper xy object"); |
|---|
| 139 |
t.eq(handler.evt.altKey, testEvent.altKey, |
|---|
| 140 |
"handler.evt.altKey correct"); |
|---|
| 141 |
t.eq(handler.evt.shiftKey, testEvent.shiftKey, |
|---|
| 142 |
"handler.evt.shiftKey correct"); |
|---|
| 143 |
t.eq(handler.evt.ctrlKey, testEvent.ctrlKey, |
|---|
| 144 |
"handler.evt.ctrlKey correct"); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
function test_Handler_destroy(t) { |
|---|
| 148 |
t.plan(5); |
|---|
| 149 |
var map = new OpenLayers.Map('map'); |
|---|
| 150 |
var control = new OpenLayers.Control(); |
|---|
| 151 |
map.addControl(control); |
|---|
| 152 |
var handler = new OpenLayers.Handler(control); |
|---|
| 153 |
var deactivated = false; |
|---|
| 154 |
handler.deactivate = function() { |
|---|
| 155 |
deactivated = true; |
|---|
| 156 |
}; |
|---|
| 157 |
t.ok(handler.control, |
|---|
| 158 |
"handler has a control prior to destroy"); |
|---|
| 159 |
t.ok(handler.map, |
|---|
| 160 |
"handler has a map prior to destroy"); |
|---|
| 161 |
handler.destroy(); |
|---|
| 162 |
t.eq(handler.control, null, |
|---|
| 163 |
"hanlder.control is null after destroy"); |
|---|
| 164 |
t.eq(handler.map, null, |
|---|
| 165 |
"handler.map is null after destroy"); |
|---|
| 166 |
t.ok(deactivated, |
|---|
| 167 |
"handler.deactivate is called by destroy"); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
function test_Handler_checkModifiers(t) { |
|---|
| 171 |
t.plan(26); |
|---|
| 172 |
var handler = new OpenLayers.Handler({}); |
|---|
| 173 |
handler.keyMask = null; |
|---|
| 174 |
var proceed = handler.checkModifiers({}); |
|---|
| 175 |
t.ok(proceed, |
|---|
| 176 |
"checkModifiers returns true if no keyMask on the handler"); |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
var constants = { |
|---|
| 187 |
MOD_NONE: null, |
|---|
| 188 |
MOD_SHIFT: "shiftKey", |
|---|
| 189 |
MOD_CTRL: "ctrlKey", |
|---|
| 190 |
MOD_ALT: "altKey" |
|---|
| 191 |
} |
|---|
| 192 |
var proceed, evt, value, c, k; |
|---|
| 193 |
for(c in constants) { |
|---|
| 194 |
handler.keyMask = OpenLayers.Handler[c]; |
|---|
| 195 |
|
|---|
| 196 |
for(k in constants) { |
|---|
| 197 |
value = constants[k]; |
|---|
| 198 |
evt = {}; |
|---|
| 199 |
if(value) { |
|---|
| 200 |
|
|---|
| 201 |
evt[value] = true; |
|---|
| 202 |
} |
|---|
| 203 |
proceed = handler.checkModifiers(evt); |
|---|
| 204 |
|
|---|
| 205 |
t.eq(k == c, proceed, |
|---|
| 206 |
"returns " + proceed + " if keyMask is " + c + |
|---|
| 207 |
" and " + ((value) ? value : "no key") + " is down"); |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
var constants = ["MOD_SHIFT", "MOD_CTRL", "MOD_ALT"]; |
|---|
| 219 |
var keys = ["shiftKey", "ctrlKey", "altKey"]; |
|---|
| 220 |
var proceed, evt, c1, c2, k1, k2; |
|---|
| 221 |
for(var i=0; i<constants.length-1; ++i) { |
|---|
| 222 |
c1 = constants[i]; |
|---|
| 223 |
for(var j=i+1; j<constants.length; ++j) { |
|---|
| 224 |
c2 = constants[j]; |
|---|
| 225 |
handler.keyMask = OpenLayers.Handler[c1] | |
|---|
| 226 |
OpenLayers.Handler[c2]; |
|---|
| 227 |
|
|---|
| 228 |
for(var x=0; x<keys.length-1; ++x) { |
|---|
| 229 |
k1 = keys[x]; |
|---|
| 230 |
for(var y=x+1; y<keys.length; ++y) { |
|---|
| 231 |
k2 = keys[y]; |
|---|
| 232 |
evt = {}; |
|---|
| 233 |
evt[k1] = true; |
|---|
| 234 |
evt[k2] = true; |
|---|
| 235 |
proceed = handler.checkModifiers(evt); |
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
t.eq(((i == x) || (i == y)) && |
|---|
| 239 |
((j == x) || (j == y)), |
|---|
| 240 |
proceed, |
|---|
| 241 |
"returns " + proceed + " if " + c1 + " | " + c2 + |
|---|
| 242 |
" and " + k1 + " + " + k2 + " is down"); |
|---|
| 243 |
} |
|---|
| 244 |
} |
|---|
| 245 |
} |
|---|
| 246 |
} |
|---|
| 247 |
|
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
</script> |
|---|
| 252 |
</head> |
|---|
| 253 |
<body> |
|---|
| 254 |
<div id="map" style="width: 1024px; height: 512px;"/> |
|---|
| 255 |
</body> |
|---|
| 256 |
</html> |
|---|