Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.app.Activity;
      4 import android.location.Address;
      5 import android.location.Geocoder;
      6 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
      7 import org.junit.Before;
      8 import org.junit.Test;
      9 import org.junit.runner.RunWith;
     10 
     11 import java.util.List;
     12 
     13 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
     14 import static junit.framework.Assert.assertFalse;
     15 import static org.junit.Assert.assertEquals;
     16 import static org.junit.Assert.assertTrue;
     17 
     18 @RunWith(WithTestDefaultsRunner.class)
     19 public class GeocoderTest {
     20 
     21     private Geocoder geocoder;
     22 
     23     @Before
     24     public void setUp() throws Exception {
     25         geocoder = new Geocoder(new Activity());
     26     }
     27 
     28     @Test
     29     public void shouldRecordLastLocationName() throws Exception {
     30         geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1);
     31         String lastLocationName = shadowOf(geocoder).getLastLocationName();
     32 
     33         assertEquals("731 Market St, San Francisco, CA 94103", lastLocationName);
     34     }
     35 
     36     @Test
     37     public void setsUpHasLocationInAddressFromLocationName() throws Exception {
     38         shadowOf(geocoder).setSimulatedHasLatLong(true, true);
     39         Address address = geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1).get(0);
     40         assertTrue(address.hasLatitude());
     41         assertTrue(address.hasLongitude());
     42         shadowOf(geocoder).setSimulatedHasLatLong(false, false);
     43         address = geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1).get(0);
     44         assertFalse(address.hasLatitude());
     45         assertFalse(address.hasLongitude());
     46     }
     47 
     48     @Test
     49     public void canReturnNoAddressesOnRequest() throws Exception {
     50         shadowOf(geocoder).setReturnNoResults(true);
     51         List<Address> result = geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1);
     52         assertEquals(0, result.size());
     53     }
     54 
     55 
     56     @Test
     57     public void answersWhetherResolutionHappened() throws Exception {
     58         assertFalse(shadowOf(geocoder).didResolution());
     59         shadowOf(geocoder).setReturnNoResults(true);
     60         geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1);
     61         assertTrue(shadowOf(geocoder).didResolution());
     62     }
     63 }
     64