Search in sources :

Example 16 with Result

use of com.squareup.picasso3.RequestHandler.Result in project picasso by square.

the class BitmapHunterTest method onlyScaleDownOriginalBiggerWidthIs0.

@Test
public void onlyScaleDownOriginalBiggerWidthIs0() {
    Bitmap source = Bitmap.createBitmap(50, 50, ARGB_8888);
    Request data = new Request.Builder(URI_1).resize(0, 40).onlyScaleDown().build();
    Bitmap result = transformResult(data, source, 0);
    ShadowBitmap shadowBitmap = shadowOf(result);
    assertThat(shadowBitmap.getCreatedFromBitmap()).isSameInstanceAs(source);
    Matrix matrix = shadowBitmap.getCreatedFromMatrix();
    ShadowMatrix shadowMatrix = shadowOf(matrix);
    assertThat(shadowMatrix.getPreOperations()).containsExactly("scale 0.8 0.8");
}
Also used : TestUtils.makeBitmap(com.squareup.picasso3.TestUtils.makeBitmap) ShadowBitmap(org.robolectric.shadows.ShadowBitmap) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) ShadowMatrix(org.robolectric.shadows.ShadowMatrix) BitmapHunter.forRequest(com.squareup.picasso3.BitmapHunter.forRequest) ShadowBitmap(org.robolectric.shadows.ShadowBitmap) ShadowMatrix(org.robolectric.shadows.ShadowMatrix) Test(org.junit.Test)

Example 17 with Result

use of com.squareup.picasso3.RequestHandler.Result in project picasso by square.

the class BitmapHunterTest method reusedBitmapIsNotRecycled.

@Test
public void reusedBitmapIsNotRecycled() {
    Request data = new Request.Builder(URI_1).build();
    Bitmap source = Bitmap.createBitmap(10, 10, ARGB_8888);
    Bitmap result = transformResult(data, source, 0);
    assertThat(result).isSameInstanceAs(source);
    assertThat(result.isRecycled()).isFalse();
}
Also used : TestUtils.makeBitmap(com.squareup.picasso3.TestUtils.makeBitmap) ShadowBitmap(org.robolectric.shadows.ShadowBitmap) Bitmap(android.graphics.Bitmap) BitmapHunter.forRequest(com.squareup.picasso3.BitmapHunter.forRequest) Test(org.junit.Test)

Example 18 with Result

use of com.squareup.picasso3.RequestHandler.Result in project picasso by square.

the class BitmapHunterTest method centerCropWideTooSmall.

@Test
public void centerCropWideTooSmall() {
    Bitmap source = Bitmap.createBitmap(20, 10, ARGB_8888);
    Request data = new Request.Builder(URI_1).resize(40, 40).centerCrop().build();
    Bitmap result = transformResult(data, source, 0);
    ShadowBitmap shadowBitmap = shadowOf(result);
    assertThat(shadowBitmap.getCreatedFromBitmap()).isSameInstanceAs(source);
    assertThat(shadowBitmap.getCreatedFromX()).isEqualTo(5);
    assertThat(shadowBitmap.getCreatedFromY()).isEqualTo(0);
    assertThat(shadowBitmap.getCreatedFromWidth()).isEqualTo(10);
    assertThat(shadowBitmap.getCreatedFromHeight()).isEqualTo(10);
    Matrix matrix = shadowBitmap.getCreatedFromMatrix();
    ShadowMatrix shadowMatrix = shadowOf(matrix);
    assertThat(shadowMatrix.getPreOperations()).containsExactly("scale 4.0 4.0");
}
Also used : TestUtils.makeBitmap(com.squareup.picasso3.TestUtils.makeBitmap) ShadowBitmap(org.robolectric.shadows.ShadowBitmap) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) ShadowMatrix(org.robolectric.shadows.ShadowMatrix) BitmapHunter.forRequest(com.squareup.picasso3.BitmapHunter.forRequest) ShadowBitmap(org.robolectric.shadows.ShadowBitmap) ShadowMatrix(org.robolectric.shadows.ShadowMatrix) Test(org.junit.Test)

Example 19 with Result

use of com.squareup.picasso3.RequestHandler.Result 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 20 with Result

use of com.squareup.picasso3.RequestHandler.Result 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)

Aggregations

Test (org.junit.Test)52 Bitmap (android.graphics.Bitmap)46 TestUtils.makeBitmap (com.squareup.picasso3.TestUtils.makeBitmap)44 BitmapHunter.forRequest (com.squareup.picasso3.BitmapHunter.forRequest)42 ShadowBitmap (org.robolectric.shadows.ShadowBitmap)42 Matrix (android.graphics.Matrix)31 ShadowMatrix (org.robolectric.shadows.ShadowMatrix)31 Result (com.squareup.picasso3.RequestHandler.Result)7 TestUtils.mockAction (com.squareup.picasso3.TestUtils.mockAction)6 MatrixTransformation.transformResult (com.squareup.picasso3.MatrixTransformation.transformResult)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 FakeAction (com.squareup.picasso3.TestUtils.FakeAction)2 Uri (android.net.Uri)1 Message (android.os.Message)1 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ResponseBody (okhttp3.ResponseBody)1 Buffer (okio.Buffer)1