|
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 package com.nokia.mj.test.lapi; |
|
18 |
|
19 import j2meunit.framework.*; |
|
20 import javax.microedition.location.*; |
|
21 |
|
22 public class GetLocationTest extends TestCase |
|
23 { |
|
24 private Location iLoc; |
|
25 |
|
26 final static int TIMETOFIX = 5; |
|
27 |
|
28 protected LocationProvider iLP = null; |
|
29 |
|
30 long[] iTimeArray; |
|
31 |
|
32 public GetLocationTest() |
|
33 { |
|
34 } |
|
35 |
|
36 public GetLocationTest(String sTestName, TestMethod rTestMethod) |
|
37 { |
|
38 super(sTestName, rTestMethod); |
|
39 } |
|
40 |
|
41 /*************************************************************************** |
|
42 * Creates the test suite. You need to add a new aSuite.addTest antry for |
|
43 * any new test methods, otherwise they won't be run. |
|
44 */ |
|
45 public Test suite() |
|
46 { |
|
47 TestSuite aSuite = new TestSuite(); |
|
48 |
|
49 aSuite.addTest(new GetLocationTest("testBadArguments", |
|
50 new TestMethod() |
|
51 { |
|
52 public void run(TestCase tc) |
|
53 { |
|
54 ((GetLocationTest) tc).testBadArguments(); |
|
55 } |
|
56 })); |
|
57 |
|
58 aSuite.addTest(new GetLocationTest("testGetLocation", new TestMethod() |
|
59 { |
|
60 public void run(TestCase tc) |
|
61 { |
|
62 ((GetLocationTest) tc).testGetLocation(); |
|
63 } |
|
64 })); |
|
65 |
|
66 aSuite.addTest(new GetLocationTest("testAltitudeData", |
|
67 new TestMethod() |
|
68 { |
|
69 public void run(TestCase tc) |
|
70 { |
|
71 ((GetLocationTest) tc).testAltitudeData(); |
|
72 } |
|
73 })); |
|
74 |
|
75 aSuite.addTest(new GetLocationTest("testSpeedData", new TestMethod() |
|
76 { |
|
77 public void run(TestCase tc) |
|
78 { |
|
79 ((GetLocationTest) tc).testSpeedData(); |
|
80 } |
|
81 })); |
|
82 |
|
83 aSuite.addTest(new GetLocationTest("testAddressInfo", new TestMethod() |
|
84 { |
|
85 public void run(TestCase tc) |
|
86 { |
|
87 ((GetLocationTest) tc).testAddressInfo(); |
|
88 } |
|
89 })); |
|
90 |
|
91 aSuite.addTest(new GetLocationTest("testGetLocationTimeout", |
|
92 new TestMethod() |
|
93 { |
|
94 public void run(TestCase tc) |
|
95 { |
|
96 ((GetLocationTest) tc).testGetLocationTimeout(); |
|
97 } |
|
98 })); |
|
99 |
|
100 aSuite.addTest(new GetLocationTest("testGetLatest", new TestMethod() |
|
101 { |
|
102 public void run(TestCase tc) |
|
103 { |
|
104 ((GetLocationTest) tc).testGetLatest(); |
|
105 } |
|
106 })); |
|
107 |
|
108 return aSuite; |
|
109 |
|
110 } |
|
111 |
|
112 private void assertContinue(String aReason, boolean aCond) |
|
113 { |
|
114 if (!aCond) |
|
115 assertTrue(aReason, false); |
|
116 } |
|
117 |
|
118 public void testBadArguments() |
|
119 { |
|
120 try |
|
121 { |
|
122 providerSetUp(null); |
|
123 assertContinue("LocationProvider is null", iLP != null); |
|
124 |
|
125 assertBad(0); |
|
126 assertBad(-2); |
|
127 assertBad(-12345); // Sanity check |
|
128 assertBad(Integer.MIN_VALUE); |
|
129 |
|
130 assertTrue("", true); |
|
131 } |
|
132 catch (Exception e) |
|
133 { |
|
134 assertTrue(e.getMessage(), false); |
|
135 } |
|
136 } |
|
137 |
|
138 public void testGetLocation() |
|
139 { |
|
140 try |
|
141 { |
|
142 providerSetUp(null); |
|
143 assertContinue("LocationProvider is null", iLP != null); |
|
144 simpleGetLocation(); |
|
145 |
|
146 assertTrue("", true); |
|
147 } |
|
148 catch (Exception e) |
|
149 { |
|
150 assertTrue(e.getMessage(), false); |
|
151 } |
|
152 } |
|
153 |
|
154 public void testAltitudeData() |
|
155 { |
|
156 try |
|
157 { |
|
158 Criteria criteria = new Criteria(); |
|
159 criteria.setAltitudeRequired(true); |
|
160 criteria.setPreferredResponseTime(100); // 100 ms |
|
161 |
|
162 providerSetUp(criteria); |
|
163 assertContinue("LocationProvider is null", iLP != null); |
|
164 simpleGetLocation(); |
|
165 |
|
166 QualifiedCoordinates coords = iLoc.getQualifiedCoordinates(); |
|
167 float altitude = coords.getAltitude(); |
|
168 assertContinue("No altitude included", !Float.isNaN(altitude)); |
|
169 assertTrue("", true); |
|
170 } |
|
171 catch (Exception e) |
|
172 { |
|
173 assertTrue(e.getMessage(), false); |
|
174 } |
|
175 } |
|
176 |
|
177 public void testSpeedData() |
|
178 { |
|
179 try |
|
180 { |
|
181 |
|
182 Criteria criteria = new Criteria(); |
|
183 criteria.setPreferredResponseTime(100); // 100 ms |
|
184 criteria.setSpeedAndCourseRequired(true); |
|
185 |
|
186 providerSetUp(criteria); |
|
187 assertContinue("LocationProvider is null", iLP != null); |
|
188 simpleGetLocation(); |
|
189 |
|
190 float speed = iLoc.getSpeed(); |
|
191 assertContinue("No speed included", !Float.isNaN(speed)); |
|
192 float course = iLoc.getCourse(); |
|
193 assertContinue("No course included", !Float.isNaN(course)); |
|
194 |
|
195 assertTrue("", true); |
|
196 } |
|
197 catch (Exception e) |
|
198 { |
|
199 assertTrue(e.getMessage(), false); |
|
200 } |
|
201 } |
|
202 |
|
203 public void testAddressInfo() |
|
204 { |
|
205 try |
|
206 { |
|
207 Criteria criteria = new Criteria(); |
|
208 criteria.setPreferredResponseTime(100); // 100 ms |
|
209 criteria.setAddressInfoRequired(true); |
|
210 |
|
211 providerSetUp(criteria); |
|
212 if (iLP != null) |
|
213 { |
|
214 assertContinue("LocationProvider is null", iLP != null); |
|
215 simpleGetLocation(); |
|
216 |
|
217 AddressInfo addr = iLoc.getAddressInfo(); |
|
218 if (addr != null) |
|
219 { |
|
220 boolean hasNonNullField = false; |
|
221 for (int i = 1; i <= 17; ++i) |
|
222 { |
|
223 String field = addr.getField(i); |
|
224 if (field != null) |
|
225 { |
|
226 hasNonNullField = true; |
|
227 } |
|
228 } |
|
229 assertContinue("All AddressInfo fields are null", |
|
230 hasNonNullField); |
|
231 } |
|
232 } |
|
233 assertTrue("", true); |
|
234 } |
|
235 catch (Exception e) |
|
236 { |
|
237 assertTrue(e.getMessage(), false); |
|
238 } |
|
239 } |
|
240 |
|
241 public void testGetLocationTimeout() |
|
242 { |
|
243 try |
|
244 { |
|
245 Criteria criteria = new Criteria(); |
|
246 criteria.setCostAllowed(false); // This will select the right |
|
247 // provider |
|
248 providerSetUp(criteria); |
|
249 assertContinue("LocationProvider is null", iLP != null); |
|
250 iLoc = null; |
|
251 |
|
252 try |
|
253 { |
|
254 int TIMEOUT = 1; // seconds |
|
255 iLoc = iLP.getLocation(TIMEOUT); |
|
256 assertContinue("No timeout for getLocation", false); |
|
257 } |
|
258 catch (LocationException le) |
|
259 { |
|
260 // Timed out correctly |
|
261 } |
|
262 |
|
263 // Sanity check - test that normal request works ok |
|
264 simpleGetLocation(); |
|
265 |
|
266 try |
|
267 { |
|
268 int TIMEOUT = 10; // seconds |
|
269 long startTime = System.currentTimeMillis(); |
|
270 iLoc = iLP.getLocation(TIMEOUT); |
|
271 long duration = System.currentTimeMillis() - startTime; |
|
272 assertContinue("GetLocation took too long", |
|
273 duration <= TIMEOUT * 1000); |
|
274 checkLocationData(iLoc); |
|
275 } |
|
276 catch (LocationException le) |
|
277 { |
|
278 assertContinue("Illegal timeout", false); |
|
279 } |
|
280 |
|
281 assertTrue("", true); |
|
282 } |
|
283 catch (Exception e) |
|
284 { |
|
285 assertTrue(e.getMessage(), false); |
|
286 } |
|
287 } |
|
288 |
|
289 public void testGetLatest() |
|
290 { |
|
291 iLP = null; |
|
292 iLoc = null; |
|
293 |
|
294 // Get the last known location |
|
295 iLoc = LocationProvider.getLastKnownLocation(); |
|
296 |
|
297 assertTrue("", true); |
|
298 } |
|
299 |
|
300 public void testGetLocationOutOfService() |
|
301 { |
|
302 try |
|
303 { |
|
304 Criteria criteria = new Criteria(); |
|
305 criteria.setCostAllowed(false); // This will select the right |
|
306 // provider |
|
307 providerSetUp(criteria); |
|
308 |
|
309 assertContinue("LocationProvider is null", iLP != null); |
|
310 |
|
311 simpleGetLocation(); |
|
312 |
|
313 // now we get the last known Location: |
|
314 Location lastKnownLocation = LocationProvider |
|
315 .getLastKnownLocation(); |
|
316 checkLocationData(lastKnownLocation); |
|
317 |
|
318 assertContinue("Not the last known location returned", |
|
319 lastKnownLocation.getTimestamp() == iLoc.getTimestamp()); |
|
320 |
|
321 long start = System.currentTimeMillis(); |
|
322 long end = 0; |
|
323 try |
|
324 { |
|
325 iLoc = iLP.getLocation(-1); |
|
326 assertContinue("No exception thrown for getLocation", false); |
|
327 } |
|
328 catch (LocationException le) |
|
329 { |
|
330 // Exception thrown correctly |
|
331 end = System.currentTimeMillis(); |
|
332 } |
|
333 assertContinue("Expected LocationException immediately", end |
|
334 - start < 500); |
|
335 |
|
336 assertTrue("", true); |
|
337 } |
|
338 catch (Exception e) |
|
339 { |
|
340 assertTrue(e.getMessage(), false); |
|
341 } |
|
342 } |
|
343 |
|
344 // ------------------------ Helper methods ----------------------- |
|
345 |
|
346 private void simpleGetLocation() throws Exception |
|
347 { |
|
348 iLoc = null; |
|
349 |
|
350 try |
|
351 { |
|
352 iLoc = iLP.getLocation(-1); |
|
353 checkLocationData(iLoc); |
|
354 } |
|
355 catch (LocationException le) |
|
356 { |
|
357 assertContinue("Could not get location: " + le, false); |
|
358 } |
|
359 } |
|
360 |
|
361 private void assertBad(int aBadParam) throws Exception |
|
362 { |
|
363 try |
|
364 { |
|
365 iLoc = iLP.getLocation(aBadParam); |
|
366 assertContinue("IllegalArgumentException was not thrown for: " |
|
367 + aBadParam, false); |
|
368 } |
|
369 catch (IllegalArgumentException iae) |
|
370 { |
|
371 // Exception thrown correctly |
|
372 } |
|
373 } |
|
374 |
|
375 // ------------------------ Helper class ----------------------- |
|
376 |
|
377 protected void providerSetUp(Criteria aCriteria) |
|
378 { |
|
379 iLP = null; |
|
380 try |
|
381 { |
|
382 iLP = LocationProvider.getInstance(aCriteria); |
|
383 if (iLP != null) |
|
384 { |
|
385 int state = iLP.getState(); |
|
386 assertContinue("Initial state=" + state |
|
387 + ", expected AVAILABLE", |
|
388 state == LocationProvider.AVAILABLE); |
|
389 } |
|
390 } |
|
391 catch (LocationException le) |
|
392 { |
|
393 } |
|
394 } |
|
395 |
|
396 protected void checkLocationData(Location aLoc) |
|
397 { |
|
398 assertContinue("Location is null", aLoc != null); |
|
399 assertContinue("Location is invalid", aLoc.isValid()); |
|
400 assertContinue("Location is valid, but Coordinates are null", aLoc |
|
401 .getQualifiedCoordinates() != null); |
|
402 |
|
403 long timestamp = aLoc.getTimestamp(); |
|
404 long now = System.currentTimeMillis(); |
|
405 assertContinue("Timestamp incorrect: t=" + timestamp + ", now=" + now, |
|
406 now >= timestamp && (now - timestamp < 30000) && timestamp > 0); |
|
407 |
|
408 QualifiedCoordinates coords = aLoc.getQualifiedCoordinates(); |
|
409 double lat = coords.getLatitude(); |
|
410 double lon = coords.getLongitude(); |
|
411 |
|
412 assertContinue("Latitude out of range", lat >= -90.0 || lat <= 90.0); |
|
413 assertContinue("Longitude out of range", lon >= -180.0 || lon < 180.0); |
|
414 |
|
415 float hacc = coords.getHorizontalAccuracy(); |
|
416 assertContinue("Horizontal accuracy is negative", Float.isNaN(hacc) |
|
417 || hacc >= 0); |
|
418 |
|
419 float vacc = coords.getVerticalAccuracy(); |
|
420 assertContinue("Vertical accuracy is negative", Float.isNaN(vacc) |
|
421 || vacc >= 0); |
|
422 |
|
423 float speed = aLoc.getSpeed(); |
|
424 assertContinue("Speed is negative", Float.isNaN(speed) || speed >= 0); |
|
425 |
|
426 float course = aLoc.getCourse(); |
|
427 assertContinue("Course out of range", Float.isNaN(course) |
|
428 || (course >= 0 && course < 360)); |
|
429 |
|
430 String nmea = aLoc.getExtraInfo("application/X-jsr179-location-nmea"); |
|
431 if (nmea != null) |
|
432 { |
|
433 assertTrue("Bad NMEA data", nmea.startsWith("$GP")); |
|
434 } |
|
435 } |
|
436 |
|
437 } |
|
438 |
|
439 // End of file |