ServerPositioningSource implements PositioningSource { @Override public void loadPositions(@NonNull String adUnitId, @NonNull PositioningListener listener) { if (mRequest != null) { mRequest.cancel(); mRequest = 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); @Override void loadPositions(@NonNull String adUnitId, @NonNull PositioningListener listener);  }
@Test public void loadPositions_shouldAddToRequestQueue() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockRequestQueue).add(any(Request.class)); }
@Test public void loadPositionsTwice_shouldCancelPreviousRequest_shouldNotCallListener() { subject.loadPositions("test_ad_unit", mockPositioningListener); subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockRequestQueue, times(2)).add(any(Request.class)); verify(mockPositioningListener, never()).onFailed(); verify(mockPositioningListener, never()).onLoad(any(MoPubClientPositioning.class)); }
@Test public void loadPositionsTwice_afterSuccess_shouldNotCancelPreviousRequest() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockRequestQueue).add(positionRequestCaptor.capture()); reset(mockRequestQueue); subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockRequestQueue).add(any(Request.class)); }
@Test public void loadPositions_thenComplete_withValidResponse_shouldCallOnLoadListener() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockRequestQueue).add(positionRequestCaptor.capture()); final PositioningRequest value = positionRequestCaptor.getValue(); final MoPubClientPositioning response = new MoPubClientPositioning().enableRepeatingPositions(3); value.deliverResponse(response); verify(mockPositioningListener).onLoad(eq(response)); }
@Config(reportSdk = VERSION_CODES.ICE_CREAM_SANDWICH) @Test public void loadPositions_thenComplete_withErrorResponse_shouldRetry() throws Exception { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockRequestQueue).add(positionRequestCaptor.capture()); reset(mockRequestQueue); positionRequestCaptor.getValue().deliverError(new VolleyError("Some test error")); Robolectric.getUiThreadScheduler().advanceToLastPostedRunnable(); verify(mockRequestQueue).add(any(Request.class)); }
@Config(reportSdk = VERSION_CODES.ICE_CREAM_SANDWICH) @Test public void loadPositions_withPendingRetry_shouldNotRetry() { subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockRequestQueue).add(positionRequestCaptor.capture()); reset(mockRequestQueue); positionRequestCaptor.getValue().deliverError(new VolleyError("testError")); subject.loadPositions("test_ad_unit", mockPositioningListener); Robolectric.getUiThreadScheduler().advanceToLastPostedRunnable(); verify(mockRequestQueue).add(any(Request.class)); }
@Test public void loadPositions_thenFailAfterMaxRetryTime_shouldCallFailureHandler() { ServerPositioningSource.MAXIMUM_RETRY_TIME_MILLISECONDS = 999; subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockRequestQueue).add(positionRequestCaptor.capture()); positionRequestCaptor.getValue().deliverError(new VolleyError("test error")); verify(mockPositioningListener).onFailed(); }
@Test public void loadPositions_withNoConnection_shouldLogMoPubErrorCodeNoConnection_shouldCallFailureHandler() { LogManager.getLogManager().getLogger("com.mopub").setLevel(Level.ALL); subject.loadPositions("test_ad_unit", mockPositioningListener); verify(mockRequestQueue).add(positionRequestCaptor.capture()); positionRequestCaptor.getValue().deliverError(new NoConnectionError()); verify(mockPositioningListener).onFailed(); final List<ShadowLog.LogItem> allLogMessages = ShadowLog.getLogs(); final ShadowLog.LogItem latestLogMessage = allLogMessages.get(allLogMessages.size() - 2); assertThat(latestLogMessage.msg.trim()).isEqualTo(MoPubErrorCode.NO_CONNECTION.toString()); }