summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Handler.html
blob: eb266d7307843d2224e5547c0587881886984cad (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<html>
<head>
  <script src="OLLoader.js"></script>
  <script type="text/javascript">
    function test_Handler_constructor(t) {
        t.plan(4);
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
    
        var callbacks = {foo: "bar"};
        var options = {bar: "foo"};
        var handler = new OpenLayers.Handler(control, callbacks, options);
        t.ok(handler instanceof OpenLayers.Handler,
             "new OpenLayers.Handler returns object");
        t.eq(handler.map.id, map.id,
             "constructing a handler with a map sets the map on the handler");
        t.eq(handler.callbacks.foo, callbacks.foo,
             "constructor correctly sets callbacks");
        t.eq(handler.bar, options.bar,
             "constructor correctly extends handler with options");
    }
    
    function test_Handler_activate(t) {
        t.plan(52);
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
    
        var events = ["mouseover", "mouseout", "mousedown",
                      "mouseup", "mousemove", "click",
                      "dblclick", "resize", "focus", "blur"];

        var handler = new OpenLayers.Handler(control);
        handler.active = true;
        var activated = handler.activate();
        t.ok(!activated,
             "activate returns false if the handler is already active");

        handler.active = false;
        map.events.registerPriority = function(type, obj, func) {
            var r = func();
            if(typeof r == "string") {
                // this is one of the mock handler methods
                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(r, type,
                     "activate calls registerPriority with the correct method");
                t.eq(obj["CLASS_NAME"], "OpenLayers.Handler",
                     "activate calls registerPriority with the handler");
            } else {
                // this is the call with handler.setEvent as the func
                t.ok(r, "activate calls registerPriority with handler.setEvent");
            }
        }
        
        // set browser event like properties on the handler
        for(var i=0; i<events.length; ++i) {
            setMethod(events[i]);
        }
        function setMethod(key) {
            handler[key] = function() {return key};
        }
        activated = handler.activate();
        t.ok(activated,
             "activated returns true if the handler is not already active");
        
    }
    
    function test_Handler_deactivate(t) {
        t.plan(52);
        var map = new OpenLayers.Map('map', { controls: []});
        // No controls so that we don't get more thingies than we expect
        // when we actually clean up after ourselves: r5891 caused this
        // because we actually destroy things now on the navigation control. 
        var control = new OpenLayers.Control();
        map.addControl(control);
    
        var events = ["mouseover", "mouseout", "mousedown",
                      "mouseup", "mousemove", "click",
                      "dblclick", "resize", "focus", "blur"];

        var handler = new OpenLayers.Handler(control);
        handler.active = false;
        var deactivated = handler.deactivate();
        t.ok(!deactivated,
             "deactivate returns false if the handler is already deactive");

        handler.activate();
        map.events.unregister = function(type, obj, func) {
            var r = func();
            if(typeof r == "string") {
                // this is one of the mock handler methods
                t.ok(OpenLayers.Util.indexOf(events, type) > -1,
                     "deactivate calls unregister with browser event: " + type);
                t.eq(typeof func, "function",
                     "activate calls unregister with a function");
                t.eq(func(), type,
                     "activate calls unregister with the correct method");
                t.eq(obj["CLASS_NAME"], "OpenLayers.Handler",
                     "activate calls unregister with the handler");
            } else {
                // this is the call with handler.setEvent as the func
                t.ok(r, "activate calls registerPriority with handler.setEvent");
            }
        }
        
        // set browser event like properties on the handler
        for(var i=0; i<events.length; ++i) {
            // add in a closure for key
            (function(key) {
                handler[key] = function() {return key};
            })(events[i]);
        }
        deactivated = handler.deactivate();
        t.ok(deactivated,
             "deactivated returns true if the handler is already active");
        map.events.unregister = OpenLayers.Events.prototype.unregister;
        map.destroy();
        
    }
    
    function test_Handler_setEvent(t) {
        t.plan(5);
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler(control);
        handler.click = function(evt) {
        }
        handler.activate();
        var testEvent = {
            xy: new OpenLayers.Pixel(Math.random(), Math.random()),
            altKey: (Math.random() > 0.5),
            shiftKey: (Math.random() > 0.5),
            ctrlKey: (Math.random() > 0.5),
            metaKey: (Math.random() > 0.5)
        }
        map.events.triggerEvent("click", testEvent);
        t.ok(handler.evt.xy.x == testEvent.xy.x &&
             handler.evt.xy.y == testEvent.xy.y,
             "handler.evt has proper xy object");
        t.eq(handler.evt.altKey, testEvent.altKey,
             "handler.evt.altKey correct");
        t.eq(handler.evt.shiftKey, testEvent.shiftKey,
             "handler.evt.shiftKey correct");
        t.eq(handler.evt.ctrlKey, testEvent.ctrlKey,
             "handler.evt.ctrlKey correct");
        t.eq(handler.evt.metaKey, testEvent.metaKey,
             "handler.evt.metaKey correct");
    }
    
    function test_Handler_destroy(t) {
        t.plan(5);
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler(control);
        var deactivated = false;
        handler.deactivate = function() {
            deactivated = true;
        };
        t.ok(handler.control,
             "handler has a control prior to destroy");
        t.ok(handler.map,
             "handler has a map prior to destroy");
        handler.destroy();
        t.eq(handler.control, null,
             "hanlder.control is null after destroy");
        t.eq(handler.map, null,
             "handler.map is null after destroy");
        t.ok(deactivated,
             "handler.deactivate is called by destroy");
    }
    
    function test_Handler_checkModifiers(t) {
        t.plan(62);
        var handler = new OpenLayers.Handler({});
        handler.keyMask = null;
        var proceed = handler.checkModifiers({});
        t.ok(proceed,
             "checkModifiers returns true if no keyMask on the handler");
        
        
        /**
         * Test checkModifiers for single keyMask values.  The method should
         *     return true if the corresponding key is associated with the
         *     event.  For example, if evt.shiftKey is true and handler.keyMask
         *     is OpenLayers.Handler.MOD_SHIFT, checkModifiers should return
         *     true.
         */
        var constants = {
            MOD_NONE: null,
            MOD_SHIFT: "shiftKey",
            MOD_CTRL: "ctrlKey",
            MOD_ALT: "altKey",
            MOD_META: "metaKey"
        }
        var proceed, evt, value, c, k;
        for(c in constants) {
            handler.keyMask = OpenLayers.Handler[c];
            // for this key mask, test all single key possibilities
            for(k in constants) {
                value = constants[k];
                evt = {};
                if(value) {
                    // mimic a key down on an event
                    evt[value] = true;
                }
                proceed = handler.checkModifiers(evt);
                // if k == c, proceed should be true - false otherwise
                t.eq(k == c, proceed,
                     "returns " + proceed + " if keyMask is " + c +
                     " and " + ((value) ? value : "no key") + " is down");
            }
        }

        /**
         * Test checkModifiers for double keyMask values.  The method should
         *     return true if the corresponding key combo is associated with the
         *     event.  For example, if evt.shiftKey is true and handler.keyMask
         *     is OpenLayers.Handler.MOD_SHIFT, checkModifiers should return
         *     true.
         */
        var constants = ["MOD_SHIFT", "MOD_CTRL", "MOD_ALT", "MOD_META"];
        var keys = ["shiftKey", "ctrlKey", "altKey", "metaKey"];
        var proceed, evt, c1, c2, k1, k2;
        for(var i=0; i<constants.length-1; ++i) {
            c1 = constants[i];
            for(var j=i+1; j<constants.length; ++j) {
                c2 = constants[j];
                handler.keyMask = OpenLayers.Handler[c1] |
                                  OpenLayers.Handler[c2];
                // for this key mask, test all double key possibilities
                for(var x=0; x<keys.length-1; ++x) {
                    k1 = keys[x];
                    for(var y=x+1; y<keys.length; ++y) {
                        k2 = keys[y];
                        evt = {};
                        evt[k1] = true;
                        evt[k2] = true;
                        proceed = handler.checkModifiers(evt);
                        // if the combo matches, proceed should be true
                        // at this point we know that i != j and x != y
                        t.eq(((i == x) || (i == y)) &&
                             ((j == x) || (j == y)),
                             proceed,
                             "returns " + proceed + " if " + c1 + " | " + c2 +
                             " and " + k1 + " + " + k2 + " is down");
                    }
                }
            }
        }
            
    }


  </script>
</head>
<body>
    <div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>