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