3
<script src="../../lib/OpenLayers.js"></script>
4
<script type="text/javascript">
5
function test_Handler_Drag_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.Drag(control, callbacks, options);
24
OpenLayers.Handler.prototype.initialize = oldInit;
27
function test_Handler_Drag_activate(t) {
29
var map = new OpenLayers.Map('map');
30
var control = new OpenLayers.Control();
31
map.addControl(control);
32
var handler = new OpenLayers.Handler.Drag(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");
42
t.ok(!handler.dragging,
43
"activate sets dragging to false");
47
function test_Handler_Drag_events(t) {
50
var map = new OpenLayers.Map('map');
51
var control = new OpenLayers.Control();
52
map.addControl(control);
53
var handler = new OpenLayers.Handler.Drag(control);
55
// list below events that should be handled (events) and those
56
// that should not be handled (nonevents) by the handler
57
var events = ["mousedown", "mouseup", "mousemove", "mouseout", "click"];
58
var nonevents = ["dblclick", "resize", "focus", "blur"];
59
map.events.registerPriority = function(type, obj, func) {
61
if(typeof r == "string") {
62
// this is one of the mock handler methods
63
t.eq(OpenLayers.Util.indexOf(nonevents, type), -1,
64
"registered method is not one of the events " +
65
"that should not be handled");
66
t.ok(OpenLayers.Util.indexOf(events, type) > -1,
67
"activate calls registerPriority with browser event: " + type);
68
t.eq(typeof func, "function",
69
"activate calls registerPriority with a function");
71
"activate calls registerPriority with the correct method");
72
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Drag",
73
"activate calls registerPriority with the handler");
77
// set browser event like properties on the handler
78
for(var i=0; i<events.length; ++i) {
81
function setMethod(key) {
82
handler[key] = function() {return key};
85
var activated = handler.activate();
89
function test_Handler_Drag_callbacks(t) {
92
var map = new OpenLayers.Map('map', {controls: []});
94
var control = new OpenLayers.Control();
95
map.addControl(control);
97
// set callback methods (out doesn't get an xy)
98
var events = ["down", "move", "up", "done"];
102
for(var i=0; i<events.length; ++i) {
103
var px = new OpenLayers.Pixel(Math.random(), Math.random());
104
testEvents[events[i]] = {xy: px};
105
setCallback(events[i]);
107
function setCallback(key) {
108
callbacks[key] = function(evtxy) {
109
t.ok(evtxy.x == testEvents[key].xy.x &&
110
evtxy.y == testEvents[key].xy.y,
111
key + " callback called with the proper evt.xy");
115
var handler = new OpenLayers.Handler.Drag(control, callbacks);
118
var oldIsLeftClick = OpenLayers.Event.isLeftClick;
119
var oldStop = OpenLayers.Event.stop;
120
var oldCheckModifiers = handler.checkModifiers;
122
// test mousedown with right click
123
OpenLayers.Event.isLeftClick = function() {
126
handler.checkModifiers = function() {
129
handler.started = true;
130
handler.start = {x: "foo", y: "bar"};
131
handler.last = {x: "foo", y: "bar"};
132
map.events.triggerEvent("mousedown", testEvents.down);
133
t.ok(!handler.started, "right-click sets started to false");
134
t.eq(handler.start, null, "right-click sets start to null");
135
t.eq(handler.last, null, "right-click sets last to null");
137
// test mousedown with improper modifier
138
OpenLayers.Event.isLeftClick = function() {
141
handler.checkModifiers = function() {
144
handler.started = true;
145
handler.start = {x: "foo", y: "bar"};
146
handler.last = {x: "foo", y: "bar"};
147
map.events.triggerEvent("mousedown", testEvents.down);
148
t.ok(!handler.started, "bad modifier sets started to false");
149
t.eq(handler.start, null, "bad modifier sets start to null");
150
t.eq(handler.last, null, "bad modifier sets last to null");
153
handler.checkModifiers = function(evt) {
154
t.ok(evt.xy.x == testEvents.down.xy.x &&
155
evt.xy.y == testEvents.down.xy.y,
156
"mousedown calls checkModifiers with the proper event");
159
OpenLayers.Event.isLeftClick = function(evt) {
160
t.ok(evt.xy.x == testEvents.down.xy.x &&
161
evt.xy.y == testEvents.down.xy.y,
162
"mousedown calls isLeftClick with the proper event");
165
OpenLayers.Event.stop = function(evt, allowDefault) {
167
t.ok(evt.xy.x == testEvents.down.xy.x &&
168
evt.xy.y == testEvents.down.xy.y,
169
"mousedown default action is disabled");
172
"mousedown is prevented from falling to other elements");
175
map.events.triggerEvent("mousedown", testEvents.down);
176
t.ok(handler.started, "mousedown sets the started flag to true");
177
t.ok(!handler.dragging, "mouse down sets the dragging flag to false");
178
t.ok(handler.start.x == testEvents.down.xy.x &&
179
handler.start.y == testEvents.down.xy.y,
180
"mouse down sets handler.start correctly");
181
t.ok(handler.last.x == testEvents.down.xy.x &&
182
handler.last.y == testEvents.down.xy.y,
183
"mouse down sets handler.last correctly");
185
OpenLayers.Event.stop = oldStop;
186
OpenLayers.Event.isLeftClick = oldIsLeftClick;
187
handler.checkModifiers = oldCheckModifiers;
189
// test mouseup before mousemove
190
var realUp = testEvents.up;
191
testEvents.up = testEvents.down;
192
// this will fail with notice about the done callback being called
193
// if done is called when it shouldn't be
194
map.events.triggerEvent("mouseup", testEvents.up);
195
testEvents.up = realUp;
198
handler.started = false;
199
map.events.triggerEvent("mousemove", {xy: {x: null, y: null}});
200
// if the handler triggers the move callback, it will be with the
203
"mousemove before the handler has started doesn't call move");
205
handler.started = true;
206
map.events.triggerEvent("mousemove", testEvents.move);
207
t.ok(handler.dragging, "mousemove sets the dragging flag to true");
208
t.ok(handler.start.x == testEvents.down.xy.x &&
209
handler.start.y == testEvents.down.xy.y,
210
"mouse move leaves handler.start alone");
211
t.ok(handler.last.x == testEvents.move.xy.x &&
212
handler.last.y == testEvents.move.xy.y,
213
"mouse move sets handler.last correctly");
215
// a second move with the same evt.xy should not trigger move callback
216
// if it does, the test page will complain about a bad plan number
217
var oldMove = handler.callbacks.move;
218
handler.callbacks.move = function() {
220
"a second move with the same evt.xy should not trigger a move callback");
222
map.events.triggerEvent("mousemove", testEvents.move);
223
handler.callbacks.move = oldMove;
226
handler.started = false;
227
map.events.triggerEvent("mouseup", {xy: {x: null, y: null}});
228
// if the handler triggers the up callback, it will be with the
231
"mouseup before the handler has started doesn't call up");
233
handler.started = true;
234
// mouseup triggers the up and done callbacks
235
testEvents.done = testEvents.up;
236
map.events.triggerEvent("mouseup", testEvents.up);
237
t.ok(!this.started, "mouseup sets the started flag to false");
238
t.ok(!this.dragging, "mouseup sets the dragging flag to false");
241
handler.started = false;
242
map.events.triggerEvent("mouseout", {xy: {x: null, y: null}});
243
// if the handler triggers the out or done callback, it will be with the
246
"mouseout before the handler has started doesn't call out or done");
248
handler.started = true;
249
var oldMouseLeft = OpenLayers.Util.mouseLeft;
250
OpenLayers.Util.mouseLeft = function(evt, element) {
251
t.ok(evt.xy.x == testEvents.done.xy.x &&
252
evt.xy.y == testEvents.done.xy.y,
253
"mouseout calls Util.mouseLeft with the correct event");
254
t.eq(element.id, map.div.id,
255
"mouseout calls Util.mouseLeft with the correct element");
258
// mouseup triggers the out and done callbacks
259
// out callback gets no arguments
260
handler.callbacks.out = function() {
261
t.eq(arguments.length, 0,
262
"mouseout calls out callback with no arguments");
264
map.events.triggerEvent("mouseout", testEvents.done);
265
t.ok(!handler.started, "mouseout sets started flag to false");
266
t.ok(!handler.dragging, "mouseout sets dragging flag to false");
267
OpenLayers.Util.mouseLeft = oldMouseLeft;
269
// test click with the click.html example - the click method on the
270
// drag handler returns (handler.start == handler.last), stopping
271
// propagation of the click event if the mouse moved during a drag.
273
// regression tests will assure that the drag handler doesn't mess
274
// with anything else on a click
275
function getProperties(obj) {
278
if(typeof obj[key] != "function" && typeof obj[key] != "object") {
279
props[key] = obj[key];
284
var before = getProperties(handler);
285
map.events.triggerEvent("click", null);
286
var after = getProperties(handler);
287
t.eq(before, after, "click doesn't mess with handler");
291
function test_Handler_Drag_submethods(t) {
294
var map = new OpenLayers.Map('map', {controls: []});
296
var control = new OpenLayers.Control();
297
map.addControl(control);
300
var handler = new OpenLayers.Handler.Drag(control, {});
302
var events = ["down", "move", "up", "out"];
305
for(var i=0; i<events.length; ++i) {
307
px = new OpenLayers.Pixel(Math.random(), Math.random());
308
testEvents[type] = {xy: px};
311
function setMethod(type) {
312
handler[type] = function(evt) {
313
t.ok(evt.xy.x == testEvents[type].xy.x &&
314
evt.xy.y == testEvents[type].xy.y,
315
"handler." + type + " called with the right event");
321
handler.checkModifiers = function(evt) {
324
var oldIsLeftClick = OpenLayers.Event.isLeftClick;
325
OpenLayers.Event.isLeftClick = function(evt) {
328
map.events.triggerEvent("mousedown", testEvents.down);
329
OpenLayers.Event.isLeftClick = oldIsLeftClick;
332
map.events.triggerEvent("mousemove", testEvents.move);
335
map.events.triggerEvent("mouseup", testEvents.up);
338
var oldMouseLeft = OpenLayers.Util.mouseLeft;
339
OpenLayers.Util.mouseLeft = function() {
342
handler.started = true;
343
map.events.triggerEvent("mouseout", testEvents.out);
344
OpenLayers.Util.mouseLeft = oldMouseLeft;
348
function test_Handler_Drag_deactivate(t) {
350
var map = new OpenLayers.Map('map');
351
var control = new OpenLayers.Control();
352
map.addControl(control);
353
var handler = new OpenLayers.Handler.Drag(control);
354
handler.active = false;
355
var deactivated = handler.deactivate();
357
"deactivate returns false if the handler was not already active");
358
handler.active = true;
359
handler.dragging = true;
360
deactivated = handler.deactivate();
362
"deactivate returns true if the handler was active already");
363
t.ok(!handler.started,
364
"deactivate sets started to false");
365
t.ok(!handler.dragging,
366
"deactivate sets dragging to false");
367
t.ok(handler.start == null,
368
"deactivate sets start to null");
369
t.ok(handler.last == null,
370
"deactivate sets start to null");
377
<div id="map" style="width: 300px; height: 150px;"/>