ServerPositioningSource implements PositioningSource { @Override public void loadPositions(@NonNull String adUnitId, @NonNull PositioningListener listener) { if (mDownloadTask != null) { mDownloadTask.cancel(true); mDownloadTask = null; } if (mRetryCount > 0) { mRetryHandler.removeCallbacks(mRetryRunnable); mRetryCount = 0; } mListener = listener; mRetryUrl = new PositioningUrlGenerator(mContext) .withAdUnitId(adUnitId) .generateUrlString(Constants.HOST); requestPositioningInternal(); } ServerPositioningSource(@NonNull final Context context); @VisibleForTesting  ServerPositioningSource(@NonNull final Context context,
            @NonNull final DownloadTaskProvider downloadTaskProvider); @Override void loadPositions(@NonNull String adUnitId, @NonNull PositioningListener listener);  }
@Config(reportSdk = VERSION_CODES.ICE_CREAM_SANDWICH) @Test public void loadPositions_atLeastIcs_shouldExecuteDownloadTask() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTask).executeOnExecutor(any(Executor.class), any(HttpGet.class)); }
@Config(reportSdk = VERSION_CODES.GINGERBREAD_MR1) @Test public void loadPositions_beforeIcs_shouldExecuteDownloadTask() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTask).execute(any(HttpGet.class)); }
@Test public void loadPositionsTwice_shouldCancelPreviousDownloadTask_shouldNotCallListener() { subject.loadPositions("test_ad_unit", mockPositioningListener); subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTask).cancel(true); verify(mockPositioningListener, never()).onFailed(); verify(mockPositioningListener, never()).onLoad(any(MoPubClientPositioning.class)); }
@Test public void loadPositionsTwice_withPendingRetry_shouldNotCancelPreviousDownloadTask() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTaskProvider).get(taskListenerCaptor.capture()); taskListenerCaptor.getValue().onComplete("some_url", mockValidResponse); subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTask, never()).cancel(anyBoolean()); }
@Test public void loadPositions_thenComplete_withValidResponse_shouldCallOnLoadListener() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTaskProvider).get(taskListenerCaptor.capture()); taskListenerCaptor.getValue().onComplete("some_url", mockValidResponse); verify(mockPositioningListener).onLoad(positioningCaptor.capture()); MoPubClientPositioning positioning = positioningCaptor.getValue(); assertThat(positioning.getFixedPositions()).isEmpty(); assertThat(positioning.getRepeatingInterval()).isEqualTo(MoPubClientPositioning.NO_REPEAT); }
@Config(reportSdk = VERSION_CODES.ICE_CREAM_SANDWICH) @Test public void loadPositions_thenComplete_withNullResponse_shouldRetry() throws Exception { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTaskProvider).get(taskListenerCaptor.capture()); taskListenerCaptor.getValue().onComplete("some_url", null); Robolectric.getUiThreadScheduler().advanceToLastPostedRunnable(); verify(mockDownloadTask, times(2)) .executeOnExecutor(any(Executor.class), any(HttpGet.class)); }
@Config(reportSdk = VERSION_CODES.ICE_CREAM_SANDWICH) @Test public void loadPositions_thenComplete_withNotFoundResponse_shouldRetry() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTaskProvider).get(taskListenerCaptor.capture()); taskListenerCaptor.getValue().onComplete("some_url", mockNotFoundResponse); Robolectric.getUiThreadScheduler().advanceToLastPostedRunnable(); verify(mockDownloadTask, times(2)) .executeOnExecutor(any(Executor.class), any(HttpGet.class)); }
@Config(reportSdk = VERSION_CODES.ICE_CREAM_SANDWICH) @Test public void loadPositions_thenComplete_withWarmingUpResponse_shouldRetry() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTaskProvider).get(taskListenerCaptor.capture()); taskListenerCaptor.getValue().onComplete("some_url", mockWarmingUpJsonResponse); Robolectric.getUiThreadScheduler().advanceToLastPostedRunnable(); verify(mockDownloadTask, times(2)) .executeOnExecutor(any(Executor.class), any(HttpGet.class)); }
@Config(reportSdk = VERSION_CODES.ICE_CREAM_SANDWICH) @Test public void loadPositions_thenComplete_withInvalidJsonResponse_shouldRetry() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTaskProvider).get(taskListenerCaptor.capture()); taskListenerCaptor.getValue().onComplete("some_url", mockInvalidJsonResponse); Robolectric.getUiThreadScheduler().advanceToLastPostedRunnable(); verify(mockDownloadTask, times(2)) .executeOnExecutor(any(Executor.class), any(HttpGet.class)); }
@Config(reportSdk = VERSION_CODES.ICE_CREAM_SANDWICH) @Test public void loadPositions_withPendingRetry_shouldNotRetry() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTaskProvider).get(taskListenerCaptor.capture()); taskListenerCaptor.getValue().onComplete("some_url", mockInvalidJsonResponse); subject.loadPositions("test_ad_unit", mockPositioningListener); Robolectric.getUiThreadScheduler().advanceToLastPostedRunnable(); verify(mockDownloadTask, times(2)) .executeOnExecutor(any(Executor.class), any(HttpGet.class)); }
@Test public void loadPositions_thenFailAfterMaxRetryTime_shouldCallFailureHandler() { ServerPositioningSource.MAXIMUM_RETRY_TIME_MILLISECONDS = 999; subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockDownloadTaskProvider).get(taskListenerCaptor.capture()); taskListenerCaptor.getValue().onComplete("some_url", mockInvalidJsonResponse); Robolectric.getUiThreadScheduler().advanceToLastPostedRunnable(); verify(mockPositioningListener).onFailed(); }