1 /* 2 * Copyright (C) 2009 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.content.res.cts; 17 18 import android.content.cts.R; 19 import android.content.pm.ActivityInfo; 20 import android.content.res.ColorStateList; 21 import android.content.res.Resources; 22 import android.content.res.Resources.Theme; 23 import android.graphics.Color; 24 import android.os.Parcel; 25 import android.test.AndroidTestCase; 26 import android.test.suitebuilder.annotation.SmallTest; 27 28 import androidx.core.graphics.ColorUtils; 29 30 public class ColorStateListTest extends AndroidTestCase { 31 32 @SmallTest testConstructor()33 public void testConstructor() { 34 final int[][] state = new int[][]{{0}, {0}}; 35 final int[] colors = new int[]{Color.RED, Color.BLUE}; 36 final ColorStateList c = new ColorStateList(state, colors); 37 assertTrue(c.isStateful()); 38 assertEquals(Color.RED, c.getDefaultColor()); 39 } 40 41 @SmallTest testCreateFromXml()42 public void testCreateFromXml() throws Exception { 43 final int xmlId = R.color.testcolor; 44 final int colorInXml = 0xFFA6C839; // this color value is defined in testcolor.xml file. 45 final Resources res = getContext().getResources(); 46 final ColorStateList c = ColorStateList.createFromXml(res, res.getXml(xmlId)); 47 assertEquals(colorInXml, c.getDefaultColor()); 48 assertEquals(0, c.describeContents()); 49 assertFalse(c.isStateful()); 50 assertNotNull(c.toString()); 51 assertEquals(colorInXml, c.getColorForState(new int[]{0}, 0)); 52 } 53 54 @SmallTest testCreateFromXmlThemed()55 public void testCreateFromXmlThemed() throws Exception { 56 final int xmlId = R.color.testcolor_themed; 57 final int colorInXml = Color.BLACK; // this color value is defined in styles.xml file. 58 final Resources res = getContext().getResources(); 59 final Theme theme = res.newTheme(); 60 theme.applyStyle(R.style.Theme_ThemedDrawableTest, true); 61 final ColorStateList c = ColorStateList.createFromXml(res, res.getXml(xmlId), theme); 62 assertEquals(colorInXml, c.getDefaultColor()); 63 assertEquals(0, c.describeContents()); 64 assertFalse(c.isStateful()); 65 assertNotNull(c.toString()); 66 assertEquals(colorInXml, c.getColorForState(new int[]{0}, 0)); 67 } 68 69 @SmallTest testGetChangingConfigurations()70 public void testGetChangingConfigurations() { 71 final Resources res = getContext().getResources(); 72 ColorStateList c; 73 74 c = res.getColorStateList(R.color.testcolor, null); 75 assertEquals(c.getChangingConfigurations(), 0); 76 77 c = res.getColorStateList(R.color.testcolor_orientation, null); 78 assertEquals(ActivityInfo.CONFIG_ORIENTATION, c.getChangingConfigurations()); 79 } 80 81 @SmallTest testWithAlpha()82 public void testWithAlpha() { 83 final int[][] state = new int[][]{{0}, {0}}; 84 final int[] colors = new int[]{Color.RED, Color.BLUE}; 85 final ColorStateList c = new ColorStateList(state, colors); 86 final int alpha = 36; 87 final ColorStateList c1 = c.withAlpha(alpha); 88 assertNotSame(Color.RED, c1.getDefaultColor()); 89 assertEquals(alpha, c1.getDefaultColor() >>> 24); 90 assertEquals(Color.RED & 0x00FF0000, c1.getDefaultColor() & 0x00FF0000); 91 } 92 93 @SmallTest testWithLStar()94 public void testWithLStar() { 95 final int[][] state = new int[][]{{0}, {0}}; 96 final int[] colors = new int[]{Color.RED, Color.BLUE}; 97 final ColorStateList c = new ColorStateList(state, colors); 98 final double lStar = 50.0; 99 final ColorStateList c1 = c.withLStar((float) lStar); 100 assertNotSame(Color.RED, c1.getDefaultColor()); 101 102 final double[] labColor = new double[3]; 103 ColorUtils.colorToLAB(c1.getDefaultColor(), labColor); 104 final double targetLStar = labColor[0]; 105 106 assertEquals(lStar, targetLStar, 1.0 /* delta */); 107 } 108 109 @SmallTest testCreateFromXmlWithLStar()110 public void testCreateFromXmlWithLStar() throws Exception { 111 final int xmlId = R.color.testcolor_lstar; 112 final double lStarInXml = 50.0; 113 final int alphaInXml = 128; 114 115 final Resources res = getContext().getResources(); 116 final ColorStateList c = ColorStateList.createFromXml(res, res.getXml(xmlId)); 117 final int defaultColor = c.getDefaultColor(); 118 119 final double[] labColor = new double[3]; 120 ColorUtils.colorToLAB(defaultColor, labColor); 121 122 // There's precision loss when converting to @ColorInt. We need a small delta. 123 assertEquals(lStarInXml, labColor[0], 1.0 /* delta */); 124 assertEquals(alphaInXml, Color.alpha(defaultColor)); 125 } 126 127 @SmallTest testValueOf()128 public void testValueOf() { 129 final ColorStateList c = ColorStateList.valueOf(Color.GRAY); 130 assertEquals(Color.GRAY, c.getDefaultColor()); 131 } 132 133 @SmallTest testParcelable()134 public void testParcelable() { 135 final ColorStateList c = ColorStateList.valueOf(Color.GRAY); 136 final Parcel parcel = Parcel.obtain(); 137 c.writeToParcel(parcel, 0); 138 parcel.setDataPosition(0); 139 140 final ColorStateList actual = ColorStateList.CREATOR.createFromParcel(parcel); 141 assertEquals(c.isStateful(), actual.isStateful()); 142 assertEquals(c.getDefaultColor(), actual.getDefaultColor()); 143 144 parcel.recycle(); 145 } 146 147 @SmallTest testIsOpaque()148 public void testIsOpaque() { 149 ColorStateList c; 150 151 c = ColorStateList.valueOf(Color.GRAY); 152 assertTrue(c.isOpaque()); 153 154 c = ColorStateList.valueOf(0x80FFFFFF); 155 assertFalse(c.isOpaque()); 156 157 c = ColorStateList.valueOf(Color.TRANSPARENT); 158 assertFalse(c.isOpaque()); 159 } 160 } 161