Fix deactivate and destroy methods on the keyboard handler. Add 28 tests for previously untested handler. Thanks to Erik for the quick review and comments - appreciated as always (closes #943).

git-svn-id: http://svn.openlayers.org/trunk/openlayers@4108 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-08-29 17:37:42 +00:00
parent eb65e9c2b3
commit e5641c76e0
3 changed files with 113 additions and 6 deletions

View File

@@ -46,7 +46,7 @@ OpenLayers.Handler.Keyboard = OpenLayers.Class(OpenLayers.Handler, {
* options - {Object} Optional object whose properties will be set on the
* handler.
*/
initialize: function () {
initialize: function(control, callbacks, options) {
OpenLayers.Handler.prototype.initialize.apply(this, arguments);
// cache the bound event listener method so it can be unobserved later
this.eventListener = this.handleKeyEvent.bindAsEventListener(this);
@@ -58,7 +58,7 @@ OpenLayers.Handler.Keyboard = OpenLayers.Class(OpenLayers.Handler, {
destroy: function() {
this.deactivate();
this.eventListener = null;
OpenLayers.Control.prototype.destroy.apply(this, arguments);
OpenLayers.Handler.prototype.destroy.apply(this, arguments);
},
/**
@@ -80,15 +80,15 @@ OpenLayers.Handler.Keyboard = OpenLayers.Class(OpenLayers.Handler, {
* Method: deactivate
*/
deactivate: function() {
var deactivated = false;
if (OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
for (var i = 0; i < this.KEY_EVENTS.length; i++) {
OpenLayers.Event.stopObserving(
document, this.KEY_EVENTS[i], this.eventListener);
window, this.KEY_EVENTS[i], this.eventListener);
}
return true;
} else {
return false;
deactivated = true;
}
return deactivated;
},
/**

View File

@@ -0,0 +1,106 @@
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_Handler_Keyboard_initialize(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.Keyboard(control, callbacks,
options);
OpenLayers.Handler.prototype.initialize = oldInit;
}
function test_Handler_Keyboard_destroy(t) {
t.plan(3);
var control = new OpenLayers.Control();
var handler = new OpenLayers.Handler.Keyboard(control);
var old = OpenLayers.Handler.prototype.destroy;
t.ok(handler.eventListener != null,
"eventListener is not null before destroy");
OpenLayers.Handler.prototype.destroy = function() {
t.ok(true, "destroy calls destroy on correct parent");
};
handler.destroy();
t.ok(handler.eventListener == null,
"eventListeners is null after destroy");
OpenLayers.Handler.prototype.destroy = old;
}
function test_Handler_Keyboard_activate(t) {
t.plan(11);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.Keyboard(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;
var old = OpenLayers.Event.stopObserving;
var types = ["keydown", "keypress", "keyup"];
OpenLayers.Event.observe = function(obj, type, method) {
t.ok(obj == window,
"activate calls stopObserving with correct object");
var validType = (OpenLayers.Util.indexOf(types, type) != -1);
t.ok(validType, "activate calls stopObserving for " + type);
t.ok(method == handler.eventListener,
"activate calls stopObserving with correct method");
};
activated = handler.activate();
t.ok(activated,
"activate returns true if the handler was not already active");
OpenLayers.Event.observe = old;
}
function test_Handler_Drag_deactivate(t) {
t.plan(11);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.Keyboard(control);
handler.active = false;
var deactivated = handler.deactivate();
t.ok(!deactivated,
"deactivate returns false if the handler was not already active");
handler.active = true;
var old = OpenLayers.Event.stopObserving;
var types = ["keydown", "keypress", "keyup"];
OpenLayers.Event.stopObserving = function(obj, type, method) {
t.ok(obj == window,
"deactivate calls stopObserving with correct object");
var validType = (OpenLayers.Util.indexOf(types, type) != -1);
t.ok(validType, "deactivate calls stopObserving for " + type);
t.ok(method == handler.eventListener,
"deactivate calls stopObserving with correct method");
};
deactivated = handler.deactivate();
t.ok(deactivated,
"deactivate returns true if the handler was active already");
OpenLayers.Event.stopObserving = old;
}
</script>
</head>
<body>
<div id="map" style="width: 300px; height: 150px;"/>
</body>
</html>

View File

@@ -72,6 +72,7 @@
<li>Control/test_Scale.html</li>
<li>test_Handler.html</li>
<li>Handler/test_MouseWheel.html</li>
<li>Handler/test_Keyboard.html</li>
<li>Handler/test_Drag.html</li>
<li>Handler/test_Point.html</li>
<li>Handler/test_Path.html</li>