-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtest.geolocation.js
257 lines (224 loc) · 12 KB
/
test.geolocation.js
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
245
246
247
248
249
250
251
252
253
254
255
256
257
describe("geolocation", function () {
var geo = require('cordova/plugin/geolocation'),
Position = require('cordova/plugin/Position'),
PositionError = require('cordova/plugin/PositionError'),
s, e,
exec = require('cordova/exec');
beforeEach(function () {
s = jasmine.createSpy("success");
e = jasmine.createSpy("error");
exec.reset();
geo.lastPosition = null; // reset the cached position
});
describe("getCurrentPosition", function() {
describe("and passed-in arguments", function () {
it("uses default PositionOptions if none are specified", function () {
geo.getCurrentPosition(s, e);
expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "getLocation", [false, 0]);
});
it("uses the enableHighAccuracy option if specified", function () {
geo.getCurrentPosition(s, e, {enableHighAccuracy: true});
expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "getLocation", [true, 0]);
});
it("uses the maximumAge option if specified", function () {
geo.getCurrentPosition(s, e, {maximumAge: 100});
expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "getLocation", [false, 100]);
});
it("uses a timeout value of 0 if specified and negative, which should call the error callback immediately (since we have no cached position)", function () {
geo.getCurrentPosition(s, e, {timeout: -1000});
expect(e).toHaveBeenCalledWith({
code:PositionError.TIMEOUT,
message:"timeout value in PositionOptions set to 0 and no cached Position object available, or cached Position object's age exceed's provided PositionOptions' maximumAge parameter."
});
});
it("can be used with one, two or three arguments", function() {
expect(function() { geo.getCurrentPosition(s); }).not.toThrow();
expect(function() { geo.getCurrentPosition(s,e); }).not.toThrow();
expect(function() { geo.getCurrentPosition(s,e,{}); }).not.toThrow();
});
it("should throw an exception if used with no arguments", function() {
expect(function() { geo.getCurrentPosition();}).toThrow("getCurrentPosition must be called with at least one argument.");
});
});
describe("position acquisition", function() {
it("should provide a cached position if one exists and has a timestamp value conforming with passed in maximumAge", function() {
// Create a date object from 2 seconds ago to store as cached position.
var d = new Date();
d.setTime(d.getTime() - 2000);
var p = new Position(null, d);
geo.lastPosition = p;
geo.getCurrentPosition(s, e, {maximumAge:3000});
expect(s).toHaveBeenCalledWith(p);
expect(exec).not.toHaveBeenCalled();
});
it("should fire exec if a cached position exists but whose timestamp is longer than the maximumAge parameter", function() {
// Create a date object from 2 seconds ago to store as cached position.
var d = new Date();
d.setTime(d.getTime() - 2000);
var p = new Position(null, d);
geo.lastPosition = p;
geo.getCurrentPosition(s, e, {maximumAge:100});
expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "getLocation", [false, 100]);
});
it("should fire error callback with TIMEOUT code after timeout period has elapsed and no position is available", function() {
runs(function() {
geo.getCurrentPosition(s, e, {timeout: 50});
expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "getLocation", [false, 0]);
});
waits(75);
runs(function() {
expect(e).toHaveBeenCalledWith({
code:PositionError.TIMEOUT,
message:"Position retrieval timed out."
});
});
});
it("should not fire error callback with TIMEOUT if a position is obtained within the timeout period", function() {
runs(function() {
geo.getCurrentPosition(s, e, {timeout:50});
// Call the success callback to "fake" the native framework returning a (empty) position object.
// This should also disable the timeout timer.
exec.mostRecentCall.args[0]({});
});
waits(75);
runs(function() {
expect(e).not.toHaveBeenCalled();
expect(s).toHaveBeenCalled();
});
});
it("should fire error callback with POSITION_UNAVAILABLE if error occurs during acquisition before timeout expires", function() {
geo.getCurrentPosition(s, e, {timeout: 50});
// Call the error callback to "fake" the native framework returning an error object.
var eObj = {
code:PositionError.POSITION_UNAVAILABLE
};
exec.mostRecentCall.args[1](eObj);
expect(e).toHaveBeenCalledWith({
code:PositionError.POSITION_UNAVAILABLE,
message:""
});
});
it("should not fire error callback with TIMEOUT if error occurs during acquisition before timeout expires", function() {
runs(function() {
geo.getCurrentPosition(s, e, {timeout: 50});
// Call the error callback to "fake" the native framework returning an error object.
var eObj = {
code:PositionError.POSITION_UNAVAILABLE
};
exec.mostRecentCall.args[1](eObj);
});
waits(75);
runs(function() {
expect(e).not.toHaveBeenCalledWith({
code:PositionError.TIMEOUT,
message:"Position retrieval timed out."
});
});
});
});
});
describe("watchPosition", function () {
describe("arguments", function () {
it("uses default PositionOptions if none are specified", function () {
var id = geo.watchPosition(s, e);
expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "addWatch", [id, false]);
});
it("uses the enableHighAccuracy option if specified", function () {
var id = geo.watchPosition(s, e, {enableHighAccuracy: true});
expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "addWatch", [id, true]);
});
it("uses the maximumAge option if specified", function () {
var id = geo.watchPosition(s, e, {maximumAge: 100});
expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "addWatch", [id, false]);
});
it("uses a timeout value of 0 if specified and negative, which should call the error callback immediately", function () {
geo.watchPosition(s, e, {timeout: -1000});
expect(e).toHaveBeenCalledWith({
code:PositionError.TIMEOUT,
message:"timeout value in PositionOptions set to 0 and no cached Position object available, or cached Position object's age exceed's provided PositionOptions' maximumAge parameter."
});
});
it("can be used with one, two or three arguments", function() {
expect(function() { geo.watchPosition(s); }).not.toThrow();
expect(function() { geo.watchPosition(s,e); }).not.toThrow();
expect(function() { geo.watchPosition(s,e,{}); }).not.toThrow();
});
it("should throw an exception if used with no arguments", function() {
expect(function() { geo.watchPosition();}).toThrow("watchPosition must be called with at least one argument.");
});
});
describe("position acquisition", function() {
it("should invoke the success callback every time the position changes", function() {
runs(function() {
geo.watchPosition(s, e);
});
waits(50);
runs(function() {
exec.mostRecentCall.args[0]({}); // fake success callback from native
expect(s).toHaveBeenCalled();
s.reset();
exec.mostRecentCall.args[0]({}); // fake success callback from native
expect(s).toHaveBeenCalled();
});
});
it("should invoke the error callback if position could not be retrieved", function() {
geo.watchPosition(s, e);
exec.mostRecentCall.args[1]({
code:PositionError.POSITION_UNAVAILABLE
});
expect(e).toHaveBeenCalledWith({
code:PositionError.POSITION_UNAVAILABLE,
message:""
});
});
it("should invoke the error callback if no position could be acquired within the specified timeout", function() {
runs(function() {
geo.watchPosition(s, e, {timeout:50});
});
waits(75);
runs(function() {
expect(e).toHaveBeenCalledWith({
code:PositionError.TIMEOUT,
message:"Position retrieval timed out."
});
});
});
it("should invoke the error callback if no position could be acquired within the specified timeout, even after successfully retrieving the position once", function() {
runs(function() {
geo.watchPosition(s, e, {timeout:50});
});
waits(25);
runs(function() {
exec.mostRecentCall.args[0]({}); // fire new position return
expect(s).toHaveBeenCalled();
});
waits(30);
runs(function() {
// The error callback should NOT be fired, since the timeout should have reset when we fired a new position return above
expect(e).not.toHaveBeenCalled();
});
waits(25);
runs(function() {
// NOW the error callback should be fired with a TIMEOUT error
expect(e).toHaveBeenCalledWith({
code:PositionError.TIMEOUT,
message:"Position retrieval timed out."
});
});
});
});
});
describe("clearWatch", function () {
it("should tell native to remove an id from the watch list if it exists", function() {
var id = geo.watchPosition(s, e);
exec.reset();
geo.clearWatch(id);
expect(exec).toHaveBeenCalledWith(null, null, "Geolocation", "clearWatch", [id]);
});
it("should not call into native if id does not exist", function() {
var id = "this is bat country";
geo.clearWatch(id);
expect(exec).not.toHaveBeenCalled();
});
});
});