Add tests and improved functionality for keyboard defaults from #580.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@3024 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2007-04-06 03:13:17 +00:00
parent 895ba639ae
commit 098ed43cf6
2 changed files with 139 additions and 10 deletions

View File

@@ -14,7 +14,7 @@ OpenLayers.Control.KeyboardDefaults.prototype =
OpenLayers.Class.inherit( OpenLayers.Control, { OpenLayers.Class.inherit( OpenLayers.Control, {
/** @type int */ /** @type int */
slideFactor: 50, slideFactor: 75,
/** /**
* @constructor * @constructor
@@ -50,26 +50,48 @@ OpenLayers.Control.KeyboardDefaults.prototype =
defaultKeyPress: function (code) { defaultKeyPress: function (code) {
switch(code) { switch(code) {
case OpenLayers.Event.KEY_LEFT: case OpenLayers.Event.KEY_LEFT:
this.map.pan(-50, 0); this.map.pan(-this.slideFactor, 0);
break; break;
case OpenLayers.Event.KEY_RIGHT: case OpenLayers.Event.KEY_RIGHT:
this.map.pan(50, 0); this.map.pan(this.slideFactor, 0);
break; break;
case OpenLayers.Event.KEY_UP: case OpenLayers.Event.KEY_UP:
this.map.pan(0, -50); this.map.pan(0, -this.slideFactor);
break; break;
case OpenLayers.Event.KEY_DOWN: case OpenLayers.Event.KEY_DOWN:
this.map.pan(0, 50); this.map.pan(0, this.slideFactor);
break; break;
case 33: // Page Up
case 43: // + case 33: // Page Up
var size = this.map.getSize();
this.map.pan(0, -0.75*size.h);
break;
case 34: // Page Down
var size = this.map.getSize();
this.map.pan(0, 0.75*size.h);
break;
case 35: // End
var size = this.map.getSize();
this.map.pan(0.75*size.w, 0);
break;
case 36: // Pos1
var size = this.map.getSize();
this.map.pan(-0.75*size.w, 0);
break;
case 43: // +
this.map.zoomIn();
break;
case 45: // -
this.map.zoomOut();
break;
case 107: // + (IE only)
this.map.zoomIn(); this.map.zoomIn();
break; break;
case 45: // - case 109: // - (IE only)
case 34: // Page Down
this.map.zoomOut(); this.map.zoomOut();
break; break;
} }
}, },
/** @final @type String */ /** @final @type String */

View File

@@ -0,0 +1,107 @@
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript"><!--
var map;
function test_01_Control_KeyboardDefaults_constructor (t) {
t.plan( 1 );
control = new OpenLayers.Control.KeyboardDefaults();
t.ok( control instanceof OpenLayers.Control.KeyboardDefaults,
"new OpenLayers.Control.KeyboardDefaults returns object" );
}
function test_02_Control_KeyboardDefaults_addControl (t) {
t.plan( 4 );
map = new OpenLayers.Map('map');
control = new OpenLayers.Control.KeyboardDefaults();
t.ok( control instanceof OpenLayers.Control.KeyboardDefaults,
"new OpenLayers.Control.KeyboardDefaults returns object" );
t.ok( map instanceof OpenLayers.Map,
"new OpenLayers.Map creates map" );
map.addControl(control);
t.ok( control.map === map, "Control.map is set to the map object" );
t.ok( map.controls[3] === control, "map.controls contains control" );
}
function test_03_Control_KeyboardDefaults_KeyDownEvent (t) {
t.plan( 10 );
var evt = {which: 1};
map = new OpenLayers.Map('map');
var layer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayer(layer);
control = new OpenLayers.Control.KeyboardDefaults();
map.addControl(control);
var centerLL = new OpenLayers.LonLat(0,0);
map.setCenter(centerLL,4);
evt.keyCode = OpenLayers.Event.KEY_LEFT;
control.defaultKeyDown(evt);
t.delay_call(
1, function() {
t.ok( map.getCenter().lon < centerLL.lon, "key left works correctly" );
evt.keyCode = OpenLayers.Event.KEY_RIGHT;
control.defaultKeyDown(evt);
},
1, function() {
t.ok( map.getCenter().lon == centerLL.lon, "key right works correctly" );
evt.keyCode = OpenLayers.Event.KEY_UP;
control.defaultKeyDown(evt);
},
1, function() {
t.ok( map.getCenter().lat > centerLL.lat, "key up works correctly" );
evt.keyCode = OpenLayers.Event.KEY_DOWN;
control.defaultKeyDown(evt);
},
1, function() {
t.ok( map.getCenter().lat == centerLL.lat, "key down works correctly" );
evt.keyCode = 33; //page up
control.defaultKeyDown(evt);
},
1, function() {
t.ok( map.getCenter().lat > centerLL.lat, "key page up works correctly" );
evt.keyCode = 34; //page down
control.defaultKeyDown(evt);
},
1, function() {
t.ok( map.getCenter().lat == centerLL.lat, "key page down works correctly" );
evt.keyCode = 35; //end
control.defaultKeyDown(evt);
},
1, function() {
t.ok( map.getCenter().lon > centerLL.lon, "key end works correctly" );
evt.keyCode = 36; //pos1
control.defaultKeyDown(evt);
},
1, function() {
t.ok( map.getCenter().lon == centerLL.lon, "key pos1 works correctly" );
evt.charCode = 43; //+
control.defaultKeyDown(evt);
},
1, function() {
t.eq( map.getZoom(), 5, "key + works correctly" );
// set zoomanimation flag manually,
// reason: loadend event in layers.js will not achieved in unittests
map.zoomanimationActive = false;
evt.charCode = 45; //-
control.defaultKeyDown(evt);
},
1, function() {
t.eq( map.getZoom(), 4, "key - works correctly" );
}
);
}
// -->
</script>
</head>
<body>
<div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>