1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.volley.toolbox; 18 19import android.content.res.Resources; 20import android.graphics.Bitmap; 21import android.graphics.Bitmap.Config; 22import android.test.InstrumentationTestCase; 23import android.test.suitebuilder.annotation.SmallTest; 24 25import com.android.volley.NetworkResponse; 26import com.android.volley.Response; 27import com.android.volley.tests.R; 28 29import java.io.ByteArrayOutputStream; 30import java.io.IOException; 31import java.io.InputStream; 32 33@SmallTest 34public class ImageRequestTest extends InstrumentationTestCase { 35 36 public void testParseNetworkResponse_resizing() throws Exception { 37 byte[] jpegBytes = readRawResource( 38 getInstrumentation().getContext().getResources(), R.raw.large_jpeg_1024_500); 39 NetworkResponse jpeg = new NetworkResponse(jpegBytes); 40 41 // Exact sizes 42 verifyResize(jpeg, 512, 250, 512, 250); // exactly half 43 verifyResize(jpeg, 511, 249, 509, 249); // just under half 44 verifyResize(jpeg, 1080, 500, 1024, 500); // larger 45 verifyResize(jpeg, 500, 500, 500, 244); // keep same ratio 46 47 // Specify only width, preserve aspect ratio 48 verifyResize(jpeg, 512, 0, 512, 250); 49 verifyResize(jpeg, 800, 0, 800, 390); 50 verifyResize(jpeg, 1024, 0, 1024, 500); 51 52 // Specify only height, preserve aspect ratio 53 verifyResize(jpeg, 0, 250, 512, 250); 54 verifyResize(jpeg, 0, 391, 800, 391); 55 verifyResize(jpeg, 0, 500, 1024, 500); 56 57 // No resize 58 verifyResize(jpeg, 0, 0, 1024, 500); 59 } 60 61 private void verifyResize(NetworkResponse networkResponse, int maxWidth, int maxHeight, 62 int expectedWidth, int expectedHeight) { 63 ImageRequest request = new ImageRequest( 64 "", null, maxWidth, maxHeight, Config.RGB_565, null); 65 Response<Bitmap> response = request.parseNetworkResponse(networkResponse); 66 assertNotNull(response); 67 assertTrue(response.isSuccess()); 68 Bitmap bitmap = response.result; 69 assertNotNull(bitmap); 70 assertEquals(expectedWidth, bitmap.getWidth()); 71 assertEquals(expectedHeight, bitmap.getHeight()); 72 } 73 74 public void testFindBestSampleSize() { 75 // desired == actual == 1 76 assertEquals(1, ImageRequest.findBestSampleSize(100, 150, 100, 150)); 77 78 // exactly half == 2 79 assertEquals(2, ImageRequest.findBestSampleSize(280, 160, 140, 80)); 80 81 // just over half == 1 82 assertEquals(1, ImageRequest.findBestSampleSize(1000, 800, 501, 401)); 83 84 // just under 1/4 == 4 85 assertEquals(4, ImageRequest.findBestSampleSize(100, 200, 24, 50)); 86 } 87 88 private static byte[] readRawResource(Resources res, int resId) throws IOException { 89 InputStream in = res.openRawResource(resId); 90 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 91 byte[] buffer = new byte[1024]; 92 int count; 93 while ((count = in.read(buffer)) != -1) { 94 bytes.write(buffer, 0, count); 95 } 96 in.close(); 97 return bytes.toByteArray(); 98 } 99 100} 101