Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.location.LocationManager.GPS_PROVIDER;
      4 import static android.location.LocationManager.NETWORK_PROVIDER;
      5 import static org.assertj.core.api.Assertions.assertThat;
      6 import static org.junit.Assert.assertEquals;
      7 import static org.junit.Assert.assertFalse;
      8 import static org.junit.Assert.assertNull;
      9 import static org.junit.Assert.assertSame;
     10 import static org.junit.Assert.assertTrue;
     11 import static org.robolectric.Shadows.shadowOf;
     12 
     13 import android.app.PendingIntent;
     14 import android.content.Context;
     15 import android.content.Intent;
     16 import android.location.Criteria;
     17 import android.location.GpsStatus.Listener;
     18 import android.location.Location;
     19 import android.location.LocationListener;
     20 import android.location.LocationManager;
     21 import android.os.Bundle;
     22 import java.util.ArrayList;
     23 import java.util.HashMap;
     24 import java.util.HashSet;
     25 import java.util.List;
     26 import java.util.Map;
     27 import java.util.Set;
     28 import org.junit.Assert;
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.robolectric.RobolectricTestRunner;
     33 import org.robolectric.RuntimeEnvironment;
     34 
     35 @RunWith(RobolectricTestRunner.class)
     36 public class ShadowLocationManagerTest {
     37   private LocationManager locationManager;
     38   private ShadowLocationManager shadowLocationManager;
     39 
     40   @Before
     41   public void setUp() {
     42     locationManager = (LocationManager) RuntimeEnvironment.application.getSystemService(Context.LOCATION_SERVICE);
     43     shadowLocationManager = shadowOf(locationManager);
     44   }
     45 
     46   @Test
     47   public void shouldReturnNoProviderEnabledByDefault() {
     48     Boolean enabled = locationManager.isProviderEnabled(GPS_PROVIDER);
     49     assertFalse(enabled);
     50     enabled = locationManager.isProviderEnabled(NETWORK_PROVIDER);
     51     assertFalse(enabled);
     52     enabled = locationManager.isProviderEnabled("RANDOM_PROVIDER");
     53     assertFalse(enabled);
     54   }
     55 
     56   @Test
     57   public void shouldDisableProvider() {
     58     // No provider is enabled by default, so it must be manually enabled
     59     shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true);
     60     shadowLocationManager.setProviderEnabled(GPS_PROVIDER, false);
     61     assertFalse(locationManager.isProviderEnabled(GPS_PROVIDER));
     62   }
     63 
     64   @Test
     65   public void shouldHaveListenerOnceAdded() {
     66     Listener listener = addGpsListenerToLocationManager();
     67     assertTrue(shadowLocationManager.hasGpsStatusListener(listener));
     68   }
     69 
     70   @Test
     71   public void shouldNotHaveListenerOnceRemoved() {
     72     Listener listener = addGpsListenerToLocationManager();
     73 
     74     locationManager.removeGpsStatusListener(listener);
     75 
     76     assertFalse(shadowLocationManager.hasGpsStatusListener(listener));
     77   }
     78 
     79   @Test
     80   public void getProviders_returnsProvidersBasedOnEnabledParameter() throws Exception {
     81     assertTrue(locationManager.getProviders(true).isEmpty());
     82     assertThat(locationManager.getProviders(false).size()).isEqualTo(3);
     83 
     84     shadowLocationManager.setProviderEnabled(NETWORK_PROVIDER, true);
     85 
     86     List<String> providers = locationManager.getProviders(true);
     87     assertTrue(providers.contains(NETWORK_PROVIDER));
     88     assertThat(providers.size()).isEqualTo(1);
     89 
     90     shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true);
     91     providers = locationManager.getProviders(true);
     92     assertTrue(providers.contains(NETWORK_PROVIDER));
     93     assertTrue(providers.contains(GPS_PROVIDER));
     94     assertThat(providers.size()).isEqualTo(2);
     95 
     96     shadowLocationManager.setProviderEnabled(LocationManager.PASSIVE_PROVIDER, true);
     97     providers = locationManager.getProviders(true);
     98     assertTrue(providers.contains(NETWORK_PROVIDER));
     99     assertTrue(providers.contains(GPS_PROVIDER));
    100     assertTrue(providers.contains(LocationManager.PASSIVE_PROVIDER));
    101     assertThat(providers.size()).isEqualTo(3);
    102   }
    103 
    104   @Test
    105   public void shouldReturnAllProviders() throws Exception {
    106     assertThat(locationManager.getAllProviders().size()).isEqualTo(3);
    107 
    108     shadowLocationManager.setProviderEnabled("MY_PROVIDER", false);
    109     assertThat(locationManager.getAllProviders().size()).isEqualTo(4);
    110   }
    111 
    112   @Test
    113   public void shouldReturnLastKnownLocationForAProvider() throws Exception {
    114     assertNull(locationManager.getLastKnownLocation(NETWORK_PROVIDER));
    115 
    116     Location networkLocation = new Location(NETWORK_PROVIDER);
    117     Location gpsLocation = new Location(GPS_PROVIDER);
    118 
    119     shadowLocationManager.setLastKnownLocation(NETWORK_PROVIDER, networkLocation);
    120     shadowLocationManager.setLastKnownLocation(GPS_PROVIDER, gpsLocation);
    121 
    122     assertSame(locationManager.getLastKnownLocation(NETWORK_PROVIDER), networkLocation);
    123     assertSame(locationManager.getLastKnownLocation(GPS_PROVIDER), gpsLocation);
    124   }
    125 
    126   @Test
    127   public void shouldStoreRequestLocationUpdateListeners() throws Exception {
    128     TestLocationListener listener = new TestLocationListener();
    129     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 2.0f, listener);
    130     assertSame(shadowLocationManager.getRequestLocationUpdateListeners().get(0), listener);
    131   }
    132 
    133   @Test
    134   public void shouldKeepTrackOfWhichProvidersAListenerIsBoundTo_withoutDuplicates_inAnyOrder() throws Exception {
    135     TestLocationListener listener1 = new TestLocationListener();
    136     TestLocationListener listener2 = new TestLocationListener();
    137 
    138     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, listener1);
    139     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, listener1);
    140 
    141     Set<String> listOfExpectedProvidersForListener1 = new HashSet<>();
    142     listOfExpectedProvidersForListener1.add(LocationManager.NETWORK_PROVIDER);
    143     listOfExpectedProvidersForListener1.add(LocationManager.GPS_PROVIDER);
    144 
    145     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, listener2);
    146     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, listener2);
    147 
    148     Set<String> listOfExpectedProvidersForListener2 = new HashSet<>();
    149     listOfExpectedProvidersForListener2.add(LocationManager.NETWORK_PROVIDER);
    150 
    151     assertEquals(listOfExpectedProvidersForListener1, new HashSet<>(shadowLocationManager.getProvidersForListener(listener1)));
    152     assertEquals(listOfExpectedProvidersForListener2, new HashSet<>(shadowLocationManager.getProvidersForListener(listener2)));
    153 
    154     locationManager.removeUpdates(listener1);
    155     assertEquals(0, shadowLocationManager.getProvidersForListener(listener1).size());
    156   }
    157 
    158   @Test
    159   public void shouldRemoveLocationListeners() throws Exception {
    160     TestLocationListener listener = new TestLocationListener();
    161     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 2.0f, listener);
    162     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 2.0f, listener);
    163 
    164     TestLocationListener otherListener = new TestLocationListener();
    165     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 2.0f, otherListener);
    166 
    167     locationManager.removeUpdates(listener);
    168 
    169     List<LocationListener> expected = new ArrayList<>();
    170     expected.add(otherListener);
    171     assertThat(shadowLocationManager.getRequestLocationUpdateListeners()).isEqualTo(expected);
    172   }
    173 
    174   @Test
    175   public void shouldRemovePendingIntentsWhenRequestingLocationUpdatesUsingCriteria() throws Exception {
    176     Intent someIntent = new Intent("some_action");
    177     PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(ShadowApplication.getInstance().getApplicationContext(), 0, someIntent,
    178         PendingIntent.FLAG_UPDATE_CURRENT);
    179     Intent someOtherIntent = new Intent("some_other_action");
    180     PendingIntent someOtherLocationListenerPendingIntent = PendingIntent.getBroadcast(
    181         ShadowApplication.getInstance().getApplicationContext(), 0, someOtherIntent,
    182         PendingIntent.FLAG_UPDATE_CURRENT);
    183 
    184     shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true);
    185     shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true);
    186     Criteria criteria = new Criteria();
    187     criteria.setAccuracy(Criteria.ACCURACY_FINE);
    188 
    189     locationManager.requestLocationUpdates(0, 0, criteria, someLocationListenerPendingIntent);
    190     locationManager.requestLocationUpdates(0, 0, criteria, someOtherLocationListenerPendingIntent);
    191 
    192     locationManager.removeUpdates(someLocationListenerPendingIntent);
    193 
    194     Map<PendingIntent, Criteria> expectedCriteria = new HashMap<>();
    195     expectedCriteria.put(someOtherLocationListenerPendingIntent, criteria);
    196     assertThat(shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents()).isEqualTo(expectedCriteria);
    197   }
    198 
    199   @Test
    200   public void shouldNotSetBestEnabledProviderIfProviderIsDisabled() throws Exception {
    201     shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true);
    202     assertTrue(shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true));
    203   }
    204 
    205   @Test
    206   public void shouldNotSetBestDisabledProviderIfProviderIsEnabled() throws Exception {
    207     shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true);
    208     assertFalse(shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, false));
    209   }
    210 
    211   @Test
    212   public void shouldRemovePendingIntentsWhenRequestingLocationUpdatesUsingLocationListeners() throws Exception {
    213     Intent someIntent = new Intent("some_action");
    214     PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(ShadowApplication.getInstance().getApplicationContext(), 0,
    215         someIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    216     Intent someOtherIntent = new Intent("some_other_action");
    217     PendingIntent someOtherLocationListenerPendingIntent = PendingIntent.getBroadcast(ShadowApplication.getInstance().getApplicationContext(),
    218         0, someOtherIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    219 
    220     shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true);
    221     shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true);
    222     shadowLocationManager.setProviderEnabled(NETWORK_PROVIDER, true);
    223 
    224     locationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, someLocationListenerPendingIntent);
    225     locationManager.requestLocationUpdates(NETWORK_PROVIDER, 0, 0, someOtherLocationListenerPendingIntent);
    226 
    227     locationManager.removeUpdates(someLocationListenerPendingIntent);
    228 
    229     Map<PendingIntent, String> expectedProviders = new HashMap<>();
    230     expectedProviders.put(someOtherLocationListenerPendingIntent, NETWORK_PROVIDER);
    231     assertThat(shadowLocationManager.getRequestLocationUdpateProviderPendingIntents()).isEqualTo(expectedProviders);
    232   }
    233 
    234   @Test
    235   public void shouldStoreBestProviderCriteriaAndEnabledOnlyFlag() throws Exception {
    236     Criteria criteria = new Criteria();
    237     assertNull(locationManager.getBestProvider(criteria, true));
    238     assertSame(criteria, shadowLocationManager.getLastBestProviderCriteria());
    239     assertTrue(shadowLocationManager.getLastBestProviderEnabledOnly());
    240   }
    241 
    242   @Test
    243   public void getBestProvider_returnsProviderBasedOnCriteriaAndEnabledState() throws Exception {
    244     Criteria criteria = new Criteria();
    245     criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    246     assertThat(locationManager.getBestProvider(null, false)).isEqualTo(LocationManager.GPS_PROVIDER);
    247     assertThat(locationManager.getBestProvider(null, true)).isNull();
    248     assertThat(locationManager.getBestProvider(criteria, false)).isEqualTo(LocationManager.NETWORK_PROVIDER);
    249     assertThat(locationManager.getBestProvider(criteria, true)).isNull();
    250   }
    251 
    252   @Test
    253   public void shouldThrowExceptionWhenRequestingLocationUpdatesWithANullIntent() throws Exception {
    254     try {
    255       shadowLocationManager.requestLocationUpdates(0, 0, new Criteria(), null);
    256       Assert.fail("When requesting location updates the intent must not be null!");
    257     } catch (Exception e) {
    258       // No worries, everything is fine...
    259     }
    260   }
    261 
    262   @Test
    263   public void shouldThrowExceptionWhenRequestingLocationUpdatesAndNoProviderIsFound() throws Exception {
    264     Intent someIntent = new Intent("some_action");
    265     PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(ShadowApplication.getInstance().getApplicationContext(), 0,
    266         someIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    267     Criteria criteria = new Criteria();
    268     criteria.setAccuracy(Criteria.ACCURACY_FINE);
    269     try {
    270       shadowLocationManager.requestLocationUpdates(0, 0, criteria, someLocationListenerPendingIntent);
    271       Assert.fail("When requesting location updates the intent must not be null!");
    272     } catch (Exception e) {
    273       // No worries, everything is fine...
    274     }
    275   }
    276 
    277   @Test
    278   public void shouldThrowExceptionIfTheBestProviderIsUnknown() throws Exception {
    279     Criteria criteria = new Criteria();
    280     criteria.setAccuracy(Criteria.ACCURACY_FINE);
    281     try {
    282       shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER", true);
    283       Assert.fail("The best provider is unknown!");
    284     } catch (Exception e) {
    285       // No worries, everything is fine...
    286     }
    287   }
    288 
    289   @Test
    290   public void shouldReturnBestCustomProviderUsingCriteria() throws Exception {
    291     Criteria criteria = new Criteria();
    292     Criteria customProviderCriteria = new Criteria();
    293 
    294     // Manually set best provider should be returned
    295     ArrayList<Criteria> criteriaList = new ArrayList<>();
    296     customProviderCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
    297     criteriaList.add(customProviderCriteria);
    298     shadowLocationManager.setProviderEnabled("BEST_ENABLED_PROVIDER_WITH_CRITERIA", true, criteriaList);
    299     assertTrue(shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER_WITH_CRITERIA", true));
    300     criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    301     criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    302     assertThat("BEST_ENABLED_PROVIDER_WITH_CRITERIA").isEqualTo(locationManager.getBestProvider(criteria, true));
    303     assertTrue(shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER_WITH_CRITERIA", true));
    304     assertThat("BEST_ENABLED_PROVIDER_WITH_CRITERIA").isEqualTo(locationManager.getBestProvider(criteria, false));
    305     assertThat("BEST_ENABLED_PROVIDER_WITH_CRITERIA").isEqualTo(locationManager.getBestProvider(criteria, true));
    306   }
    307 
    308   @Test
    309   public void shouldReturnBestProviderUsingCriteria() {
    310     Criteria criteria = new Criteria();
    311 
    312     shadowLocationManager.setProviderEnabled(LocationManager.GPS_PROVIDER, false);
    313     criteria.setAccuracy(Criteria.ACCURACY_FINE);
    314     assertThat(LocationManager.GPS_PROVIDER).isEqualTo(locationManager.getBestProvider(criteria, false));
    315 
    316     shadowLocationManager.setProviderEnabled(LocationManager.NETWORK_PROVIDER, false);
    317     criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    318     assertThat(LocationManager.NETWORK_PROVIDER).isEqualTo(locationManager.getBestProvider(criteria, false));
    319 
    320     criteria.setPowerRequirement(Criteria.POWER_LOW);
    321     criteria.setAccuracy(Criteria.ACCURACY_FINE);
    322     assertThat(LocationManager.NETWORK_PROVIDER).isEqualTo(locationManager.getBestProvider(criteria, false));
    323   }
    324 
    325   @Test
    326   public void shouldReturnBestDisabledProvider() throws Exception {
    327     shadowLocationManager.setProviderEnabled("BEST_DISABLED_PROVIDER", false);
    328     shadowLocationManager.setBestProvider("BEST_DISABLED_PROVIDER", false);
    329     shadowLocationManager.setProviderEnabled("BEST_ENABLED_PROVIDER", true);
    330     shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER", true);
    331 
    332     assertTrue(shadowLocationManager.setBestProvider("BEST_DISABLED_PROVIDER", false));
    333     assertThat("BEST_DISABLED_PROVIDER").isEqualTo(locationManager.getBestProvider(null, false));
    334     assertThat("BEST_ENABLED_PROVIDER").isEqualTo(locationManager.getBestProvider(null, true));
    335   }
    336 
    337   @Test
    338   public void getBestProvider_returnsBestProviderBasedOnEnabledState() throws Exception {
    339     shadowLocationManager.setProviderEnabled("BEST_ENABLED_PROVIDER", true);
    340 
    341     assertThat(shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER", true)).isTrue();
    342     assertThat(shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER", false)).isFalse();
    343     assertThat(locationManager.getBestProvider(null, true)).isEqualTo("BEST_ENABLED_PROVIDER");
    344     assertThat(locationManager.getBestProvider(null, false)).isEqualTo(LocationManager.GPS_PROVIDER);
    345   }
    346 
    347   @Test
    348   public void shouldNotifyAllListenersIfProviderStateChanges() {
    349     TestLocationListener listener = new TestLocationListener();
    350     locationManager.requestLocationUpdates("TEST_PROVIDER", 0, 0, listener);
    351     shadowLocationManager.setProviderEnabled("TEST_PROVIDER", true);
    352     assertTrue(listener.providerEnabled);
    353     shadowLocationManager.setProviderEnabled("TEST_PROVIDER", false);
    354     assertFalse(listener.providerEnabled);
    355   }
    356 
    357   @Test
    358   public void shouldRegisterLocationUpdatesWhenProviderGiven() throws Exception {
    359     shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true);
    360     shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true);
    361 
    362     Intent someIntent = new Intent("some_action");
    363     PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(ShadowApplication.getInstance().getApplicationContext(), 0,
    364         someIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    365     locationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, someLocationListenerPendingIntent);
    366 
    367     assertThat(shadowLocationManager.getRequestLocationUdpateProviderPendingIntents().get(someLocationListenerPendingIntent)).isEqualTo(GPS_PROVIDER);
    368   }
    369 
    370   @Test
    371   public void shouldRegisterLocationUpdatesWhenCriteriaGiven() throws Exception {
    372     shadowLocationManager.setProviderEnabled(NETWORK_PROVIDER, true);
    373     shadowLocationManager.setBestProvider(LocationManager.NETWORK_PROVIDER, true);
    374     Criteria criteria = new Criteria();
    375     criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    376 
    377     Intent someIntent = new Intent("some_action");
    378     PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(ShadowApplication.getInstance().getApplicationContext(), 0,
    379         someIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    380     Criteria someCriteria = new Criteria();
    381     someCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
    382     locationManager.requestLocationUpdates(0, 0, someCriteria, someLocationListenerPendingIntent);
    383 
    384     assertThat(shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents().get(someLocationListenerPendingIntent)).isEqualTo(someCriteria);
    385   }
    386 
    387   @Test
    388   public void simulateLocation_shouldNotNotifyListenerIfLessThanFastestInterval() throws Exception {
    389     TestLocationListener listener = new TestLocationListener();
    390     shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 2000, 0, listener);
    391     long time = System.currentTimeMillis();
    392 
    393     Location location1 = new Location(GPS_PROVIDER);
    394     location1.setTime(time);
    395 
    396     Location location2 = new Location(GPS_PROVIDER);
    397     location2.setTime(time + 1000);
    398 
    399     shadowLocationManager.simulateLocation(location1);
    400     shadowLocationManager.simulateLocation(location2);
    401     assertThat(listener.location.getTime()).isEqualTo(location1.getTime());
    402   }
    403 
    404   @Test
    405   public void simulateLocation_shouldNotNotifyListenerIfLessThanMinimumDistance() throws Exception {
    406     TestLocationListener listener = new TestLocationListener();
    407     locationManager.requestLocationUpdates(GPS_PROVIDER, 0, 200000, listener);
    408 
    409     Location location1 = new Location(GPS_PROVIDER);
    410     location1.setLatitude(1);
    411     location1.setLongitude(2);
    412     location1.setTime(0);
    413 
    414     Location location2 = new Location(GPS_PROVIDER);
    415     location2.setLatitude(1.5);
    416     location2.setLongitude(2.5);
    417     location2.setTime(1000);
    418 
    419     shadowLocationManager.simulateLocation(location1);
    420     shadowLocationManager.simulateLocation(location2);
    421 
    422     assertThat(listener.location.getLatitude()).isEqualTo(1);
    423     assertThat(listener.location.getLongitude()).isEqualTo(2);
    424   }
    425 
    426   @Test
    427   public void shouldNotThrowExceptionIfLocationListenerRemovedInsideOnLocationChanged() throws Exception {
    428     TestLocationListenerSelfRemoval listener = new TestLocationListenerSelfRemoval(locationManager);
    429     shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, listener);
    430 
    431     Location location = new Location(GPS_PROVIDER);
    432     location.setLatitude(0);
    433     location.setLongitude(0);
    434 
    435     shadowLocationManager.simulateLocation(location);
    436     assertThat(shadowLocationManager.getRequestLocationUpdateListeners().size()).isEqualTo(0);
    437   }
    438 
    439   @Test
    440   public void requestLocationUpdates_shouldNotRegisterDuplicateListeners() throws Exception {
    441     TestLocationListener listener = new TestLocationListener();
    442     shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, listener);
    443     shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, listener);
    444     shadowLocationManager.simulateLocation(new Location(GPS_PROVIDER));
    445     assertThat(listener.updateCount).isEqualTo(1);
    446   }
    447 
    448   private Listener addGpsListenerToLocationManager() {
    449     Listener listener = new TestGpsListener();
    450     locationManager.addGpsStatusListener(listener);
    451     return listener;
    452   }
    453 
    454   private static class TestLocationListener implements LocationListener {
    455     boolean providerEnabled;
    456     Location location;
    457     int updateCount;
    458 
    459     @Override
    460     public void onLocationChanged(Location location) {
    461       this.location = location;
    462       updateCount++;
    463     }
    464 
    465     @Override
    466     public void onStatusChanged(String s, int i, Bundle bundle) {
    467     }
    468 
    469     @Override
    470     public void onProviderEnabled(String s) {
    471       providerEnabled = true;
    472     }
    473 
    474     @Override
    475     public void onProviderDisabled(String s) {
    476       providerEnabled = false;
    477     }
    478   }
    479 
    480   private static class TestLocationListenerSelfRemoval implements LocationListener {
    481     LocationManager locationManager;
    482 
    483     public TestLocationListenerSelfRemoval(LocationManager locationManager) {
    484       this.locationManager = locationManager;
    485     }
    486 
    487     @Override
    488     public void onLocationChanged(Location location) {
    489       locationManager.removeUpdates(this);
    490     }
    491 
    492     @Override
    493     public void onStatusChanged(String s, int i, Bundle bundle) {
    494     }
    495 
    496     @Override
    497     public void onProviderEnabled(String s) {
    498     }
    499 
    500     @Override
    501     public void onProviderDisabled(String s) {
    502     }
    503   }
    504 
    505   private static class TestGpsListener implements Listener {
    506 
    507     @Override
    508     public void onGpsStatusChanged(int event) {
    509 
    510     }
    511   }
    512 }
    513