@Test public void disableLocationCollection_whenLocationServiceHasMostRecentLocation_shouldNotIncludeLocationInUrl() { MoPub.setLocationAwareness(MoPub.LocationAwareness.DISABLED); subject = new NativeUrlGenerator(context); // Mock out the LocationManager's last known location. ShadowLocationManager shadowLocationManager = Robolectric.shadowOf( (LocationManager) application.getSystemService(Context.LOCATION_SERVICE)); Location locationFromSdk = new Location(""); locationFromSdk.setLatitude(37); locationFromSdk.setLongitude(-122); locationFromSdk.setAccuracy(5.0f); locationFromSdk.setTime(2000); shadowLocationManager.setLastKnownLocation(LocationManager.GPS_PROVIDER, locationFromSdk); String requestString = generateMinimumUrlString(); assertThat(getParameterFromRequestUrl(requestString, "ll")).isNullOrEmpty(); }
@Test public void generateAdUrl_whenLocationServiceGpsProviderHasMostRecentLocation_shouldUseLocationServiceValue() { Location locationFromDeveloper = new Location(""); locationFromDeveloper.setLatitude(42); locationFromDeveloper.setLongitude(-42); locationFromDeveloper.setAccuracy(3.5f); locationFromDeveloper.setTime(1000); // Mock out the LocationManager's last known location to be more recent than the // developer-supplied location. ShadowLocationManager shadowLocationManager = Robolectric.shadowOf( (LocationManager) application.getSystemService(Context.LOCATION_SERVICE)); Location locationFromSdk = new Location(""); locationFromSdk.setLatitude(37); locationFromSdk.setLongitude(-122); locationFromSdk.setAccuracy(5.0f); locationFromSdk.setTime(2000); shadowLocationManager.setLastKnownLocation(LocationManager.GPS_PROVIDER, locationFromSdk); String adUrl = subject.withLocation(locationFromDeveloper) .generateUrlString("ads.mopub.com"); assertThat(getParameterFromRequestUrl(adUrl, "ll")).isEqualTo("37.0,-122.0"); assertThat(getParameterFromRequestUrl(adUrl, "lla")).isEqualTo("5"); assertThat(getParameterFromRequestUrl(adUrl, "llsdk")).isEqualTo("1"); }
@Test public void generateAdUrl_whenDeveloperSuppliesMoreRecentLocationThanLocationService_shouldUseDeveloperSuppliedLocation() { Location locationFromDeveloper = new Location(""); locationFromDeveloper.setLatitude(42); locationFromDeveloper.setLongitude(-42); locationFromDeveloper.setAccuracy(3.5f); locationFromDeveloper.setTime(1000); ShadowLocationManager shadowLocationManager = Robolectric.shadowOf( (LocationManager) application.getSystemService(Context.LOCATION_SERVICE)); // Mock out the LocationManager's last known location to be older than the // developer-supplied location. Location olderLocation = new Location(""); olderLocation.setLatitude(40); olderLocation.setLongitude(-105); olderLocation.setAccuracy(8.0f); olderLocation.setTime(500); shadowLocationManager.setLastKnownLocation(LocationManager.GPS_PROVIDER, olderLocation); String adUrl = subject.withLocation(locationFromDeveloper) .generateUrlString("ads.mopub.com"); assertThat(getParameterFromRequestUrl(adUrl, "ll")).isEqualTo("42.0,-42.0"); assertThat(getParameterFromRequestUrl(adUrl, "lla")).isEqualTo("3"); assertThat(getParameterFromRequestUrl(adUrl, "llsdk")).isEmpty(); }
@Test public void generateAdUrl_whenLocationServiceNetworkProviderHasMostRecentLocation_shouldUseLocationServiceValue() { Location locationFromDeveloper = new Location(""); locationFromDeveloper.setLatitude(42); locationFromDeveloper.setLongitude(-42); locationFromDeveloper.setAccuracy(3.5f); locationFromDeveloper.setTime(1000); // Mock out the LocationManager's last known location to be more recent than the // developer-supplied location. ShadowLocationManager shadowLocationManager = Robolectric.shadowOf( (LocationManager) application.getSystemService(Context.LOCATION_SERVICE)); Location locationFromSdk = new Location(""); locationFromSdk.setLatitude(38); locationFromSdk.setLongitude(-123); locationFromSdk.setAccuracy(5.0f); locationFromSdk.setTime(2000); shadowLocationManager.setLastKnownLocation(LocationManager.NETWORK_PROVIDER, locationFromSdk); String adUrl = subject.withLocation(locationFromDeveloper) .generateUrlString("ads.mopub.com"); assertThat(getParameterFromRequestUrl(adUrl, "ll")).isEqualTo("38.0,-123.0"); assertThat(getParameterFromRequestUrl(adUrl, "lla")).isEqualTo("5"); assertThat(getParameterFromRequestUrl(adUrl, "llsdk")).isEqualTo("1"); }
@Test public void disableLocationCollection_whenLocationServiceHasMostRecentLocation_shouldNotIncludeLocationInUrl() { MoPub.setLocationAwareness(MoPub.LocationAwareness.DISABLED); // Mock out the LocationManager's last known location. ShadowLocationManager shadowLocationManager = Robolectric.shadowOf( (LocationManager) application.getSystemService(Context.LOCATION_SERVICE)); Location locationFromSdk = new Location(""); locationFromSdk.setLatitude(37); locationFromSdk.setLongitude(-122); locationFromSdk.setAccuracy(5.0f); locationFromSdk.setTime(2000); shadowLocationManager.setLastKnownLocation(LocationManager.GPS_PROVIDER, locationFromSdk); String adUrl = generateMinimumUrlString(); assertThat(getParameterFromRequestUrl(adUrl, "ll")).isNullOrEmpty(); }
@Test public void generateUrlString_whenLocationServiceGpsProviderHasMostRecentLocation_shouldUseLocationServiceValue() { Location locationFromDeveloper = new Location(""); locationFromDeveloper.setLatitude(42); locationFromDeveloper.setLongitude(-42); locationFromDeveloper.setAccuracy(3.5f); locationFromDeveloper.setTime(1000); // Mock out the LocationManager's last known location to be more recent than the // developer-supplied location. ShadowLocationManager shadowLocationManager = Robolectric.shadowOf( (LocationManager) application.getSystemService(Context.LOCATION_SERVICE)); Location locationFromSdk = new Location(""); locationFromSdk.setLatitude(37); locationFromSdk.setLongitude(-122); locationFromSdk.setAccuracy(5.0f); locationFromSdk.setTime(System.currentTimeMillis() - 555555); shadowLocationManager.setLastKnownLocation(LocationManager.GPS_PROVIDER, locationFromSdk); RequestParameters requestParameters = new RequestParameters.Builder() .location(locationFromDeveloper) .build(); subject = new NativeUrlGenerator(context).withAdUnitId(AD_UNIT_ID); String adUrl = subject.withRequest(requestParameters) .generateUrlString("ads.mopub.com"); assertThat(getParameterFromRequestUrl(adUrl, "ll")).isEqualTo("37.0,-122.0"); assertThat(getParameterFromRequestUrl(adUrl, "lla")).isEqualTo("5"); assertThat(getParameterFromRequestUrl(adUrl, "llsdk")).isEqualTo("1"); // Only test to the full second (as there may be small differences) assertThat(getParameterFromRequestUrl(adUrl, "llf")).startsWith("555"); assertThat(getParameterFromRequestUrl(adUrl, "llf").length()).isEqualTo(6); }
@Test public void generateUrlString_whenDeveloperSuppliesMoreRecentLocationThanLocationService_shouldUseDeveloperSuppliedLocation() { Location locationFromDeveloper = new Location(""); locationFromDeveloper.setLatitude(42); locationFromDeveloper.setLongitude(-42); locationFromDeveloper.setAccuracy(3.5f); locationFromDeveloper.setTime(System.currentTimeMillis() - 777777); ShadowLocationManager shadowLocationManager = Robolectric.shadowOf( (LocationManager) application.getSystemService(Context.LOCATION_SERVICE)); // Mock out the LocationManager's last known location to be older than the // developer-supplied location. Location olderLocation = new Location(""); olderLocation.setLatitude(40); olderLocation.setLongitude(-105); olderLocation.setAccuracy(8.0f); olderLocation.setTime(System.currentTimeMillis() - 888888); shadowLocationManager.setLastKnownLocation(LocationManager.GPS_PROVIDER, olderLocation); RequestParameters requestParameters = new RequestParameters.Builder() .location(locationFromDeveloper) .build(); subject = new NativeUrlGenerator(context).withAdUnitId(AD_UNIT_ID); String adUrl = subject.withRequest(requestParameters) .generateUrlString("ads.mopub.com"); assertThat(getParameterFromRequestUrl(adUrl, "ll")).isEqualTo("42.0,-42.0"); assertThat(getParameterFromRequestUrl(adUrl, "lla")).isEqualTo("3"); assertThat(getParameterFromRequestUrl(adUrl, "llsdk")).isEmpty(); // Only test to the full second (as there may be small differences) assertThat(getParameterFromRequestUrl(adUrl, "llf")).startsWith("777"); assertThat(getParameterFromRequestUrl(adUrl, "llf").length()).isEqualTo(6); }
@Test public void generateUrlString_whenLocationServiceNetworkProviderHasMostRecentLocation_shouldUseLocationServiceValue() { Location locationFromDeveloper = new Location(""); locationFromDeveloper.setLatitude(42); locationFromDeveloper.setLongitude(-42); locationFromDeveloper.setAccuracy(3.5f); locationFromDeveloper.setTime(1000); // Mock out the LocationManager's last known location to be more recent than the // developer-supplied location. ShadowLocationManager shadowLocationManager = Robolectric.shadowOf( (LocationManager) application.getSystemService(Context.LOCATION_SERVICE)); Location locationFromSdk = new Location(""); locationFromSdk.setLatitude(38); locationFromSdk.setLongitude(-123); locationFromSdk.setAccuracy(5.0f); locationFromSdk.setTime(System.currentTimeMillis() - 123456); shadowLocationManager.setLastKnownLocation(LocationManager.NETWORK_PROVIDER, locationFromSdk); RequestParameters requestParameters = new RequestParameters.Builder() .location(locationFromDeveloper) .build(); subject = new NativeUrlGenerator(context).withAdUnitId(AD_UNIT_ID); String adUrl = subject.withRequest(requestParameters) .generateUrlString("ads.mopub.com"); assertThat(getParameterFromRequestUrl(adUrl, "ll")).isEqualTo("38.0,-123.0"); assertThat(getParameterFromRequestUrl(adUrl, "lla")).isEqualTo("5"); assertThat(getParameterFromRequestUrl(adUrl, "llsdk")).isEqualTo("1"); // Only test to the full second (as there may be small differences) assertThat(getParameterFromRequestUrl(adUrl, "llf")).startsWith("123"); assertThat(getParameterFromRequestUrl(adUrl, "llf").length()).isEqualTo(6); }
@Ignore @Test public void shouldReturnTheLatestLocation() { LocationManager locationManager = (LocationManager) RuntimeEnvironment.application.getSystemService(Context.LOCATION_SERVICE); ShadowLocationManager shadowLocationManager = Shadows.shadowOf(locationManager); Location expectedLocation = location(LocationManager.GPS_PROVIDER, 12.0, 20.0); shadowLocationManager.simulateLocation(expectedLocation); // -- todo assertEquals(expectedLocation.getLatitude(), skyLinesApp.lastLat); assertEquals(expectedLocation.getLongitude(), skyLinesApp.lastLon); //assertEquals(expectedLocation, actualLocation); }
@Test public void onResume_shouldCheckIfConnectedBeforeConnectingAgain() throws Exception { ShadowLocationManager shadowLocationManager = shadowOf(getLocationManager()); List<android.location.LocationListener> listeners = shadowLocationManager.getRequestLocationUpdateListeners(); for (android.location.LocationListener listener : listeners) { shadowLocationManager.removeUpdates(listener); } activity.locationClient.connect(); activity.onResume(); assertThat(shadowLocationManager.getRequestLocationUpdateListeners()).hasSize(2); }
@Test public void onRoutePreviewEvent_shouldDisplayGPSPromptIfNotEnabled() throws Exception { ShadowLocationManager manager = shadowOf(getLocationManager()); manager.setProviderEnabled(LocationManager.GPS_PROVIDER, false); activity.onRoutePreviewEvent(new RoutePreviewEvent(getTestSimpleFeature())); assertThat(activity.getSupportFragmentManager()).hasFragmentWithTag("gps_dialog"); }
public static ShadowLocationManager shadowOf(LocationManager instance) { return (ShadowLocationManager) shadowOf_(instance); }
public static void initLastKnownLocation() { LocationManager locationManager = (LocationManager) application.getSystemService(LOCATION_SERVICE); ShadowLocationManager shadowLocationManager = shadowOf(locationManager); shadowLocationManager.setLastKnownLocation(GPS_PROVIDER, new Location(GPS_PROVIDER)); }