HtmlBanner extends CustomEventBanner { @Override protected void loadBanner( Context context, CustomEventBannerListener customEventBannerListener, Map<String, Object> localExtras, Map<String, String> serverExtras) { String htmlData; String redirectUrl; String clickthroughUrl; Boolean isScrollable; AdReport adReport; if (extrasAreValid(serverExtras)) { htmlData = serverExtras.get(DataKeys.HTML_RESPONSE_BODY_KEY); redirectUrl = serverExtras.get(DataKeys.REDIRECT_URL_KEY); clickthroughUrl = serverExtras.get(DataKeys.CLICKTHROUGH_URL_KEY); isScrollable = Boolean.valueOf(serverExtras.get(DataKeys.SCROLLABLE_KEY)); try { adReport = (AdReport) localExtras.get(AD_REPORT_KEY); } catch (ClassCastException e) { MoPubLog.e("LocalExtras contained an incorrect type."); customEventBannerListener.onBannerFailed(INTERNAL_ERROR); return; } } else { customEventBannerListener.onBannerFailed(NETWORK_INVALID_STATE); return; } mHtmlBannerWebView = HtmlBannerWebViewFactory.create(context, adReport, customEventBannerListener, isScrollable, redirectUrl, clickthroughUrl); AdViewController.setShouldHonorServerDimensions(mHtmlBannerWebView); mHtmlBannerWebView.loadHtmlResponse(htmlData); }    }
@Test public void loadBanner_shouldPopulateTheHtmlWebViewWithHtml() throws Exception { subject.loadBanner(context, customEventBannerListener, localExtras, serverExtras); assertThat(TestHtmlBannerWebViewFactory.getLatestListener()).isSameAs(customEventBannerListener); assertThat(TestHtmlBannerWebViewFactory.getLatestIsScrollable()).isFalse(); assertThat(TestHtmlBannerWebViewFactory.getLatestRedirectUrl()).isNull(); assertThat(TestHtmlBannerWebViewFactory.getLatestClickthroughUrl()).isNull(); verify(htmlBannerWebView).loadHtmlResponse(responseBody); }
@Test public void loadBanner_whenNoHtmlResponse_shouldNotifyBannerFailed() throws Exception { serverExtras.remove(HTML_RESPONSE_BODY_KEY); subject.loadBanner(context, customEventBannerListener, localExtras, serverExtras); verify(customEventBannerListener).onBannerFailed(eq(NETWORK_INVALID_STATE)); assertThat(TestHtmlBannerWebViewFactory.getLatestListener()).isNull(); assertThat(TestHtmlBannerWebViewFactory.getLatestIsScrollable()).isFalse(); assertThat(TestHtmlBannerWebViewFactory.getLatestRedirectUrl()).isNull(); assertThat(TestHtmlBannerWebViewFactory.getLatestClickthroughUrl()).isNull(); verify(htmlBannerWebView, never()).loadHtmlResponse(anyString()); }
@Test public void loadBanner_shouldPassParametersThrough() throws Exception { serverExtras.put(SCROLLABLE_KEY, "true"); serverExtras.put(REDIRECT_URL_KEY, "redirectUrl"); serverExtras.put(CLICKTHROUGH_URL_KEY, "clickthroughUrl"); subject.loadBanner(context, customEventBannerListener, localExtras, serverExtras); assertThat(TestHtmlBannerWebViewFactory.getLatestListener()).isSameAs(customEventBannerListener); assertThat(TestHtmlBannerWebViewFactory.getLatestIsScrollable()).isTrue(); assertThat(TestHtmlBannerWebViewFactory.getLatestRedirectUrl()).isEqualTo("redirectUrl"); assertThat(TestHtmlBannerWebViewFactory.getLatestClickthroughUrl()).isEqualTo("clickthroughUrl"); verify(htmlBannerWebView).loadHtmlResponse(responseBody); }
@Test public void loadBanner_shouldCauseServerDimensionsToBeHonoredWhenLayingOutView() throws Exception { subject.loadBanner(context, customEventBannerListener, localExtras, serverExtras); MoPubView moPubView = TestMoPubViewFactory.getSingletonMock(); stub(moPubView.getContext()).toReturn(context); AdViewController adViewController = new AdViewController(context, moPubView); AdResponse adResponse = new AdResponse.Builder().setDimensions(320, 50).build(); adViewController.onAdLoadSuccess(adResponse); adViewController.setAdContentView(htmlBannerWebView); ArgumentCaptor<FrameLayout.LayoutParams> layoutParamsCaptor = ArgumentCaptor.forClass(FrameLayout.LayoutParams.class); verify(moPubView).addView(eq(htmlBannerWebView), layoutParamsCaptor.capture()); FrameLayout.LayoutParams layoutParams = layoutParamsCaptor.getValue(); assertThat(layoutParams.width).isEqualTo(320); assertThat(layoutParams.height).isEqualTo(50); assertThat(layoutParams.gravity).isEqualTo(Gravity.CENTER); }