Java 类org.robolectric.shadows.ShadowLocationManager 实例源码

项目:Fabric-Example-App-Android    文件:NativeUrlGeneratorTest.java   
@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();
}
项目:Fabric-Example-App-Android    文件:WebViewAdUrlGeneratorTest.java   
@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");
}
项目:Fabric-Example-App-Android    文件:WebViewAdUrlGeneratorTest.java   
@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();
}
项目:Fabric-Example-App-Android    文件:WebViewAdUrlGeneratorTest.java   
@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");
}
项目:Fabric-Example-App-Android    文件:WebViewAdUrlGeneratorTest.java   
@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();
}
项目:Fabric-Example-App-Android    文件:NativeUrlGeneratorTest.java   
@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);
}
项目:Fabric-Example-App-Android    文件:NativeUrlGeneratorTest.java   
@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);
}
项目:Fabric-Example-App-Android    文件:NativeUrlGeneratorTest.java   
@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);
}
项目:SkyLinesTracker    文件:MainActivityGPSTest.java   
@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);
}
项目:open    文件:BaseActivityTest.java   
@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);
}
项目:open    文件:BaseActivityTest.java   
@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");
}
项目:FullRobolectricTestSample    文件:Robolectric.java   
public static ShadowLocationManager shadowOf(LocationManager instance) {
  return (ShadowLocationManager) shadowOf_(instance);
}
项目:open    文件:TestHelper.java   
public static void initLastKnownLocation() {
    LocationManager locationManager = (LocationManager)
            application.getSystemService(LOCATION_SERVICE);
    ShadowLocationManager shadowLocationManager = shadowOf(locationManager);
    shadowLocationManager.setLastKnownLocation(GPS_PROVIDER, new Location(GPS_PROVIDER));
}