Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2012 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 
     17 package com.android.contacts.common.util;
     18 
     19 import android.graphics.Bitmap;
     20 import android.test.AndroidTestCase;
     21 import android.test.suitebuilder.annotation.SmallTest;
     22 
     23 import com.android.contacts.common.util.BitmapUtil;
     24 
     25 import java.io.ByteArrayOutputStream;
     26 import java.io.IOException;
     27 
     28 /**
     29  * Tests for {@link com.android.contacts.common.util.BitmapUtil}.
     30  */
     31 @SmallTest
     32 public class BitmapUtilTests extends AndroidTestCase {
     33     public void testGetSmallerExtentFromBytes1() throws Exception {
     34         assertEquals(100, BitmapUtil.getSmallerExtentFromBytes(createJpegRawData(100, 100)));
     35         assertEquals(100, BitmapUtil.getSmallerExtentFromBytes(createPngRawData(100, 100)));
     36     }
     37 
     38     public void testGetSmallerExtentFromBytes2() throws Exception {
     39         assertEquals(50, BitmapUtil.getSmallerExtentFromBytes(createJpegRawData(200, 50)));
     40         assertEquals(50, BitmapUtil.getSmallerExtentFromBytes(createPngRawData(200, 50)));
     41     }
     42 
     43     public void testGetSmallerExtentFromBytes3() throws Exception {
     44         assertEquals(40, BitmapUtil.getSmallerExtentFromBytes(createJpegRawData(40, 150)));
     45         assertEquals(40, BitmapUtil.getSmallerExtentFromBytes(createPngRawData(40, 150)));
     46     }
     47 
     48     public void testFindOptimalSampleSizeExact() throws Exception {
     49         assertEquals(1, BitmapUtil.findOptimalSampleSize(512, 512));
     50     }
     51 
     52     public void testFindOptimalSampleSizeBigger() throws Exception {
     53         assertEquals(1, BitmapUtil.findOptimalSampleSize(512, 1024));
     54     }
     55 
     56     public void testFindOptimalSampleSizeSmaller1() throws Exception {
     57         assertEquals(2, BitmapUtil.findOptimalSampleSize(512, 256));
     58     }
     59 
     60     public void testFindOptimalSampleSizeSmaller2() throws Exception {
     61         assertEquals(2, BitmapUtil.findOptimalSampleSize(512, 230));
     62     }
     63 
     64     public void testFindOptimalSampleSizeSmaller3() throws Exception {
     65         assertEquals(4, BitmapUtil.findOptimalSampleSize(512, 129));
     66     }
     67 
     68     public void testFindOptimalSampleSizeSmaller4() throws Exception {
     69         assertEquals(4, BitmapUtil.findOptimalSampleSize(512, 128));
     70     }
     71 
     72     public void testFindOptimalSampleSizeUnknownOriginal() throws Exception {
     73         assertEquals(1, BitmapUtil.findOptimalSampleSize(-1, 128));
     74     }
     75 
     76     public void testFindOptimalSampleSizeUnknownTarget() throws Exception {
     77         assertEquals(1, BitmapUtil.findOptimalSampleSize(128, -1));
     78     }
     79 
     80     public void testDecodeWithSampleSize1() throws IOException {
     81         assertBitmapSize(128, 64, BitmapUtil.decodeBitmapFromBytes(createJpegRawData(128, 64), 1));
     82         assertBitmapSize(128, 64, BitmapUtil.decodeBitmapFromBytes(createPngRawData(128, 64), 1));
     83     }
     84 
     85     public void testDecodeWithSampleSize2() throws IOException {
     86         assertBitmapSize(64, 32, BitmapUtil.decodeBitmapFromBytes(createJpegRawData(128, 64), 2));
     87         assertBitmapSize(64, 32, BitmapUtil.decodeBitmapFromBytes(createPngRawData(128, 64), 2));
     88     }
     89 
     90     public void testDecodeWithSampleSize2a() throws IOException {
     91         assertBitmapSize(25, 20, BitmapUtil.decodeBitmapFromBytes(createJpegRawData(50, 40), 2));
     92         assertBitmapSize(25, 20, BitmapUtil.decodeBitmapFromBytes(createPngRawData(50, 40), 2));
     93     }
     94 
     95     public void testDecodeWithSampleSize4() throws IOException {
     96         assertBitmapSize(32, 16, BitmapUtil.decodeBitmapFromBytes(createJpegRawData(128, 64), 4));
     97         assertBitmapSize(32, 16, BitmapUtil.decodeBitmapFromBytes(createPngRawData(128, 64), 4));
     98     }
     99 
    100     private void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) {
    101         assertEquals(expectedWidth, bitmap.getWidth());
    102         assertEquals(expectedHeight, bitmap.getHeight());
    103     }
    104 
    105     private byte[] createJpegRawData(int sourceWidth, int sourceHeight) throws IOException {
    106         return createRawData(Bitmap.CompressFormat.JPEG, sourceWidth, sourceHeight);
    107     }
    108 
    109     private byte[] createPngRawData(int sourceWidth, int sourceHeight) throws IOException {
    110         return createRawData(Bitmap.CompressFormat.PNG, sourceWidth, sourceHeight);
    111     }
    112 
    113     private byte[] createRawData(Bitmap.CompressFormat format, int sourceWidth,
    114             int sourceHeight) throws IOException {
    115         // Create a temp bitmap as our source
    116         Bitmap b = Bitmap.createBitmap(sourceWidth, sourceHeight, Bitmap.Config.ARGB_8888);
    117         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    118         b.compress(format, 50, outputStream);
    119         final byte[] data = outputStream.toByteArray();
    120         outputStream.close();
    121         return data;
    122     }
    123 }
    124