Search in sources :

Example 1 with TestUtils.mockAction

use of com.squareup.picasso3.TestUtils.mockAction in project picasso by square.

the class NetworkRequestHandlerTest method cachedResponseDoesNotDispatchToStats.

@Test
public void cachedResponseDoesNotDispatchToStats() throws Exception {
    responses.add(responseOf(ResponseBody.create(null, new byte[10])).newBuilder().cacheResponse(responseOf(null)).build());
    Action action = TestUtils.mockAction(URI_KEY_1, URI_1);
    final CountDownLatch latch = new CountDownLatch(1);
    networkHandler.load(picasso, action.request, new RequestHandler.Callback() {

        @Override
        public void onSuccess(Result result) {
            verify(picasso, never()).downloadFinished(anyInt());
            latch.countDown();
        }

        @Override
        public void onError(@NonNull Throwable t) {
            throw new AssertionError(t);
        }
    });
    assertThat(latch.await(10, SECONDS)).isTrue();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Result(com.squareup.picasso3.RequestHandler.Result) Test(org.junit.Test)

Example 2 with TestUtils.mockAction

use of com.squareup.picasso3.TestUtils.mockAction in project picasso by square.

the class NetworkRequestHandlerTest method unknownContentLengthFromDiskThrows.

@Test
public void unknownContentLengthFromDiskThrows() throws Exception {
    final AtomicBoolean closed = new AtomicBoolean();
    ResponseBody body = new ResponseBody() {

        @Override
        public MediaType contentType() {
            return null;
        }

        @Override
        public long contentLength() {
            return 0;
        }

        @Override
        public BufferedSource source() {
            return new Buffer();
        }

        @Override
        public void close() {
            closed.set(true);
            super.close();
        }
    };
    responses.add(responseOf(body).newBuilder().cacheResponse(responseOf(null)).build());
    Action action = TestUtils.mockAction(URI_KEY_1, URI_1);
    final CountDownLatch latch = new CountDownLatch(1);
    networkHandler.load(picasso, action.request, new RequestHandler.Callback() {

        @Override
        public void onSuccess(Result result) {
            throw new AssertionError();
        }

        @Override
        public void onError(@NonNull Throwable t) {
            verify(picasso, never()).downloadFinished(anyInt());
            assertTrue(closed.get());
            latch.countDown();
        }
    });
    assertThat(latch.await(10, SECONDS)).isTrue();
}
Also used : Buffer(okio.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) ResponseBody(okhttp3.ResponseBody) Result(com.squareup.picasso3.RequestHandler.Result) Test(org.junit.Test)

Example 3 with TestUtils.mockAction

use of com.squareup.picasso3.TestUtils.mockAction in project picasso by square.

the class NetworkRequestHandlerTest method noCacheAndKnownContentLengthDispatchToStats.

@Test
public void noCacheAndKnownContentLengthDispatchToStats() throws Exception {
    responses.add(responseOf(ResponseBody.create(null, new byte[10])));
    Action action = TestUtils.mockAction(URI_KEY_1, URI_1);
    final CountDownLatch latch = new CountDownLatch(1);
    networkHandler.load(picasso, action.request, new RequestHandler.Callback() {

        @Override
        public void onSuccess(Result result) {
            verify(picasso).downloadFinished(10);
            latch.countDown();
        }

        @Override
        public void onError(@NonNull Throwable t) {
            throw new AssertionError(t);
        }
    });
    assertThat(latch.await(10, SECONDS)).isTrue();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Result(com.squareup.picasso3.RequestHandler.Result) Test(org.junit.Test)

Example 4 with TestUtils.mockAction

use of com.squareup.picasso3.TestUtils.mockAction in project picasso by square.

the class NetworkRequestHandlerTest method doesNotForceLocalCacheOnlyWithAirplaneModeOffAndRetryCount.

@Test
public void doesNotForceLocalCacheOnlyWithAirplaneModeOffAndRetryCount() throws Exception {
    responses.add(responseOf(ResponseBody.create(null, new byte[10])));
    Action action = TestUtils.mockAction(URI_KEY_1, URI_1);
    final CountDownLatch latch = new CountDownLatch(1);
    networkHandler.load(picasso, action.request, new RequestHandler.Callback() {

        @Override
        public void onSuccess(Result result) {
            try {
                assertThat(requests.takeFirst().cacheControl().toString()).isEmpty();
                latch.countDown();
            } catch (InterruptedException e) {
                throw new AssertionError(e);
            }
        }

        @Override
        public void onError(@NonNull Throwable t) {
            throw new AssertionError(t);
        }
    });
    assertThat(latch.await(10, SECONDS)).isTrue();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Result(com.squareup.picasso3.RequestHandler.Result) Test(org.junit.Test)

Aggregations

Result (com.squareup.picasso3.RequestHandler.Result)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ResponseBody (okhttp3.ResponseBody)1 Buffer (okio.Buffer)1