|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 import javax.microedition.location.*; |
|
18 |
|
19 class PeriodicUpdateTest extends ViperUnitTest implements LocationListener |
|
20 { |
|
21 final static int ONLY_STATUS_INTERVAL = 0; |
|
22 |
|
23 final static int DEFAULT_INTERVAL = -1; |
|
24 |
|
25 final static int INTERVAL = 10; |
|
26 |
|
27 final static int TIMEOUT = 3; |
|
28 |
|
29 final static int MAXAGE = 2; |
|
30 |
|
31 final static int LARGE_TIMEOUT = 12; |
|
32 |
|
33 final static int LARGE_MAXAGE = 11; |
|
34 |
|
35 final static int[] badInterval = new int[] { -2, -12345, Integer.MIN_VALUE }; |
|
36 |
|
37 final static int[] badTimeout = new int[] { 0, -2, -12345, |
|
38 Integer.MIN_VALUE |
|
39 }; |
|
40 |
|
41 final static int[] badMaxAge = badTimeout; |
|
42 |
|
43 private Coordinates currentCoords; |
|
44 |
|
45 private Location[] iLocations; |
|
46 |
|
47 private int iNumUpdates; |
|
48 |
|
49 private long[] iLocUpdateCallbackTime; |
|
50 |
|
51 PeriodicUpdateTest() |
|
52 { |
|
53 super("PeriodicUpdateTest"); |
|
54 } |
|
55 |
|
56 protected void runTest() throws Throwable |
|
57 { |
|
58 testBadArguments(); |
|
59 testGoodArguments(); |
|
60 testLocationListenerDefault(); |
|
61 testLocationListenerInterval_1s(); |
|
62 testLocationListenerInterval_3s(); |
|
63 testLocationListenerInterval_7s(); |
|
64 testLocationListenerTimeout(); |
|
65 testLocationListenerMaxAge(); |
|
66 testChangeListener(); |
|
67 } |
|
68 |
|
69 private void testBadArguments() |
|
70 { |
|
71 setCurrentTest("testBadArguments()"); |
|
72 providerSetUp(null); |
|
73 assertTrue(iLP != null, "LocationProvider is null"); |
|
74 |
|
75 assertBad(INTERVAL, TIMEOUT, LARGE_MAXAGE); // MaxAge > Interval |
|
76 assertBad(INTERVAL, LARGE_TIMEOUT, MAXAGE); // Timeout > Interval |
|
77 assertBad(INTERVAL, LARGE_TIMEOUT, LARGE_MAXAGE); |
|
78 |
|
79 for (int i = 0; i < badInterval.length; ++i) |
|
80 { |
|
81 assertBad(badInterval[i], TIMEOUT, MAXAGE); |
|
82 } |
|
83 |
|
84 for (int i = 0; i < badTimeout.length; ++i) |
|
85 { |
|
86 assertBad(INTERVAL, badTimeout[i], MAXAGE); |
|
87 assertBad(INTERVAL, badTimeout[i], LARGE_MAXAGE); |
|
88 } |
|
89 |
|
90 for (int i = 0; i < badMaxAge.length; ++i) |
|
91 { |
|
92 assertBad(INTERVAL, TIMEOUT, badMaxAge[i]); |
|
93 assertBad(INTERVAL, LARGE_TIMEOUT, badMaxAge[i]); |
|
94 } |
|
95 |
|
96 for (int i = 0; i < badInterval.length; ++i) |
|
97 { |
|
98 for (int j = 0; j < badTimeout.length; ++j) |
|
99 { |
|
100 for (int k = 0; k < badMaxAge.length; ++k) |
|
101 { |
|
102 assertBad(badInterval[i], badTimeout[j], badMaxAge[k]); |
|
103 } |
|
104 } |
|
105 } |
|
106 |
|
107 } |
|
108 |
|
109 private void testGoodArguments() |
|
110 { |
|
111 setCurrentTest("testGoodArguments()"); |
|
112 providerSetUp(null); |
|
113 assertTrue(iLP != null, "LocationProvider is null"); |
|
114 |
|
115 // These arguments should not throw exceptions |
|
116 testGoodWithBad(this, DEFAULT_INTERVAL); |
|
117 testGoodWithBad(this, ONLY_STATUS_INTERVAL); |
|
118 |
|
119 testGoodWithBad(null, INTERVAL); |
|
120 testGoodWithBad(null, DEFAULT_INTERVAL); |
|
121 testGoodWithBad(null, ONLY_STATUS_INTERVAL); |
|
122 |
|
123 for (int i = 0; i < badInterval.length; ++i) |
|
124 { |
|
125 testGoodWithBad(null, badInterval[i]); |
|
126 } |
|
127 |
|
128 int bigValue = Integer.MAX_VALUE / 1000000; |
|
129 assertGood(this, bigValue, bigValue, bigValue); |
|
130 |
|
131 assertGood(this, Integer.MAX_VALUE, Integer.MAX_VALUE, |
|
132 Integer.MAX_VALUE); |
|
133 } |
|
134 |
|
135 private void testLocationListenerDefault() |
|
136 { |
|
137 setCurrentTest("testLocationListenerDefault()"); |
|
138 final int MAXTIME = 1000 * (TIMETOFIX + 13 + 1); // Interval ~ 6.25 |
|
139 |
|
140 providerSetUp(null); // Simulation PSY should be default |
|
141 assertTrue(iLP != null, "LocationProvider is null"); |
|
142 |
|
143 // Start listening for location changes |
|
144 iLP.setLocationListener(this, -1, -1, -1); |
|
145 |
|
146 getUpdates(2, MAXTIME); |
|
147 |
|
148 checkLocationData(iLocations[0]); |
|
149 checkLocationData(iLocations[1]); |
|
150 |
|
151 iLocations = null; |
|
152 } |
|
153 |
|
154 private void testLocationListenerInterval_1s() |
|
155 { |
|
156 setCurrentTest("testLocationListenerInterval_1s()"); |
|
157 |
|
158 testInterval(1, 4); |
|
159 } |
|
160 |
|
161 private void testLocationListenerInterval_3s() |
|
162 { |
|
163 setCurrentTest("testLocationListenerInterval_3s()"); |
|
164 |
|
165 testInterval(3, 5); |
|
166 } |
|
167 |
|
168 private void testLocationListenerInterval_7s() |
|
169 { |
|
170 setCurrentTest("testLocationListenerInterval_7s()"); |
|
171 |
|
172 testInterval(7, 3); |
|
173 } |
|
174 |
|
175 private void testLocationListenerTimeout() |
|
176 { |
|
177 } |
|
178 |
|
179 private void testLocationListenerMaxAge() |
|
180 { |
|
181 } |
|
182 |
|
183 private void testChangeListener() |
|
184 { |
|
185 setCurrentTest("testChangeListener()"); |
|
186 providerSetUp(null); |
|
187 assertTrue(iLP != null, "LocationProvider is null"); |
|
188 |
|
189 final int PERIOD1 = 10; // seconds |
|
190 final int PERIOD2 = 7; // seconds |
|
191 |
|
192 LocationListener listener = new LocationListener() |
|
193 { |
|
194 public void providerStateChanged(LocationProvider lp, int s) |
|
195 { |
|
196 } |
|
197 |
|
198 public void locationUpdated(LocationProvider lp, Location l) |
|
199 { |
|
200 } |
|
201 }; |
|
202 |
|
203 // Change interval |
|
204 iLP.setLocationListener(this, PERIOD1, -1, -1); |
|
205 int maxtime = 1000 * (2 * PERIOD1 + TIMETOFIX); |
|
206 getUpdates(2, maxtime); |
|
207 |
|
208 iLP.setLocationListener(this, PERIOD2, -1, -1); |
|
209 maxtime = 1000 * (2 * PERIOD2 + TIMETOFIX); |
|
210 getUpdates(2, maxtime); |
|
211 |
|
212 // Change listener |
|
213 iLP.setLocationListener(listener, -1, -1, -1); |
|
214 |
|
215 tearDown(); |
|
216 } |
|
217 |
|
218 private void tearDown() |
|
219 { |
|
220 // Remove this class as listener |
|
221 try |
|
222 { |
|
223 if (iLP != null) |
|
224 { |
|
225 iLP.setLocationListener(null, -1, -1, -1); |
|
226 } |
|
227 } |
|
228 catch (Exception e) |
|
229 { |
|
230 echo("PeriodicUpdateTest: Could not remove listener!"); |
|
231 echo("PeriodicUpdateTest: Exception thrown: " + e); |
|
232 } |
|
233 } |
|
234 |
|
235 public void providerStateChanged(LocationProvider l, int event) |
|
236 { |
|
237 } |
|
238 |
|
239 public void locationUpdated(LocationProvider l, Location fix) |
|
240 { |
|
241 iLocUpdateCallbackTime[iNumUpdates] = System.currentTimeMillis(); |
|
242 |
|
243 echo("PeriodicUpdateTest.locationUpdated() called"); |
|
244 iLocations[iNumUpdates] = fix; |
|
245 ++iNumUpdates; |
|
246 assertTrue(l != null, "LocationProvider is null"); |
|
247 } |
|
248 |
|
249 // ------------------------ Helper methods ----------------------- |
|
250 |
|
251 private void assertBad(int aInterval, int aTimeout, int aMaxAge) |
|
252 { |
|
253 try |
|
254 { |
|
255 iLP.setLocationListener(this, aInterval, aTimeout, aMaxAge); |
|
256 // Oops, no exception thrown, remove listener |
|
257 iLP.setLocationListener(null, -1, -1, -1); |
|
258 assertTrue(false, "Bad arguments should throw exception: " |
|
259 + aInterval + "," + aTimeout + "," + aMaxAge); |
|
260 } |
|
261 catch (IllegalArgumentException iae) |
|
262 { |
|
263 // OK |
|
264 assertNoMessage(iae); |
|
265 } |
|
266 catch (Exception e) |
|
267 { |
|
268 assertTrue(false, "Unknown error: " + e); |
|
269 } |
|
270 } |
|
271 |
|
272 private void testGoodWithBad(LocationListener aListener, int aInterval) |
|
273 { |
|
274 assertGood(aListener, aInterval, LARGE_TIMEOUT, LARGE_MAXAGE); |
|
275 |
|
276 for (int i = 0; i < badTimeout.length; ++i) |
|
277 { |
|
278 assertGood(aListener, aInterval, badTimeout[i], LARGE_MAXAGE); |
|
279 } |
|
280 |
|
281 for (int i = 0; i < badMaxAge.length; ++i) |
|
282 { |
|
283 assertGood(aListener, aInterval, LARGE_TIMEOUT, badMaxAge[i]); |
|
284 } |
|
285 |
|
286 for (int j = 0; j < badTimeout.length; ++j) |
|
287 { |
|
288 for (int k = 0; k < badMaxAge.length; ++k) |
|
289 { |
|
290 assertGood(aListener, aInterval, badTimeout[j], badMaxAge[k]); |
|
291 } |
|
292 } |
|
293 } |
|
294 |
|
295 private void assertGood(LocationListener aListener, int aInterval, |
|
296 int aTimeout, int aMaxAge) |
|
297 { |
|
298 try |
|
299 { |
|
300 iLP.setLocationListener(aListener, aInterval, aTimeout, aMaxAge); |
|
301 } |
|
302 catch (IllegalArgumentException iae) |
|
303 { |
|
304 assertTrue(false, "setLocationListener(" + aListener + "," |
|
305 + aInterval + "," + aTimeout + "," + aMaxAge |
|
306 + ") threw exception:\n" + iae); |
|
307 } |
|
308 |
|
309 // remove listener |
|
310 tearDown(); |
|
311 } |
|
312 |
|
313 private void testInterval(int aInterval, int aNumberOfFixes) |
|
314 { |
|
315 assertTrue(aNumberOfFixes > 0, "Must test with at least one fix!"); |
|
316 |
|
317 final int TIMETOFIX = 100; // 100 ms |
|
318 final int TIMEOUT = 1000 + 2 * TIMETOFIX; |
|
319 final int MAXTIME = 1000 * (aNumberOfFixes * aInterval) + TIMEOUT; |
|
320 |
|
321 Criteria criteria = new Criteria(); |
|
322 criteria.setPreferredResponseTime(TIMETOFIX); |
|
323 |
|
324 providerSetUp(criteria); |
|
325 assertTrue(iLP != null, "LocationProvider is null"); |
|
326 |
|
327 // Start listening for location changes |
|
328 iLP.setLocationListener(this, aInterval, -1, -1); |
|
329 |
|
330 getUpdates(aNumberOfFixes, MAXTIME); |
|
331 |
|
332 checkLocationData(iLocations[0]); |
|
333 // Fixes at interval starts after first fix |
|
334 long startTime = iLocUpdateCallbackTime[0]; |
|
335 long lastTimeOfFix = startTime; |
|
336 |
|
337 for (int i = 1; i < aNumberOfFixes; ++i) |
|
338 { |
|
339 checkLocationData(iLocations[i]); |
|
340 long timeOfFix = iLocUpdateCallbackTime[i]; |
|
341 long timeDiff = timeOfFix - lastTimeOfFix; |
|
342 assertTrue(timeDiff > (aInterval - TIMEOUT) * 1000, |
|
343 "Period difference is less than allowed for fix" + i + ": " |
|
344 + timeDiff); |
|
345 assertTrue(timeDiff < (aInterval + TIMEOUT) * 1000, |
|
346 "Period difference is more than allowed for fix" + i + ": " |
|
347 + timeDiff); |
|
348 assertTrue((timeOfFix - startTime) < ((i + 1) * aInterval * 1000), |
|
349 "Timestamp too late for fix" + i); |
|
350 lastTimeOfFix = timeOfFix; |
|
351 } |
|
352 |
|
353 iLocations = null; |
|
354 } |
|
355 |
|
356 private void getUpdates(int aNumUpdates, int aTimeout) |
|
357 { |
|
358 iLocations = new Location[aNumUpdates]; |
|
359 iLocUpdateCallbackTime = new long[aNumUpdates]; |
|
360 iNumUpdates = 0; |
|
361 |
|
362 // Wait for the first result or timeout |
|
363 boolean timeout = false; |
|
364 long startTime = System.currentTimeMillis(); |
|
365 while (iNumUpdates < aNumUpdates) |
|
366 { |
|
367 // Wait a moment before checking things |
|
368 try |
|
369 { |
|
370 Thread.sleep(250); |
|
371 } |
|
372 catch (InterruptedException ie) |
|
373 { |
|
374 } |
|
375 |
|
376 // Use a safeguard timeout in case the updating thread hangs |
|
377 // or takes too long |
|
378 if (System.currentTimeMillis() - startTime > aTimeout) |
|
379 { |
|
380 echo("Timeout (updates = " + iNumUpdates + ")"); |
|
381 timeout = true; |
|
382 break; |
|
383 } |
|
384 } |
|
385 |
|
386 tearDown(); |
|
387 |
|
388 assertTrue(!timeout, "Got a timeout"); |
|
389 } |
|
390 |
|
391 } |