BallExecutorDelivery implements BallResponseDelivery { @Override public void postEmptyIntermediateResponse(BallRequest request, BallResponse.ResponseSource responseSource) { request.addMarker(MARKER_POST_EMPTY_INTERMEDIATE_RESPONSE); mResponsePoster.execute(new EmptyIntermediateDeliveryRunnable(request, responseSource, mNetworkQueue)); } BallExecutorDelivery(final Handler handler, PriorityBlockingQueue<BallRequest> networkQueue);  BallExecutorDelivery(Executor executor); @Override void postResponse(BallRequest<?> request, BallResponse<?> response); @Override void postResponseAndForwardToNetwork(BallRequest<?> request, BallResponse<?> response); @Override void postError(BallRequest<?> request, VolleyError error); @Override void postEmptyIntermediateResponse(BallRequest request, BallResponse.ResponseSource responseSource); static final String MARKER_POST_EMPTY_INTERMEDIATE_RESPONSE; static final String MARKER_POST_RESPONSE; static final String MARKER_POST_ERROR; static final String MARKER_INTERMEDIATE_RESPONSE_ALREADY_DELIVERED; static final String MARKER_DONE_WITH_RESPONSE_FROM; static final String MARKER_ERROR_IN_FINAL_RESPONSE_LET_INTERMEDIATE_CONTINUE; static final String MARKER_DONE_WITH_INTERMEDIATE_EMPTY_RESPONSE; static final String MARKER_DONE_WITH_INTERMEDIATE_RESPONSE; }
@Test public void shouldPostEmptyIntermediateResponseAndAddMarkerLog() { final BallResponse.ResponseSource responseSource = BallResponse.ResponseSource.LOCAL; Executor executorMock = mock(Executor.class); BallResponseDelivery responseDelivery = new BallExecutorDelivery(executorMock); doNothing().when(executorMock).execute(any(Runnable.class)); responseDelivery.postEmptyIntermediateResponse(mRequest, responseSource); verify(mRequest).addMarker(BallExecutorDelivery.MARKER_POST_EMPTY_INTERMEDIATE_RESPONSE); verify(executorMock).execute(argThat(new ArgumentMatcher<Runnable>() { @Override public boolean matches(Object argument) { BallExecutorDelivery.EmptyIntermediateDeliveryRunnable r = (BallExecutorDelivery.EmptyIntermediateDeliveryRunnable) argument; return r.mRequest == mRequest && r.mResponseSource == responseSource; } })); verifyNoMoreInteractions(mRequest); }
@Test public void shouldIgnoreEmptyIntermediateResponseWhenRequestIsFinished() { when(mRequest.isFinished()).thenReturn(true); when(mRequest.isIntermediateResponseDelivered()).thenReturn(false); mResponseDelivery.postEmptyIntermediateResponse(mRequest, BallResponse.ResponseSource.LOCAL); verify(mRequest, new LastInteraction()).isFinished(); }
@Test public void shouldIgnoreEmptyIntermediateResponseWhenIntermediateWasAlreadyDelivered() { when(mRequest.isFinished()).thenReturn(false); when(mRequest.isIntermediateResponseDelivered()).thenReturn(true); mResponseDelivery.postEmptyIntermediateResponse(mRequest, BallResponse.ResponseSource.LOCAL); verify(mRequest, new LastInteraction()).isIntermediateResponseDelivered(); }
@Test public void shouldPostFinalErrorWhenFromEmptyIntermediateResponseWhenFinalWasAlreadyDelivered() { VolleyError error = new VolleyError("Some error"); when(mRequest.isFinished()).thenReturn(false); when(mRequest.isIntermediateResponseDelivered()).thenReturn(false); when(mRequest.isFinalResponseDelivered()).thenReturn(true); when(mRequest.getFinalResponseError()).thenReturn(error); mResponseDelivery.postEmptyIntermediateResponse(mRequest, BallResponse.ResponseSource.LOCAL); verify(mRequest).setIntermediateResponseDelivered(true); verify(mRequest).deliverError(error); verify(mRequest).finish(BallExecutorDelivery.MARKER_DONE_WITH_INTERMEDIATE_EMPTY_RESPONSE); }
@Test public void shouldIgnoreEmptyIntermediateResponseWhenFinalIsNotDelivered() { when(mRequest.isFinished()).thenReturn(false); when(mRequest.isIntermediateResponseDelivered()).thenReturn(false); when(mRequest.isFinalResponseDelivered()).thenReturn(false); mResponseDelivery.postEmptyIntermediateResponse(mRequest, BallResponse.ResponseSource.LOCAL); verify(mRequest).setIntermediateResponseDelivered(true); verify(mRequest, never()).deliverError(any(VolleyError.class)); verify(mRequest, never()).finish(anyString()); }