Search in sources :

Example 11 with MockRequest

use of com.android.volley.mock.MockRequest in project iosched by google.

the class RequestQueueTest method makeRequests.

/**
     * Make a list of requests with random priorities.
     * @param count Number of requests to make
     */
private List<MockRequest> makeRequests(int count) {
    Request.Priority[] allPriorities = Request.Priority.values();
    Random random = new Random();
    List<MockRequest> requests = new ArrayList<MockRequest>();
    for (int i = 0; i < count; i++) {
        MockRequest request = new MockRequest();
        Request.Priority priority = allPriorities[random.nextInt(allPriorities.length)];
        request.setCacheKey(String.valueOf(i));
        request.setPriority(priority);
        requests.add(request);
    }
    return requests;
}
Also used : Random(java.util.Random) Priority(com.android.volley.Request.Priority) ArrayList(java.util.ArrayList) MockRequest(com.android.volley.mock.MockRequest) Priority(com.android.volley.Request.Priority) MockRequest(com.android.volley.mock.MockRequest)

Example 12 with MockRequest

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

the class CacheDispatcherTest method setUp.

@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();
}
Also used : MockResponseDelivery(com.android.volley.mock.MockResponseDelivery) WaitableQueue(com.android.volley.mock.WaitableQueue) MockRequest(com.android.volley.mock.MockRequest) MockCache(com.android.volley.mock.MockCache) Before(org.junit.Before)

Example 13 with MockRequest

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

the class RequestQueueIntegrationTest method add_dedupeByCacheKey.

/**
     * 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();
}
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 14 with MockRequest

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

the class RequestQueueIntegrationTest method add_requestFinishedListenerSuccess.

/**
     * 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();
}
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)

Example 15 with MockRequest

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

the class ResponseDeliveryTest method setUp.

@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);
}
Also used : ImmediateResponseDelivery(com.android.volley.utils.ImmediateResponseDelivery) MockRequest(com.android.volley.mock.MockRequest) Before(org.junit.Before)

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