Files
openlayers/tests/Handler/test_Drag.html

340 lines
13 KiB
HTML

<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_Handler_Drag_constructor(t) {
t.plan(3);
var control = new OpenLayers.Control();
control.id = Math.random();
var callbacks = {foo: "bar"};
var options = {bar: "foo"};
var oldInit = OpenLayers.Handler.prototype.initialize;
OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
t.eq(con.id, control.id,
"constructor calls parent with the correct control");
t.eq(call, callbacks,
"constructor calls parent with the correct callbacks");
t.eq(opt, options,
"constructor calls parent with the correct options");
}
var handler = new OpenLayers.Handler.Drag(control, callbacks, options);
OpenLayers.Handler.prototype.initialize = oldInit;
}
function test_Handler_Drag_activate(t) {
t.plan(3);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.Drag(control);
handler.active = true;
var activated = handler.activate();
t.ok(!activated,
"activate returns false if the handler was already active");
handler.active = false;
handler.dragging = true;
activated = handler.activate();
t.ok(activated,
"activate returns true if the handler was not already active");
t.ok(!handler.dragging,
"activate sets dragging to false");
}
function test_Handler_Drag_events(t) {
t.plan(25);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.Drag(control);
// list below events that should be handled (events) and those
// that should not be handled (nonevents) by the handler
var events = ["mousedown", "mouseup", "mousemove", "mouseout", "click"];
var nonevents = ["dblclick", "resize", "focus", "blur"];
map.events.registerPriority = function(type, obj, func) {
var r = func();
if(typeof r == "string") {
// this is one of the mock handler methods
t.eq(OpenLayers.Util.indexOf(nonevents, type), -1,
"registered method is not one of the events " +
"that should not be handled");
t.ok(OpenLayers.Util.indexOf(events, type) > -1,
"activate calls registerPriority with browser event: " + type);
t.eq(typeof func, "function",
"activate calls registerPriority with a function");
t.eq(func(), type,
"activate calls registerPriority with the correct method");
t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Drag",
"activate calls registerPriority with the handler");
}
}
// set browser event like properties on the handler
for(var i=0; i<events.length; ++i) {
setMethod(events[i]);
}
function setMethod(key) {
handler[key] = function() {return key};
}
var activated = handler.activate();
}
function test_Handler_Drag_callbacks(t) {
t.plan(30);
var map = new OpenLayers.Map('map', {controls: []});
var control = new OpenLayers.Control();
map.addControl(control);
// set callback methods (out doesn't get an xy)
var events = ["down", "move", "up", "done"];
var testEvents = {};
var xys = {};
var callbacks = {};
for(var i=0; i<events.length; ++i) {
var px = new OpenLayers.Pixel(Math.random(), Math.random());
testEvents[events[i]] = {xy: px};
setCallback(events[i]);
}
function setCallback(key) {
callbacks[key] = function(evtxy) {
t.ok(evtxy.x == testEvents[key].xy.x &&
evtxy.y == testEvents[key].xy.y,
key + " callback called with the proper evt.xy");
}
}
var handler = new OpenLayers.Handler.Drag(control, callbacks);
handler.activate();
// test mousedown
var oldIsLeftClick = OpenLayers.Event.isLeftClick;
var oldStop = OpenLayers.Event.stop;
var oldCheckModifiers = handler.checkModifiers;
handler.checkModifiers = function(evt) {
t.ok(evt.xy.x == testEvents.down.xy.x &&
evt.xy.y == testEvents.down.xy.y,
"mousedown calls checkModifiers with the proper event");
return true;
}
OpenLayers.Event.isLeftClick = function(evt) {
t.ok(evt.xy.x == testEvents.down.xy.x &&
evt.xy.y == testEvents.down.xy.y,
"mousedown calls isLeftClick with the proper event");
return true;
}
OpenLayers.Event.stop = function(evt, allowDefault) {
if(!allowDefault) {
t.ok(evt.xy.x == testEvents.down.xy.x &&
evt.xy.y == testEvents.down.xy.y,
"mousedown default action is disabled");
} else {
t.ok(evt.xy.x == testEvents.down.xy.x &&
evt.xy.y == testEvents.down.xy.y,
"mousedown is prevented from falling to other elements");
}
}
map.events.triggerEvent("mousedown", testEvents.down);
t.ok(handler.started, "mousedown sets the started flag to true");
t.ok(!handler.dragging, "mouse down sets the dragging flag to false");
t.ok(handler.start.x == testEvents.down.xy.x &&
handler.start.y == testEvents.down.xy.y,
"mouse down sets handler.start correctly");
t.ok(handler.last.x == testEvents.down.xy.x &&
handler.last.y == testEvents.down.xy.y,
"mouse down sets handler.last correctly");
OpenLayers.Event.isLeftClick = oldIsLeftClick;
OpenLayers.Event.stop = oldStop;
handler.checkModifiers = oldCheckModifiers;
// test mousemove
handler.started = false;
map.events.triggerEvent("mousemove", {xy: {x: null, y: null}});
// if the handler triggers the move callback, it will be with the
// incorrect evt.xy
t.ok(true,
"mousemove before the handler has started doesn't call move");
handler.started = true;
map.events.triggerEvent("mousemove", testEvents.move);
t.ok(handler.dragging, "mousemove sets the dragging flag to true");
t.ok(handler.start.x == testEvents.down.xy.x &&
handler.start.y == testEvents.down.xy.y,
"mouse move leaves handler.start alone");
t.ok(handler.last.x == testEvents.move.xy.x &&
handler.last.y == testEvents.move.xy.y,
"mouse move sets handler.last correctly");
// a second move with the same evt.xy should not trigger move callback
// if it does, the test page will complain about a bad plan number
var oldMove = handler.callbacks.move;
handler.callbacks.move = function() {
t.ok(false,
"a second move with the same evt.xy should not trigger a move callback");
}
map.events.triggerEvent("mousemove", testEvents.move);
handler.callbacks.move = oldMove;
// test mouseup
handler.started = false;
map.events.triggerEvent("mouseup", {xy: {x: null, y: null}});
// if the handler triggers the up callback, it will be with the
// incorrect evt.xy
t.ok(true,
"mouseup before the handler has started doesn't call up");
handler.started = true;
// mouseup triggers the up and done callbacks
testEvents.done = testEvents.up;
map.events.triggerEvent("mouseup", testEvents.up);
t.ok(!this.started, "mouseup sets the started flag to false");
t.ok(!this.dragging, "mouseup sets the dragging flag to false");
// test mouseout
handler.started = false;
map.events.triggerEvent("mouseout", {xy: {x: null, y: null}});
// if the handler triggers the out or done callback, it will be with the
// incorrect evt.xy
t.ok(true,
"mouseout before the handler has started doesn't call out or done");
handler.started = true;
var oldMouseLeft = OpenLayers.Util.mouseLeft;
OpenLayers.Util.mouseLeft = function(evt, element) {
t.ok(evt.xy.x == testEvents.done.xy.x &&
evt.xy.y == testEvents.done.xy.y,
"mouseout calls Util.mouseLeft with the correct event");
t.eq(element.id, map.div.id,
"mouseout calls Util.mouseLeft with the correct element");
return true;
}
// mouseup triggers the out and done callbacks
// out callback gets no arguments
handler.callbacks.out = function() {
t.eq(arguments.length, 0,
"mouseout calls out callback with no arguments");
}
map.events.triggerEvent("mouseout", testEvents.done);
t.ok(!handler.started, "mouseout sets started flag to false");
t.ok(!handler.dragging, "mouseout sets dragging flag to false");
OpenLayers.Util.mouseLeft = oldMouseLeft;
// test click
handler.dragging = true;
handler.started = "foo";
map.events.triggerEvent("click", null);
t.ok(!handler.dragging,
"click while dragging sets dragging to false");
t.eq(handler.started, "foo",
"click while dragging doesn't mess with started");
handler.dragging = null;
handler.started = true;
map.events.triggerEvent("click", null);
t.ok(handler.dragging == null,
"click while not dragging doesn't mess with dragging flag");
t.ok(!handler.started,
"click while not dragging sets started to false");
}
function test_Handler_Drag_submethods(t) {
t.plan(4);
var map = new OpenLayers.Map('map', {controls: []});
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.Drag(control, {});
// set test events
var events = ["down", "move", "up", "out"];
var testEvents = {};
var type, px;
for(var i=0; i<events.length; ++i) {
type = events[i];
px = new OpenLayers.Pixel(Math.random(), Math.random());
testEvents[type] = {xy: px};
setMethod(type);
}
function setMethod(type) {
handler[type] = function(evt) {
t.ok(evt.xy.x == testEvents[type].xy.x &&
evt.xy.y == testEvents[type].xy.y,
"handler." + type + " called with the right event");
}
}
handler.activate();
// test mousedown
handler.checkModifiers = function(evt) {
return true;
}
var oldIsLeftClick = OpenLayers.Event.isLeftClick;
OpenLayers.Event.isLeftClick = function(evt) {
return true;
}
map.events.triggerEvent("mousedown", testEvents.down);
OpenLayers.Event.isLeftClick = oldIsLeftClick;
// test mousemove
map.events.triggerEvent("mousemove", testEvents.move);
// test mouseup
map.events.triggerEvent("mouseup", testEvents.up);
// test mouseout
var oldMouseLeft = OpenLayers.Util.mouseLeft;
OpenLayers.Util.mouseLeft = function() {
return true;
};
handler.started = true;
map.events.triggerEvent("mouseout", testEvents.out);
OpenLayers.Util.mouseLeft = oldMouseLeft;
}
function test_Handler_Drag_deactivate(t) {
t.plan(6);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.Drag(control);
handler.active = false;
var deactivated = handler.deactivate();
t.ok(!deactivated,
"deactivate returns false if the handler was not already active");
handler.active = true;
handler.dragging = true;
deactivated = handler.deactivate();
t.ok(deactivated,
"deactivate returns true if the handler was active already");
t.ok(!handler.started,
"deactivate sets started to false");
t.ok(!handler.dragging,
"deactivate sets dragging to false");
t.ok(handler.start == null,
"deactivate sets start to null");
t.ok(handler.last == null,
"deactivate sets start to null");
}
</script>
</head>
<body>
<div id="map" style="width: 300px; height: 150px;"/>
</body>
</html>