Java 类com.android.volley.toolbox.NoCache 实例源码

项目:GitHub    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:GitHub    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:GitHub    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:GitHub    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:GitHub    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:GitHub    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:Codeforces    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:Codeforces    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:Codeforces    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:iosched-reader    文件:RequestQueueTest.java   
@UiThreadTest
public void testAdd_requestProcessedInCorrectOrder() throws Exception {
    int requestsToMake = 100;

    OrderCheckingNetwork network = new OrderCheckingNetwork();
    RequestQueue queue = new RequestQueue(new NoCache(), network, 1, mDelivery);

    for (Request<?> request : makeRequests(requestsToMake)) {
        queue.add(request);
    }

    network.setExpectedRequests(requestsToMake);
    queue.start();
    network.waitUntilExpectedDone(2000); // 2 seconds
    queue.stop();
}
项目:iosched-reader    文件:RequestQueueTest.java   
public void testAdd_dedupeByCacheKey() throws Exception {
    OrderCheckingNetwork network = new OrderCheckingNetwork();
    final AtomicInteger parsed = new AtomicInteger();
    final AtomicInteger delivered = new AtomicInteger();
    // Enqueue 2 requests with the same cache key. The first request takes 1.5s. Assert that the
    // second request is only handled after the first one has been parsed and delivered.
    DelayedRequest req1 = new DelayedRequest(1500, parsed, delivered);
    DelayedRequest req2 = new DelayedRequest(0, parsed, delivered) {
        @Override
        protected Response<Object> parseNetworkResponse(NetworkResponse response) {
            assertEquals(1, parsed.get());  // req1 must have been parsed.
            assertEquals(1, delivered.get());  // req1 must have been parsed.
            return super.parseNetworkResponse(response);
        }
    };
    network.setExpectedRequests(2);
    RequestQueue queue = new RequestQueue(new NoCache(), network, 3, mDelivery);
    queue.add(req1);
    queue.add(req2);
    queue.start();
    network.waitUntilExpectedDone(2000);
    queue.stop();
}
项目:iosched-reader    文件:RequestQueueTest.java   
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
}
项目:VoV    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:VoV    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:VoV    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:smconf-android    文件:RequestQueueTest.java   
@UiThreadTest
public void testAdd_requestProcessedInCorrectOrder() throws Exception {
    int requestsToMake = 100;

    OrderCheckingNetwork network = new OrderCheckingNetwork();
    RequestQueue queue = new RequestQueue(new NoCache(), network, 1, mDelivery);

    for (Request<?> request : makeRequests(requestsToMake)) {
        queue.add(request);
    }

    network.setExpectedRequests(requestsToMake);
    queue.start();
    network.waitUntilExpectedDone(2000); // 2 seconds
    queue.stop();
}
项目:smconf-android    文件:RequestQueueTest.java   
public void testAdd_dedupeByCacheKey() throws Exception {
    OrderCheckingNetwork network = new OrderCheckingNetwork();
    final AtomicInteger parsed = new AtomicInteger();
    final AtomicInteger delivered = new AtomicInteger();
    // Enqueue 2 requests with the same cache key. The first request takes 1.5s. Assert that the
    // second request is only handled after the first one has been parsed and delivered.
    DelayedRequest req1 = new DelayedRequest(1500, parsed, delivered);
    DelayedRequest req2 = new DelayedRequest(0, parsed, delivered) {
        @Override
        protected Response<Object> parseNetworkResponse(NetworkResponse response) {
            assertEquals(1, parsed.get());  // req1 must have been parsed.
            assertEquals(1, delivered.get());  // req1 must have been parsed.
            return super.parseNetworkResponse(response);
        }
    };
    network.setExpectedRequests(2);
    RequestQueue queue = new RequestQueue(new NoCache(), network, 3, mDelivery);
    queue.add(req1);
    queue.add(req2);
    queue.start();
    network.waitUntilExpectedDone(2000);
    queue.stop();
}
项目:smconf-android    文件:RequestQueueTest.java   
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
}
项目:volley    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:volley    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:volley    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:InfoQ-Android-App    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:InfoQ-Android-App    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:InfoQ-Android-App    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:volley-public-key-pinning    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:volley-public-key-pinning    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:volley-public-key-pinning    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:device-database    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:device-database    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:device-database    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:para-client-android    文件:ParaClient.java   
public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        if (ctx == null) {
            requestQueue = new RequestQueue(new NoCache(), new BasicNetwork(new OkHttp3Stack()));
        } else {
            if (StringUtils.isBlank(trustedHostname)) {
                requestQueue = Volley.newRequestQueue(ctx.getApplicationContext(),
                        new OkHttp3Stack());
            } else {
                requestQueue = Volley.newRequestQueue(ctx.getApplicationContext(),
                    new OkHttp3Stack(ClientUtils.newCustomSocketFactory(trustedHostname)));
            }
        }
    }
    return requestQueue;
}
项目:ShoppingApp    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:ShoppingApp    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:ShoppingApp    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:2015-Google-I-O-app    文件:RequestQueueTest.java   
@UiThreadTest
public void testAdd_requestProcessedInCorrectOrder() throws Exception {
    int requestsToMake = 100;

    OrderCheckingNetwork network = new OrderCheckingNetwork();
    RequestQueue queue = new RequestQueue(new NoCache(), network, 1, mDelivery);

    for (Request<?> request : makeRequests(requestsToMake)) {
        queue.add(request);
    }

    network.setExpectedRequests(requestsToMake);
    queue.start();
    network.waitUntilExpectedDone(2000); // 2 seconds
    queue.stop();
}
项目:2015-Google-I-O-app    文件:RequestQueueTest.java   
public void testAdd_dedupeByCacheKey() throws Exception {
    OrderCheckingNetwork network = new OrderCheckingNetwork();
    final AtomicInteger parsed = new AtomicInteger();
    final AtomicInteger delivered = new AtomicInteger();
    // Enqueue 2 requests with the same cache key. The first request takes 1.5s. Assert that the
    // second request is only handled after the first one has been parsed and delivered.
    DelayedRequest req1 = new DelayedRequest(1500, parsed, delivered);
    DelayedRequest req2 = new DelayedRequest(0, parsed, delivered) {
        @Override
        protected Response<Object> parseNetworkResponse(NetworkResponse response) {
            assertEquals(1, parsed.get());  // req1 must have been parsed.
            assertEquals(1, delivered.get());  // req1 must have been parsed.
            return super.parseNetworkResponse(response);
        }
    };
    network.setExpectedRequests(2);
    RequestQueue queue = new RequestQueue(new NoCache(), network, 3, mDelivery);
    queue.add(req1);
    queue.add(req2);
    queue.start();
    network.waitUntilExpectedDone(2000);
    queue.stop();
}
项目:2015-Google-I-O-app    文件:RequestQueueTest.java   
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
}
项目:Simplified-Zhihu-Daily    文件:RequestQueueTest.java   
@Test public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    Request req1 = mock(Request.class);
    when(req1.getTag()).thenReturn(tagA);
    Request req2 = mock(Request.class);
    when(req2.getTag()).thenReturn(tagB);
    Request req3 = mock(Request.class);
    when(req3.getTag()).thenReturn(tagA);
    Request req4 = mock(Request.class);
    when(req4.getTag()).thenReturn(tagA);

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

    verify(req1).cancel(); // A cancelled
    verify(req3).cancel(); // A cancelled
    verify(req2, never()).cancel(); // B not cancelled
    verify(req4, never()).cancel(); // A added after cancel not cancelled
}
项目:Simplified-Zhihu-Daily    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}
项目:Simplified-Zhihu-Daily    文件:RequestQueueIntegrationTest.java   
/**
 * 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();
}