@Test public void requestLocationUpdates_shouldNotRegisterDuplicateListeners() throws Exception { TestLocationListener listener = new TestLocationListener(); shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, listener); shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, listener); shadowLocationManager.simulateLocation(new Location(GPS_PROVIDER)); assertThat(listener.updateCount).isEqualTo(1); }
@Test public void shouldThrowExceptionWhenRequestingLocationUpdatesAndNoProviderIsFound() throws Exception { Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); try { shadowLocationManager.requestLocationUpdates(0, 0, criteria, someLocationListenerPendingIntent); Assert.fail("When requesting location updates the intent must not be null!"); } catch (Exception e) { // No worries, everything is fine... } }
@Test public void shouldThrowExceptionWhenRequestingLocationUpdatesWithANullIntent() throws Exception { try { shadowLocationManager.requestLocationUpdates(0, 0, new Criteria(), null); Assert.fail("When requesting location updates the intent must not be null!"); } catch (Exception e) { // No worries, everything is fine... } }
@Test public void shouldNotThrowExceptionIfLocationListenerRemovedInsideOnLocationChanged() throws Exception { TestLocationListenerSelfRemoval listener = new TestLocationListenerSelfRemoval(locationManager); shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, listener); Location location = new Location(GPS_PROVIDER); location.setLatitude(0); location.setLongitude(0); shadowLocationManager.simulateLocation(location); assertThat(shadowLocationManager.getRequestLocationUpdateListeners().size()).isEqualTo(0); }
@Test public void simulateLocation_shouldNotNotifyListenerIfLessThanFastestInterval() throws Exception { TestLocationListener listener = new TestLocationListener(); shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 2000, 0, listener); long time = System.currentTimeMillis(); Location location1 = new Location(GPS_PROVIDER); location1.setTime(time); Location location2 = new Location(GPS_PROVIDER); location2.setTime(time + 1000); shadowLocationManager.simulateLocation(location1); shadowLocationManager.simulateLocation(location2); assertThat(listener.location.getTime()).isEqualTo(location1.getTime()); }