Java Code Examples for com.android.volley.utils.CacheTestUtils

The following examples show how to use com.android.volley.utils.CacheTestUtils. These examples are extracted from open source projects. 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 Project: volley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void softExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
    when(mCache.get(anyString())).thenReturn(entry);
    mDispatcher.processRequest(mRequest);

    // Soft expiration needs to use the deferred Runnable variant of postResponse,
    // so make sure it gets to run.
    ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
    verify(mDelivery).postResponse(any(Request.class), any(Response.class), runnable.capture());
    runnable.getValue().run();
    // This way we can verify the behavior of the Runnable as well.
    verify(mNetworkQueue).put(mRequest);
    assertSame(entry, mRequest.getCacheEntry());

    verify(mDelivery, never()).postError(any(Request.class), any(VolleyError.class));
}
 
Example 2
Source Project: volley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void freshCacheHit_parseError() throws Exception {
    Request request = mock(Request.class);
    when(request.parseNetworkResponse(any(NetworkResponse.class)))
            .thenReturn(Response.error(new ParseError()));
    when(request.getCacheKey()).thenReturn("cache/key");
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
    when(mCache.get(anyString())).thenReturn(entry);

    mDispatcher.processRequest(request);

    verifyNoResponse(mDelivery);
    verify(mNetworkQueue).put(request);
    assertNull(request.getCacheEntry());
    verify(mCache).invalidate("cache/key", true);
    verify(request).addMarker("cache-parsing-failed");
}
 
Example 3
Source Project: volley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void processRequestNotifiesListener() throws Exception {
    RequestQueue.RequestEventListener listener = mock(RequestQueue.RequestEventListener.class);
    RequestQueue queue = new RequestQueue(mCache, mNetwork, 0, mDelivery);
    queue.addRequestEventListener(listener);
    mRequest.setRequestQueue(queue);

    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
    when(mCache.get(anyString())).thenReturn(entry);
    mDispatcher.processRequest(mRequest);

    InOrder inOrder = inOrder(listener);
    inOrder.verify(listener)
            .onRequestEvent(mRequest, RequestQueue.RequestEvent.REQUEST_CACHE_LOOKUP_STARTED);
    inOrder.verify(listener)
            .onRequestEvent(mRequest, RequestQueue.RequestEvent.REQUEST_CACHE_LOOKUP_FINISHED);
    inOrder.verifyNoMoreInteractions();
}
 
Example 4
Source Project: volley   Source File: ResponseDeliveryTest.java    License: 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 5
Source Project: volley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void nonExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
    when(mCache.get(anyString())).thenReturn(entry);
    mDispatcher.processRequest(mRequest);
    verify(mDelivery).postResponse(any(Request.class), any(Response.class));
    verify(mDelivery, never()).postError(any(Request.class), any(VolleyError.class));
}
 
Example 6
Source Project: volley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void expiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, true, true);
    when(mCache.get(anyString())).thenReturn(entry);
    mDispatcher.processRequest(mRequest);
    verifyNoResponse(mDelivery);
    verify(mNetworkQueue).put(mRequest);
    assertSame(entry, mRequest.getCacheEntry());
}
 
Example 7
Source Project: volley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void duplicateSoftExpiredCacheHit_failedRequest() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
    when(mCache.get(anyString())).thenReturn(entry);

    StringRequest secondRequest =
            new StringRequest(Request.Method.GET, "http://foo", null, null);
    mRequest.setSequence(1);
    secondRequest.setSequence(2);

    mDispatcher.processRequest(mRequest);
    mDispatcher.processRequest(secondRequest);

    // Soft expiration needs to use the deferred Runnable variant of postResponse,
    // so make sure it gets to run.
    ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
    verify(mDelivery).postResponse(any(Request.class), any(Response.class), runnable.capture());
    runnable.getValue().run();
    // This way we can verify the behavior of the Runnable as well.

    verify(mNetworkQueue).put(mRequest);
    verify(mDelivery)
            .postResponse(any(Request.class), any(Response.class), any(Runnable.class));

    ((Request<?>) mRequest).notifyListenerResponseNotUsable();
    // Second request should now be in network queue.
    verify(mNetworkQueue).put(secondRequest);
}
 
Example 8
Source Project: volley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void duplicateSoftExpiredCacheHit_successfulRequest() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
    when(mCache.get(anyString())).thenReturn(entry);

    StringRequest secondRequest =
            new StringRequest(Request.Method.GET, "http://foo", null, null);
    mRequest.setSequence(1);
    secondRequest.setSequence(2);

    mDispatcher.processRequest(mRequest);
    mDispatcher.processRequest(secondRequest);

    // Soft expiration needs to use the deferred Runnable variant of postResponse,
    // so make sure it gets to run.
    ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
    verify(mDelivery).postResponse(any(Request.class), any(Response.class), runnable.capture());
    runnable.getValue().run();
    // This way we can verify the behavior of the Runnable as well.

    verify(mNetworkQueue).put(mRequest);
    verify(mDelivery)
            .postResponse(any(Request.class), any(Response.class), any(Runnable.class));

    ((Request<?>) mRequest).notifyListenerResponseReceived(Response.success(null, entry));
    // Second request should have delivered response.
    verify(mNetworkQueue, never()).put(secondRequest);
    verify(mDelivery)
            .postResponse(any(Request.class), any(Response.class), any(Runnable.class));
}
 
Example 9
Source Project: device-database   Source File: ResponseDeliveryTest.java    License: 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 10
Source Project: device-database   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void nonExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
}
 
Example 11
Source Project: device-database   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void softExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 12
Source Project: device-database   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void expiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, true, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertFalse(mDelivery.wasEitherResponseCalled());
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 13
Source Project: SaveVolley   Source File: ResponseDeliveryTest.java    License: 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 14
Source Project: SaveVolley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void nonExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
}
 
Example 15
Source Project: SaveVolley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void softExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 16
Source Project: SaveVolley   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void expiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, true, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertFalse(mDelivery.wasEitherResponseCalled());
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 17
Source Project: android-project-wo2b   Source File: ResponseDeliveryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();

    // 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 18
Source Project: android-project-wo2b   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testNonExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
}
 
Example 19
Source Project: android-project-wo2b   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testSoftExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 20
Source Project: android-project-wo2b   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, true, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertFalse(mDelivery.wasEitherResponseCalled());
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 21
Source Project: android-discourse   Source File: ResponseDeliveryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();

    // 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 22
Source Project: android-discourse   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testNonExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
}
 
Example 23
Source Project: android-discourse   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testSoftExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 24
Source Project: android-discourse   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, true, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertFalse(mDelivery.wasEitherResponseCalled());
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 25
Source Project: product-emm   Source File: ResponseDeliveryTest.java    License: 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 26
Source Project: product-emm   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void nonExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
}
 
Example 27
Source Project: product-emm   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void softExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 28
Source Project: product-emm   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void expiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, true, true);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertFalse(mDelivery.wasEitherResponseCalled());
    assertTrue(mNetworkQueue.size() > 0);
    Request request = mNetworkQueue.take();
    assertSame(entry, request.getCacheEntry());
}
 
Example 29
Source Project: product-emm   Source File: ResponseDeliveryTest.java    License: 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 30
Source Project: product-emm   Source File: CacheDispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void nonExpiredCacheHit() throws Exception {
    Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
    mCache.setEntryToReturn(entry);
    mCacheQueue.add(mRequest);
    mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
    assertTrue(mDelivery.postResponse_called);
    assertFalse(mDelivery.postError_called);
}