1 /* 2 * Copyright (C) 2007 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 17 package android.graphics; 18 19 import android.content.res.Resources; 20 import android.content.res.ColorStateList; 21 import android.test.AndroidTestCase; 22 import android.core.R; 23 import android.test.suitebuilder.annotation.SmallTest; 24 25 /** 26 * Tests of {@link android.graphics.ColorStateList} 27 */ 28 29 public class ColorStateListTest extends AndroidTestCase { 30 31 private Resources mResources; 32 private int mFailureColor; 33 34 @Override setUp()35 protected void setUp() throws Exception { 36 super.setUp(); 37 mResources = mContext.getResources(); 38 mFailureColor = mResources.getColor(R.color.failColor); 39 } 40 41 @SmallTest testStateIsInList()42 public void testStateIsInList() throws Exception { 43 ColorStateList colorStateList = mResources.getColorStateList(R.color.color1); 44 int[] focusedState = {android.R.attr.state_focused}; 45 int focusColor = colorStateList.getColorForState(focusedState, R.color.failColor); 46 assertEquals(mResources.getColor(R.color.testcolor1), focusColor); 47 } 48 49 @SmallTest testEmptyState()50 public void testEmptyState() throws Exception { 51 ColorStateList colorStateList = mResources.getColorStateList(R.color.color1); 52 int[] emptyState = {}; 53 int defaultColor = colorStateList.getColorForState(emptyState, mFailureColor); 54 assertEquals(mResources.getColor(R.color.testcolor2), defaultColor); 55 } 56 57 @SmallTest testGetColor()58 public void testGetColor() throws Exception { 59 int defaultColor = mResources.getColor(R.color.color1); 60 assertEquals(mResources.getColor(R.color.testcolor2), defaultColor); 61 } 62 63 @SmallTest testGetColorWhenListHasNoDefault()64 public void testGetColorWhenListHasNoDefault() throws Exception { 65 int defaultColor = mResources.getColor(R.color.color_no_default); 66 assertEquals(mResources.getColor(R.color.testcolor1), defaultColor); 67 } 68 } 69