1 /* 2 * Copyright (C) 2008 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 package android.graphics.cts; 17 18 import static android.support.test.InstrumentationRegistry.getInstrumentation; 19 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertTrue; 22 23 import android.content.res.Resources; 24 import android.graphics.Color; 25 import android.support.test.filters.SmallTest; 26 import android.support.test.runner.AndroidJUnit4; 27 import android.util.TypedValue; 28 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.util.Arrays; 33 import java.util.List; 34 35 @SmallTest 36 @RunWith(AndroidJUnit4.class) 37 public class ColorTest { 38 39 @Test 40 public void resourceColor() { 41 int colors [][] = { 42 { 0xff000000, android.R.color.background_dark }, 43 { 0xffffffff, android.R.color.background_light }, 44 { 0xff000000, android.R.color.black }, 45 { 0xffaaaaaa, android.R.color.darker_gray }, 46 { 0xff00ddff, android.R.color.holo_blue_bright }, 47 { 0xff0099cc, android.R.color.holo_blue_dark }, 48 { 0xff33b5e5, android.R.color.holo_blue_light }, 49 { 0xff669900, android.R.color.holo_green_dark }, 50 { 0xff99cc00, android.R.color.holo_green_light }, 51 { 0xffff8800, android.R.color.holo_orange_dark }, 52 { 0xffffbb33, android.R.color.holo_orange_light }, 53 { 0xffaa66cc, android.R.color.holo_purple }, 54 { 0xffcc0000, android.R.color.holo_red_dark }, 55 { 0xffff4444, android.R.color.holo_red_light }, 56 { 0xffffffff, android.R.color.primary_text_dark }, 57 { 0xffffffff, android.R.color.primary_text_dark_nodisable }, 58 { 0xff000000, android.R.color.primary_text_light }, 59 { 0xff000000, android.R.color.primary_text_light_nodisable }, 60 { 0xffbebebe, android.R.color.secondary_text_dark }, 61 { 0xffbebebe, android.R.color.secondary_text_dark_nodisable }, 62 { 0xff323232, android.R.color.secondary_text_light }, 63 { 0xffbebebe, android.R.color.secondary_text_light_nodisable }, 64 { 0xff808080, android.R.color.tab_indicator_text }, 65 { 0xff808080, android.R.color.tertiary_text_dark }, 66 { 0xff808080, android.R.color.tertiary_text_light }, 67 { 0x00000000, android.R.color.transparent }, 68 { 0xffffffff, android.R.color.white }, 69 { 0xff000000, android.R.color.widget_edittext_dark }, 70 }; 71 72 List<Integer> expectedColorStateLists = Arrays.asList( 73 android.R.color.primary_text_dark, 74 android.R.color.primary_text_dark_nodisable, 75 android.R.color.primary_text_light, 76 android.R.color.primary_text_light_nodisable, 77 android.R.color.secondary_text_dark, 78 android.R.color.secondary_text_dark_nodisable, 79 android.R.color.secondary_text_light, 80 android.R.color.secondary_text_light_nodisable, 81 android.R.color.tab_indicator_text, 82 android.R.color.tertiary_text_dark, 83 android.R.color.tertiary_text_light, 84 android.R.color.widget_edittext_dark 85 ); 86 87 Resources resources = getInstrumentation().getTargetContext().getResources(); 88 for (int[] pair : colors) { 89 final int resourceId = pair[1]; 90 final int expectedColor = pair[0]; 91 92 // validate color from getColor 93 int observedColor = resources.getColor(resourceId, null); 94 assertEquals("Color = " + Integer.toHexString(observedColor) + ", " 95 + Integer.toHexString(expectedColor) + " expected", 96 expectedColor, 97 observedColor); 98 99 // validate color from getValue 100 TypedValue value = new TypedValue(); 101 resources.getValue(resourceId, value, true); 102 103 // colors shouldn't depend on config changes 104 assertEquals(0, value.changingConfigurations); 105 106 if (expectedColorStateLists.contains(resourceId)) { 107 // ColorStateLists are strings 108 assertEquals("CSLs should be strings", TypedValue.TYPE_STRING, value.type); 109 } else { 110 // colors should be raw ints 111 assertTrue("Type should be int", 112 value.type >= TypedValue.TYPE_FIRST_INT 113 && value.type <= TypedValue.TYPE_LAST_INT); 114 115 // Validate color from getValue 116 assertEquals("Color should be expected value", expectedColor, value.data); 117 } 118 } 119 assertEquals("Test no longer in sync with colors in android.R.color", 120 colors.length, 121 android.R.color.class.getDeclaredFields().length); 122 } 123 124 @Test 125 public void testAlpha() { 126 assertEquals(0xff, Color.alpha(Color.RED)); 127 assertEquals(0xff, Color.alpha(Color.YELLOW)); 128 } 129 130 @Test 131 public void testArgb() { 132 assertEquals(Color.RED, Color.argb(0xff, 0xff, 0x00, 0x00)); 133 assertEquals(Color.YELLOW, Color.argb(0xff, 0xff, 0xff, 0x00)); 134 assertEquals(Color.RED, Color.argb(1.0f, 1.0f, 0.0f, 0.0f)); 135 assertEquals(Color.YELLOW, Color.argb(1.0f, 1.0f, 1.0f, 0.0f)); 136 } 137 138 @Test 139 public void testBlue() { 140 assertEquals(0x00, Color.blue(Color.RED)); 141 assertEquals(0x00, Color.blue(Color.YELLOW)); 142 } 143 144 @Test 145 public void testGreen() { 146 assertEquals(0x00, Color.green(Color.RED)); 147 assertEquals(0xff, Color.green(Color.GREEN)); 148 } 149 150 @Test(expected=RuntimeException.class) 151 public void testHSVToColorArrayTooShort() { 152 // abnormal case: hsv length less than 3 153 float[] hsv = new float[2]; 154 Color.HSVToColor(hsv); 155 } 156 157 @Test 158 public void testHSVToColor() { 159 float[] hsv = new float[3]; 160 Color.colorToHSV(Color.RED, hsv); 161 assertEquals(Color.RED, Color.HSVToColor(hsv)); 162 } 163 164 @Test 165 public void testHSVToColorWithAlpha() { 166 float[] hsv = new float[3]; 167 Color.colorToHSV(Color.RED, hsv); 168 assertEquals(Color.RED, Color.HSVToColor(0xff, hsv)); 169 } 170 171 @Test(expected=IllegalArgumentException.class) 172 public void testParseColorStringOfInvalidLength() { 173 // abnormal case: colorString starts with '#' but length is neither 7 nor 9 174 Color.parseColor("#ff00ff0"); 175 } 176 177 @Test 178 public void testParseColor() { 179 assertEquals(Color.RED, Color.parseColor("#ff0000")); 180 assertEquals(Color.RED, Color.parseColor("#ffff0000")); 181 182 assertEquals(Color.BLACK, Color.parseColor("black")); 183 assertEquals(Color.DKGRAY, Color.parseColor("darkgray")); 184 assertEquals(Color.GRAY, Color.parseColor("gray")); 185 assertEquals(Color.LTGRAY, Color.parseColor("lightgray")); 186 assertEquals(Color.WHITE, Color.parseColor("white")); 187 assertEquals(Color.RED, Color.parseColor("red")); 188 assertEquals(Color.GREEN, Color.parseColor("green")); 189 assertEquals(Color.BLUE, Color.parseColor("blue")); 190 assertEquals(Color.YELLOW, Color.parseColor("yellow")); 191 assertEquals(Color.CYAN, Color.parseColor("cyan")); 192 assertEquals(Color.MAGENTA, Color.parseColor("magenta")); 193 } 194 195 @Test(expected=IllegalArgumentException.class) 196 public void testParseColorUnsupportedFormat() { 197 // abnormal case: colorString doesn't start with '#' and is unknown color 198 Color.parseColor("hello"); 199 } 200 201 @Test 202 public void testRed() { 203 assertEquals(0xff, Color.red(Color.RED)); 204 assertEquals(0xff, Color.red(Color.YELLOW)); 205 } 206 207 @Test 208 public void testRgb() { 209 assertEquals(Color.RED, Color.rgb(0xff, 0x00, 0x00)); 210 assertEquals(Color.YELLOW, Color.rgb(0xff, 0xff, 0x00)); 211 assertEquals(Color.RED, Color.rgb(1.0f, 0.0f, 0.0f)); 212 assertEquals(Color.YELLOW, Color.rgb(1.0f, 1.0f, 0.0f)); 213 } 214 215 @Test(expected=RuntimeException.class) 216 public void testRGBToHSVArrayTooShort() { 217 // abnormal case: hsv length less than 3 218 float[] hsv = new float[2]; 219 Color.RGBToHSV(0xff, 0x00, 0x00, hsv); 220 } 221 222 @Test 223 public void testRGBToHSV() { 224 float[] hsv = new float[3]; 225 Color.RGBToHSV(0xff, 0x00, 0x00, hsv); 226 assertEquals(Color.RED, Color.HSVToColor(hsv)); 227 } 228 229 @Test 230 public void testLuminance() { 231 assertEquals(0, Color.luminance(Color.BLACK), 0); 232 float eps = 0.000001f; 233 assertEquals(0.0722, Color.luminance(Color.BLUE), eps); 234 assertEquals(0.2126, Color.luminance(Color.RED), eps); 235 assertEquals(0.7152, Color.luminance(Color.GREEN), eps); 236 assertEquals(1, Color.luminance(Color.WHITE), 0); 237 } 238 } 239