com.android.volley.utils.CacheTestUtils Java Examples

The following examples show how to use com.android.volley.utils.CacheTestUtils. 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: CacheDispatcherTest.java    From volley with 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 File: CacheDispatcherTest.java    From volley with 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 File: CacheDispatcherTest.java    From volley with 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 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 #5
Source File: CacheDispatcherTest.java    From android-project-wo2b with 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 #6
Source File: CacheDispatcherTest.java    From android-project-wo2b with 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 #7
Source File: ResponseDeliveryTest.java    From android-discourse with 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 #8
Source File: CacheDispatcherTest.java    From android-discourse with 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 #9
Source File: CacheDispatcherTest.java    From android-discourse with 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 #10
Source File: CacheDispatcherTest.java    From android-discourse with 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 #11
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 #12
Source File: CacheDispatcherTest.java    From product-emm with 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 #13
Source File: CacheDispatcherTest.java    From product-emm with 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 #14
Source File: CacheDispatcherTest.java    From product-emm with 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 #15
Source File: CacheDispatcherTest.java    From android-project-wo2b with 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 #16
Source File: CacheDispatcherTest.java    From product-emm with 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 #17
Source File: CacheDispatcherTest.java    From product-emm with 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 #18
Source File: CacheDispatcherTest.java    From product-emm with 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 #19
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 #20
Source File: CacheDispatcherTest.java    From CrossBow with 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 #21
Source File: CacheDispatcherTest.java    From CrossBow with 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 #22
Source File: CacheDispatcherTest.java    From CrossBow with 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 #23
Source File: CacheDispatcherTest.java    From device-database with 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 #24
Source File: ResponseDeliveryTest.java    From volley 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 #25
Source File: CacheDispatcherTest.java    From volley with 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 #26
Source File: CacheDispatcherTest.java    From volley with 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 #27
Source File: CacheDispatcherTest.java    From volley with 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 #28
Source File: CacheDispatcherTest.java    From volley with 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 #29
Source File: ResponseDeliveryTest.java    From device-database 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 #30
Source File: CacheDispatcherTest.java    From device-database with 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);
}