public boolean setBestProvider(String provider, boolean enabled) throws Exception { return setBestProvider(provider, enabled, null); }
@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 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 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 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 shouldNotSetBestEnabledProviderIfProviderIsDisabled() throws Exception { shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); assertTrue(shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true)); }
@Test public void shouldNotSetBestDisabledProviderIfProviderIsEnabled() throws Exception { shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); assertFalse(shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, false)); }
@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 shouldThrowExceptionIfTheBestProviderIsUnknown() throws Exception { Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); try { shadowLocationManager.setBestProvider("BEST_ENABLED_PROVIDER", true); Assert.fail("The best provider is unknown!"); } catch (Exception e) { // No worries, everything is fine... } }
@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 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"); }
public boolean setBestProvider(String provider, boolean enabled) throws Exception { return setBestProvider(provider, enabled, null); }
public boolean setBestProvider(String provider, boolean enabled) throws Exception { return setBestProvider(provider, enabled, null); }
public boolean setBestProvider(String provider, boolean enabled) throws Exception { return setBestProvider(provider, enabled, null); }
public boolean setBestProvider(String provider, boolean enabled) throws Exception { return setBestProvider(provider, enabled, null); }