FileDocCategorySizeDatePackage
ImageRequestTest.javaAPI DocAndroid 5.1 API3709Thu Mar 12 22:22:56 GMT 2015com.android.volley.toolbox

ImageRequestTest

public class ImageRequestTest extends android.test.InstrumentationTestCase

Fields Summary
Constructors Summary
Methods Summary
private static byte[]readRawResource(android.content.res.Resources res, int resId)

        InputStream in = res.openRawResource(resId);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        in.close();
        return bytes.toByteArray();
    
public voidtestFindBestSampleSize()

        // desired == actual == 1
        assertEquals(1, ImageRequest.findBestSampleSize(100, 150, 100, 150));

        // exactly half == 2
        assertEquals(2, ImageRequest.findBestSampleSize(280, 160, 140, 80));

        // just over half == 1
        assertEquals(1, ImageRequest.findBestSampleSize(1000, 800, 501, 401));

        // just under 1/4 == 4
        assertEquals(4, ImageRequest.findBestSampleSize(100, 200, 24, 50));
    
public voidtestParseNetworkResponse_resizing()

        byte[] jpegBytes = readRawResource(
                getInstrumentation().getContext().getResources(), R.raw.large_jpeg_1024_500);
        NetworkResponse jpeg = new NetworkResponse(jpegBytes);

        // Exact sizes
        verifyResize(jpeg, 512, 250, 512, 250); // exactly half
        verifyResize(jpeg, 511, 249, 509, 249); // just under half
        verifyResize(jpeg, 1080, 500, 1024, 500); // larger
        verifyResize(jpeg, 500, 500, 500, 244); // keep same ratio

        // Specify only width, preserve aspect ratio
        verifyResize(jpeg, 512, 0, 512, 250);
        verifyResize(jpeg, 800, 0, 800, 390);
        verifyResize(jpeg, 1024, 0, 1024, 500);

        // Specify only height, preserve aspect ratio
        verifyResize(jpeg, 0, 250, 512, 250);
        verifyResize(jpeg, 0, 391, 800, 391);
        verifyResize(jpeg, 0, 500, 1024, 500);

        // No resize
        verifyResize(jpeg, 0, 0, 1024, 500);
    
private voidverifyResize(com.android.volley.NetworkResponse networkResponse, int maxWidth, int maxHeight, int expectedWidth, int expectedHeight)

        ImageRequest request = new ImageRequest(
                "", null, maxWidth, maxHeight, Config.RGB_565, null);
        Response<Bitmap> response = request.parseNetworkResponse(networkResponse);
        assertNotNull(response);
        assertTrue(response.isSuccess());
        Bitmap bitmap = response.result;
        assertNotNull(bitmap);
        assertEquals(expectedWidth, bitmap.getWidth());
        assertEquals(expectedHeight, bitmap.getHeight());