3 <script src="../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
9 function test_Events_constructor (t) {
10 var mapDiv = OpenLayers.Util.getElement('map');
11 var obj = {result: 0};
12 var eventTypes = ["doThingA", "doThingB", "doThingC"];
13 var browserTypes = OpenLayers.Events.prototype.BROWSER_EVENTS;
15 var totalTests = 4 + 2*eventTypes.length + 2*browserTypes.length;
19 events = new OpenLayers.Events(obj, mapDiv, eventTypes);
21 t.ok( events instanceof OpenLayers.Events, "new OpenLayers.Control returns object" );
23 t.ok(events.object ==obj, " 'object' property correctly set");
24 t.ok(events.element == mapDiv, " 'element' property correctly set");
27 for (var i = 0; i < browserTypes.length; i++) {
28 var type = browserTypes[i];
29 t.ok( events.listeners[type] instanceof Array, "browser event: " + type + " correctly registered" );
30 t.ok( OpenLayers.Util.indexOf(events.eventTypes, type) != -1, "browser type " + type + "' correctly added to eventTypes array");
32 for (var i = 0; i < eventTypes.length; i++) {
33 var type = eventTypes[i];
34 t.ok( events.listeners[type] instanceof Array, "custom event: " + type + " correctly registered");
35 t.ok( OpenLayers.Util.indexOf(events.eventTypes, type) != -1, "eventType " + type + "' correctly added to eventTypes array");
39 events = new OpenLayers.Events(null, null, null);
40 t.ok( events.listeners != null,
41 "init of Events with null object/element/eventTypes still creates listeners array" );
44 function test_Events_register(t){
51 'push': function(options){
53 gFuncA = options.func;
57 'push': function(options){
59 gFuncB = options.func;
63 'eventTypes': ['listenerA', 'listenerB']
71 gObjA = null; gFuncA = null; gObjB = null; gFuncB = null;
72 OpenLayers.Events.prototype.register.apply(ev, [type, object, func]);
73 t.ok((gObjA == null) && (gFuncA == null) &&
74 (gObjB == null) && (gFuncB == null), "no push called func null");
76 //valid func, type not in ev.eventTypes
78 gObjA = null; gFuncA = null; gObjB = null; gFuncB = null;
79 OpenLayers.Events.prototype.register.apply(ev, [type, object, func]);
80 t.ok((gObjA == null) && (gFuncA == null) &&
81 (gObjB == null) && (gFuncB == null), "no push called func null");
83 //valid func, type in ev.eventTypes, null obj
85 gObjA = null; gFuncA = null; gObjB = null; gFuncB = null;
86 OpenLayers.Events.prototype.register.apply(ev, [type, object, func]);
87 t.ok((gObjA == ev.object) && (gFuncA == func) &&
88 (gObjB == null) && (gFuncB == null), "push called on listenerA's mock array when type passed in 'listenerA'. events.object taken since obj is null.");
90 //valid func, type in ev.eventTypes, valid obj
93 gObjA = null; gFuncA = null; gObjB = null; gFuncB = null;
94 OpenLayers.Events.prototype.register.apply(ev, [type, object, func]);
95 t.ok((gObjA == null) && (gFuncA == null) &&
96 (gObjB == object) && (gFuncB == func), "push called on listenerB's mock array when type passed in 'listenerB'.");
100 function test_Events_register_unregister(t) {
104 var mapDiv = OpenLayers.Util.getElement('map');
105 var obj = {result: 0};
106 var eventTypes = ["doThingA", "doThingB", "doThingC"];
108 events = new OpenLayers.Events(obj, mapDiv, eventTypes);
110 var func = function () { this.result++ }
111 events.register( "doThingA", obj, func );
113 var listenerList = events.listeners["doThingA"];
114 t.eq( listenerList.length, 1, "register correctly adds to event.listeners" );
115 t.ok( listenerList[0].obj == obj, "obj property correctly registered");
116 t.ok( listenerList[0].func == func, "func property correctly registered");
118 var func2 = function () { this.result-- }
119 events.register( "doThingA", obj, func2 );
121 var listenerList = events.listeners["doThingA"];
122 t.eq( listenerList.length, 2, "register correctly appends new callback to event.listeners[doThingA]" );
123 t.ok( listenerList[1].obj == obj, "obj property correctly registered");
124 t.ok( listenerList[1].func == func2, "func property correctly registered");
126 var func3 = function () { this.result = this.result + 3; }
127 events.register( "doThingA", null, func3 );
129 var listenerList = events.listeners["doThingA"];
130 t.eq( listenerList.length, 3, "register correctly appends new callback to event.listeners[doThingA] even when obj passed in is null" );
131 t.ok( listenerList[2].obj == obj, "obj is correctly set to Events.object default when null is passed in.");
132 t.ok( listenerList[2].func == func3, "func property correctly registered");
134 events.register( "doThingA", obj, null);
136 var listenerList = events.listeners["doThingA"];
137 t.eq( listenerList.length, 3, "register correctly does not append null callback to event.listeners[doThingA] even when obj passed in is null" );
139 events.register( "chicken", obj, func);
140 t.ok( events.listeners["chicken"] == null, "register() does nothing on attempt to register un-enabled event");
142 events.unregister("doThingA", obj, null);
143 var listenerList = events.listeners["doThingA"];
144 t.eq( listenerList.length, 3, "trying to unregister a null callback does nothing -- but doesnt crash :-)" );
146 events.unregister("doThingA", obj, func);
148 for (var i = 0; i < listenerList.length; i++) {
149 var listener = listenerList[i];
150 if (listener.obj == obj && listener.func == func) {
154 t.ok( (listenerList.length == 2) && !found, "unregister correctly removes callback" );
156 events.unregister("doThingA", null, func3);
158 for (var i = 0; i < listenerList.length; i++) {
159 var listener = listenerList[i];
160 if (listener.obj == obj && listener.func == func) {
164 t.ok( (listenerList.length == 1) && !found, "unregister correctly removes callback when no obj specified" );
166 var func4 = function () { this.result = "chicken"; }
167 events.unregister("doThingA", obj, func4);
168 t.ok( (listenerList.length == 1), "unregister does not bomb if you try to remove an unregistered callback" );
170 var obj2 = { chicken: 151 };
171 events.unregister("doThingA", obj2, func2);
172 t.ok( (listenerList.length == 1), "unregister does not bomb or accidntally remove if you try to remove a valid callback on a valid event type, but with the wrong context object" );
174 events.unregister("doThingA", obj, null);
175 t.ok( (listenerList.length == 1), "unregister does not bomb if you try to remove a null callback" );
177 events.unregister("chicken", null, func3);
178 t.ok( events.listeners["chicken"] == null, "unregistering an event that is not enabled does not wierdly enable it -- or cause a script error")
183 function test_Events_remove(t) {
187 var mapDiv = OpenLayers.Util.getElement('map');
188 var obj = {result: 0};
189 var eventTypes = ["doThingA", "doThingB", "doThingC"];
191 events = new OpenLayers.Events(obj, mapDiv, eventTypes);
193 var func = function () { this.result++ }
194 var func2 = function () { this.result-- }
195 var func3 = function () { this.result = this.result + 3; }
197 events.register( "doThingA", obj, func );
198 events.register( "doThingA", obj, func2 );
199 events.register( "doThingA", null, func3 );
201 events.remove("doThingA");
203 t.eq( events.listeners["doThingA"].length, 0, "remove() correctly clears the event listeners" );
205 events.remove("chicken");
206 t.ok( events.listeners["chicken"] == null, "remove on non-enabled event does not break or accidentally enable the event");
210 function test_Events_triggerEvent(t) {
214 var mapDiv = OpenLayers.Util.getElement('map');
215 var obj = {result: 0};
216 var eventTypes = ["doThingA", "doThingB", "doThingC"];
218 events = new OpenLayers.Events(obj, mapDiv, eventTypes);
221 var func = function () { this.result++ }
222 events.register( "doThingA", obj, func );
224 events.triggerEvent("doThingA", {});
225 t.eq( obj.result, 1, "result is 1 after we call triggerEvent" );
226 events.triggerEvent("doThingA");
227 t.eq( obj.result, 2, "result is 2 after we call triggerEvent with no event" );
229 var funcB = function() { this.FUNK = "B"; return false; }
230 events.register( "doThingA", obj, funcB);
232 events.triggerEvent("doThingA");
233 t.ok ((obj.result == 3) && (obj.FUNK == "B"), "executing multiple callbacks works")
235 var funcZ = function() { this.FUNK = "Z"; }
236 events.register( "doThingA", obj, funcZ);
238 events.triggerEvent("doThingA");
239 t.ok ((obj.result == 4) && (obj.FUNK == "B"), "executing multiple callbacks works, and when one returns false, it stops chain correctly")
242 var func2 = function() { this.result = this.result + 10; }
243 events.register( "doThingB", null, func2);
245 events.triggerEvent("doThingB");
246 t.eq( obj.result, 14, "passing a null object default gets set correctly");
248 //no specific t.ok for this one, but if it breaks, you will know it.
249 events.triggerEvent("chicken");
251 events = new OpenLayers.Events(null, mapDiv, eventTypes);
253 // a is global variable (context-irrelevant)
255 var func = function () { a = 5; }
256 events.register( "doThingC", null, func );
257 events.triggerEvent("doThingC");
259 t.eq(a, 5, "if Events has no object set and an event is registered also with no object, triggerEvent() calls it without trying to set the context to null");
261 // trigger events with additional arguments
262 eventTypes = ["something"];
263 events = new OpenLayers.Events(null, null, eventTypes);
264 var instance = {id: Math.random()};
265 var listener = function(obj) {
266 t.eq(this.id, instance.id,
267 "listener called with proper scope");
268 t.eq(arguments.length, 1,
269 "listener called with a single argument");
270 t.eq(typeof arguments, "object",
271 "listener called with an object");
272 t.eq(obj.foo, evt.foo,
273 "foo property set on the layer");
275 events.register("something", instance, listener);
280 events.triggerEvent("something", evt);
281 events.unregister("something", instance, listener);
283 // test return from triggerEvent
284 var listener1 = function() {
287 var listener2 = function() {
290 var listener3 = function() {
291 t.fail("never call me again!");
293 events.register("something", instance, listener1);
294 var ret = events.triggerEvent("something", evt);
295 t.eq(ret, "foo", "correct return from single listener");
297 events.register("something", instance, listener2);
298 ret = events.triggerEvent("something", evt);
299 t.eq(ret, false, "correct return for two listeners");
301 events.register("something", instance, listener3);
302 ret = events.triggerEvent("something", evt);
303 t.eq(ret, false, "correct return for three listeners where second cancels");
305 events.unregister("something", instance, listener1);
306 events.unregister("something", instance, listener2);
307 events.unregister("something", instance, listener3);
311 function test_Events_destroy (t) {
314 var div = OpenLayers.Util.getElement('test');
316 var events = new OpenLayers.Events(obj, div);
318 // +1 because of blocking dragstart in attachToElement()
319 t.eq(OpenLayers.Event.observers[div._eventCacheID].length,
320 OpenLayers.Events.prototype.BROWSER_EVENTS.length + 1,
321 "construction creates new arrayin hash, registers appropriate events");
325 t.eq(OpenLayers.Event.observers["test"], null,
326 "destruction removes the event observer from hash");
329 function test_Event(t) {
332 var div = OpenLayers.Util.getElement('test');
333 var name = "mouseover";
334 var func = function() {};
336 //1st elem 1st listener
337 OpenLayers.Event.observe(div, name, func);
339 var cacheID = div._eventCacheID;
340 t.ok(cacheID, "element given new cache id");
342 var elementObservers = OpenLayers.Event.observers[cacheID];
344 t.ok(elementObservers, "new cache bucket made for event");
345 t.eq(elementObservers.length, 1, "one listener registered");
347 var listener = elementObservers[0];
349 t.ok(listener.element == div, "element registered");
350 t.eq(listener.name, name, "name registered");
351 t.ok(listener.observer == func, "function registered");
352 t.eq(listener.useCapture, false, "useCapture defaults to false");
354 //1st elem 2nd listener
356 var newFunc = function() {};
358 OpenLayers.Event.observe(div, name, newFunc, true);
359 var newCacheID = div._eventCacheID;
360 t.eq(newCacheID, cacheID, "element's cache id not overridden");
362 t.eq(elementObservers.length, 2, "listener added to existing bucket");
364 var listener = elementObservers[1];
366 t.ok(listener.element == div, "element registered");
367 t.eq(listener.name, name, "name registered");
368 t.ok(listener.observer == newFunc, "function registered");
369 t.eq(listener.useCapture, true, "useCapture correctly registered");
371 //2st elem 1st listener
372 div = OpenLayers.Util.getElement('test2');
373 OpenLayers.Event.observe(div, name, func);
375 var cacheID = div._eventCacheID;
376 t.ok(cacheID, "new element given new cache id");
377 t.ok(cacheID != newCacheID, "new cache id is unique");
379 elementObservers = OpenLayers.Event.observers[cacheID];
381 t.ok(elementObservers, "new cache bucket made for event");
382 t.eq(elementObservers.length, 1, "one listener registered");
384 var listener = elementObservers[0];
386 t.ok(listener.element == div, "element registered");
387 t.eq(listener.name, name, "name registered");
388 t.ok(listener.observer == func, "function registered");
389 t.eq(listener.useCapture, false, "useCapture defaults to false");
391 //stopObservingElement by element
392 OpenLayers.Event.stopObservingElement(div);
393 elementObservers = OpenLayers.Event.observers[cacheID];
394 t.ok(elementObservers == null, "stopObservingElement by elem works");
396 //stopObservingElement by id
397 OpenLayers.Event.stopObservingElement("test");
398 elementObservers = OpenLayers.Event.observers[newCacheID];
399 t.ok(elementObservers == null, "stopObservingElement by id works");
402 //unloadCache by element
403 OpenLayers.Event.observe(div, name, func);
405 OpenLayers.Event.unloadCache();
407 elementObservers = OpenLayers.Event.observers[cacheID];
408 t.ok(elementObservers == null, "stopObservingElement by elem works");
413 function test_Events_addEventType(t) {
417 var mapDiv = OpenLayers.Util.getElement('map');
418 var obj = {result: 0};
419 var eventTypes = ["doThingA", "doThingB"];
421 events = new OpenLayers.Events(obj, mapDiv, eventTypes);
424 t.eq( events.listeners["doThingA"].length, 0, "event type passed as passed as param to OpenLayers.Events constructor correctly set up" );
425 t.eq( events.listeners["doThingB"].length, 0, "event type passed as passed as param to OpenLayers.Events constructor correctly set up" );
427 var newEventType = "onFoo";
428 t.ok( events.listeners[newEventType] == null, "event type not yet registered has null entry in listeners array");
429 t.ok( OpenLayers.Util.indexOf(events.eventTypes, newEventType) == -1, "event type not yet registered not in eventTypes array");
431 events.addEventType(newEventType);
432 t.eq( events.listeners[newEventType].length, 0, "event type passed to addEventType correctly set up" );
433 t.ok( OpenLayers.Util.indexOf(events.eventTypes, newEventType) != -1, "event type passed to addEventType in eventTypes array");
435 var func = function () {};
436 events.register( "doThingA", obj, func );
437 t.eq( events.listeners["doThingA"].length, 1, "listener correctly registered" );
439 events.addEventType("doThingsA");
440 t.eq( events.listeners["doThingA"].length, 1, "event type passed to addEventType correctly does nothing if clashes with already defined event type" );
443 /** HACK HACK HACK Deprecated This test should be removed in 3.0 **/
444 function test_Events_globalEventObject(t) {
446 t.ok(Event.stop, "a stop() function exists on the global JS Event object, if indeed it exists. This is legacy deprecated and will be gone in 3.0");
453 <div id="map" style="width: 1024px; height: 512px;"/>
454 <div id="test"></div>
455 <div id="test2"></div>