AdFetcher { public void fetchAdForUrl(String url) { mTaskTracker.newTaskStarted(); MoPubLog.i("Fetching ad for task #" + getCurrentTaskId()); if (mCurrentTask != null) { mCurrentTask.cancel(true); } mCurrentTask = AdFetchTaskFactory.create(mTaskTracker, mAdViewController, mUserAgent, mTimeoutMilliseconds); try { AsyncTasks.safeExecuteOnExecutor(mCurrentTask, url); } catch (Exception exception) { MoPubLog.d("Error executing AdFetchTask", exception); } } AdFetcher(AdViewController adview, String userAgent); void fetchAdForUrl(String url); void cancelFetch(); static final String HTML_RESPONSE_BODY_KEY; static final String REDIRECT_URL_KEY; static final String CLICKTHROUGH_URL_KEY; static final String SCROLLABLE_KEY; static final String AD_CONFIGURATION_KEY; }
@Test public void shouldSendResponseToAdView() { Robolectric.addPendingHttpResponse(response); subject.fetchAdForUrl("url"); verify(adViewController).configureUsingHttpResponse(eq(response)); }
@Test public void fetchAdForUrl_shouldRouteMillennialBannerToCustomEventHandling() throws Exception { String json = "{\"adWidth\": 320, \"adHeight\": 50, \"adUnitID\": \"44310\"}"; stub(adViewController.getAdConfiguration()).toReturn(mock(AdConfiguration.class)); response.addHeader(AD_TYPE.getKey(), "millennial_native"); response.addHeader(NATIVE_PARAMS.getKey(), json); Robolectric.addPendingHttpResponse(response); subject.fetchAdForUrl("ignored_url"); Map<String, String> paramsMap = new HashMap<String, String>(); paramsMap.put(CUSTOM_EVENT_NAME.getKey(), "com.mopub.mobileads.MillennialBanner"); paramsMap.put(CUSTOM_EVENT_DATA.getKey(), json); verify(moPubView).loadCustomEvent(eq(paramsMap)); }
@Test public void fetchAdForUrl_shouldRouteMillennialInterstitialToCustomEventHandling() throws Exception { AdViewController interstitialAdViewController = mock(AdViewController.class); MoPubInterstitial.MoPubInterstitialView moPubInterstitialView = mock(MoPubInterstitial.MoPubInterstitialView.class); stub(interstitialAdViewController.getMoPubView()).toReturn(moPubInterstitialView); stub(interstitialAdViewController.getAdConfiguration()).toReturn(mock(AdConfiguration.class)); subject = new AdFetcher(interstitialAdViewController, "expected userAgent"); String json = "{\"adWidth\": 320, \"adHeight\": 480, \"adUnitID\": \"44310\"}"; response.addHeader(AD_TYPE.getKey(), "interstitial"); response.addHeader(FULL_AD_TYPE.getKey(), "millennial_full"); response.addHeader(NATIVE_PARAMS.getKey(), json); Robolectric.addPendingHttpResponse(response); subject.fetchAdForUrl("ignored_url"); Map<String, String> paramsMap = new HashMap<String, String>(); paramsMap.put(CUSTOM_EVENT_NAME.getKey(), "com.mopub.mobileads.MillennialInterstitial"); paramsMap.put(CUSTOM_EVENT_DATA.getKey(), json); verify(moPubInterstitialView).loadCustomEvent(eq(paramsMap)); }
@Config(reportSdk = VERSION_CODES.ICE_CREAM_SANDWICH) @Test public void fetchAdForUrl_atLeastIcs_shouldExecuteUsingAnExecutor() throws Exception { AdFetchTaskFactory.setInstance(new TestAdFetchTaskFactory()); AdFetchTask adFetchTask = TestAdFetchTaskFactory.getSingletonMock(); subject.fetchAdForUrl("some url"); verify(adFetchTask).executeOnExecutor(any(Executor.class), eq("some url")); verify(adFetchTask, never()).execute(anyString()); }
@Config(reportSdk = VERSION_CODES.GINGERBREAD_MR1) @Test public void fetchAdForUrl_beforeHoneycomb_shouldExecuteWithoutAnExecutor() throws Exception { AdFetchTaskFactory.setInstance(new TestAdFetchTaskFactory()); AdFetchTask adFetchTask = TestAdFetchTaskFactory.getSingletonMock(); subject.fetchAdForUrl("some url"); verify(adFetchTask, never()).executeOnExecutor(any(Executor.class), anyString()); verify(adFetchTask).execute(eq("some url")); }