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 android.support.v7.graphics; 18 19import static android.support.v7.graphics.TestUtils.assertCloseColors; 20import static android.support.v7.graphics.TestUtils.loadSampleBitmap; 21 22import static org.junit.Assert.assertEquals; 23import static org.junit.Assert.assertFalse; 24 25import android.graphics.Bitmap; 26import android.graphics.Canvas; 27import android.graphics.Color; 28import android.support.test.runner.AndroidJUnit4; 29import android.test.suitebuilder.annotation.SmallTest; 30 31import org.junit.Test; 32import org.junit.runner.RunWith; 33 34import java.util.ArrayList; 35 36@RunWith(AndroidJUnit4.class) 37public class BucketTests { 38 39 @Test 40 @SmallTest 41 public void testSourceBitmapNotRecycled() { 42 final Bitmap sample = loadSampleBitmap(); 43 44 Palette.from(sample).generate(); 45 assertFalse(sample.isRecycled()); 46 } 47 48 @Test(expected = UnsupportedOperationException.class) 49 @SmallTest 50 public void testSwatchesUnmodifiable() { 51 Palette p = Palette.from(loadSampleBitmap()).generate(); 52 p.getSwatches().remove(0); 53 } 54 55 @Test 56 @SmallTest 57 public void testSwatchesBuilder() { 58 ArrayList<Palette.Swatch> swatches = new ArrayList<>(); 59 swatches.add(new Palette.Swatch(Color.BLACK, 40)); 60 swatches.add(new Palette.Swatch(Color.GREEN, 60)); 61 swatches.add(new Palette.Swatch(Color.BLUE, 10)); 62 63 Palette p = Palette.from(swatches); 64 65 assertEquals(swatches, p.getSwatches()); 66 } 67 68 @Test 69 @SmallTest 70 public void testRegionWhole() { 71 final Bitmap sample = loadSampleBitmap(); 72 73 Palette.Builder b = new Palette.Builder(sample); 74 b.setRegion(0, 0, sample.getWidth(), sample.getHeight()); 75 b.generate(); 76 } 77 78 @Test 79 @SmallTest 80 public void testRegionUpperLeft() { 81 final Bitmap sample = loadSampleBitmap(); 82 83 Palette.Builder b = new Palette.Builder(sample); 84 b.setRegion(0, 0, sample.getWidth() / 2, sample.getHeight() / 2); 85 b.generate(); 86 } 87 88 @Test 89 @SmallTest 90 public void testRegionBottomRight() { 91 final Bitmap sample = loadSampleBitmap(); 92 93 Palette.Builder b = new Palette.Builder(sample); 94 b.setRegion(sample.getWidth() / 2, sample.getHeight() / 2, 95 sample.getWidth(), sample.getHeight()); 96 b.generate(); 97 } 98 99 @Test 100 @SmallTest 101 public void testOnePixelTallBitmap() { 102 final Bitmap bitmap = Bitmap.createBitmap(1000, 1, Bitmap.Config.ARGB_8888); 103 104 Palette.Builder b = new Palette.Builder(bitmap); 105 b.generate(); 106 } 107 108 @Test 109 @SmallTest 110 public void testOnePixelWideBitmap() { 111 final Bitmap bitmap = Bitmap.createBitmap(1, 1000, Bitmap.Config.ARGB_8888); 112 113 Palette.Builder b = new Palette.Builder(bitmap); 114 b.generate(); 115 } 116 117 @Test 118 @SmallTest 119 public void testBlueBitmapReturnsBlueSwatch() { 120 final Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); 121 Canvas canvas = new Canvas(bitmap); 122 canvas.drawColor(Color.BLUE); 123 124 final Palette palette = Palette.from(bitmap).generate(); 125 126 assertEquals(1, palette.getSwatches().size()); 127 128 final Palette.Swatch swatch = palette.getSwatches().get(0); 129 assertCloseColors(Color.BLUE, swatch.getRgb()); 130 } 131 132 @Test 133 @SmallTest 134 public void testBlueBitmapWithRegionReturnsBlueSwatch() { 135 final Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); 136 Canvas canvas = new Canvas(bitmap); 137 canvas.drawColor(Color.BLUE); 138 139 final Palette palette = Palette.from(bitmap) 140 .setRegion(0, bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight()) 141 .generate(); 142 143 assertEquals(1, palette.getSwatches().size()); 144 145 final Palette.Swatch swatch = palette.getSwatches().get(0); 146 assertCloseColors(Color.BLUE, swatch.getRgb()); 147 } 148 149} 150