com.android.volley.mock.MockRequest Java Examples

The following examples show how to use com.android.volley.mock.MockRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #2
Source File: RequestQueueIntegrationTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
/**
 * Verify RequestFinishedListeners are informed when requests are successfully delivered
 *
 * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue
 * interaction
 */
@Test public void add_requestFinishedListenerSuccess() throws Exception {
    NetworkResponse response = mock(NetworkResponse.class);
    Request request = new MockRequest();
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    RequestFinishedListener listener2 = mock(RequestFinishedListener.class);
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);

    queue.addRequestFinishedListener(listener);
    queue.addRequestFinishedListener(listener2);
    queue.start();
    queue.add(request);

    verify(listener, timeout(100)).onRequestFinished(request);
    verify(listener2, timeout(100)).onRequestFinished(request);

    queue.stop();
}
 
Example #3
Source File: RequestQueueIntegrationTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #4
Source File: RequestQueueTest.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
public void testCancelAll_onlyCorrectTag() throws Exception {
    MockNetwork network = new MockNetwork();
    RequestQueue queue = new RequestQueue(new NoCache(), network, 3, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    MockRequest req1 = new MockRequest();
    req1.setTag(tagA);
    MockRequest req2 = new MockRequest();
    req2.setTag(tagB);
    MockRequest req3 = new MockRequest();
    req3.setTag(tagA);
    MockRequest req4 = new MockRequest();
    req4.setTag(tagA);

    queue.add(req1); // A
    queue.add(req2); // B
    queue.add(req3); // A
    queue.cancelAll(tagA);
    queue.add(req4); // A

    assertTrue(req1.cancel_called); // A cancelled
    assertFalse(req2.cancel_called); // B not cancelled
    assertTrue(req3.cancel_called); // A cancelled
    assertFalse(req4.cancel_called); // A added after cancel not cancelled
}
 
Example #5
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #6
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * Verify RequestFinishedListeners are informed when requests are successfully delivered
 *
 * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
 */
@Test public void add_requestFinishedListenerSuccess() throws Exception {
    NetworkResponse response = mock(NetworkResponse.class);
    Request request = new MockRequest();
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    RequestFinishedListener listener2 = mock(RequestFinishedListener.class);
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);

    queue.addRequestFinishedListener(listener);
    queue.addRequestFinishedListener(listener2);
    queue.start();
    queue.add(request);

    verify(listener, timeout(100)).onRequestFinished(request);
    verify(listener2, timeout(100)).onRequestFinished(request);

    queue.stop();
}
 
Example #7
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * Verify RequestFinishedListeners are informed when requests are successfully delivered
 *
 * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
 */
@Test public void add_requestFinishedListenerSuccess() throws Exception {
    NetworkResponse response = mock(NetworkResponse.class);
    Request request = new MockRequest();
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    RequestFinishedListener listener2 = mock(RequestFinishedListener.class);
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);

    queue.addRequestFinishedListener(listener);
    queue.addRequestFinishedListener(listener2);
    queue.start();
    queue.add(request);

    verify(listener, timeout(100)).onRequestFinished(request);
    verify(listener2, timeout(100)).onRequestFinished(request);

    queue.stop();
}
 
Example #8
Source File: RequestQueueIntegrationTest.java    From device-database with Apache License 2.0 6 votes vote down vote up
/**
 * Verify RequestFinishedListeners are informed when requests are successfully delivered
 *
 * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
 */
@Test public void add_requestFinishedListenerSuccess() throws Exception {
    NetworkResponse response = mock(NetworkResponse.class);
    Request request = new MockRequest();
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    RequestFinishedListener listener2 = mock(RequestFinishedListener.class);
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);

    queue.addRequestFinishedListener(listener);
    queue.addRequestFinishedListener(listener2);
    queue.start();
    queue.add(request);

    verify(listener, timeout(100)).onRequestFinished(request);
    verify(listener2, timeout(100)).onRequestFinished(request);

    queue.stop();
}
 
Example #9
Source File: RequestQueueIntegrationTest.java    From device-database with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #10
Source File: RequestQueueTest.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
public void testCancelAll_onlyCorrectTag() throws Exception {
    MockNetwork network = new MockNetwork();
    RequestQueue queue = new RequestQueue(new NoCache(), network, 3, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    MockRequest req1 = new MockRequest();
    req1.setTag(tagA);
    MockRequest req2 = new MockRequest();
    req2.setTag(tagB);
    MockRequest req3 = new MockRequest();
    req3.setTag(tagA);
    MockRequest req4 = new MockRequest();
    req4.setTag(tagA);

    queue.add(req1); // A
    queue.add(req2); // B
    queue.add(req3); // A
    queue.cancelAll(tagA);
    queue.add(req4); // A

    assertTrue(req1.cancel_called); // A cancelled
    assertFalse(req2.cancel_called); // B not cancelled
    assertTrue(req3.cancel_called); // A cancelled
    assertFalse(req4.cancel_called); // A added after cancel not cancelled
}
 
Example #11
Source File: RequestQueueIntegrationTest.java    From CrossBow with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #12
Source File: RequestQueueIntegrationTest.java    From volley with Apache License 2.0 6 votes vote down vote up
/** Verify RequestFinishedListeners are informed when requests are successfully delivered. */
@Test
public void add_requestFinishedListenerSuccess() throws Exception {
    MockRequest request = new MockRequest();
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);

    queue.addRequestFinishedListener(mMockListener);
    queue.addRequestFinishedListener(mMockListener2);
    queue.start();
    queue.add(request);

    verify(mMockListener, timeout(10000)).onRequestFinished(request);
    verify(mMockListener2, timeout(10000)).onRequestFinished(request);

    queue.stop();
}
 
