| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
function test_Handler_Hover_events(t) { |
|---|
| 6 |
t.plan(10); |
|---|
| 7 |
|
|---|
| 8 |
var map = new OpenLayers.Map('map'); |
|---|
| 9 |
var control = { |
|---|
| 10 |
map: map |
|---|
| 11 |
}; |
|---|
| 12 |
map.events.registerPriority = function(type, obj, func) { |
|---|
| 13 |
var r = func(); |
|---|
| 14 |
if(typeof r == "string") { |
|---|
| 15 |
// this is one of the mock handler methods |
|---|
| 16 |
t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, |
|---|
| 17 |
"registered method is not one of the events " + |
|---|
| 18 |
"that should not be handled"); |
|---|
| 19 |
t.ok(OpenLayers.Util.indexOf(events, type) > -1, |
|---|
| 20 |
"activate calls registerPriority with browser event: " + type); |
|---|
| 21 |
t.eq(typeof func, "function", |
|---|
| 22 |
"activate calls registerPriority with a function"); |
|---|
| 23 |
t.eq(func(), type, |
|---|
| 24 |
"activate calls registerPriority with the correct method"); |
|---|
| 25 |
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Hover", |
|---|
| 26 |
"activate calls registerPriority with the handler"); |
|---|
| 27 |
} |
|---|
| 28 |
} |
|---|
| 29 |
function setMethod(key) { |
|---|
| 30 |
handler[key] = function() {return key}; |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
// list below events that should be handled (events) and those |
|---|
| 34 |
// that should not be handled (nonevents) by the handler |
|---|
| 35 |
var events = ["mousemove", "mouseout"]; |
|---|
| 36 |
var nonevents = ["mousedown", "mouseup", "click", "dblclick", "resize", "focus", "blur"]; |
|---|
| 37 |
var handler = new OpenLayers.Handler.Hover(control); |
|---|
| 38 |
// set browser event like properties on the handler |
|---|
| 39 |
for(var i=0; i<events.length; ++i) { |
|---|
| 40 |
setMethod(events[i]); |
|---|
| 41 |
} |
|---|
| 42 |
handler.activate(); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
function test_Handler_Hover_callbacks(t) { |
|---|
| 46 |
t.plan(8); |
|---|
| 47 |
|
|---|
| 48 |
var map = new OpenLayers.Map('map', {controls: []}); |
|---|
| 49 |
|
|---|
| 50 |
var control = { |
|---|
| 51 |
map: map |
|---|
| 52 |
}; |
|---|
| 53 |
|
|---|
| 54 |
var timers = {}; |
|---|
| 55 |
var sto = window.setTimeout; |
|---|
| 56 |
window.setTimeout = function(func, delay) { |
|---|
| 57 |
var key = Math.random(); |
|---|
| 58 |
timers[key] = true; |
|---|
| 59 |
t.ok(typeof func == "function", |
|---|
| 60 |
"setTimeout called with a function"); |
|---|
| 61 |
t.eq(delay, handler.delay, |
|---|
| 62 |
"setTimeout called with proper delay"); |
|---|
| 63 |
// execute function that is supposed to be delayed |
|---|
| 64 |
func(); |
|---|
| 65 |
return key; |
|---|
| 66 |
} |
|---|
| 67 |
var cto = window.clearTimeout; |
|---|
| 68 |
window.clearTimeout = function(key) { |
|---|
| 69 |
if(timers[key] === true) { |
|---|
| 70 |
delete timers[key]; |
|---|
| 71 |
} else { |
|---|
| 72 |
t.fail("clearTimeout called with non-existent timerId"); |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
var handler = new OpenLayers.Handler.Hover(control, {}); |
|---|
| 77 |
handler.activate(); |
|---|
| 78 |
var testEvt; |
|---|
| 79 |
|
|---|
| 80 |
// test pause and move callbacks - four tests here (2 from setTimeout above) |
|---|
| 81 |
testEvt = {id: Math.random()}; |
|---|
| 82 |
handler.callbacks = { |
|---|
| 83 |
"pause": function(evt) { |
|---|
| 84 |
t.eq(evt.id, testEvt.id, |
|---|
| 85 |
"pause callback called with correct evt"); |
|---|
| 86 |
}, |
|---|
| 87 |
"move": function(evt) { |
|---|
| 88 |
t.eq(evt.id, testEvt.id, |
|---|
| 89 |
"move callback called with correct evt"); |
|---|
| 90 |
} |
|---|
| 91 |
}; |
|---|
| 92 |
map.events.triggerEvent("mousemove", testEvt); |
|---|
| 93 |
handler.clearTimer(); |
|---|
| 94 |
|
|---|
| 95 |
// test pixelTolerance - four tests here (2 from setTimeout above) |
|---|
| 96 |
handler.pixelTolerance = 2; |
|---|
| 97 |
handler.px = new OpenLayers.Pixel(0, 0); |
|---|
| 98 |
testEvt = { |
|---|
| 99 |
xy: new OpenLayers.Pixel(0, 1) |
|---|
| 100 |
}; |
|---|
| 101 |
// mouse moves one pixel, callbacks shouldn't be called |
|---|
| 102 |
handler.callbacks = { |
|---|
| 103 |
"pause": function(evt) { |
|---|
| 104 |
t.fail("(pixelTolerance met) pause callback shouldn't be called"); |
|---|
| 105 |
}, |
|---|
| 106 |
"move": function(evt) { |
|---|
| 107 |
t.fail("(pixelTolerance met) move callback shoudln't be called"); |
|---|
| 108 |
} |
|---|
| 109 |
}; |
|---|
| 110 |
map.events.triggerEvent("mousemove", testEvt); |
|---|
| 111 |
handler.clearTimer(); |
|---|
| 112 |
handler.px = new OpenLayers.Pixel(0, 0); |
|---|
| 113 |
testEvt = { |
|---|
| 114 |
xy: new OpenLayers.Pixel(3, 3) |
|---|
| 115 |
}; |
|---|
| 116 |
// mouse moves 3x3 pixels, callbacks should be called |
|---|
| 117 |
handler.callbacks = { |
|---|
| 118 |
"pause": function(evt) { |
|---|
| 119 |
t.ok(evt.xy == testEvt.xy, "(pixelTolerance unmet) pause callback called"); |
|---|
| 120 |
}, |
|---|
| 121 |
"move": function(evt) { |
|---|
| 122 |
t.ok(evt == testEvt, "(pixelTolerance unmet) move callback called"); |
|---|
| 123 |
} |
|---|
| 124 |
}; |
|---|
| 125 |
map.events.triggerEvent("mousemove", testEvt); |
|---|
| 126 |
handler.clearTimer(); |
|---|
| 127 |
|
|---|
| 128 |
window.setTimeout = sto; |
|---|
| 129 |
window.clearTimeout = cto; |
|---|
| 130 |
} |
|---|
| 131 |
</script> |
|---|
| 132 |
</head> |
|---|
| 133 |
<body> |
|---|
| 134 |
<div id="map" style="width: 300px; height: 150px;"/> |
|---|
| 135 |
</body> |
|---|
| 136 |
</html> |
|---|