| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
function test_Handler_Click_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.Click(control, callbacks, options); |
|---|
| 23 |
|
|---|
| 24 |
OpenLayers.Handler.prototype.initialize = oldInit; |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
function test_Handler_Click_activate(t) { |
|---|
| 28 |
t.plan(2); |
|---|
| 29 |
var control = { |
|---|
| 30 |
map: new OpenLayers.Map('map') |
|---|
| 31 |
}; |
|---|
| 32 |
var handler = new OpenLayers.Handler.Click(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 |
handler.dragging = true; |
|---|
| 39 |
activated = handler.activate(); |
|---|
| 40 |
t.ok(activated, |
|---|
| 41 |
"activate returns true if the handler was not already active"); |
|---|
| 42 |
|
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
function test_Handler_Click_events(t) { |
|---|
| 46 |
t.plan(50); |
|---|
| 47 |
|
|---|
| 48 |
var map = new OpenLayers.Map('map'); |
|---|
| 49 |
var control = { |
|---|
| 50 |
map: map |
|---|
| 51 |
}; |
|---|
| 52 |
map.events.registerPriority = function(type, obj, func) { |
|---|
| 53 |
var r = func(); |
|---|
| 54 |
if(typeof r == "string") { |
|---|
| 55 |
// this is one of the mock handler methods |
|---|
| 56 |
t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, |
|---|
| 57 |
"registered method is not one of the events " + |
|---|
| 58 |
"that should not be handled"); |
|---|
| 59 |
t.ok(OpenLayers.Util.indexOf(events, type) > -1, |
|---|
| 60 |
"activate calls registerPriority with browser event: " + type); |
|---|
| 61 |
t.eq(typeof func, "function", |
|---|
| 62 |
"activate calls registerPriority with a function"); |
|---|
| 63 |
t.eq(func(), type, |
|---|
| 64 |
"activate calls registerPriority with the correct method"); |
|---|
| 65 |
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Click", |
|---|
| 66 |
"activate calls registerPriority with the handler"); |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
function setMethod(key) { |
|---|
| 70 |
handler[key] = function() {return key}; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
// list below events that should be handled (events) and those |
|---|
| 74 |
// that should not be handled (nonevents) by the handler |
|---|
| 75 |
var events = ["click", "dblclick", "mousedown", "mouseup", "rightclick"]; |
|---|
| 76 |
var nonevents = ["mousemove", "resize", "focus", "blur"]; |
|---|
| 77 |
var handler = new OpenLayers.Handler.Click(control); |
|---|
| 78 |
// set browser event like properties on the handler |
|---|
| 79 |
for(var i=0; i<events.length; ++i) { |
|---|
| 80 |
setMethod(events[i]); |
|---|
| 81 |
} |
|---|
| 82 |
handler.activate(); |
|---|
| 83 |
|
|---|
| 84 |
// different listeners registered for pixelTolerance option |
|---|
| 85 |
var events = ["click", "dblclick", "mousedown", "mouseup", "rightclick"]; |
|---|
| 86 |
var nonevents = ["mousemove", "resize", "focus", "blur"]; |
|---|
| 87 |
var handler = new OpenLayers.Handler.Click(control, {}, { |
|---|
| 88 |
pixelTolerance: 2 |
|---|
| 89 |
}); |
|---|
| 90 |
for(var i=0; i<events.length; ++i) { |
|---|
| 91 |
setMethod(events[i]); |
|---|
| 92 |
} |
|---|
| 93 |
handler.activate(); |
|---|
| 94 |
|
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
function test_Handler_Click_callbacks(t) { |
|---|
| 98 |
t.plan(13); |
|---|
| 99 |
|
|---|
| 100 |
var map = new OpenLayers.Map('map', {controls: []}); |
|---|
| 101 |
|
|---|
| 102 |
var control = { |
|---|
| 103 |
map: map |
|---|
| 104 |
}; |
|---|
| 105 |
|
|---|
| 106 |
var handler = new OpenLayers.Handler.Click(control, {}); |
|---|
| 107 |
handler.activate(); |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
// set up for single click - three tests here |
|---|
| 111 |
var timers = {}; |
|---|
| 112 |
var sto = window.setTimeout; |
|---|
| 113 |
window.setTimeout = function(func, delay) { |
|---|
| 114 |
var key = Math.random(); |
|---|
| 115 |
timers[key] = true; |
|---|
| 116 |
t.ok(typeof func == "function", |
|---|
| 117 |
"setTimeout called with a function"); |
|---|
| 118 |
t.eq(delay, handler.delay, |
|---|
| 119 |
"setTimeout called with proper delay"); |
|---|
| 120 |
// execute function that is supposed to be delayed |
|---|
| 121 |
func(); |
|---|
| 122 |
return key; |
|---|
| 123 |
} |
|---|
| 124 |
var cto = window.clearTimeout; |
|---|
| 125 |
window.clearTimeout = function(key) { |
|---|
| 126 |
if(timers[key] === true) { |
|---|
| 127 |
delete timers[key]; |
|---|
| 128 |
} else { |
|---|
| 129 |
t.fail("clearTimeout called with non-existent timerId"); |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
var testEvt = {id: Math.random()}; |
|---|
| 133 |
handler.callbacks = { |
|---|
| 134 |
"click": function(evt) { |
|---|
| 135 |
t.eq(evt.id, testEvt.id, |
|---|
| 136 |
"(click w/ single true) click callback called with correct evt"); |
|---|
| 137 |
}, |
|---|
| 138 |
"dblclick": function(evt) { |
|---|
| 139 |
t.fail("(click w/ single true) dblclick should not be called here"); |
|---|
| 140 |
} |
|---|
| 141 |
}; |
|---|
| 142 |
map.events.triggerEvent("click", testEvt); |
|---|
| 143 |
|
|---|
| 144 |
// set up for double click with double false - no tests here (only failures) |
|---|
| 145 |
handler.callbacks = { |
|---|
| 146 |
"click": function(evt) { |
|---|
| 147 |
t.fail("(dblclick w/ double false) click should not be called here"); |
|---|
| 148 |
}, |
|---|
| 149 |
"dblclick": function(evt) { |
|---|
| 150 |
t.fail("(dblclick w/ double false) dblclick should not be called here"); |
|---|
| 151 |
} |
|---|
| 152 |
}; |
|---|
| 153 |
testEvt = Math.random(); |
|---|
| 154 |
map.events.triggerEvent("dblclick", testEvt); |
|---|
| 155 |
|
|---|
| 156 |
// set up for double click with double true - one test here |
|---|
| 157 |
handler.double = true; |
|---|
| 158 |
handler.callbacks = { |
|---|
| 159 |
"click": function(evt) { |
|---|
| 160 |
t.fail("(dblclick w/ double true) click should not be called here"); |
|---|
| 161 |
}, |
|---|
| 162 |
"dblclick": function(evt) { |
|---|
| 163 |
t.eq(evt, testEvt, |
|---|
| 164 |
"(dblclick w/ double true) dblclick called with correct evt"); |
|---|
| 165 |
} |
|---|
| 166 |
}; |
|---|
| 167 |
testEvt = Math.random(); |
|---|
| 168 |
map.events.triggerEvent("dblclick", testEvt); |
|---|
| 169 |
|
|---|
| 170 |
// set up for two clicks with double true - 6 tests here (with timeout ones from above) |
|---|
| 171 |
handler.double = true; |
|---|
| 172 |
handler.callbacks = { |
|---|
| 173 |
"click": function(evt) { |
|---|
| 174 |
t.ok(evt != null, "(two clicks w/ double true) click will not be called here if next three tests pass"); |
|---|
| 175 |
}, |
|---|
| 176 |
"dblclick": function(evt) { |
|---|
| 177 |
t.eq(evt, testEvt, |
|---|
| 178 |
"(two clicks w/ double true) dblclick called with correct evt"); |
|---|
| 179 |
} |
|---|
| 180 |
}; |
|---|
| 181 |
testEvt = Math.random(); |
|---|
| 182 |
map.events.triggerEvent("click", testEvt); |
|---|
| 183 |
t.ok(handler.timerId != null, |
|---|
| 184 |
"(two clicks w/ double true) timer is set to call click"); |
|---|
| 185 |
map.events.triggerEvent("click", testEvt); |
|---|
| 186 |
t.ok(handler.timerId == null, |
|---|
| 187 |
"(two clicks w/ double true) timer is cleared to call click"); |
|---|
| 188 |
map.events.triggerEvent("dblclick", testEvt); |
|---|
| 189 |
handler.destroy(); |
|---|
| 190 |
|
|---|
| 191 |
// set up to tests pixelTolerance - three tests here (2 from setTimeout above) |
|---|
| 192 |
handler = new OpenLayers.Handler.Click(control, {}, { |
|---|
| 193 |
pixelTolerance: 2 |
|---|
| 194 |
}); |
|---|
| 195 |
handler.activate(); |
|---|
| 196 |
var downEvt = { |
|---|
| 197 |
xy: new OpenLayers.Pixel(0, 0) |
|---|
| 198 |
}; |
|---|
| 199 |
map.events.triggerEvent("mousedown", downEvt); |
|---|
| 200 |
var clickEvt = { |
|---|
| 201 |
xy: new OpenLayers.Pixel(0, 1) |
|---|
| 202 |
}; |
|---|
| 203 |
// mouse moves one pixel, click should be called |
|---|
| 204 |
handler.callbacks = { |
|---|
| 205 |
"click": function(evt) { |
|---|
| 206 |
t.ok(evt.xy == clickEvt.xy, "(pixelTolerance met) click called"); |
|---|
| 207 |
} |
|---|
| 208 |
}; |
|---|
| 209 |
map.events.triggerEvent("click", clickEvt); |
|---|
| 210 |
handler.clearTimer(); |
|---|
| 211 |
|
|---|
| 212 |
// mouse moves 3x3 pixels, click should not be called |
|---|
| 213 |
map.events.triggerEvent("mousedown", downEvt); |
|---|
| 214 |
var clickEvt = { |
|---|
| 215 |
xy: new OpenLayers.Pixel(3, 3) |
|---|
| 216 |
}; |
|---|
| 217 |
// mouse moves one pixel, click should be called |
|---|
| 218 |
handler.callbacks = { |
|---|
| 219 |
"click": function(evt) { |
|---|
| 220 |
t.fail("(pixelTolerance not met) click should not be called"); |
|---|
| 221 |
} |
|---|
| 222 |
}; |
|---|
| 223 |
map.events.triggerEvent("click", clickEvt); // no test run |
|---|
| 224 |
handler.clearTimer(); |
|---|
| 225 |
|
|---|
| 226 |
window.setTimeout = sto; |
|---|
| 227 |
window.clearTimeout = cto; |
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
function test_Handler_Click_deactivate(t) { |
|---|
| 233 |
t.plan(4); |
|---|
| 234 |
var control = { |
|---|
| 235 |
map: new OpenLayers.Map('map') |
|---|
| 236 |
}; |
|---|
| 237 |
var handler = new OpenLayers.Handler.Click(control); |
|---|
| 238 |
handler.active = false; |
|---|
| 239 |
var deactivated = handler.deactivate(); |
|---|
| 240 |
t.ok(!deactivated, |
|---|
| 241 |
"deactivate returns false if the handler was not already active"); |
|---|
| 242 |
handler.active = true; |
|---|
| 243 |
handler.down = true; |
|---|
| 244 |
handler.timerId = true; |
|---|
| 245 |
deactivated = handler.deactivate(); |
|---|
| 246 |
t.ok(deactivated, |
|---|
| 247 |
"deactivate returns true if the handler was active already"); |
|---|
| 248 |
t.eq(handler.down, null, |
|---|
| 249 |
"deactivate sets down to null"); |
|---|
| 250 |
t.eq(handler.timerId, null, |
|---|
| 251 |
"deactivate sets timerId to null"); |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
function test_Handler_Click_mouseup(t) { |
|---|
| 255 |
t.plan(4); |
|---|
| 256 |
g_Propagate = {}; |
|---|
| 257 |
g_evt = {}; |
|---|
| 258 |
|
|---|
| 259 |
//no modifiers, no handlerightclicks, no isrightclick |
|---|
| 260 |
var temp = OpenLayers.Event.isRightClick; |
|---|
| 261 |
OpenLayers.Event.isRightClick = function(e) { |
|---|
| 262 |
t.ok(e == g_evt, 'correct event passed in to checkModifiers'); |
|---|
| 263 |
return false; |
|---|
| 264 |
}; |
|---|
| 265 |
|
|---|
| 266 |
var h = { |
|---|
| 267 |
'checkModifiers': function(e) { |
|---|
| 268 |
t.ok(e == g_evt, 'correct event passed in to checkModifiers'); |
|---|
| 269 |
return false; |
|---|
| 270 |
}, |
|---|
| 271 |
'control': { |
|---|
| 272 |
'handleRightClicks': false |
|---|
| 273 |
}, |
|---|
| 274 |
'rightclick': function(e) { |
|---|
| 275 |
t.ok(e == g_evt, 'correct event passed in to checkModifiers'); |
|---|
| 276 |
return g_Propagate; |
|---|
| 277 |
} |
|---|
| 278 |
}; |
|---|
| 279 |
var propagate = OpenLayers.Handler.Click.prototype.mouseup.apply(h, [g_evt]); |
|---|
| 280 |
t.ok(propagate, "default propagate is true when no modifiers, no handlerightclicks, no isrightclick") |
|---|
| 281 |
|
|---|
| 282 |
//modifiers, handlerightclicks, and isrightclick |
|---|
| 283 |
h.checkModifiers = function() { return true; }; |
|---|
| 284 |
h.control.handleRightClicks = true; |
|---|
| 285 |
OpenLayers.Event.isRightClick = function(e) { return true; }; |
|---|
| 286 |
propagate = OpenLayers.Handler.Click.prototype.mouseup.apply(h, [g_evt]); |
|---|
| 287 |
|
|---|
| 288 |
t.ok(propagate == g_Propagate, "return from handler's rightClick() returned from mouseup"); |
|---|
| 289 |
|
|---|
| 290 |
OpenLayers.Event.isRightClick = temp; |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
</script> |
|---|
| 295 |
</head> |
|---|
| 296 |
<body> |
|---|
| 297 |
<div id="map" style="width: 300px; height: 150px;"/> |
|---|
| 298 |
</body> |
|---|
| 299 |
</html> |
|---|