3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_initialize(t) {
8 control = new OpenLayers.Control.NavigationHistory();
9 t.ok(control instanceof OpenLayers.Control.NavigationHistory,
10 "constructor returns correct instance");
11 t.eq(control.displayClass, "olControlNavigationHistory",
12 "displayClass is correct");
13 t.ok(control.next instanceof OpenLayers.Control.Button,
14 "constructor creates next control");
15 t.ok(control.previous instanceof OpenLayers.Control.Button,
16 "constructor creates previous control");
19 function test_destroy(t) {
21 control = new OpenLayers.Control.NavigationHistory();
22 control.next.destroy = function() {
23 t.ok(true, "destroy calls next.destroy");
25 control.previous.destroy = function() {
26 t.ok(true, "destroy calls previous.destroy");
31 function test_previous(t) {
35 numStates * 3 // for lon, lat, zoom
36 + 3 // for confirming that previous with empty stack works
39 var history = new Array(numStates);
40 for(var i=0; i<numStates; ++i) {
42 center: new OpenLayers.LonLat(
43 (i * 360 / numStates) - 180, (i * 180 / numStates) - 90
49 var map = new OpenLayers.Map("map");
50 var layer = new OpenLayers.Layer(
51 "test", {isBaseLayer: true}
54 var control = new OpenLayers.Control.NavigationHistory();
55 map.addControl(control);
57 // set previous states
58 for(i=0; i<numStates; ++i) {
59 map.setCenter(history[i].center, history[i].zoom);
61 // test previous states
62 for(i=numStates-1; i>=0; --i) {
63 t.eq(map.getCenter().lon, history[i].center.lon, "(step " + i + ") lon correct");
64 t.eq(map.getCenter().lat, history[i].center.lat, "(step " + i + ") lat correct");
65 t.eq(map.getZoom(), history[i].zoom, "(step " + i + ") zoom correct");
66 control.previous.trigger();
68 // test previous with empty stack
69 t.eq(map.getCenter().lon, history[0].center.lon, "(step 0 again) lon correct");
70 t.eq(map.getCenter().lat, history[0].center.lat, "(step 0 again) lat correct");
71 t.eq(map.getZoom(), history[0].zoom, "(step 0 again) zoom correct");
74 function test_next(t) {
78 numStates * 3 // for lon, lat, zoom
79 + 3 // for confirming that next with empty stack works
82 var history = new Array(numStates);
83 for(var i=0; i<numStates; ++i) {
85 center: new OpenLayers.LonLat(
86 (i * 360 / numStates) - 180, (i * 180 / numStates) - 90
92 var map = new OpenLayers.Map("map");
93 var layer = new OpenLayers.Layer(
94 "test", {isBaseLayer: true}
97 var control = new OpenLayers.Control.NavigationHistory();
98 map.addControl(control);
100 // set previous states
101 for(i=0; i<numStates; ++i) {
102 map.setCenter(history[i].center, history[i].zoom);
105 for(i=numStates-1; i>=0; --i) {
106 control.previous.trigger();
109 for(i=0; i<numStates; ++i) {
110 t.eq(map.getCenter().lon, history[i].center.lon, "(step " + i + ") lon correct");
111 t.eq(map.getCenter().lat, history[i].center.lat, "(step " + i + ") lat correct");
112 t.eq(map.getZoom(), history[i].zoom, "(step " + i + ") zoom correct");
113 control.next.trigger();
115 // test next with empty stack
116 t.eq(map.getCenter().lon, history[numStates-1].center.lon, "(step " + (numStates-1) + " again) lon correct");
117 t.eq(map.getCenter().lat, history[numStates-1].center.lat, "(step " + (numStates-1) + " again) lat correct");
118 t.eq(map.getZoom(), history[numStates-1].zoom, "(step " + (numStates-1) + " again) zoom correct");
121 function test_limit(t) {
126 numStates * 6 // for previous & next lon, lat, zoom
129 var history = new Array(numStates);
130 for(var i=0; i<numStates; ++i) {
132 center: new OpenLayers.LonLat(
133 (i * 360 / numStates) - 180, (i * 180 / numStates) - 90
139 var map = new OpenLayers.Map("map");
140 var layer = new OpenLayers.Layer(
141 "test", {isBaseLayer: true}
144 var control = new OpenLayers.Control.NavigationHistory({limit: limit});
145 map.addControl(control);
147 // set previous states
148 for(i=0; i<numStates; ++i) {
149 map.setCenter(history[i].center, history[i].zoom);
151 // test previous states (only up to limit should work)
153 for(i=numStates-1; i>=0; --i) {
154 state = Math.max(i, numStates - limit - 1);
155 t.eq(map.getCenter().lon, history[state].center.lon, "(previous step " + i + ") lon correct: state " + state);
156 t.eq(map.getCenter().lat, history[state].center.lat, "(previous step " + i + ") lat correct: state " + state);
157 t.eq(map.getZoom(), history[state].zoom, "(previous step " + i + ") zoom correct: state " + state);
158 control.previous.trigger();
161 for(i=0; i<numStates; ++i) {
162 state = Math.min(numStates - 1, numStates - limit - 1 + i);
163 t.eq(map.getCenter().lon, history[state].center.lon, "(next step " + i + ") lon correct: state " + state);
164 t.eq(map.getCenter().lat, history[state].center.lat, "(next step " + i + ") lat correct: state " + state);
165 t.eq(map.getZoom(), history[state].zoom, "(next step " + i + ") zoom correct: state " + state);
166 control.next.trigger();
174 <div id="map" style="width: 100px; height: 100px;"/>