]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Handler.html
initial commit
[syp.git] / openlayers / tests / Handler.html
1 <html>
2 <head>
3   <script src="../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5     function test_Handler_constructor(t) {
6         t.plan(4);
7         var map = new OpenLayers.Map('map');
8         var control = new OpenLayers.Control();
9         map.addControl(control);
10     
11         var callbacks = {foo: "bar"};
12         var options = {bar: "foo"};
13         var handler = new OpenLayers.Handler(control, callbacks, options);
14         t.ok(handler instanceof OpenLayers.Handler,
15              "new OpenLayers.Handler returns object");
16         t.eq(handler.map.id, map.id,
17              "constructing a handler with a map sets the map on the handler");
18         t.eq(handler.callbacks.foo, callbacks.foo,
19              "constructor correctly sets callbacks");
20         t.eq(handler.bar, options.bar,
21              "constructor correctly extends handler with options");
22     }
23     
24     function test_Handler_activate(t) {
25         t.plan(52);
26         var map = new OpenLayers.Map('map');
27         var control = new OpenLayers.Control();
28         map.addControl(control);
29     
30         var events = ["mouseover", "mouseout", "mousedown",
31                       "mouseup", "mousemove", "click",
32                       "dblclick", "resize", "focus", "blur"];
33
34         var handler = new OpenLayers.Handler(control);
35         handler.active = true;
36         var activated = handler.activate();
37         t.ok(!activated,
38              "activate returns false if the handler is already active");
39
40         handler.active = false;
41         map.events.registerPriority = function(type, obj, func) {
42             var r = func();
43             if(typeof r == "string") {
44                 // this is one of the mock handler methods
45                 t.ok(OpenLayers.Util.indexOf(events, type) > -1,
46                      "activate calls registerPriority with browser event: " + type);
47                 t.eq(typeof func, "function",
48                      "activate calls registerPriority with a function");
49                 t.eq(r, type,
50                      "activate calls registerPriority with the correct method");
51                 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler",
52                      "activate calls registerPriority with the handler");
53             } else {
54                 // this is the call with handler.setEvent as the func
55                 t.ok(r, "activate calls registerPriority with handler.setEvent");
56             }
57         }
58         
59         // set browser event like properties on the handler
60         for(var i=0; i<events.length; ++i) {
61             setMethod(events[i]);
62         }
63         function setMethod(key) {
64             handler[key] = function() {return key};
65         }
66         activated = handler.activate();
67         t.ok(activated,
68              "activated returns true if the handler is not already active");
69         
70     }
71     
72     function test_Handler_deactivate(t) {
73         t.plan(52);
74         var map = new OpenLayers.Map('map', { controls: []});
75         // No controls so that we don't get more thingies than we expect
76         // when we actually clean up after ourselves: r5891 caused this
77         // because we actually destroy things now on the navigation control. 
78         var control = new OpenLayers.Control();
79         map.addControl(control);
80     
81         var events = ["mouseover", "mouseout", "mousedown",
82                       "mouseup", "mousemove", "click",
83                       "dblclick", "resize", "focus", "blur"];
84
85         var handler = new OpenLayers.Handler(control);
86         handler.active = false;
87         var deactivated = handler.deactivate();
88         t.ok(!deactivated,
89              "deactivate returns false if the handler is already deactive");
90
91         handler.activate();
92         map.events.unregister = function(type, obj, func) {
93             var r = func();
94             if(typeof r == "string") {
95                 // this is one of the mock handler methods
96                 t.ok(OpenLayers.Util.indexOf(events, type) > -1,
97                      "deactivate calls unregister with browser event: " + type);
98                 t.eq(typeof func, "function",
99                      "activate calls unregister with a function");
100                 t.eq(func(), type,
101                      "activate calls unregister with the correct method");
102                 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler",
103                      "activate calls unregister with the handler");
104             } else {
105                 // this is the call with handler.setEvent as the func
106                 t.ok(r, "activate calls registerPriority with handler.setEvent");
107             }
108         }
109         
110         // set browser event like properties on the handler
111         for(var i=0; i<events.length; ++i) {
112             // add in a closure for key
113             (function(key) {
114                 handler[key] = function() {return key};
115             })(events[i]);
116         }
117         deactivated = handler.deactivate();
118         t.ok(deactivated,
119              "deactivated returns true if the handler is already active");
120         map.events.unregister = OpenLayers.Events.prototype.unregister;
121         map.destroy();
122         
123     }
124     
125     function test_Handler_setEvent(t) {
126         t.plan(4);
127         var map = new OpenLayers.Map('map');
128         var control = new OpenLayers.Control();
129         map.addControl(control);
130         var handler = new OpenLayers.Handler(control);
131         handler.click = function(evt) {
132         }
133         handler.activate();
134         var testEvent = {
135             xy: new OpenLayers.Pixel(Math.random(), Math.random()),
136             altKey: (Math.random() > 0.5),
137             shiftKey: (Math.random() > 0.5),
138             ctrlKey: (Math.random() > 0.5)
139         }
140         map.events.triggerEvent("click", testEvent);
141         t.ok(handler.evt.xy.x == testEvent.xy.x &&
142              handler.evt.xy.y == testEvent.xy.y,
143              "handler.evt has proper xy object");
144         t.eq(handler.evt.altKey, testEvent.altKey,
145              "handler.evt.altKey correct");
146         t.eq(handler.evt.shiftKey, testEvent.shiftKey,
147              "handler.evt.shiftKey correct");
148         t.eq(handler.evt.ctrlKey, testEvent.ctrlKey,
149              "handler.evt.ctrlKey correct");
150     }
151     
152     function test_Handler_destroy(t) {
153         t.plan(5);
154         var map = new OpenLayers.Map('map');
155         var control = new OpenLayers.Control();
156         map.addControl(control);
157         var handler = new OpenLayers.Handler(control);
158         var deactivated = false;
159         handler.deactivate = function() {
160             deactivated = true;
161         };
162         t.ok(handler.control,
163              "handler has a control prior to destroy");
164         t.ok(handler.map,
165              "handler has a map prior to destroy");
166         handler.destroy();
167         t.eq(handler.control, null,
168              "hanlder.control is null after destroy");
169         t.eq(handler.map, null,
170              "handler.map is null after destroy");
171         t.ok(deactivated,
172              "handler.deactivate is called by destroy");
173     }
174     
175     function test_Handler_checkModifiers(t) {
176         t.plan(26);
177         var handler = new OpenLayers.Handler({});
178         handler.keyMask = null;
179         var proceed = handler.checkModifiers({});
180         t.ok(proceed,
181              "checkModifiers returns true if no keyMask on the handler");
182         
183         
184         /**
185          * Test checkModifiers for single keyMask values.  The method should
186          *     return true if the corresponding key is associated with the
187          *     event.  For example, if evt.shiftKey is true and handler.keyMask
188          *     is OpenLayers.Handler.MOD_SHIFT, checkModifiers should return
189          *     true.
190          */
191         var constants = {
192             MOD_NONE: null,
193             MOD_SHIFT: "shiftKey",
194             MOD_CTRL: "ctrlKey",
195             MOD_ALT: "altKey"
196         }
197         var proceed, evt, value, c, k;
198         for(c in constants) {
199             handler.keyMask = OpenLayers.Handler[c];
200             // for this key mask, test all single key possibilities
201             for(k in constants) {
202                 value = constants[k];
203                 evt = {};
204                 if(value) {
205                     // mimic a key down on an event
206                     evt[value] = true;
207                 }
208                 proceed = handler.checkModifiers(evt);
209                 // if k == c, proceed should be true - false otherwise
210                 t.eq(k == c, proceed,
211                      "returns " + proceed + " if keyMask is " + c +
212                      " and " + ((value) ? value : "no key") + " is down");
213             }
214         }
215
216         /**
217          * Test checkModifiers for double keyMask values.  The method should
218          *     return true if the corresponding key combo is associated with the
219          *     event.  For example, if evt.shiftKey is true and handler.keyMask
220          *     is OpenLayers.Handler.MOD_SHIFT, checkModifiers should return
221          *     true.
222          */
223         var constants = ["MOD_SHIFT", "MOD_CTRL", "MOD_ALT"];
224         var keys = ["shiftKey", "ctrlKey", "altKey"];
225         var proceed, evt, c1, c2, k1, k2;
226         for(var i=0; i<constants.length-1; ++i) {
227             c1 = constants[i];
228             for(var j=i+1; j<constants.length; ++j) {
229                 c2 = constants[j];
230                 handler.keyMask = OpenLayers.Handler[c1] |
231                                   OpenLayers.Handler[c2];
232                 // for this key mask, test all double key possibilities
233                 for(var x=0; x<keys.length-1; ++x) {
234                     k1 = keys[x];
235                     for(var y=x+1; y<keys.length; ++y) {
236                         k2 = keys[y];
237                         evt = {};
238                         evt[k1] = true;
239                         evt[k2] = true;
240                         proceed = handler.checkModifiers(evt);
241                         // if the combo matches, proceed should be true
242                         // at this point we know that i != j and x != y
243                         t.eq(((i == x) || (i == y)) &&
244                              ((j == x) || (j == y)),
245                              proceed,
246                              "returns " + proceed + " if " + c1 + " | " + c2 +
247                              " and " + k1 + " + " + k2 + " is down");
248                     }
249                 }
250             }
251         }
252             
253     }
254
255
256   </script>
257 </head>
258 <body>
259     <div id="map" style="width: 1024px; height: 512px;"/>
260 </body>
261 </html>