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