• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016, 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.managedprovisioning.common;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.Color;
22 import android.graphics.Paint;
23 import android.graphics.PorterDuff;
24 import android.graphics.drawable.Drawable;
25 import android.net.Uri;
26 import android.test.AndroidTestCase;
27 import android.test.suitebuilder.annotation.SmallTest;
28 
29 import java.io.File;
30 import java.io.FileOutputStream;
31 
32 @SmallTest
33 public class LogoUtilsTest extends AndroidTestCase {
34     private static final int SAMPLE_COLOR = Color.RED;
35 
testPartiallyResizedBitmap()36     public void testPartiallyResizedBitmap() throws Exception {
37         Bitmap bitmap = createSampleBitmap(20, 30);
38         File tempFile = writeBitmapToTempFile(bitmap);
39         try {
40             Bitmap newBitmap = LogoUtils.getBitmapPartiallyResized(tempFile.getPath(), 10, 15);
41             // It should have been able to get only the half bitmap.
42             assertEquals(10, newBitmap.getWidth());
43             assertEquals(15, newBitmap.getHeight());
44             assertEquals(SAMPLE_COLOR, newBitmap.getPixel(5, 5));
45         } finally {
46             tempFile.delete();
47         }
48     }
49 
testPartiallyResizedElongatedBitmap()50     public void testPartiallyResizedElongatedBitmap() throws Exception {
51         Bitmap bitmap = createSampleBitmap(8, 32);
52         File tempFile = writeBitmapToTempFile(bitmap);
53         try {
54             Bitmap newBitmap = LogoUtils.getBitmapPartiallyResized(tempFile.getPath(), 8, 8);
55             // It should have been able to get only the quarter bitmap.
56             assertEquals(2, newBitmap.getWidth());
57             assertEquals(8, newBitmap.getHeight());
58             assertEquals(SAMPLE_COLOR, newBitmap.getPixel(1, 1));
59         } finally {
60             tempFile.delete();
61         }
62     }
63 
testResizeBitmapKeepRatio()64     public void testResizeBitmapKeepRatio() throws Exception {
65         Bitmap bitmap = createSampleBitmap(16, 32);
66         Bitmap newBitmap = LogoUtils.resizeBitmap(bitmap, 8, 8);
67         // Should have kept the ratio.
68         assertEquals(4, newBitmap.getWidth());
69         assertEquals(8, newBitmap.getHeight());
70         assertEquals(SAMPLE_COLOR, newBitmap.getPixel(2, 2));
71     }
72 
testResizeBitmapNoScalingNeeded()73     public void testResizeBitmapNoScalingNeeded() throws Exception {
74         Bitmap bitmap = createSampleBitmap(16, 32);
75         Bitmap newBitmap = LogoUtils.resizeBitmap(bitmap, 20, 40);
76         // The maximum dimensions are larger than the actual ones: should
77         // not have changed the dimensions.
78         assertEquals(16, newBitmap.getWidth());
79         assertEquals(32, newBitmap.getHeight());
80         assertEquals(SAMPLE_COLOR, newBitmap.getPixel(2, 2));
81     }
82 
testResizeBitmapNoIntegerRatio()83     public void testResizeBitmapNoIntegerRatio() throws Exception {
84         Bitmap bitmap = createSampleBitmap(15, 15);
85         Bitmap newBitmap = LogoUtils.resizeBitmap(bitmap, 10, 10);
86         assertEquals(10, newBitmap.getWidth());
87         assertEquals(10, newBitmap.getHeight());
88         assertEquals(SAMPLE_COLOR, newBitmap.getPixel(2, 2));
89     }
90 
testSaveGetOrganisationLogo()91     public void testSaveGetOrganisationLogo() throws Exception {
92         Bitmap bitmap = createSampleBitmap(7, 5);
93         File tempFile = writeBitmapToTempFile(bitmap);
94         try {
95             LogoUtils.saveOrganisationLogo(getContext(), Uri.fromFile(tempFile));
96             Drawable drawable = LogoUtils.getOrganisationLogo(getContext(), SAMPLE_COLOR);
97             // We should have the original drawable.
98             assertEquals(7, drawable.getIntrinsicWidth());
99             assertEquals(5, drawable.getIntrinsicHeight());
100         } finally {
101             LogoUtils.cleanUp(getContext());
102         }
103     }
104 
testDefaultOrganisationLogo()105     public void testDefaultOrganisationLogo() throws Exception {
106         // First let's compute the expected logo. It is the default one.
107         Drawable expected = getContext().getResources().getDrawable(
108                 LogoUtils.DEFAULT_LOGO_ID, getContext().getTheme());
109         expected.setColorFilter(SAMPLE_COLOR, PorterDuff.Mode.SRC_ATOP);
110 
111         // Now, get the actual logo
112         Drawable actual = LogoUtils.getOrganisationLogo(getContext(), SAMPLE_COLOR);
113 
114         // They should be equal.
115         assertBitmapEquals(bitmapFromDrawable(expected), bitmapFromDrawable(actual));
116     }
117 
createSampleBitmap(int width, int height)118     private Bitmap createSampleBitmap(int width, int height) {
119         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
120         Canvas canvas = new Canvas(bitmap);
121         Paint paint = new Paint();
122         paint.setColor(SAMPLE_COLOR);
123         canvas.drawRect(0, 0, width, height, paint);
124         return bitmap;
125     }
126 
assertBitmapEquals(Bitmap b1, Bitmap b2)127     private void assertBitmapEquals(Bitmap b1, Bitmap b2) {
128         assertEquals(b1.getWidth(), b2.getWidth());
129         assertEquals(b1.getHeight(), b2.getHeight());
130         for (int x = 0; x < b1.getWidth(); x++) {
131             for (int y = 0; y < b1.getHeight(); y++) {
132                 assertEquals(b1.getPixel(x, y), b2.getPixel(x, y));
133             }
134         }
135     }
136 
bitmapFromDrawable(Drawable drawable)137     private Bitmap bitmapFromDrawable(Drawable drawable) {
138         int width = drawable.getIntrinsicWidth();
139         int height = drawable.getIntrinsicHeight();
140         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
141         Canvas canvas = new Canvas(bitmap);
142         drawable.setBounds(0, 0, width, height);
143         drawable.draw(canvas);
144         return bitmap;
145     }
146 
147     // Returns the temporary file where the bitmap was written.
writeBitmapToTempFile(Bitmap bitmap)148     private File writeBitmapToTempFile(Bitmap bitmap) throws Exception {
149         File tempFile = File.createTempFile("temp_bitmap", "", getContext().getCacheDir());
150         FileOutputStream fos = new FileOutputStream(tempFile);
151         bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
152         fos.close();
153         return tempFile;
154     }
155 }