1/* 2 * Copyright 2018 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 androidx.palette.graphics; 18 19import static androidx.core.graphics.ColorUtils.HSLToColor; 20import static androidx.core.graphics.ColorUtils.calculateContrast; 21 22import static org.junit.Assert.assertEquals; 23import static org.junit.Assert.assertFalse; 24import static org.junit.Assert.assertNotNull; 25import static org.junit.Assert.assertTrue; 26 27import android.graphics.Color; 28import android.support.test.filters.SmallTest; 29import android.support.test.runner.AndroidJUnit4; 30 31import org.junit.Test; 32import org.junit.runner.RunWith; 33 34@RunWith(AndroidJUnit4.class) 35public class SwatchTests { 36 37 private static final float MIN_CONTRAST_TITLE_TEXT = 3.0f; 38 private static final float MIN_CONTRAST_BODY_TEXT = 4.5f; 39 40 @Test 41 @SmallTest 42 public void testTextColorContrasts() { 43 final Palette p = Palette.from(TestUtils.loadSampleBitmap()).generate(); 44 45 for (Palette.Swatch swatch : p.getSwatches()) { 46 testSwatchTextColorContrasts(swatch); 47 } 48 } 49 50 @Test 51 @SmallTest 52 public void testHslNotNull() { 53 final Palette p = Palette.from(TestUtils.loadSampleBitmap()).generate(); 54 55 for (Palette.Swatch swatch : p.getSwatches()) { 56 assertNotNull(swatch.getHsl()); 57 } 58 } 59 60 @Test 61 @SmallTest 62 public void testHslIsRgb() { 63 final Palette p = Palette.from(TestUtils.loadSampleBitmap()).generate(); 64 65 for (Palette.Swatch swatch : p.getSwatches()) { 66 assertEquals(HSLToColor(swatch.getHsl()), swatch.getRgb()); 67 } 68 } 69 70 private void testSwatchTextColorContrasts(Palette.Swatch swatch) { 71 final int bodyTextColor = swatch.getBodyTextColor(); 72 assertTrue(calculateContrast(bodyTextColor, swatch.getRgb()) >= MIN_CONTRAST_BODY_TEXT); 73 74 final int titleTextColor = swatch.getTitleTextColor(); 75 assertTrue(calculateContrast(titleTextColor, swatch.getRgb()) >= MIN_CONTRAST_TITLE_TEXT); 76 } 77 78 @Test 79 @SmallTest 80 public void testEqualsWhenSame() { 81 Palette.Swatch swatch1 = new Palette.Swatch(Color.WHITE, 50); 82 Palette.Swatch swatch2 = new Palette.Swatch(Color.WHITE, 50); 83 assertEquals(swatch1, swatch2); 84 } 85 86 @Test 87 @SmallTest 88 public void testEqualsWhenColorDifferent() { 89 Palette.Swatch swatch1 = new Palette.Swatch(Color.BLACK, 50); 90 Palette.Swatch swatch2 = new Palette.Swatch(Color.WHITE, 50); 91 assertFalse(swatch1.equals(swatch2)); 92 } 93 94 @Test 95 @SmallTest 96 public void testEqualsWhenPopulationDifferent() { 97 Palette.Swatch swatch1 = new Palette.Swatch(Color.BLACK, 50); 98 Palette.Swatch swatch2 = new Palette.Swatch(Color.BLACK, 100); 99 assertFalse(swatch1.equals(swatch2)); 100 } 101 102 @Test 103 @SmallTest 104 public void testEqualsWhenDifferent() { 105 Palette.Swatch swatch1 = new Palette.Swatch(Color.BLUE, 50); 106 Palette.Swatch swatch2 = new Palette.Swatch(Color.BLACK, 100); 107 assertFalse(swatch1.equals(swatch2)); 108 } 109} 110