/** * Sets the value to return from {@link #isProviderEnabled(String)} for the given {@code provider} * * @param provider * name of the provider whose status to set * @param isEnabled * whether that provider should appear enabled */ public void setProviderEnabled(String provider, boolean isEnabled) { setProviderEnabled(provider, isEnabled, null); }
@Test public void shouldDisableProvider() { // No provider is enabled by default, so it must be manually enabled shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); shadowLocationManager.setProviderEnabled(GPS_PROVIDER, false); assertFalse(locationManager.isProviderEnabled(GPS_PROVIDER)); }
@Test public void shouldRegisterLocationUpdatesWhenProviderGiven() throws Exception { shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true); Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); locationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, someLocationListenerPendingIntent); assertThat(shadowLocationManager.getRequestLocationUdpateProviderPendingIntents().get(someLocationListenerPendingIntent)).isEqualTo(GPS_PROVIDER); }
@Test public void shouldReturnAllProviders() throws Exception { assertThat(locationManager.getAllProviders().size()).isEqualTo(3); shadowLocationManager.setProviderEnabled("MY_PROVIDER", false); assertThat(locationManager.getAllProviders().size()).isEqualTo(4); }
@Test public void shouldRemovePendingIntentsWhenRequestingLocationUpdatesUsingLocationListeners() throws Exception { Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); Intent someOtherIntent = new Intent("some_other_action"); PendingIntent someOtherLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someOtherIntent, PendingIntent.FLAG_UPDATE_CURRENT); shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true); shadowLocationManager.setProviderEnabled(NETWORK_PROVIDER, true); locationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, someLocationListenerPendingIntent); locationManager.requestLocationUpdates(NETWORK_PROVIDER, 0, 0, someOtherLocationListenerPendingIntent); locationManager.removeUpdates(someLocationListenerPendingIntent); Map<PendingIntent, String> expectedProviders = new HashMap<>(); expectedProviders.put(someOtherLocationListenerPendingIntent, NETWORK_PROVIDER); assertThat(shadowLocationManager.getRequestLocationUdpateProviderPendingIntents()).isEqualTo(expectedProviders); }
@Test public void shouldNotifyAllListenersIfProviderStateChanges() { TestLocationListener listener = new TestLocationListener(); locationManager.requestLocationUpdates("TEST_PROVIDER", 0, 0, listener); shadowLocationManager.setProviderEnabled("TEST_PROVIDER", true); assertTrue(listener.providerEnabled); shadowLocationManager.setProviderEnabled("TEST_PROVIDER", false); assertFalse(listener.providerEnabled); }
@Test public void shouldNotSetBestDisabledProviderIfProviderIsEnabled() throws Exception { shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); assertFalse(shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, false)); }
@Test public void shouldNotSetBestEnabledProviderIfProviderIsDisabled() throws Exception { shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); assertTrue(shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true)); }
@Test public void getProviders_returnsProvidersBasedOnEnabledParameter() throws Exception { assertTrue(locationManager.getProviders(true).isEmpty()); assertThat(locationManager.getProviders(false).size()).isEqualTo(3); shadowLocationManager.setProviderEnabled(NETWORK_PROVIDER, true); List<String> providers = locationManager.getProviders(true); assertTrue(providers.contains(NETWORK_PROVIDER)); assertThat(providers.size()).isEqualTo(1); shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); providers = locationManager.getProviders(true); assertTrue(providers.contains(NETWORK_PROVIDER)); assertTrue(providers.contains(GPS_PROVIDER)); assertThat(providers.size()).isEqualTo(2); shadowLocationManager.setProviderEnabled(LocationManager.PASSIVE_PROVIDER, true); providers = locationManager.getProviders(true); assertTrue(providers.contains(NETWORK_PROVIDER)); assertTrue(providers.contains(GPS_PROVIDER)); assertTrue(providers.contains(LocationManager.PASSIVE_PROVIDER)); assertThat(providers.size()).isEqualTo(3); }
@Test public void shouldReturnBestDisabledProvider() throws Exception { shadowLocationManager.setProviderEnabled("BEST_DISABLED_PROVIDER", false); shadowLocationManager.setBestProvider("BEST_DISABLED_PROVIDER", false); shadowLocationManager.setProviderEnabled("BEST_ENABLED_PROVIDER", true); shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER", true); assertTrue(shadowLocationManager.setBestProvider("BEST_DISABLED_PROVIDER", false)); assertThat(locationManager.getBestProvider(null, false)).isEqualTo("BEST_DISABLED_PROVIDER"); assertThat(locationManager.getBestProvider(null, true)).isEqualTo("BEST_ENABLED_PROVIDER"); }
@Test public void shouldRemovePendingIntentsWhenRequestingLocationUpdatesUsingCriteria() throws Exception { Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); Intent someOtherIntent = new Intent("some_other_action"); PendingIntent someOtherLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someOtherIntent, PendingIntent.FLAG_UPDATE_CURRENT); shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); locationManager.requestLocationUpdates(0, 0, criteria, someLocationListenerPendingIntent); locationManager.requestLocationUpdates(0, 0, criteria, someOtherLocationListenerPendingIntent); locationManager.removeUpdates(someLocationListenerPendingIntent); Map<PendingIntent, Criteria> expectedCriteria = new HashMap<>(); expectedCriteria.put(someOtherLocationListenerPendingIntent, criteria); assertThat(shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents()).isEqualTo(expectedCriteria); }
@Test public void getBestProvider_returnsBestProviderBasedOnEnabledState() throws Exception { shadowLocationManager.setProviderEnabled("BEST_ENABLED_PROVIDER", true); assertThat(shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER", true)).isTrue(); assertThat(shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER", false)).isFalse(); assertThat(locationManager.getBestProvider(null, true)).isEqualTo("BEST_ENABLED_PROVIDER"); assertThat(locationManager.getBestProvider(null, false)).isEqualTo(LocationManager.GPS_PROVIDER); }
@Test public void shouldRegisterLocationUpdatesWhenCriteriaGiven() throws Exception { shadowLocationManager.setProviderEnabled(NETWORK_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.NETWORK_PROVIDER, true); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); Criteria someCriteria = new Criteria(); someCriteria.setAccuracy(Criteria.ACCURACY_COARSE); locationManager.requestLocationUpdates(0, 0, someCriteria, someLocationListenerPendingIntent); assertThat(shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents().get(someLocationListenerPendingIntent)).isEqualTo(someCriteria); }
@Test public void shouldReturnBestProviderUsingCriteria() { Criteria criteria = new Criteria(); shadowLocationManager.setProviderEnabled(LocationManager.GPS_PROVIDER, false); criteria.setAccuracy(Criteria.ACCURACY_FINE); assertThat(locationManager.getBestProvider(criteria, false)) .isEqualTo(LocationManager.GPS_PROVIDER); shadowLocationManager.setProviderEnabled(LocationManager.NETWORK_PROVIDER, false); criteria.setAccuracy(Criteria.ACCURACY_COARSE); assertThat(locationManager.getBestProvider(criteria, false)) .isEqualTo(LocationManager.NETWORK_PROVIDER); criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAccuracy(Criteria.ACCURACY_FINE); assertThat(locationManager.getBestProvider(criteria, false)) .isEqualTo(LocationManager.NETWORK_PROVIDER); }
@Test public void shouldReturnBestCustomProviderUsingCriteria() throws Exception { Criteria criteria = new Criteria(); Criteria customProviderCriteria = new Criteria(); // Manually set best provider should be returned ArrayList<Criteria> criteriaList = new ArrayList<>(); customProviderCriteria.setAccuracy(Criteria.ACCURACY_COARSE); criteriaList.add(customProviderCriteria); shadowLocationManager.setProviderEnabled("BEST_ENABLED_PROVIDER_WITH_CRITERIA", true, criteriaList); assertTrue(shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER_WITH_CRITERIA", true)); criteria.setAccuracy(Criteria.ACCURACY_COARSE); criteria.setPowerRequirement(Criteria.NO_REQUIREMENT); assertThat(locationManager.getBestProvider(criteria, true)) .isEqualTo("BEST_ENABLED_PROVIDER_WITH_CRITERIA"); assertThat(shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER_WITH_CRITERIA", true)) .isTrue(); assertThat(locationManager.getBestProvider(criteria, false)) .isEqualTo("BEST_ENABLED_PROVIDER_WITH_CRITERIA"); assertThat(locationManager.getBestProvider(criteria, true)) .isEqualTo("BEST_ENABLED_PROVIDER_WITH_CRITERIA"); }
/** * Sets the value to return from {@link #isProviderEnabled(String)} for the given {@code provider} * * @param provider * name of the provider whose status to set * @param isEnabled * whether that provider should appear enabled */ public void setProviderEnabled(String provider, boolean isEnabled) { setProviderEnabled(provider, isEnabled, null); }
/** * Sets the value to return from {@link #isProviderEnabled(String)} for the given {@code provider} * * @param provider * name of the provider whose status to set * @param isEnabled * whether that provider should appear enabled */ public void setProviderEnabled(String provider, boolean isEnabled) { setProviderEnabled(provider, isEnabled, null); }
/** * Sets the value to return from {@link #isProviderEnabled(String)} for the given {@code provider} * * @param provider * name of the provider whose status to set * @param isEnabled * whether that provider should appear enabled */ public void setProviderEnabled(String provider, boolean isEnabled) { setProviderEnabled(provider, isEnabled, null); }
/** * Sets the value to return from {@link #isProviderEnabled(String)} for the given {@code provider} * * @param provider * name of the provider whose status to set * @param isEnabled * whether that provider should appear enabled */ public void setProviderEnabled(String provider, boolean isEnabled) { setProviderEnabled(provider, isEnabled, null); }