CustomEventBannerAdapter implements CustomEventBannerListener { void loadAd() { if (isInvalidated() || mCustomEventBanner == null) { return; } if (getTimeoutDelayMilliseconds() > 0) { mHandler.postDelayed(mTimeout, getTimeoutDelayMilliseconds()); } mCustomEventBanner.loadBanner(mContext, this, mLocalExtras, mServerExtras); } CustomEventBannerAdapter(MoPubView moPubView, String className, String classData); @Override void onBannerLoaded(View bannerView); @Override void onBannerFailed(MoPubErrorCode errorCode); @Override void onBannerExpanded(); @Override void onBannerCollapsed(); @Override void onBannerClicked(); @Override void onLeaveApplication(); static final int DEFAULT_BANNER_TIMEOUT_DELAY; }
@Test public void loadAd_shouldHaveEmptyServerExtrasOnInvalidJsonParams() throws Exception { subject = new CustomEventBannerAdapter(moPubView, CLASS_NAME, "{this is terrible JSON"); subject.loadAd(); verify(banner).loadBanner( any(Context.class), eq(subject), eq(expectedLocalExtras), eq(expectedServerExtras) ); }
@Test public void loadAd_shouldPropagateLocationInLocalExtras() throws Exception { Location expectedLocation = new Location(""); expectedLocation.setLongitude(10.0); expectedLocation.setLongitude(20.1); stub(moPubView.getLocation()).toReturn(expectedLocation); subject = new CustomEventBannerAdapter(moPubView, CLASS_NAME, null); subject.loadAd(); expectedLocalExtras.put("location", moPubView.getLocation()); verify(banner).loadBanner( any(Context.class), eq(subject), eq(expectedLocalExtras), eq(expectedServerExtras) ); }
@Test public void loadAd_shouldPropagateJsonParamsInServerExtras() throws Exception { subject.loadAd(); expectedServerExtras.put("key", "value"); expectedServerExtras.put("a different key", "a different value"); verify(banner).loadBanner( any(Context.class), eq(subject), eq(expectedLocalExtras), eq(expectedServerExtras) ); }
@Test public void loadAd_shouldScheduleTimeoutRunnableBeforeCallingLoadBanner() throws Exception { Robolectric.pauseMainLooper(); assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(0); Answer assertTimeoutRunnableHasStarted = new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(1); return null; } }; doAnswer(assertTimeoutRunnableHasStarted) .when(banner) .loadBanner( any(Context.class), any(CustomEventBannerListener.class), any(Map.class), any(Map.class) ); subject.loadAd(); }
@Test public void init_whenPassedHtmlData_shouldPutItInLocalExtras() throws Exception { String expectedHtmlData = "expected html data"; expectedServerExtras.put(HTML_RESPONSE_BODY_KEY, expectedHtmlData); subject = new CustomEventBannerAdapter(moPubView, CLASS_NAME, "{\"Html-Response-Body\":\"expected html data\"}"); subject.loadAd(); verify(banner).loadBanner( any(Context.class), eq(subject), eq(expectedLocalExtras), eq(expectedServerExtras) ); }