• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.location;
18 
19 import android.test.suitebuilder.annotation.SmallTest;
20 
21 import junit.framework.TestCase;
22 
23 /**
24  * Unit tests for android.location.Location
25  */
26 @SmallTest
27 public class LocationTest extends TestCase {
28 
29     // ***** Tests for Location.convert
testConvert_DegreesToDouble()30     public void testConvert_DegreesToDouble(){
31         String testDegreesCoord = "-80.075";
32         String message;
33         double result;
34 
35         result = Location.convert(testDegreesCoord);
36         message = "degreesToDoubleTest: Double should be -80.075, actual value is " +
37                 String.valueOf(result);
38         assertEquals(message, -80.075, result);
39     }
40 
testConvert_MinutesToDouble()41     public void testConvert_MinutesToDouble(){
42         String testMinutesCoord = "-80:05.10000";
43         String message;
44         double result;
45 
46         result = Location.convert(testMinutesCoord);
47         message = "minutesToDoubleTest: Double should be -80.085, actual value is " +
48                 String.valueOf(result);
49         assertEquals(message, -80.085, result);
50     }
51 
testConvert_SecondsToDouble()52     public void testConvert_SecondsToDouble(){
53         String testSecondsCoord = "-80:04:03.00000";
54         String message;
55         double result;
56 
57         result = Location.convert(testSecondsCoord);
58         message = "secondsToDoubleTest: Double should be -80.0675, actual value is " +
59                 String.valueOf(result);
60         assertEquals(message, -80.0675, result);
61     }
62 
testConvert_SecondsToDouble2()63     public void testConvert_SecondsToDouble2(){
64         String testSecondsCoord = "-80:4:3";
65         String message;
66         double result;
67 
68         result = Location.convert(testSecondsCoord);
69         message = "secondsToDouble2Test: Double should be -80.0675, actual value is " +
70                 String.valueOf(result);
71         assertEquals(message, -80.0675, result);
72     }
73 
74     // Testing the Convert(Double, Int)
testConvert_CoordinateToDegrees()75     public void testConvert_CoordinateToDegrees(){
76         String message;
77         String result;
78 
79         result = Location.convert(-80.075, Location.FORMAT_DEGREES);
80         message = "coordinateToDegreesTest: Should return a string -80.075, but returned " + result;
81         assertEquals(message, "-80.075", result);
82     }
83 
testConvert_CoordinateToDegrees2()84     public void testConvert_CoordinateToDegrees2(){
85         String message;
86         String result;
87         result = Location.convert(-80.0, Location.FORMAT_DEGREES);
88         message = "coordinateToDegrees2Test: Should return a string -80, but returned " + result;
89         assertEquals(message, "-80", result);
90     }
91 
testConvert_CoordinateToMinutes()92     public void testConvert_CoordinateToMinutes(){
93         String message;
94         String result;
95         double input = -80.085;
96         result = Location.convert(input, Location.FORMAT_MINUTES);
97         message = "coordinateToMinuteTest: Should return a string -80:5.1, but returned " +
98                 result;
99         assertEquals(message, "-80:5.1", result);
100     }
101 
testConvert_CoordinateToMinutes2()102     public void testConvert_CoordinateToMinutes2(){
103         String message;
104         String result;
105         double input = -80;
106         result = Location.convert(input, Location.FORMAT_MINUTES);
107         message = "coordinateToMinute2Test: Should return a string -80:0, but returned " +
108                 result;
109         assertEquals(message, "-80:0", result);
110     }
111 
testConvert_CoordinateToSeconds()112     public void testConvert_CoordinateToSeconds(){
113         String message;
114         String result;
115 
116         result = Location.convert(-80.075, Location.FORMAT_SECONDS);
117         message = "coordinateToSecondsTest: Should return a string -80:4:30, but returned " +
118                 result;
119         assertEquals(message, "-80:4:30", result);
120     }
121     // **** end tests for Location.convert
122 
123 
testBearingTo()124     public void testBearingTo(){
125         String message;
126         float bearing;
127         Location zeroLocation = new Location("");
128         zeroLocation.setLatitude(0);
129         zeroLocation.setLongitude(0);
130 
131         Location testLocation = new Location("");
132         testLocation.setLatitude(1000000);
133         testLocation.setLongitude(0);
134 
135         bearing = zeroLocation.bearingTo(zeroLocation);
136         message = "bearingToTest: Bearing should be 0, actual value is " + String.valueOf(bearing);
137         assertEquals(message, 0, bearing, 0);
138 
139         bearing = zeroLocation.bearingTo(testLocation);
140         message = "bearingToTest: Bearing should be 180, actual value is " +
141                 String.valueOf(bearing);
142         assertEquals(message, 180, bearing, 0);
143 
144         testLocation.setLatitude(0);
145         testLocation.setLongitude(1000000);
146         bearing = zeroLocation.bearingTo(testLocation);
147         message = "bearingToTest: Bearing should be -90, actual value is " +
148                 String.valueOf(bearing);
149         assertEquals(message, -90, bearing, 0);
150 
151         //TODO: Test a Random Middle Value
152     }
153 
testDistanceTo()154     public void testDistanceTo() {
155         String message;
156         boolean result = true;
157         float distance;
158         Location zeroLocation = new Location("");
159         zeroLocation.setLatitude(0);
160         zeroLocation.setLongitude(0);
161 
162         Location testLocation = new Location("");
163         testLocation.setLatitude(1000000);
164         testLocation.setLongitude(0);
165 
166         distance = zeroLocation.distanceTo(zeroLocation);
167         message = "distanceToTest: Distance should be 0, actual value is " +
168         String.valueOf(distance);
169         assertEquals(message, distance, 0, 0);
170 
171         distance = zeroLocation.distanceTo(testLocation);
172         message = "distanceToTest: Distance should be 8885140, actual value is " +
173         String.valueOf(distance);
174         assertEquals(message, distance, 8885140.0, 1);
175     }
176 
testAltitude()177     public void testAltitude() {
178         String message;
179         Location loc = new Location("");
180 
181         loc.setAltitude(1);
182         message = "altitudeTest: set/getAltitude to 1 didn't work.";
183         assertEquals(message, loc.getAltitude(), 1, 0);
184         message = "altitudeTest: hasAltitude (a) didn't work.";
185         assertTrue(message, loc.hasAltitude());
186 
187         loc.removeAltitude();
188         message = "altitudeTest: hasAltitude (b) didn't work.";
189         assertFalse(message, loc.hasAltitude());
190         message = "altitudeTest: getAltitude didn't return 0 when there was no altitude.";
191         assertEquals(message, loc.getAltitude(), 0, 0);
192     }
193 
testSpeed()194     public void testSpeed() {
195         String message;
196         Location loc = new Location("");
197 
198         loc.setSpeed(1);
199         message = "speedTest: set/getSpeed to 1 didn't work.";
200         assertEquals(message, loc.getSpeed(), 1, 0);
201         message = "speedTest: hasSpeed (a) didn't work.";
202         assertTrue(message, loc.hasSpeed());
203 
204         loc.removeSpeed();
205         message = "speedTest: hasSpeed (b) didn't work.";
206         assertFalse(message, loc.hasSpeed());
207         message = "speedTest: getSpeed didn't return 0 when there was no speed.";
208         assertEquals(message, loc.getSpeed(), 0, 0);
209     }
210 
testBearing()211     public void testBearing() {
212         String message;
213         Location loc = new Location("");
214 
215         loc.setBearing(1);
216         message = "bearingTest: set/getBearing to 1 didn't work.";
217         assertEquals(message, loc.getBearing(), 1, 0);
218         message = "bearingTest: hasBearing (a) didn't work.";
219         assertTrue(message, loc.hasBearing());
220 
221         loc.removeBearing();
222         message = "bearingTest: hasBearing (b) didn't work.";
223         assertFalse(message, loc.hasBearing());
224         message = "bearingTest: getBearing didn't return 0 when there was no bearing.";
225         assertEquals(message, loc.getBearing(), 0, 0);
226     }
227 
228 }
229 
230 
231