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
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
var map, control, centerLL
watch = null,
geolocation= {
getCurrentPosition: function(f) {
f({
coords: { latitude: 10, longitude: 10 }
});
},
watchPosition: function(f) {
watch = true;
},
clearWatch: function() {
watch = null;
}
};
function test_initialize(t) {
t.plan(3);
control = new OpenLayers.Control.Geolocate({geolocationOptions: {foo: 'bar'}});
t.ok(control instanceof OpenLayers.Control.Geolocate,
"new OpenLayers.Control returns object" );
t.eq(control.displayClass, "olControlGeolocate", "displayClass is correct" );
t.eq(control.geolocationOptions.foo, 'bar',
'provided geolocation options are set in the geolocationOptions prop');
}
function test_bind(t) {
t.plan(3);
var control = new OpenLayers.Control.Geolocate({
geolocation: geolocation
});
control.events.register('locationupdated', null, function() {
t.ok(true, 'locationupdated event is fired when bound');
});
map.addControl(control);
control.activate();
var center = map.getCenter();
t.eq(center.lon, 10, 'bound control sets the map lon');
t.eq(center.lat, 10, 'bound control sets the map lat');
control.deactivate();
map.removeControl(control);
map.setCenter(centerLL);
}
function test_unbind(t) {
t.plan(3);
var control = new OpenLayers.Control.Geolocate({
geolocation: geolocation,
bind: false
});
control.events.register('locationupdated', null, function() {
t.ok(true, 'locationupdated event is fired when unbound');
});
map.addControl(control);
control.activate();
var center = map.getCenter();
t.eq(center.lon, 0, 'unbound control doesnt sets the map lon');
t.eq(center.lat, 0, 'unbound control doesnt sets the map lat');
control.deactivate();
map.removeControl(control);
map.setCenter(centerLL);
}
function test_getCurrentLocation(t) {
t.plan(5);
var control = new OpenLayers.Control.Geolocate({
geolocation: geolocation
});
map.addControl(control);
t.eq(control.getCurrentLocation(), false, 'getCurrentLocation return false if control hasnt been activated');
control.activate();
map.setCenter(centerLL);
t.eq(control.getCurrentLocation(), true, 'getCurrentLocation return true if control has been activated');
var center = map.getCenter();
t.eq(center.lon, 10, 'bound control sets the map lon when calling getCurrentLocation');
t.eq(center.lat, 10, 'bound control sets the map lat when calling getCurrentLocation');
control.deactivate();
map.removeControl(control);
map.setCenter(centerLL);
var control2 = new OpenLayers.Control.Geolocate({
geolocation: geolocation
});
map.addControl(control2);
t.eq(control2.getCurrentLocation(), false, 'getCurrentLocation return false if control is in watch mode');
control2.deactivate();
map.removeControl(control2);
map.setCenter(centerLL);
}
function test_watch(t) {
t.plan(2);
var control = new OpenLayers.Control.Geolocate({
geolocation: geolocation,
watch: true
});
map.addControl(control);
control.activate();
t.eq(watch, true, 'watch option makes calls to watchPosition');
control.deactivate();
t.eq(watch, null, 'deactivate control calls the clearwatch');
map.removeControl(control);
map.setCenter(centerLL);
}
function test_destroy(t) {
t.plan(1);
var control = new OpenLayers.Control.Geolocate({
geolocation: geolocation,
watch: true
});
control.activate();
control.destroy();
t.ok(control.active === false, "control deactivated before being destroyed");
}
function loader() {
map = new OpenLayers.Map('map');
var layer = new OpenLayers.Layer.WMS("Test Layer",
"http://labs.metacarta.com/wms-c/Basic.py?",
{layers: "basic"});
map.addLayer(layer);
centerLL = new OpenLayers.LonLat(0,0);
map.setCenter(centerLL, 5);
}
</script>
</head>
<body onload="loader()">
<div id="map" style="width: 256px; height: 256px;"/>
</body>
</html>
|