3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_Handler_Hover_events(t) {
8 var map = new OpenLayers.Map('map');
12 map.events.registerPriority = function(type, obj, 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");
24 "activate calls registerPriority with the correct method");
25 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Hover",
26 "activate calls registerPriority with the handler");
29 function setMethod(key) {
30 handler[key] = function() {return key};
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) {
45 function test_Handler_Hover_callbacks(t) {
48 var map = new OpenLayers.Map('map', {controls: []});
55 var sto = window.setTimeout;
56 window.setTimeout = function(func, delay) {
57 var key = Math.random();
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
67 var cto = window.clearTimeout;
68 window.clearTimeout = function(key) {
69 if(timers[key] === true) {
72 t.fail("clearTimeout called with non-existent timerId");
76 var handler = new OpenLayers.Handler.Hover(control, {});
80 // test pause and move callbacks - four tests here (2 from setTimeout above)
81 testEvt = {id: Math.random()};
83 "pause": function(evt) {
84 t.eq(evt.id, testEvt.id,
85 "pause callback called with correct evt");
87 "move": function(evt) {
88 t.eq(evt.id, testEvt.id,
89 "move callback called with correct evt");
92 map.events.triggerEvent("mousemove", testEvt);
95 // test pixelTolerance - four tests here (2 from setTimeout above)
96 handler.pixelTolerance = 2;
97 handler.px = new OpenLayers.Pixel(0, 0);
99 xy: new OpenLayers.Pixel(0, 1)
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");
106 "move": function(evt) {
107 t.fail("(pixelTolerance met) move callback shoudln't be called");
110 map.events.triggerEvent("mousemove", testEvt);
111 handler.clearTimer();
112 handler.px = new OpenLayers.Pixel(0, 0);
114 xy: new OpenLayers.Pixel(3, 3)
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");
121 "move": function(evt) {
122 t.ok(evt == testEvt, "(pixelTolerance unmet) move callback called");
125 map.events.triggerEvent("mousemove", testEvt);
126 handler.clearTimer();
128 window.setTimeout = sto;
129 window.clearTimeout = cto;
134 <div id="map" style="width: 300px; height: 150px;"/>