Search in sources :

Example 26 with MockRequest

use of com.android.volley.mock.MockRequest in project TaEmCasa by Dionen.

the class NetworkDispatcherTest method setUp.

@Before
public void setUp() throws Exception {
    mDelivery = new MockResponseDelivery();
    mNetworkQueue = new WaitableQueue();
    mNetwork = new MockNetwork();
    mCache = new MockCache();
    mRequest = new MockRequest();
    mDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery);
    mDispatcher.start();
}
Also used : MockResponseDelivery(com.android.volley.mock.MockResponseDelivery) WaitableQueue(com.android.volley.mock.WaitableQueue) MockNetwork(com.android.volley.mock.MockNetwork) MockRequest(com.android.volley.mock.MockRequest) MockCache(com.android.volley.mock.MockCache) Before(org.junit.Before)

Example 27 with MockRequest

use of com.android.volley.mock.MockRequest in project TaEmCasa by Dionen.

the class RequestQueueIntegrationTest method add_requestProcessedInCorrectOrder.

@Test
public void add_requestProcessedInCorrectOrder() throws Exception {
    // Enqueue 2 requests with different cache keys, and different priorities. The second, higher priority request
    // takes 20ms.
    // Assert that first request is only handled after the first one has been parsed and delivered.
    MockRequest lowerPriorityReq = new MockRequest();
    MockRequest higherPriorityReq = new MockRequest();
    lowerPriorityReq.setCacheKey("1");
    higherPriorityReq.setCacheKey("2");
    lowerPriorityReq.setPriority(Priority.LOW);
    higherPriorityReq.setPriority(Priority.HIGH);
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {

        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for higher request
    when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(lowerPriorityReq);
    queue.add(higherPriorityReq);
    queue.start();
    // you cannot do strict order verification in combination with timeouts with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, while higherPriorityReq should be processing
    verifyNoMoreInteractions(listener);
    // verify higherPriorityReq goes through first
    verify(listener, timeout(100)).onRequestFinished(higherPriorityReq);
    // verify lowerPriorityReq goes last
    verify(listener, timeout(10)).onRequestFinished(lowerPriorityReq);
    queue.stop();
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockRequest(com.android.volley.mock.MockRequest) NoCache(com.android.volley.toolbox.NoCache) RequestFinishedListener(com.android.volley.RequestQueue.RequestFinishedListener) Test(org.junit.Test)

Example 28 with MockRequest

use of com.android.volley.mock.MockRequest in project TaEmCasa by Dionen.

the class RequestQueueIntegrationTest method add_requestFinishedListenerCanceled.

/**
     * Verify RequestFinishedListeners are informed when requests are canceled
     *
     * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
     */
@Test
public void add_requestFinishedListenerCanceled() throws Exception {
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Request request = new MockRequest();
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {

        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(200);
            return mock(NetworkResponse.class);
        }
    };
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    when(mMockNetwork.performRequest(request)).thenAnswer(delayAnswer);
    queue.addRequestFinishedListener(listener);
    queue.start();
    queue.add(request);
    request.cancel();
    verify(listener, timeout(100)).onRequestFinished(request);
    queue.stop();
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockRequest(com.android.volley.mock.MockRequest) MockRequest(com.android.volley.mock.MockRequest) NoCache(com.android.volley.toolbox.NoCache) RequestFinishedListener(com.android.volley.RequestQueue.RequestFinishedListener) Test(org.junit.Test)

Example 29 with MockRequest

use of com.android.volley.mock.MockRequest in project TaEmCasa by Dionen.

the class RequestQueueIntegrationTest method add_requestFinishedListenerError.

/**
     * Verify RequestFinishedListeners are informed when request errors
     *
     * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
     */
@Test
public void add_requestFinishedListenerError() throws Exception {
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Request request = new MockRequest();
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    when(mMockNetwork.performRequest(request)).thenThrow(new VolleyError());
    queue.addRequestFinishedListener(listener);
    queue.start();
    queue.add(request);
    verify(listener, timeout(100)).onRequestFinished(request);
    queue.stop();
}
Also used : MockRequest(com.android.volley.mock.MockRequest) MockRequest(com.android.volley.mock.MockRequest) NoCache(com.android.volley.toolbox.NoCache) RequestFinishedListener(com.android.volley.RequestQueue.RequestFinishedListener) Test(org.junit.Test)

Aggregations

MockRequest (com.android.volley.mock.MockRequest)29 NoCache (com.android.volley.toolbox.NoCache)16 RequestFinishedListener (com.android.volley.RequestQueue.RequestFinishedListener)15 Test (org.junit.Test)15 Before (org.junit.Before)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 Answer (org.mockito.stubbing.Answer)9 MockCache (com.android.volley.mock.MockCache)8 MockResponseDelivery (com.android.volley.mock.MockResponseDelivery)8 WaitableQueue (com.android.volley.mock.WaitableQueue)8 MockNetwork (com.android.volley.mock.MockNetwork)5 ImmediateResponseDelivery (com.android.volley.utils.ImmediateResponseDelivery)4 Priority (com.android.volley.Request.Priority)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1