3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_Handler_Click_constructor(t) {
7 var control = new OpenLayers.Control();
8 control.id = Math.random();
9 var callbacks = {foo: "bar"};
10 var options = {bar: "foo"};
12 var oldInit = OpenLayers.Handler.prototype.initialize;
14 OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
15 t.eq(con.id, control.id,
16 "constructor calls parent with the correct control");
18 "constructor calls parent with the correct callbacks");
20 "constructor calls parent with the correct options");
22 var handler = new OpenLayers.Handler.Click(control, callbacks, options);
24 OpenLayers.Handler.prototype.initialize = oldInit;
27 function test_Handler_Click_activate(t) {
30 map: new OpenLayers.Map('map')
32 var handler = new OpenLayers.Handler.Click(control);
33 handler.active = true;
34 var activated = handler.activate();
36 "activate returns false if the handler was already active");
37 handler.active = false;
38 handler.dragging = true;
39 activated = handler.activate();
41 "activate returns true if the handler was not already active");
45 function test_Handler_Click_events(t) {
48 var map = new OpenLayers.Map('map');
52 map.events.registerPriority = function(type, obj, func) {
54 if(typeof r == "string") {
55 // this is one of the mock handler methods
56 t.eq(OpenLayers.Util.indexOf(nonevents, type), -1,
57 "registered method is not one of the events " +
58 "that should not be handled");
59 t.ok(OpenLayers.Util.indexOf(events, type) > -1,
60 "activate calls registerPriority with browser event: " + type);
61 t.eq(typeof func, "function",
62 "activate calls registerPriority with a function");
64 "activate calls registerPriority with the correct method");
65 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Click",
66 "activate calls registerPriority with the handler");
69 function setMethod(key) {
70 handler[key] = function() {return key};
73 // list below events that should be handled (events) and those
74 // that should not be handled (nonevents) by the handler
75 var events = ["click", "dblclick", "mousedown", "mouseup", "rightclick"];
76 var nonevents = ["mousemove", "resize", "focus", "blur"];
77 var handler = new OpenLayers.Handler.Click(control);
78 // set browser event like properties on the handler
79 for(var i=0; i<events.length; ++i) {
84 // different listeners registered for pixelTolerance option
85 var events = ["click", "dblclick", "mousedown", "mouseup", "rightclick"];
86 var nonevents = ["mousemove", "resize", "focus", "blur"];
87 var handler = new OpenLayers.Handler.Click(control, {}, {
90 for(var i=0; i<events.length; ++i) {
97 function test_Handler_Click_callbacks(t) {
100 var map = new OpenLayers.Map('map', {controls: []});
106 var handler = new OpenLayers.Handler.Click(control, {});
110 // set up for single click - three tests here
112 var sto = window.setTimeout;
113 window.setTimeout = function(func, delay) {
114 var key = Math.random();
116 t.ok(typeof func == "function",
117 "setTimeout called with a function");
118 t.eq(delay, handler.delay,
119 "setTimeout called with proper delay");
120 // execute function that is supposed to be delayed
124 var cto = window.clearTimeout;
125 window.clearTimeout = function(key) {
126 if(timers[key] === true) {
129 t.fail("clearTimeout called with non-existent timerId");
132 var testEvt = {id: Math.random()};
133 handler.callbacks = {
134 "click": function(evt) {
135 t.eq(evt.id, testEvt.id,
136 "(click w/ single true) click callback called with correct evt");
138 "dblclick": function(evt) {
139 t.fail("(click w/ single true) dblclick should not be called here");
142 map.events.triggerEvent("click", testEvt);
144 // set up for double click with double false - no tests here (only failures)
145 handler.callbacks = {
146 "click": function(evt) {
147 t.fail("(dblclick w/ double false) click should not be called here");
149 "dblclick": function(evt) {
150 t.fail("(dblclick w/ double false) dblclick should not be called here");
153 testEvt = Math.random();
154 map.events.triggerEvent("dblclick", testEvt);
156 // set up for double click with double true - one test here
157 handler.double = true;
158 handler.callbacks = {
159 "click": function(evt) {
160 t.fail("(dblclick w/ double true) click should not be called here");
162 "dblclick": function(evt) {
164 "(dblclick w/ double true) dblclick called with correct evt");
167 testEvt = Math.random();
168 map.events.triggerEvent("dblclick", testEvt);
170 // set up for two clicks with double true - 6 tests here (with timeout ones from above)
171 handler.double = true;
172 handler.callbacks = {
173 "click": function(evt) {
174 t.ok(evt != null, "(two clicks w/ double true) click will not be called here if next three tests pass");
176 "dblclick": function(evt) {
178 "(two clicks w/ double true) dblclick called with correct evt");
181 testEvt = Math.random();
182 map.events.triggerEvent("click", testEvt);
183 t.ok(handler.timerId != null,
184 "(two clicks w/ double true) timer is set to call click");
185 map.events.triggerEvent("click", testEvt);
186 t.ok(handler.timerId == null,
187 "(two clicks w/ double true) timer is cleared to call click");
188 map.events.triggerEvent("dblclick", testEvt);
191 // set up to tests pixelTolerance - three tests here (2 from setTimeout above)
192 handler = new OpenLayers.Handler.Click(control, {}, {
197 xy: new OpenLayers.Pixel(0, 0)
199 map.events.triggerEvent("mousedown", downEvt);
201 xy: new OpenLayers.Pixel(0, 1)
203 // mouse moves one pixel, click should be called
204 handler.callbacks = {
205 "click": function(evt) {
206 t.ok(evt.xy == clickEvt.xy, "(pixelTolerance met) click called");
209 map.events.triggerEvent("click", clickEvt);
210 handler.clearTimer();
212 // mouse moves 3x3 pixels, click should not be called
213 map.events.triggerEvent("mousedown", downEvt);
215 xy: new OpenLayers.Pixel(3, 3)
217 // mouse moves one pixel, click should be called
218 handler.callbacks = {
219 "click": function(evt) {
220 t.fail("(pixelTolerance not met) click should not be called");
223 map.events.triggerEvent("click", clickEvt); // no test run
224 handler.clearTimer();
226 window.setTimeout = sto;
227 window.clearTimeout = cto;
232 function test_Handler_Click_deactivate(t) {
235 map: new OpenLayers.Map('map')
237 var handler = new OpenLayers.Handler.Click(control);
238 handler.active = false;
239 var deactivated = handler.deactivate();
241 "deactivate returns false if the handler was not already active");
242 handler.active = true;
244 handler.timerId = true;
245 deactivated = handler.deactivate();
247 "deactivate returns true if the handler was active already");
248 t.eq(handler.down, null,
249 "deactivate sets down to null");
250 t.eq(handler.timerId, null,
251 "deactivate sets timerId to null");
254 function test_Handler_Click_mouseup(t) {
259 //no modifiers, no handlerightclicks, no isrightclick
260 var temp = OpenLayers.Event.isRightClick;
261 OpenLayers.Event.isRightClick = function(e) {
262 t.ok(e == g_evt, 'correct event passed in to checkModifiers');
267 'checkModifiers': function(e) {
268 t.ok(e == g_evt, 'correct event passed in to checkModifiers');
272 'handleRightClicks': false
274 'rightclick': function(e) {
275 t.ok(e == g_evt, 'correct event passed in to checkModifiers');
279 var propagate = OpenLayers.Handler.Click.prototype.mouseup.apply(h, [g_evt]);
280 t.ok(propagate, "default propagate is true when no modifiers, no handlerightclicks, no isrightclick")
282 //modifiers, handlerightclicks, and isrightclick
283 h.checkModifiers = function() { return true; };
284 h.control.handleRightClicks = true;
285 OpenLayers.Event.isRightClick = function(e) { return true; };
286 propagate = OpenLayers.Handler.Click.prototype.mouseup.apply(h, [g_evt]);
288 t.ok(propagate == g_Propagate, "return from handler's rightClick() returned from mouseup");
290 OpenLayers.Event.isRightClick = temp;
297 <div id="map" style="width: 300px; height: 150px;"/>