3 <script src="../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_Handler_constructor(t) {
7 var map = new OpenLayers.Map('map');
8 var control = new OpenLayers.Control();
9 map.addControl(control);
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");
24 function test_Handler_activate(t) {
26 var map = new OpenLayers.Map('map');
27 var control = new OpenLayers.Control();
28 map.addControl(control);
30 var events = ["mouseover", "mouseout", "mousedown",
31 "mouseup", "mousemove", "click",
32 "dblclick", "resize", "focus", "blur"];
34 var handler = new OpenLayers.Handler(control);
35 handler.active = true;
36 var activated = handler.activate();
38 "activate returns false if the handler is already active");
40 handler.active = false;
41 map.events.registerPriority = function(type, obj, 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");
50 "activate calls registerPriority with the correct method");
51 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler",
52 "activate calls registerPriority with the handler");
54 // this is the call with handler.setEvent as the func
55 t.ok(r, "activate calls registerPriority with handler.setEvent");
59 // set browser event like properties on the handler
60 for(var i=0; i<events.length; ++i) {
63 function setMethod(key) {
64 handler[key] = function() {return key};
66 activated = handler.activate();
68 "activated returns true if the handler is not already active");
72 function test_Handler_deactivate(t) {
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);
81 var events = ["mouseover", "mouseout", "mousedown",
82 "mouseup", "mousemove", "click",
83 "dblclick", "resize", "focus", "blur"];
85 var handler = new OpenLayers.Handler(control);
86 handler.active = false;
87 var deactivated = handler.deactivate();
89 "deactivate returns false if the handler is already deactive");
92 map.events.unregister = function(type, obj, 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");
101 "activate calls unregister with the correct method");
102 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler",
103 "activate calls unregister with the handler");
105 // this is the call with handler.setEvent as the func
106 t.ok(r, "activate calls registerPriority with handler.setEvent");
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
114 handler[key] = function() {return key};
117 deactivated = handler.deactivate();
119 "deactivated returns true if the handler is already active");
120 map.events.unregister = OpenLayers.Events.prototype.unregister;
125 function test_Handler_setEvent(t) {
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) {
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)
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");
152 function test_Handler_destroy(t) {
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() {
162 t.ok(handler.control,
163 "handler has a control prior to destroy");
165 "handler has a map prior to 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");
172 "handler.deactivate is called by destroy");
175 function test_Handler_checkModifiers(t) {
177 var handler = new OpenLayers.Handler({});
178 handler.keyMask = null;
179 var proceed = handler.checkModifiers({});
181 "checkModifiers returns true if no keyMask on the handler");
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
193 MOD_SHIFT: "shiftKey",
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];
205 // mimic a key down on an event
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");
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
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) {
228 for(var j=i+1; j<constants.length; ++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) {
235 for(var y=x+1; y<keys.length; ++y) {
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)),
246 "returns " + proceed + " if " + c1 + " | " + c2 +
247 " and " + k1 + " + " + k2 + " is down");
259 <div id="map" style="width: 1024px; height: 512px;"/>