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; if (extrasAreValid(serverExtras)) { htmlData = Uri.decode(serverExtras.get(HTML_RESPONSE_BODY_KEY)); redirectUrl = serverExtras.get(REDIRECT_URL_KEY); clickthroughUrl = serverExtras.get(CLICKTHROUGH_URL_KEY); isScrollable = Boolean.valueOf(serverExtras.get(SCROLLABLE_KEY)); } else { customEventBannerListener.onBannerFailed(NETWORK_INVALID_STATE); return; } AdConfiguration adConfiguration = AdConfiguration.extractFromMap(localExtras); mHtmlBannerWebView = HtmlBannerWebViewFactory.create(context, customEventBannerListener, isScrollable, redirectUrl, clickthroughUrl, adConfiguration); 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); HttpResponse response = new TestHttpResponseWithHeaders(200, "I ain't got no-body"); response.addHeader("X-Width", "320"); response.addHeader("X-Height", "50"); adViewController.configureUsingHttpResponse(response); 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); }