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 java.util.Enumeration; |
|
18 |
|
19 import javax.microedition.location.*; |
|
20 import javax.microedition.lcdui.*; |
|
21 |
|
22 public class ViperUnitTest |
|
23 { |
|
24 final static int TIMETOFIX = 5; |
|
25 |
|
26 protected LocationProvider iLP = null; |
|
27 |
|
28 final protected String iTestSuite; |
|
29 |
|
30 protected String iTestName; |
|
31 |
|
32 long[] iTimeArray; |
|
33 |
|
34 private static Display iDisplay; |
|
35 |
|
36 ViperUnitTest() |
|
37 { |
|
38 iTestSuite = "Test (no name)"; |
|
39 } |
|
40 |
|
41 protected ViperUnitTest(String aTestSuite) |
|
42 { |
|
43 iTestSuite = aTestSuite; |
|
44 } |
|
45 |
|
46 static String run1() |
|
47 { |
|
48 ViperUnitTest testRunner = new ViperUnitTest(); |
|
49 String title = "Running fast unit tests"; |
|
50 ViperUnitTest[] tests = new ViperUnitTest[] { new CoordinatesTest(), |
|
51 new QualifiedCoordinatesTest(), new AddressInfoTest(), |
|
52 new CriteriaTest(), new LandmarkTest(), |
|
53 new LandmarkStoreTest(), new MultipleLandmarkStoresTest(), |
|
54 new ErrorsTest() |
|
55 }; |
|
56 |
|
57 return testRunner.run(tests, title); |
|
58 } |
|
59 |
|
60 static String run2(Display aDisplay) |
|
61 { |
|
62 iDisplay = aDisplay; |
|
63 ViperUnitTest testRunner = new ViperUnitTest(); |
|
64 String title = "Running LocationProvider unit tests"; |
|
65 ViperUnitTest[] tests = new ViperUnitTest[] { new GetLocationTest(), |
|
66 new PeriodicUpdateTest() |
|
67 }; |
|
68 |
|
69 return testRunner.run(tests, title); |
|
70 } |
|
71 |
|
72 String run(ViperUnitTest[] tests, String title) |
|
73 { |
|
74 echo(title); |
|
75 String result = null; |
|
76 int current = 0; |
|
77 iTimeArray = new long[tests.length]; |
|
78 long startTime = System.currentTimeMillis(); |
|
79 |
|
80 try |
|
81 { |
|
82 for (current = 0; current < tests.length; ++current) |
|
83 { |
|
84 tests[current].runTest(); |
|
85 iTimeArray[current] = System.currentTimeMillis(); |
|
86 } |
|
87 |
|
88 echo("\n-= Time info =-"); |
|
89 long testStartTime = startTime; |
|
90 for (int i = 0; i < tests.length; ++i) |
|
91 { |
|
92 long time = iTimeArray[i] - testStartTime; |
|
93 echo(tests[i].iTestSuite + ": " + time + " ms"); |
|
94 testStartTime = iTimeArray[i]; |
|
95 } |
|
96 echo("---------------"); |
|
97 echo("Total = " + (iTimeArray[tests.length - 1] - startTime) + "ms"); |
|
98 echo("---------------\n"); |
|
99 |
|
100 result = "All tests completed successfully"; |
|
101 } |
|
102 catch (Throwable t) |
|
103 { |
|
104 echo("\n\n##### TEST FAILED #####\n\n"); |
|
105 ViperUnitTest tst = tests[current]; |
|
106 result = tst.iTestSuite + "." + tst.iTestName + " failed!\n" + t; |
|
107 } |
|
108 echo(result); |
|
109 return result; |
|
110 } |
|
111 |
|
112 protected void runTest() throws Throwable |
|
113 { |
|
114 } |
|
115 |
|
116 // ------------------------ Helper methods ----------------------- |
|
117 |
|
118 // Unit test helper methods |
|
119 |
|
120 protected void assertTrue(boolean aCondition, String aErrorMessage) |
|
121 throws Error |
|
122 { |
|
123 if (!aCondition) |
|
124 throw new Error(aErrorMessage); |
|
125 } |
|
126 |
|
127 protected void assertFalse(boolean aCondition, String aErrorMessage) |
|
128 throws Error |
|
129 { |
|
130 if (aCondition) |
|
131 throw new Error(aErrorMessage); |
|
132 } |
|
133 |
|
134 protected void assertNoMessage(Exception e) |
|
135 { |
|
136 assertTrue(e.getMessage() == null, |
|
137 "Message not allowed for exception: " + e); |
|
138 } |
|
139 |
|
140 protected void setCurrentTest(String aTestName) |
|
141 { |
|
142 iTestName = aTestName; |
|
143 System.out.println("Running: " + aTestName); |
|
144 } |
|
145 |
|
146 protected void echo(String aStr) |
|
147 { |
|
148 System.out.println(aStr); |
|
149 } |
|
150 |
|
151 protected String diff(String s1, String s2) |
|
152 { |
|
153 if (s1.length() != s2.length()) |
|
154 { |
|
155 return "Different length"; |
|
156 } |
|
157 |
|
158 char[] chs = new char[s1.length()]; |
|
159 |
|
160 for (int i = 0; i < s1.length(); i++) |
|
161 { |
|
162 if (s1.charAt(i) != s2.charAt(i)) |
|
163 { |
|
164 chs[i] = '^'; |
|
165 } |
|
166 else |
|
167 { |
|
168 chs[i] = ' '; |
|
169 } |
|
170 } |
|
171 |
|
172 return new String(chs); |
|
173 } |
|
174 |
|
175 protected void userMessage(String aStr) |
|
176 { |
|
177 if (iDisplay == null) |
|
178 { |
|
179 throw new Error("userMessage: No display set"); |
|
180 } |
|
181 Alert a = new Alert(iTestName, aStr, null, AlertType.INFO); |
|
182 |
|
183 CommandListener cl = new CommandListener() |
|
184 { |
|
185 public void commandAction(Command c, Displayable d) |
|
186 { |
|
187 synchronized (this) |
|
188 { |
|
189 notifyAll(); |
|
190 } |
|
191 } |
|
192 }; |
|
193 |
|
194 a.setCommandListener(cl); |
|
195 a.setTimeout(Alert.FOREVER); |
|
196 |
|
197 Displayable d = iDisplay.getCurrent(); |
|
198 iDisplay.setCurrent(a); |
|
199 try |
|
200 { |
|
201 synchronized (cl) |
|
202 { |
|
203 cl.wait(); |
|
204 } |
|
205 } |
|
206 catch (InterruptedException ie) |
|
207 { |
|
208 } |
|
209 iDisplay.setCurrent(d); |
|
210 } |
|
211 |
|
212 // Location API specific helper methods |
|
213 |
|
214 protected void providerSetUp(Criteria aCriteria) |
|
215 { |
|
216 iLP = null; |
|
217 try |
|
218 { |
|
219 iLP = LocationProvider.getInstance(aCriteria); |
|
220 // if (aCriteria == null) { |
|
221 // echo("Default provider: " + iLP); |
|
222 // } else { |
|
223 // echo("Selected provider: " + iLP); |
|
224 // } |
|
225 if (iLP != null) |
|
226 { |
|
227 int state = iLP.getState(); |
|
228 assertTrue(state == LocationProvider.AVAILABLE, |
|
229 "Initial state=" + state + ", expected AVAILABLE"); |
|
230 } |
|
231 } |
|
232 catch (LocationException le) |
|
233 { |
|
234 echo("Could not create location provider: " + le); |
|
235 } |
|
236 } |
|
237 |
|
238 protected void checkLocationData(Location aLoc) |
|
239 { |
|
240 assertTrue(aLoc != null, "Location is null"); |
|
241 assertTrue(aLoc.isValid(), "Location is invalid"); |
|
242 assertTrue(aLoc.getQualifiedCoordinates() != null, |
|
243 "Location is valid, but Coordinates are null"); |
|
244 |
|
245 long timestamp = aLoc.getTimestamp(); |
|
246 long now = System.currentTimeMillis(); |
|
247 assertTrue(now >= timestamp && (now - timestamp < 30000) |
|
248 && timestamp > 0, "Timestamp incorrect: t=" + timestamp |
|
249 + ", now=" + now); |
|
250 |
|
251 QualifiedCoordinates coords = aLoc.getQualifiedCoordinates(); |
|
252 double lat = coords.getLatitude(); |
|
253 double lon = coords.getLongitude(); |
|
254 |
|
255 // echo("Lat: " + lat + " Lon: " + lon); |
|
256 |
|
257 assertTrue(lat >= -90.0 || lat <= 90.0, "Latitude out of range"); |
|
258 assertTrue(lon >= -180.0 || lon < 180.0, "Longitude out of range"); |
|
259 |
|
260 float hacc = coords.getHorizontalAccuracy(); |
|
261 assertTrue(Float.isNaN(hacc) || hacc >= 0, |
|
262 "Horizontal accuracy is negative"); |
|
263 |
|
264 float vacc = coords.getVerticalAccuracy(); |
|
265 assertTrue(Float.isNaN(vacc) || vacc >= 0, |
|
266 "Vertical accuracy is negative"); |
|
267 |
|
268 float speed = aLoc.getSpeed(); |
|
269 assertTrue(Float.isNaN(speed) || speed >= 0, "Speed is negative"); |
|
270 |
|
271 float course = aLoc.getCourse(); |
|
272 assertTrue(Float.isNaN(course) || (course >= 0 && course < 360), |
|
273 "Course out of range"); |
|
274 |
|
275 String nmea = aLoc.getExtraInfo("application/X-jsr179-location-nmea"); |
|
276 if (nmea != null) |
|
277 { |
|
278 assertTrue(nmea.startsWith("$GP"), "Bad NMEA data"); |
|
279 echo("Extra info:\n" + nmea); |
|
280 } |
|
281 } |
|
282 |
|
283 protected void addLandmarkToStore(LandmarkStore ls, Landmark landmark, |
|
284 String category) throws Exception |
|
285 { |
|
286 |
|
287 Enumeration e = ls.getLandmarks(); |
|
288 int numLandmarksBefore = 0; |
|
289 if (e != null) |
|
290 { |
|
291 while (e.hasMoreElements()) |
|
292 { |
|
293 Object o = e.nextElement(); |
|
294 ++numLandmarksBefore; |
|
295 } |
|
296 } |
|
297 |
|
298 ls.addLandmark(landmark, category); |
|
299 |
|
300 // check that landmark was added |
|
301 e = ls.getLandmarks(); |
|
302 assertTrue(e != null, "Landmarks enumeration is null"); |
|
303 |
|
304 int numLandmarksAfter = 0; |
|
305 while (e.hasMoreElements()) |
|
306 { |
|
307 ++numLandmarksAfter; |
|
308 Object o = e.nextElement(); |
|
309 } |
|
310 |
|
311 assertTrue(numLandmarksAfter - numLandmarksBefore == 1, |
|
312 "Expected only one landmark to be added"); |
|
313 } |
|
314 |
|
315 protected void removeExistingStores() throws Exception |
|
316 { |
|
317 String[] stores = LandmarkStore.listLandmarkStores(); |
|
318 if (stores != null) |
|
319 { |
|
320 for (int i = 0; i < stores.length; ++i) |
|
321 { |
|
322 LandmarkStore.deleteLandmarkStore(stores[i]); |
|
323 } |
|
324 } |
|
325 } |
|
326 |
|
327 protected void deleteAllLandmarksAndCategories() throws Exception |
|
328 { |
|
329 // Delete all the categories and Landmarks from the store |
|
330 LandmarkStore ls = LandmarkStore.getInstance(null); |
|
331 Enumeration c = ls.getCategories(); |
|
332 while (c.hasMoreElements()) |
|
333 { |
|
334 ls.deleteCategory((String) c.nextElement()); |
|
335 } |
|
336 |
|
337 Enumeration l = ls.getLandmarks(); |
|
338 if (l != null) |
|
339 { |
|
340 while (l.hasMoreElements()) |
|
341 { |
|
342 ls.deleteLandmark((Landmark) l.nextElement()); |
|
343 } |
|
344 } |
|
345 } |
|
346 |
|
347 } |
|