CustomEventInterstitialAdapter implements CustomEventInterstitialListener { void loadInterstitial() { if (isInvalidated() || mCustomEventInterstitial == null) { return; } if (getTimeoutDelayMilliseconds() > 0) { mHandler.postDelayed(mTimeout, getTimeoutDelayMilliseconds()); } mCustomEventInterstitial.loadInterstitial(mContext, this, mLocalExtras, mServerExtras); } CustomEventInterstitialAdapter(final MoPubInterstitial moPubInterstitial, final String className, final String jsonParams); @Override void onInterstitialLoaded(); @Override void onInterstitialFailed(MoPubErrorCode errorCode); @Override void onInterstitialShown(); @Override void onInterstitialClicked(); @Override void onLeaveApplication(); @Override void onInterstitialDismissed(); static final int DEFAULT_INTERSTITIAL_TIMEOUT_DELAY; }
@Test public void loadInterstitial_shouldHaveEmptyServerExtrasOnInvalidJsonParams() throws Exception { subject = new CustomEventInterstitialAdapter(moPubInterstitial, CLASS_NAME, "{this is terrible JSON"); subject.loadInterstitial(); expectedLocalExtras.put("Ad-Configuration", null); verify(interstitial).loadInterstitial( any(Context.class), eq(subject), eq(expectedLocalExtras), eq(expectedServerExtras) ); }
@Test public void loadInterstitial_shouldPropagateLocationInLocalExtras() throws Exception { Location expectedLocation = new Location(""); expectedLocation.setLongitude(10.0); expectedLocation.setLongitude(20.1); stub(moPubInterstitial.getLocation()).toReturn(expectedLocation); subject = new CustomEventInterstitialAdapter(moPubInterstitial, CLASS_NAME, null); subject.loadInterstitial(); expectedLocalExtras.put("Ad-Configuration", null); expectedLocalExtras.put("location", moPubInterstitial.getLocation()); verify(interstitial).loadInterstitial( any(Context.class), eq(subject), eq(expectedLocalExtras), eq(expectedServerExtras) ); }
@Test public void loadInterstitial_shouldPropagateJsonParamsInServerExtras() throws Exception { subject.loadInterstitial(); expectedLocalExtras.put("Ad-Configuration", null); expectedServerExtras.put("key", "value"); expectedServerExtras.put("a different key", "a different value"); verify(interstitial).loadInterstitial( any(Context.class), eq(subject), eq(expectedLocalExtras), eq(expectedServerExtras) ); }
@Test public void loadInterstitial_shouldScheduleTimeoutRunnableBeforeCallingLoadInterstitial() 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(interstitial) .loadInterstitial( any(Context.class), any(CustomEventInterstitialListener.class), any(Map.class), any(Map.class) ); subject.loadInterstitial(); }