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.os.Parcel; 20 import android.test.suitebuilder.annotation.SmallTest; 21 22 import junit.framework.TestCase; 23 24 /** 25 * Unit tests for android.location.Location 26 */ 27 @SmallTest 28 public class LocationTest extends TestCase { 29 30 // ***** Tests for Location.convert 31 public void testConvert_DegreesToDouble(){ 32 String testDegreesCoord = "-80.075"; 33 String message; 34 double result; 35 36 result = Location.convert(testDegreesCoord); 37 message = "degreesToDoubleTest: Double should be -80.075, actual value is " + 38 String.valueOf(result); 39 assertEquals(message, -80.075, result); 40 } 41 42 public void testConvert_MinutesToDouble(){ 43 String testMinutesCoord = "-80:05.10000"; 44 String message; 45 double result; 46 47 result = Location.convert(testMinutesCoord); 48 message = "minutesToDoubleTest: Double should be -80.085, actual value is " + 49 String.valueOf(result); 50 assertEquals(message, -80.085, result); 51 } 52 53 public void testConvert_SecondsToDouble(){ 54 String testSecondsCoord = "-80:04:03.00000"; 55 String message; 56 double result; 57 58 result = Location.convert(testSecondsCoord); 59 message = "secondsToDoubleTest: Double should be -80.0675, actual value is " + 60 String.valueOf(result); 61 assertEquals(message, -80.0675, result); 62 } 63 64 public void testConvert_SecondsToDouble2(){ 65 String testSecondsCoord = "-80:4:3"; 66 String message; 67 double result; 68 69 result = Location.convert(testSecondsCoord); 70 message = "secondsToDouble2Test: Double should be -80.0675, actual value is " + 71 String.valueOf(result); 72 assertEquals(message, -80.0675, result); 73 } 74 75 // Testing the Convert(Double, Int) 76 public void testConvert_CoordinateToDegrees(){ 77 String message; 78 String result; 79 80 result = Location.convert(-80.075, Location.FORMAT_DEGREES); 81 message = "coordinateToDegreesTest: Should return a string -80.075, but returned " + result; 82 assertEquals(message, "-80.075", result); 83 } 84 85 public void testConvert_CoordinateToDegrees2(){ 86 String message; 87 String result; 88 result = Location.convert(-80.0, Location.FORMAT_DEGREES); 89 message = "coordinateToDegrees2Test: Should return a string -80, but returned " + result; 90 assertEquals(message, "-80", result); 91 } 92 93 public void testConvert_CoordinateToMinutes(){ 94 String message; 95 String result; 96 double input = -80.085; 97 result = Location.convert(input, Location.FORMAT_MINUTES); 98 message = "coordinateToMinuteTest: Should return a string -80:5.1, but returned " + 99 result; 100 assertEquals(message, "-80:5.1", result); 101 } 102 103 public void testConvert_CoordinateToMinutes2(){ 104 String message; 105 String result; 106 double input = -80; 107 result = Location.convert(input, Location.FORMAT_MINUTES); 108 message = "coordinateToMinute2Test: Should return a string -80:0, but returned " + 109 result; 110 assertEquals(message, "-80:0", result); 111 } 112 113 public void testConvert_CoordinateToSeconds(){ 114 String message; 115 String result; 116 117 result = Location.convert(-80.075, Location.FORMAT_SECONDS); 118 message = "coordinateToSecondsTest: Should return a string -80:4:30, but returned " + 119 result; 120 assertEquals(message, "-80:4:30", result); 121 } 122 // **** end tests for Location.convert 123 124 125 public void testBearingTo(){ 126 String message; 127 float bearing; 128 Location zeroLocation = new Location(""); 129 zeroLocation.setLatitude(0); 130 zeroLocation.setLongitude(0); 131 132 Location testLocation = new Location(""); 133 testLocation.setLatitude(1000000); 134 testLocation.setLongitude(0); 135 136 bearing = zeroLocation.bearingTo(zeroLocation); 137 message = "bearingToTest: Bearing should be 0, actual value is " + String.valueOf(bearing); 138 assertEquals(message, 0, bearing, 0); 139 140 bearing = zeroLocation.bearingTo(testLocation); 141 message = "bearingToTest: Bearing should be 180, actual value is " + 142 String.valueOf(bearing); 143 assertEquals(message, 180, bearing, 0); 144 145 testLocation.setLatitude(0); 146 testLocation.setLongitude(1000000); 147 bearing = zeroLocation.bearingTo(testLocation); 148 message = "bearingToTest: Bearing should be -90, actual value is " + 149 String.valueOf(bearing); 150 assertEquals(message, -90, bearing, 0); 151 152 //TODO: Test a Random Middle Value 153 } 154 155 public void testDistanceTo() { 156 String message; 157 boolean result = true; 158 float distance; 159 Location zeroLocation = new Location(""); 160 zeroLocation.setLatitude(0); 161 zeroLocation.setLongitude(0); 162 163 Location testLocation = new Location(""); 164 testLocation.setLatitude(1000000); 165 testLocation.setLongitude(0); 166 167 distance = zeroLocation.distanceTo(zeroLocation); 168 message = "distanceToTest: Distance should be 0, actual value is " + 169 String.valueOf(distance); 170 assertEquals(message, distance, 0, 0); 171 172 distance = zeroLocation.distanceTo(testLocation); 173 message = "distanceToTest: Distance should be 8885140, actual value is " + 174 String.valueOf(distance); 175 assertEquals(message, distance, 8885140.0, 1); 176 } 177 178 public void testAltitude() { 179 String message; 180 Location loc = new Location(""); 181 182 loc.setAltitude(1); 183 message = "altitudeTest: set/getAltitude to 1 didn't work."; 184 assertEquals(message, loc.getAltitude(), 1, 0); 185 message = "altitudeTest: hasAltitude (a) didn't work."; 186 assertTrue(message, loc.hasAltitude()); 187 188 loc.removeAltitude(); 189 message = "altitudeTest: hasAltitude (b) didn't work."; 190 assertFalse(message, loc.hasAltitude()); 191 message = "altitudeTest: getAltitude didn't return 0 when there was no altitude."; 192 assertEquals(message, loc.getAltitude(), 0, 0); 193 } 194 195 public void testSpeed() { 196 String message; 197 Location loc = new Location(""); 198 199 loc.setSpeed(1); 200 message = "speedTest: set/getSpeed to 1 didn't work."; 201 assertEquals(message, loc.getSpeed(), 1, 0); 202 message = "speedTest: hasSpeed (a) didn't work."; 203 assertTrue(message, loc.hasSpeed()); 204 205 loc.removeSpeed(); 206 message = "speedTest: hasSpeed (b) didn't work."; 207 assertFalse(message, loc.hasSpeed()); 208 message = "speedTest: getSpeed didn't return 0 when there was no speed."; 209 assertEquals(message, loc.getSpeed(), 0, 0); 210 } 211 212 public void testBearing() { 213 String message; 214 Location loc = new Location(""); 215 216 loc.setBearing(1); 217 message = "bearingTest: set/getBearing to 1 didn't work."; 218 assertEquals(message, loc.getBearing(), 1, 0); 219 message = "bearingTest: hasBearing (a) didn't work."; 220 assertTrue(message, loc.hasBearing()); 221 222 loc.removeBearing(); 223 message = "bearingTest: hasBearing (b) didn't work."; 224 assertFalse(message, loc.hasBearing()); 225 message = "bearingTest: getBearing didn't return 0 when there was no bearing."; 226 assertEquals(message, loc.getBearing(), 0, 0); 227 } 228 229 public void testParcel() { 230 final double expectedLat = 33; 231 final double expectedLon = -122; 232 final float expectedAccuracy = 15; 233 final float expectedSpeed = 5; 234 Location loc = new Location("test"); 235 loc.setLatitude(expectedLat); 236 loc.setLongitude(expectedLon); 237 loc.setAccuracy(expectedAccuracy); 238 loc.setSpeed(expectedSpeed); 239 240 // Serialize location object into bytes via parcelable capability 241 Parcel parcel = Parcel.obtain(); 242 loc.writeToParcel(parcel, 0); 243 byte[] rawBytes = parcel.marshall(); 244 parcel.recycle(); 245 246 // Turn the bytes back into a location object 247 parcel = Parcel.obtain(); 248 parcel.unmarshall(rawBytes, 0, rawBytes.length); 249 parcel.setDataPosition(0); 250 Location deserialized = Location.CREATOR.createFromParcel(parcel); 251 parcel.recycle(); 252 253 assertEquals(expectedLat, deserialized.getLatitude()); 254 assertEquals(expectedLon, deserialized.getLongitude()); 255 assertEquals(expectedAccuracy, deserialized.getAccuracy()); 256 assertTrue(deserialized.hasAccuracy()); 257 assertEquals(expectedSpeed, deserialized.getSpeed()); 258 assertTrue(deserialized.hasSpeed()); 259 assertFalse(deserialized.hasBearing()); 260 assertFalse(deserialized.hasAltitude()); 261 assertFalse(deserialized.isFromMockProvider()); 262 } 263 } 264 265 266