1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
var map;
function test_Control_PanZoom_constructor (t) {
t.plan( 4 );
control = new OpenLayers.Control.PanZoom();
t.ok( control instanceof OpenLayers.Control.PanZoom, "new OpenLayers.Control.PanZoom returns object" );
t.eq( control.displayClass, "olControlPanZoom", "displayClass is correct" );
control = new OpenLayers.Control.PanZoom({position: new OpenLayers.Pixel(100,100)});
t.eq( control.position.x, 100, "PanZoom X Set correctly.");
t.eq( control.position.y, 100, "PanZoom y Set correctly.");
}
function test_Control_PanZoom_addControl (t) {
t.plan( 8 );
map = new OpenLayers.Map('map');
control = new OpenLayers.Control.PanZoom();
t.ok( control instanceof OpenLayers.Control.PanZoom, "new OpenLayers.Control.PanZoom 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[4] === control, "map.controls contains control" );
t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 5, "Control div zIndexed properly" );
t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 5, "Viewport div contains control div" );
t.eq( control.div.style.top, "4px", "Control div top located correctly by default");
var control2 = new OpenLayers.Control.PanZoom();
map.addControl(control2, new OpenLayers.Pixel(100,100));
t.eq( control2.div.style.top, "100px", "2nd control div is located correctly");
}
function test_Control_PanZoom_removeButtons(t) {
t.plan(2);
map = new OpenLayers.Map("map");
control = new OpenLayers.Control.PanZoom();
map.addControl(control);
control.removeButtons();
t.eq(control.buttons.length, 0, "buttons array cleared correctly");
t.eq(control.div.childNodes.length, 0, "control div is empty");
}
function test_Control_PanZoom_control_events (t) {
// IE 9+ does support the standard document.createEvent,
// event.initMouseEvent, and elem.dispatchEvent calls, so it
// should be possible to simulate clicks in this browser.
// For example it looks like jQuery UI does simulate events
// using document.createElement in IE 9+. See
// https://github.com/jquery/jquery-ui/blob/master/tests/jquery.simulate.js.
// I haven't been able to make it work though.
if ( !window.document.createEvent ||
OpenLayers.BROWSER_NAME == "msie" ||
OpenLayers.BROWSER_NAME == "opera" ||
!t.open_window) {
t.plan(0);
t.debug_print("FIXME: This browser does not support the PanZoom test at this time.");
} else {
t.plan(35);
t.open_window( "Control/PanZoom.html", function( wnd ) {
t.delay_call( 3, function() {
var flag;
function setFlag(evt) {
flag[evt.type] = true;
}
function resetFlags() {
flag = {
mousedown: false,
mouseup: false,
click: false,
dblclick: false
};
}
resetFlags();
wnd.mapper.events.register("mousedown", mapper, setFlag);
wnd.mapper.events.register("mouseup", mapper, setFlag);
wnd.mapper.events.register("click", mapper, setFlag);
wnd.mapper.events.register("dblclick", mapper, setFlag);
simulateClick(wnd, wnd.control.buttons[0]);
t.delay_call(2, function() {
t.ok( wnd.mapper.getCenter().lat > wnd.centerLL.lat, "1) Pan up works correctly" );
t.ok(!flag.mousedown, "1) mousedown does not get to the map");
t.ok(!flag.mouseup, "1) mouseup does not get to the map");
t.ok(!flag.click, "1) click does not get to the map");
t.ok(!flag.dblclick, "1) dblclick does not get to the map");
resetFlags();
simulateClick(wnd, wnd.control.buttons[1]);
}, 2, function() {
t.ok( wnd.mapper.getCenter().lon < wnd.centerLL.lon, "2) Pan left works correctly" );
t.ok(!flag.mousedown, "2) mousedown does not get to the map");
t.ok(!flag.mouseup, "2) mouseup does not get to the map");
t.ok(!flag.click, "2) click does not get to the map");
t.ok(!flag.dblclick, "2) dblclick does not get to the map");
resetFlags();
simulateClick(wnd, wnd.control.buttons[2]);
}, 2, function() {
t.ok( wnd.mapper.getCenter().lon == wnd.centerLL.lon, "3) Pan right works correctly" );
t.ok(!flag.mousedown, "3) mousedown does not get to the map");
t.ok(!flag.mouseup, "3) mouseup does not get to the map");
t.ok(!flag.click, "3) click does not get to the map");
t.ok(!flag.dblclick, "3) dblclick does not get to the map");
resetFlags();
simulateClick(wnd, wnd.control.buttons[3]);
}, 2, function() {
t.ok( wnd.mapper.getCenter().lat == wnd.centerLL.lat, "4) Pan down works correctly" );
t.ok(!flag.mousedown, "4) mousedown does not get to the map");
t.ok(!flag.mouseup, "4) mouseup does not get to the map");
t.ok(!flag.click, "4) click does not get to the map");
t.ok(!flag.dblclick, "4) dblclick does not get to the map");
resetFlags();
simulateClick(wnd, wnd.control.buttons[4]);
}, 2, function() {
t.eq( wnd.mapper.getZoom(), 6, "5) zoomin works correctly" );
t.ok(!flag.mousedown, "5) mousedown does not get to the map");
t.ok(!flag.mouseup, "5) mouseup does not get to the map");
t.ok(!flag.click, "5) click does not get to the map");
t.ok(!flag.dblclick, "5) dblclick does not get to the map");
resetFlags();
simulateClick(wnd, wnd.control.buttons[6]);
}, 2, function() {
t.eq( wnd.mapper.getZoom(), 5, "6) zoomout works correctly" );
t.ok(!flag.mousedown, "6) mousedown does not get to the map");
t.ok(!flag.mouseup, "6) mouseup does not get to the map");
t.ok(!flag.click, "6) click does not get to the map");
t.ok(!flag.dblclick, "6) dblclick does not get to the map");
resetFlags();
simulateClick(wnd, wnd.control.buttons[5]);
}, 2, function() {
t.eq( wnd.mapper.getZoom(), 2, "7) zoomworld works correctly" );
t.ok(!flag.mousedown, "7) mousedown does not get to the map");
t.ok(!flag.mouseup, "7) mouseup does not get to the map");
t.ok(!flag.click, "7) click does not get to the map");
t.ok(!flag.dblclick, "7) dblclick does not get to the map");
resetFlags();
});
});
});
}
}
function test_slideRatio(t) {
t.plan(4);
var control = new OpenLayers.Control.PanZoom({
slideRatio: .5
});
var map = new OpenLayers.Map();
map.addControl(control);
control.draw();
control.activate();
map.getSize = function() {
return {
w: 250,
h: 100
}
};
var delta, dir;
var buttons = control.buttons;
map.pan = function(dx, dy){
t.eq([dx,dy],delta,"Panning " + dir + " sets right delta with slideRatio");
};
//up
var delta = [0, -50];
var dir = "up";
var evt = {buttonElement: buttons[0]};
control.onButtonClick.call(control, evt);
//left
var delta = [-125, 0];
var dir = "left";
evt.buttonElement = buttons[1];
control.onButtonClick.call(control, evt);
//right
var delta = [125, 0];
var dir = "right";
evt.buttonElement = buttons[2];
control.onButtonClick.call(control, evt);
//down
var delta = [0, 50];
var dir = "down";
evt.buttonElement = buttons[3];
control.onButtonClick.call(control, evt);
map.destroy();
}
function simulateClick(wnd, elem) {
var evt = wnd.document.createEvent("MouseEvents");
evt.initMouseEvent("mousedown", true, true, wnd, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
elem.dispatchEvent(evt);
evt = wnd.document.createEvent("MouseEvents");
evt.initMouseEvent("mouseup", true, true, wnd, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
elem.dispatchEvent(evt);
evt = wnd.document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, wnd, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
elem.dispatchEvent(evt);
evt = wnd.document.createEvent("MouseEvents");
evt.initMouseEvent("dblclick", true, true, wnd, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
elem.dispatchEvent(evt);
}
function loader() {
control = new OpenLayers.Control.PanZoom();
mapper = new OpenLayers.Map('map', { controls: [control]});
var layer = new OpenLayers.Layer.WMS("Test Layer",
"http://labs.metacarta.com/wms-c/Basic.py?",
{layers: "basic"});
mapper.addLayer(layer);
centerLL = new OpenLayers.LonLat(0,0);
mapper.setCenter(centerLL, 5);
}
</script>
</head>
<body onload="loader()">
<div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>
|