WebViewAdUrlGenerator extends AdUrlGenerator { @Override public String generateUrlString(String serverHostname) { initUrlString(serverHostname, Constants.AD_HANDLER); final ClientMetadata clientMetadata = ClientMetadata.getInstance(mContext); setApiVersion("6"); setAdUnitId(mAdUnitId); setSdkVersion(clientMetadata.getSdkVersion()); setDeviceInfo(clientMetadata.getDeviceManufacturer(), clientMetadata.getDeviceModel(), clientMetadata.getDeviceProduct()); setUdid(clientMetadata.getAdvertisingId()); setDoNotTrack(clientMetadata.isDoNotTrackSet()); setKeywords(mKeywords); setLocation(mLocation); setTimezone(DateAndTime.getTimeZoneOffsetString()); setOrientation(clientMetadata.getOrientationString()); setDensity(clientMetadata.getDensity()); setMraidFlag(true); String networkOperator = clientMetadata.getNetworkOperatorForUrl(); setMccCode(networkOperator); setMncCode(networkOperator); setIsoCountryCode(clientMetadata.getIsoCountryCode()); setCarrierName(clientMetadata.getNetworkOperatorName()); setNetworkType(clientMetadata.getActiveNetworkType()); setAppVersion(clientMetadata.getAppVersion()); setExternalStoragePermission(mIsStorePictureSupported); setTwitterAppInstalledFlag(); 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") .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); String adUrl = subject .withAdUnitId("adUnitId") .withKeywords("key:value") .withLocation(location) .generateUrlString("ads.mopub.com"); assertThat(adUrl).isEqualTo(expectedAdUrl); }
@Test public void generateAdUrl_whenTwitterIsNotInstalled_shouldProcessAndNotSetTwitterInstallStatusOnFirstRequest() throws Exception { AdUrlBuilder urlBuilder = new AdUrlBuilder(expectedUdid); WebViewAdUrlGenerator spySubject = Mockito.spy(subject); AdUrlGenerator.setTwitterAppInstalledStatus(TwitterAppInstalledStatus.UNKNOWN); doReturn(TwitterAppInstalledStatus.NOT_INSTALLED).when(spySubject).getTwitterAppInstallStatus(); String adUrl = spySubject.generateUrlString("ads.mopub.com"); assertThat(adUrl).isEqualTo(urlBuilder.withTwitterAppInstalledStatus(TwitterAppInstalledStatus.NOT_INSTALLED).build()); }
@Test public void generateAdUrl_whenTwitterIsInstalled_shouldProcessAndSetTwitterInstallStatusOnFirstRequest() throws Exception { AdUrlBuilder urlBuilder = new AdUrlBuilder(expectedUdid); WebViewAdUrlGenerator spySubject = Mockito.spy(subject); AdUrlGenerator.setTwitterAppInstalledStatus(TwitterAppInstalledStatus.UNKNOWN); doReturn(TwitterAppInstalledStatus.INSTALLED).when(spySubject).getTwitterAppInstallStatus(); String adUrl = spySubject.generateUrlString("ads.mopub.com"); assertThat(adUrl).isEqualTo(urlBuilder.withTwitterAppInstalledStatus(TwitterAppInstalledStatus.INSTALLED).build()); }
@Test public void generateAdUrl_shouldNotProcessTwitterInstallStatusIfStatusIsAlreadySet() throws Exception { AdUrlBuilder urlBuilder = new AdUrlBuilder(expectedUdid); WebViewAdUrlGenerator spySubject = Mockito.spy(subject); AdUrlGenerator.setTwitterAppInstalledStatus(TwitterAppInstalledStatus.NOT_INSTALLED); doReturn(TwitterAppInstalledStatus.INSTALLED).when(spySubject).getTwitterAppInstallStatus(); String adUrl = spySubject.generateUrlString("ads.mopub.com"); assertThat(adUrl).isEqualTo(urlBuilder.withTwitterAppInstalledStatus(TwitterAppInstalledStatus.NOT_INSTALLED).build()); }
@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"); }