summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Handler/Hover.html
blob: 150218a2c3905e36816629633aa32a71a2fe8296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    function test_Handler_Hover_events(t) {
        t.plan(10);
        
        var map = new OpenLayers.Map('map');
        var control = {
            map: map
        };
        map.events.registerPriority = function(type, obj, func) {
            var r = func();
            if(typeof r == "string") {
                // this is one of the mock handler methods
                t.eq(OpenLayers.Util.indexOf(nonevents, type), -1,
                     "registered method is not one of the events " +
                     "that should not be handled");
                t.ok(OpenLayers.Util.indexOf(events, type) > -1,
                     "activate calls registerPriority with browser event: " + type);
                t.eq(typeof func, "function",
                     "activate calls registerPriority with a function");
                t.eq(func(), type,
                     "activate calls registerPriority with the correct method");
                t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Hover",
                     "activate calls registerPriority with the handler");
            }
        }
        function setMethod(key) {
            handler[key] = function() {return key};
        }

        // list below events that should be handled (events) and those
        // that should not be handled (nonevents) by the handler
        var events = ["mousemove", "mouseout"];
        var nonevents = ["mousedown", "mouseup", "click", "dblclick", "resize", "focus", "blur"];
        var handler = new OpenLayers.Handler.Hover(control);
        // set browser event like properties on the handler
        for(var i=0; i<events.length; ++i) {
            setMethod(events[i]);
        }
        handler.activate();
    }

    function test_Handler_Hover_callbacks(t) {
        t.plan(8);
        
        var map = new OpenLayers.Map('map', {controls: []});

        var control = {
            map: map
        };
        
        var timers = {};
        var sto = window.setTimeout;
        window.setTimeout = function(func, delay) {
            var key = Math.random();
            timers[key] = true;
            t.ok(typeof func == "function",
                 "setTimeout called with a function");
            t.eq(delay, handler.delay,
                 "setTimeout called with proper delay");
            // execute function that is supposed to be delayed
            func();
            return key;
        }
        var cto = window.clearTimeout;
        window.clearTimeout = function(key) {
            if(timers[key] === true) {
                delete timers[key];
            } else {
                t.fail("clearTimeout called with non-existent timerId");
            }
        }

        var handler = new OpenLayers.Handler.Hover(control, {});
        handler.activate();
        var testEvt;

        // test pause and move callbacks - four tests here (2 from setTimeout above)
        testEvt = {id: Math.random()};
        handler.callbacks = {
            "pause": function(evt) {
                t.eq(evt.id, testEvt.id,
                     "pause callback called with correct evt");
            },
            "move": function(evt) {
               t.eq(evt.id, testEvt.id,
                   "move callback called with correct evt");
            }
        };
        map.events.triggerEvent("mousemove", testEvt);
        handler.clearTimer();

        // test pixelTolerance - four tests here (2 from setTimeout above)
        handler.pixelTolerance = 2;
        handler.px = new OpenLayers.Pixel(0, 0);
        testEvt = {
            xy: new OpenLayers.Pixel(0, 1)
        };
        // mouse moves one pixel, callbacks shouldn't be called
        handler.callbacks = {
            "pause": function(evt) {
                t.fail("(pixelTolerance met) pause callback shouldn't be called");
            },
            "move": function(evt) {
                t.fail("(pixelTolerance met) move callback shoudln't be called");
            }
        };
        map.events.triggerEvent("mousemove", testEvt);
        handler.clearTimer();
        handler.px = new OpenLayers.Pixel(0, 0);
        testEvt = {
            xy: new OpenLayers.Pixel(3, 3)
        };
        // mouse moves 3x3 pixels, callbacks should be called
        handler.callbacks =  {
            "pause": function(evt) {
                t.ok(evt.xy == testEvt.xy, "(pixelTolerance unmet) pause callback called");
            },
            "move": function(evt) {
                t.ok(evt == testEvt, "(pixelTolerance unmet) move callback called");
            }
        };
        map.events.triggerEvent("mousemove", testEvt);
        handler.clearTimer();

        window.setTimeout = sto;
        window.clearTimeout = cto;
   }
  </script>
</head>
<body>
    <div id="map" style="width: 300px; height: 150px;"/>
</body>
</html>