WebViewAdUrlGenerator extends AdUrlGenerator { @Override public String generateUrlString(String serverHostname) { initUrlString(serverHostname, Constants.AD_HANDLER); setApiVersion("6"); final ClientMetadata clientMetadata = ClientMetadata.getInstance(mContext); addBaseParams(clientMetadata); setMraidFlag(true); setExternalStoragePermission(mIsStorePictureSupported); return getFinalUrlString(); } WebViewAdUrlGenerator(Context context, boolean isStorePictureSupported); @Override String generateUrlString(String serverHostname);  }
@Test public void generateAdUrl_shouldIncludeAllFields() throws Exception { final String expectedAdUrl = new AdUrlBuilder(expectedUdid) .withAdUnitId("adUnitId") .withQuery("key%3Avalue") .withLatLon("20.1%2C30.0", "1", "101325") .withMcc("123") .withMnc("456") .withCountryIso("expected%20country") .withCarrierName("expected%20carrier") .withExternalStoragePermission(false) .build(); shadowTelephonyManager.setNetworkOperator("123456"); shadowTelephonyManager.setNetworkCountryIso("expected country"); shadowTelephonyManager.setNetworkOperatorName("expected carrier"); Location location = new Location(""); location.setLatitude(20.1); location.setLongitude(30.0); location.setAccuracy(1.23f); location.setTime(System.currentTimeMillis() - 101325); String adUrl = subject .withAdUnitId("adUnitId") .withKeywords("key:value") .withLocation(location) .generateUrlString("ads.mopub.com"); adUrl = adUrl.replaceFirst("llf=101[0-9]{3}", "llf=101325"); assertThat(adUrl).isEqualTo(expectedAdUrl); }
@Test public void generateAdUrl_whenLocationServiceGpsProviderHasMostRecentLocation_shouldUseLocationServiceValue() { 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)); 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)); 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); 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"); }