1/* 2 * Copyright (C) 2015 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.setupwizardlib.test; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertTrue; 21 22import android.graphics.Bitmap; 23import android.graphics.Canvas; 24import android.graphics.Color; 25import android.graphics.Matrix; 26import android.graphics.Paint; 27import android.os.Debug; 28import android.support.test.filters.SmallTest; 29import android.support.test.runner.AndroidJUnit4; 30import android.util.Log; 31 32import com.android.setupwizardlib.GlifPatternDrawable; 33 34import junit.framework.AssertionFailedError; 35 36import org.junit.Before; 37import org.junit.Test; 38import org.junit.runner.RunWith; 39 40@RunWith(AndroidJUnit4.class) 41@SmallTest 42public class GlifPatternDrawableTest { 43 44 private static final String TAG = "GlifPatternDrawableTest"; 45 46 @Before 47 public void setUp() throws Exception { 48 GlifPatternDrawable.invalidatePattern(); 49 } 50 51 @Test 52 public void testDraw() { 53 final Bitmap bitmap = Bitmap.createBitmap(1366, 768, Bitmap.Config.ARGB_8888); 54 final Canvas canvas = new Canvas(bitmap); 55 56 final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); 57 drawable.setBounds(0, 0, 1366, 768); 58 drawable.draw(canvas); 59 60 assertSameColor("Top left pixel should be #e61a1a", 0xffe61a1a, bitmap.getPixel(0, 0)); 61 assertSameColor("Center pixel should be #d90d0d", 0xffd90d0d, bitmap.getPixel(683, 384)); 62 assertSameColor("Bottom right pixel should be #d40808", 0xffd40808, 63 bitmap.getPixel(1365, 767)); 64 } 65 66 @Test 67 public void testDrawTwice() { 68 // Test that the second time the drawable is drawn is also correct, to make sure caching is 69 // done correctly. 70 71 final Bitmap bitmap = Bitmap.createBitmap(1366, 768, Bitmap.Config.ARGB_8888); 72 final Canvas canvas = new Canvas(bitmap); 73 74 final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); 75 drawable.setBounds(0, 0, 1366, 768); 76 drawable.draw(canvas); 77 78 Paint paint = new Paint(); 79 paint.setColor(Color.WHITE); 80 canvas.drawRect(0, 0, 1366, 768, paint); // Erase the entire canvas 81 82 drawable.draw(canvas); 83 84 assertSameColor("Top left pixel should be #e61a1a", 0xffe61a1a, bitmap.getPixel(0, 0)); 85 assertSameColor("Center pixel should be #d90d0d", 0xffd90d0d, bitmap.getPixel(683, 384)); 86 assertSameColor("Bottom right pixel should be #d40808", 0xffd40808, 87 bitmap.getPixel(1365, 767)); 88 } 89 90 @Test 91 public void testScaleToCanvasSquare() { 92 final Canvas canvas = new Canvas(); 93 Matrix expected = new Matrix(canvas.getMatrix()); 94 95 Bitmap mockBitmapCache = Bitmap.createBitmap(1366, 768, Bitmap.Config.ALPHA_8); 96 97 final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); 98 drawable.setBounds(0, 0, 683, 384); // half each side of the view box 99 drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); 100 101 expected.postScale(0.5f, 0.5f); 102 103 assertEquals("Matrices should match", expected, canvas.getMatrix()); 104 } 105 106 @Test 107 public void testScaleToCanvasTall() { 108 final Canvas canvas = new Canvas(); 109 final Matrix expected = new Matrix(canvas.getMatrix()); 110 111 Bitmap mockBitmapCache = Bitmap.createBitmap(1366, 768, Bitmap.Config.ALPHA_8); 112 113 final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); 114 drawable.setBounds(0, 0, 683, 768); // half the width only 115 drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); 116 117 expected.postScale(1f, 1f); 118 expected.postTranslate(-99.718f, 0f); 119 120 assertEquals("Matrices should match", expected, canvas.getMatrix()); 121 } 122 123 @Test 124 public void testScaleToCanvasWide() { 125 final Canvas canvas = new Canvas(); 126 final Matrix expected = new Matrix(canvas.getMatrix()); 127 128 Bitmap mockBitmapCache = Bitmap.createBitmap(1366, 768, Bitmap.Config.ALPHA_8); 129 130 final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); 131 drawable.setBounds(0, 0, 1366, 384); // half the height only 132 drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); 133 134 expected.postScale(1f, 1f); 135 expected.postTranslate(0f, -87.552f); 136 137 assertEquals("Matrices should match", expected, canvas.getMatrix()); 138 } 139 140 @SmallTest 141 public void testScaleToCanvasMaxSize() { 142 final Canvas canvas = new Canvas(); 143 final Matrix expected = new Matrix(canvas.getMatrix()); 144 145 Bitmap mockBitmapCache = Bitmap.createBitmap(2049, 1152, Bitmap.Config.ALPHA_8); 146 147 final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); 148 drawable.setBounds(0, 0, 1366, 768); // original viewbox size 149 drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); 150 151 expected.postScale(1 / 1.5f, 1 / 1.5f); 152 expected.postTranslate(0f, 0f); 153 154 assertEquals("Matrices should match", expected, canvas.getMatrix()); 155 } 156 157 @Test 158 public void testMemoryAllocation() { 159 Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo(); 160 Debug.getMemoryInfo(memoryInfo); 161 final long memoryBefore = memoryInfo.getTotalPss(); // Get memory usage in KB 162 163 final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); 164 drawable.setBounds(0, 0, 1366, 768); 165 drawable.createBitmapCache(2049, 1152); 166 167 Debug.getMemoryInfo(memoryInfo); 168 final long memoryAfter = memoryInfo.getTotalPss(); 169 Log.i(TAG, "Memory allocated for bitmap cache: " + (memoryAfter - memoryBefore)); 170 assertTrue("Memory allocation should not exceed 5MB", memoryAfter < memoryBefore + 5000); 171 } 172 173 private void assertSameColor(String message, int expected, int actual) { 174 try { 175 assertEquals(expected, actual); 176 } catch (AssertionFailedError e) { 177 throw new AssertionFailedError(message + " expected <#" + Integer.toHexString(expected) 178 + "> but found <#" + Integer.toHexString(actual) + "> instead"); 179 } 180 } 181} 182