Files
openlayers/tests/Handler/Point.html
2008-03-31 05:03:49 +00:00

170 lines
6.4 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_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>