git-svn-id: http://svn.openlayers.org/trunk/openlayers@8831 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
224 lines
8.8 KiB
HTML
224 lines
8.8 KiB
HTML
<html>
|
|
<head>
|
|
<script src="../../lib/OpenLayers.js"></script>
|
|
<script type="text/javascript">
|
|
function test_Handler_Point_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.Point(control, callbacks, options);
|
|
|
|
OpenLayers.Handler.prototype.initialize = oldInit;
|
|
}
|
|
|
|
function test_Handler_Point_activation(t) {
|
|
t.plan(3);
|
|
var map = new OpenLayers.Map('map');
|
|
var control = new OpenLayers.Control();
|
|
map.addControl(control);
|
|
var handler = new OpenLayers.Handler.Point(control);
|
|
handler.active = true;
|
|
var activated = handler.activate();
|
|
t.ok(!activated,
|
|
"activate returns false if the handler was already active");
|
|
handler.active = false;
|
|
activated = handler.activate();
|
|
t.ok(activated,
|
|
"activate returns true if the handler was not already active");
|
|
activated = handler.deactivate();
|
|
t.ok(activated,
|
|
"deactivate returns true if the handler was active already");
|
|
}
|
|
|
|
function test_Handler_Point_events(t) {
|
|
t.plan(29);
|
|
|
|
var map = new OpenLayers.Map('map');
|
|
var control = {
|
|
map: map
|
|
};
|
|
var handler = new OpenLayers.Handler.Point(control);
|
|
|
|
// list below events that should be handled (events) and those
|
|
// that should not be handled (nonevents) by the handler
|
|
var events = ["click", "dblclick", "mousedown", "mouseup", "mousemove"];
|
|
var nonevents = ["mouseout", "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.Point",
|
|
"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();
|
|
handler.destroy();
|
|
|
|
// test that click and dblclick are stopped
|
|
var handler = new OpenLayers.Handler.Point(control);
|
|
var oldStop = OpenLayers.Event.stop;
|
|
OpenLayers.Event.stop = function(evt) {
|
|
t.ok(evt.type == "click" || evt.type == "dblclick",
|
|
evt.type + " stopped");
|
|
}
|
|
t.eq(handler.click({type: "click"}), false, "click returns false");
|
|
t.eq(handler.dblclick({type: "dblclick"}), false, "dblclick returns false");
|
|
OpenLayers.Event.stop = oldStop;
|
|
|
|
}
|
|
|
|
function test_callbacks(t) {
|
|
t.plan(10);
|
|
var map = new OpenLayers.Map("map", {
|
|
resolutions: [1]
|
|
});
|
|
var layer = new OpenLayers.Layer.Vector("foo", {
|
|
maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
|
|
isBaseLayer: true
|
|
});
|
|
map.addLayer(layer);
|
|
var control = new OpenLayers.Control({
|
|
});
|
|
var log = {};
|
|
var handler = new OpenLayers.Handler.Point(control, {
|
|
modify: function() {
|
|
log.type = "modify",
|
|
log.args = arguments
|
|
},
|
|
done: function() {
|
|
log.type = "done",
|
|
log.args = arguments
|
|
},
|
|
cancel: function() {
|
|
log.type = "cancel",
|
|
log.args = arguments
|
|
}
|
|
});
|
|
control.handler = handler;
|
|
map.addControl(control);
|
|
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
|
|
|
|
// mock up feature drawing
|
|
handler.activate();
|
|
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
|
|
t.eq(log.type, "modify", "[mousedown] modify called");
|
|
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct point");
|
|
t.geom_eq(log.args[1].geometry, new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct sketch feature");
|
|
handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 0)});
|
|
t.eq(log.type, "modify", "[mousemove] modify called");
|
|
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 75), "[mousemove] correct point");
|
|
t.geom_eq(log.args[1].geometry, new OpenLayers.Geometry.Point(-149, 75), "[mousemove] correct sketch feature");
|
|
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(1, 0)});
|
|
t.eq(log.type, "done", "[mouseup] done called");
|
|
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 75), "[mouseup] correct point");
|
|
|
|
// mock up feature drawing with a cancel
|
|
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
|
|
handler.deactivate();
|
|
t.eq(log.type, "cancel", "[deactivate while drawing] cancel called");
|
|
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[deactivate while drawing] correct point");
|
|
|
|
map.destroy();
|
|
}
|
|
|
|
|
|
function test_Handler_Point_deactivation(t) {
|
|
t.plan(1);
|
|
var map = new OpenLayers.Map('map');
|
|
var control = new OpenLayers.Control();
|
|
map.addControl(control);
|
|
|
|
var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'});
|
|
handler.activate();
|
|
handler.layer.destroy();
|
|
handler.deactivate();
|
|
t.eq(handler.layer, null,
|
|
"deactivate doesn't throw an error if layer was" +
|
|
" previously destroyed");
|
|
}
|
|
|
|
function test_Handler_Point_bounds(t) {
|
|
t.plan(4);
|
|
var map = new OpenLayers.Map('map');
|
|
map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
|
|
map.zoomToMaxExtent();
|
|
var control = new OpenLayers.Control();
|
|
map.addControl(control);
|
|
var handler = new OpenLayers.Handler.Point(control, {});
|
|
var activated = handler.activate();
|
|
var px = new OpenLayers.Pixel(150, 75);
|
|
var evt = {xy: px, which: 1};
|
|
handler.mousedown(evt);
|
|
var lonlat = map.getLonLatFromPixel(px);
|
|
t.eq(handler.point.geometry.x, lonlat.lon, "X is correct");
|
|
t.eq(handler.point.geometry.y, lonlat.lat, "Y is correct");
|
|
t.ok(handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(lonlat.lon,lonlat.lat,lonlat.lon,lonlat.lat)), "Correct bounds");
|
|
var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1};
|
|
handler.mousemove(evt);
|
|
t.ok(!handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(0,0,0,0)), "Bounds changed after moving mouse");
|
|
}
|
|
|
|
function test_Handler_Point_destroy(t) {
|
|
t.plan(4);
|
|
var map = new OpenLayers.Map('map');
|
|
map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
|
|
map.zoomToMaxExtent();
|
|
var control = new OpenLayers.Control();
|
|
map.addControl(control);
|
|
var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'});
|
|
|
|
handler.activate();
|
|
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
|
|
handler.mousedown(evt);
|
|
|
|
t.ok(handler.layer,
|
|
"handler has a layer prior to destroy");
|
|
t.ok(handler.point,
|
|
"handler has a point prior to destroy");
|
|
handler.destroy();
|
|
t.eq(handler.layer, null,
|
|
"handler.layer is null after destroy");
|
|
t.eq(handler.point, null,
|
|
"handler.point is null after destroy");
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<div id="map" style="width: 300px; height: 150px;"/>
|
|
</body>
|
|
</html>
|