1 package com.xtremelabs.robolectric.shadows; 2 3 import android.location.Location; 4 import android.location.LocationManager; 5 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 6 import org.junit.Before; 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 10 import static junit.framework.Assert.assertEquals; 11 import static junit.framework.Assert.assertTrue; 12 import static junit.framework.Assert.assertFalse; 13 14 @RunWith(WithTestDefaultsRunner.class) 15 public class LocationTest { 16 17 private Location location; 18 19 @Before 20 public void setUp() throws Exception { 21 location = new Location(LocationManager.GPS_PROVIDER); 22 location.setTime(1); 23 location.setLatitude(2); 24 location.setLongitude(3); 25 location.setAccuracy(4); 26 location.setBearing(0.5f); 27 location.setSpeed(5.5f); 28 location.setAltitude(3.0d); 29 } 30 31 @Test 32 public void test_copyConstructor() throws Exception { 33 Location copiedLocation = new Location(location); 34 35 assertLocationFieldsFromTestSetup(copiedLocation); 36 } 37 38 @Test 39 public void test_set() throws Exception { 40 Location newLocation = new Location(LocationManager.NETWORK_PROVIDER); 41 newLocation.set(location); 42 43 assertLocationFieldsFromTestSetup(newLocation); 44 } 45 46 @Test 47 public void removeFieldShouldReportHasFieldAsFalse() 48 { 49 assertTrue(location.hasAccuracy()); 50 location.removeAccuracy(); 51 assertFalse(location.hasAccuracy()); 52 53 assertTrue(location.hasBearing()); 54 location.removeBearing(); 55 assertFalse(location.hasBearing()); 56 57 assertTrue(location.hasAltitude()); 58 location.removeAltitude(); 59 assertFalse(location.hasAltitude()); 60 61 assertTrue(location.hasSpeed()); 62 location.removeSpeed(); 63 assertFalse(location.hasSpeed()); 64 } 65 66 @Test 67 public void defaultLocationShouldNotReportFieldsAsAvailable() 68 { 69 Location defaultLocation = new Location(LocationManager.GPS_PROVIDER); 70 assertFalse(defaultLocation.hasAccuracy()); 71 assertFalse(defaultLocation.hasBearing()); 72 assertFalse(defaultLocation.hasAltitude()); 73 assertFalse(defaultLocation.hasSpeed()); 74 75 assertEquals(0.0d, defaultLocation.getLatitude()); 76 assertEquals(0.0d, defaultLocation.getLongitude()); 77 assertEquals(0.0f, defaultLocation.getAccuracy()); 78 assertEquals(0.0f, defaultLocation.getBearing()); 79 assertEquals(0.0f, defaultLocation.getSpeed()); 80 assertEquals(0.0d, defaultLocation.getAltitude()); 81 } 82 83 @Test 84 public void settingFieldShouldMakeHasFieldReturnTrue() 85 { 86 Location l = new Location(LocationManager.GPS_PROVIDER); 87 assertFalse(l.hasAccuracy()); 88 l.setAccuracy(0.5f); 89 assertTrue(l.hasAccuracy()); 90 91 assertFalse(l.hasBearing()); 92 l.setBearing(1); 93 assertTrue(l.hasBearing()); 94 95 assertFalse(l.hasAltitude()); 96 l.setAltitude(1); 97 assertTrue(l.hasAltitude()); 98 99 assertFalse(l.hasSpeed()); 100 l.setSpeed(5); 101 assertTrue(l.hasSpeed()); 102 } 103 104 private void assertLocationFieldsFromTestSetup(Location l) { 105 assertEquals(1, l.getTime()); 106 assertEquals(2.0, l.getLatitude()); 107 assertEquals(3.0, l.getLongitude()); 108 assertEquals(4f, l.getAccuracy()); 109 assertEquals(0.5f, l.getBearing()); 110 assertEquals(5.5f, l.getSpeed()); 111 assertEquals(3.0d, l.getAltitude()); 112 assertEquals(LocationManager.GPS_PROVIDER, l.getProvider()); 113 114 assertTrue(l.hasAltitude()); 115 assertTrue(l.hasAccuracy()); 116 assertTrue(l.hasBearing()); 117 assertTrue(l.hasSpeed()); 118 119 assertEquals(location, l); 120 } 121 } 122