Example #13
Source File: RequestQueueIntegrationTest.java    From volley with Apache License 2.0 6 votes vote down vote up
/** Verify RequestFinishedListeners are informed when requests are canceled. */
@Test
public void add_requestFinishedListenerCanceled() throws Exception {
    MockRequest 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(mMockListener);
    queue.start();
    queue.add(request);

    request.cancel();
    verify(mMockListener, timeout(10000)).onRequestFinished(request);
    queue.stop();
}
 
Example #14
Source File: RequestQueueIntegrationTest.java    From CrossBow with Apache License 2.0 6 votes vote down vote up
/**
 * Verify RequestFinishedListeners are informed when requests are successfully delivered
 *
 * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
 */
@Test public void add_requestFinishedListenerSuccess() throws Exception {
    NetworkResponse response = mock(NetworkResponse.class);
    Request request = new MockRequest();
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    RequestFinishedListener listener2 = mock(RequestFinishedListener.class);
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);

    queue.addRequestFinishedListener(listener);
    queue.addRequestFinishedListener(listener2);
    queue.start();
    queue.add(request);

    verify(listener, timeout(100)).onRequestFinished(request);
    verify(listener2, timeout(100)).onRequestFinished(request);

    queue.stop();
}
 
Example #15
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that requests with same cache key are processed in order.
 *
 * Needs to be an integration test because relies on complex interations between various queues
 */
@Test public void add_dedupeByCacheKey() throws Exception {
    // Enqueue 2 requests with the same cache key. The first request takes 20ms. Assert that the
    // second request is only handled after the first one has been parsed and delivered.
    Request req1 = new MockRequest();
    Request req2 = new MockRequest();
    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 first
    when(mMockNetwork.performRequest(req1)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(req2)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 3, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(req1);
    queue.add(req2);
    queue.start();

    // you cannot do strict order verification with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, then verify req1 goes through
    verifyNoMoreInteractions(listener);
    verify(listener, timeout(100)).onRequestFinished(req1);
    verify(listener, timeout(10)).onRequestFinished(req2);
    queue.stop();
}
 
Example #16
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that requests with same cache key are processed in order.
 *
 * Needs to be an integration test because relies on complex interations between various queues
 */
@Test public void add_dedupeByCacheKey() throws Exception {
    // Enqueue 2 requests with the same cache key. The first request takes 20ms. Assert that the
    // second request is only handled after the first one has been parsed and delivered.
    Request req1 = new MockRequest();
    Request req2 = new MockRequest();
    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 first
    when(mMockNetwork.performRequest(req1)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(req2)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 3, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(req1);
    queue.add(req2);
    queue.start();

    // you cannot do strict order verification with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, then verify req1 goes through
    verifyNoMoreInteractions(listener);
    verify(listener, timeout(100)).onRequestFinished(req1);
    verify(listener, timeout(10)).onRequestFinished(req2);
    queue.stop();
}
 
Example #17
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #18
Source File: CacheDispatcherTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
    mCacheQueue = new WaitableQueue();
    mNetworkQueue = new WaitableQueue();
    mCache = new MockCache();
    mDelivery = new MockResponseDelivery();

    mRequest = new MockRequest();

    mDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
    mDispatcher.start();
}
 
Example #19
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #20
Source File: ResponseDeliveryTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
    // Make the delivery just run its posted responses immediately.
    mDelivery = new ImmediateResponseDelivery();
    mRequest = new MockRequest();
    mRequest.setSequence(1);
    byte[] data = new byte[16];
    Cache.Entry cacheEntry = CacheTestUtils.makeRandomCacheEntry(data);
    mSuccessResponse = Response.success(data, cacheEntry);
}
 
Example #21
Source File: NetworkDispatcherTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #22
Source File: NetworkDispatcherTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #23
Source File: CacheDispatcherTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
    mCacheQueue = new WaitableQueue();
    mNetworkQueue = new WaitableQueue();
    mCache = new MockCache();
    mDelivery = new MockResponseDelivery();

    mRequest = new MockRequest();

    mDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
    mDispatcher.start();
}
 
Example #24
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #25
Source File: RequestQueueIntegrationTest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #26
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #27
Source File: ResponseDeliveryTest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
    // Make the delivery just run its posted responses immediately.
    mDelivery = new ImmediateResponseDelivery();
    mRequest = new MockRequest();
    mRequest.setSequence(1);
    byte[] data = new byte[16];
    Cache.Entry cacheEntry = CacheTestUtils.makeRandomCacheEntry(data);
    mSuccessResponse = Response.success(data, cacheEntry);
}
 
Example #28
Source File: NetworkDispatcherTest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #29
Source File: CacheDispatcherTest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() throws Exception {
    mCacheQueue = new WaitableQueue();
    mNetworkQueue = new WaitableQueue();
    mCache = new MockCache();
    mDelivery = new MockResponseDelivery();

    mRequest = new MockRequest();

    mDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
    mDispatcher.start();
}
 
Example #30
Source File: RequestQueueIntegrationTest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
@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();
}