Exemple #1
0
  protected void loadCustomEvent(String customEventClassName, Map<String, String> serverExtras) {
    if (mAdViewController == null) {
      return;
    }
    if (TextUtils.isEmpty(customEventClassName)) {
      MoPubLog.d("Couldn't invoke custom event because the server did not specify one.");
      loadFailUrl(ADAPTER_NOT_FOUND);
      return;
    }

    if (mCustomEventBannerAdapter != null) {
      mCustomEventBannerAdapter.invalidate();
    }

    MoPubLog.d("Loading custom event adapter.");

    mCustomEventBannerAdapter =
        CustomEventBannerAdapterFactory.create(
            this,
            customEventClassName,
            serverExtras,
            mAdViewController.getBroadcastIdentifier(),
            mAdViewController.getAdReport());
    mCustomEventBannerAdapter.loadAd();
  }
  @Test
  public void loadAd_whenCallingOnBannerFailed_shouldCancelExistingTimeoutRunnable()
      throws Exception {
    Robolectric.pauseMainLooper();

    Answer justCallOnBannerFailed =
        new Answer() {
          @Override
          public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(1);
            subject.onBannerFailed(null);
            assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(0);
            return null;
          }
        };

    doAnswer(justCallOnBannerFailed)
        .when(banner)
        .loadBanner(
            any(Context.class),
            any(CustomEventBanner.CustomEventBannerListener.class),
            any(Map.class),
            any(Map.class));

    assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(0);
    subject.loadAd();
    assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(0);
  }
  @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_shouldScheduleTimeout_bannerLoadedAndFailed_shouldCancelTimeout()
      throws Exception {
    Robolectric.pauseMainLooper();

    assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(0);

    subject.loadAd();
    assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(1);

    subject.onBannerLoaded(null);
    assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(0);

    subject.loadAd();
    assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(1);

    subject.onBannerFailed(null);
    assertThat(Robolectric.getUiThreadScheduler().enqueuedTaskCount()).isEqualTo(0);
  }
  @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 timeout_shouldSignalFailureAndInvalidateWithDefaultDelay() throws Exception {
    subject.loadAd();

    Robolectric.idleMainLooper(CustomEventBannerAdapter.DEFAULT_BANNER_TIMEOUT_DELAY - 1);
    verify(moPubView, never()).loadFailUrl(eq(NETWORK_TIMEOUT));
    assertThat(subject.isInvalidated()).isFalse();

    Robolectric.idleMainLooper(1);
    verify(moPubView).loadFailUrl(eq(NETWORK_TIMEOUT));
    assertThat(subject.isInvalidated()).isTrue();
  }
  @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));
  }
  @Test
  public void invalidate_shouldCauseLoadAdToDoNothing() throws Exception {
    subject.invalidate();

    subject.loadAd();

    verify(banner, never())
        .loadBanner(
            any(Context.class),
            any(CustomEventBannerListener.class),
            any(Map.class),
            any(Map.class));
  }
  @Test
  public void timeout_withNonNullAdTimeoutDelay_shouldSignalFailureAndInvalidateWithCustomDelay()
      throws Exception {
    stub(moPubView.getAdTimeoutDelay()).toReturn(77);

    subject.loadAd();

    Robolectric.idleMainLooper(77000 - 1);
    verify(moPubView, never()).loadFailUrl(eq(NETWORK_TIMEOUT));
    assertThat(subject.isInvalidated()).isFalse();

    Robolectric.idleMainLooper(1);
    verify(moPubView).loadFailUrl(eq(NETWORK_TIMEOUT));
    assertThat(subject.isInvalidated()).isTrue();
  }
  @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));
  }