Home | History | Annotate | Download | only in cts
      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 java.io.IOException;
     19 
     20 import org.xmlpull.v1.XmlPullParser;
     21 import org.xmlpull.v1.XmlPullParserException;
     22 
     23 import android.content.pm.ActivityInfo;
     24 import android.content.res.ColorStateList;
     25 import android.content.res.Resources;
     26 import android.content.res.Resources.NotFoundException;
     27 import android.content.res.Resources.Theme;
     28 import android.graphics.Color;
     29 import android.os.Parcel;
     30 import android.test.AndroidTestCase;
     31 
     32 import android.content.cts.R;
     33 import android.test.suitebuilder.annotation.SmallTest;
     34 
     35 public class ColorStateListTest extends AndroidTestCase {
     36 
     37     @SmallTest
     38     public void testConstructor() {
     39         final int[][] state = new int[][]{{0}, {0}};
     40         final int[] colors = new int[]{Color.RED, Color.BLUE};
     41         final ColorStateList c = new ColorStateList(state, colors);
     42         assertTrue(c.isStateful());
     43         assertEquals(Color.RED, c.getDefaultColor());
     44     }
     45 
     46     @SmallTest
     47     public void testCreateFromXml() throws Exception {
     48         final int xmlId = R.color.testcolor;
     49         final int colorInXml = 0xFFA6C839; // this color value is defined in testcolor.xml file.
     50         final Resources res = getContext().getResources();
     51         final ColorStateList c = ColorStateList.createFromXml(res, res.getXml(xmlId));
     52         assertEquals(colorInXml, c.getDefaultColor());
     53         assertEquals(0, c.describeContents());
     54         assertFalse(c.isStateful());
     55         assertNotNull(c.toString());
     56         assertEquals(colorInXml, c.getColorForState(new int[]{0}, 0));
     57     }
     58 
     59     @SmallTest
     60     public void testCreateFromXmlThemed() throws Exception {
     61         final int xmlId = R.color.testcolor_themed;
     62         final int colorInXml = Color.BLACK; // this color value is defined in styles.xml file.
     63         final Resources res = getContext().getResources();
     64         final Theme theme = res.newTheme();
     65         theme.applyStyle(R.style.Theme_ThemedDrawableTest, true);
     66         final ColorStateList c = ColorStateList.createFromXml(res, res.getXml(xmlId), theme);
     67         assertEquals(colorInXml, c.getDefaultColor());
     68         assertEquals(0, c.describeContents());
     69         assertFalse(c.isStateful());
     70         assertNotNull(c.toString());
     71         assertEquals(colorInXml, c.getColorForState(new int[]{0}, 0));
     72     }
     73 
     74     @SmallTest
     75     public void testGetChangingConfigurations() {
     76         final Resources res = getContext().getResources();
     77         ColorStateList c;
     78 
     79         c = res.getColorStateList(R.color.testcolor, null);
     80         assertEquals(c.getChangingConfigurations(), 0);
     81 
     82         c = res.getColorStateList(R.color.testcolor_orientation, null);
     83         assertEquals(ActivityInfo.CONFIG_ORIENTATION, c.getChangingConfigurations());
     84     }
     85 
     86     @SmallTest
     87     public void testWithAlpha() {
     88         final int[][] state = new int[][]{{0}, {0}};
     89         final int[] colors = new int[]{Color.RED, Color.BLUE};
     90         final ColorStateList c = new ColorStateList(state, colors);
     91         final int alpha = 36;
     92         final ColorStateList c1 = c.withAlpha(alpha);
     93         assertNotSame(Color.RED, c1.getDefaultColor());
     94         assertEquals(alpha, c1.getDefaultColor() >>> 24);
     95         assertEquals(Color.RED & 0x00FF0000, c1.getDefaultColor() & 0x00FF0000);
     96     }
     97 
     98     @SmallTest
     99     public void testValueOf() {
    100         final ColorStateList c = ColorStateList.valueOf(Color.GRAY);
    101         assertEquals(Color.GRAY, c.getDefaultColor());
    102     }
    103 
    104     @SmallTest
    105     public void testParcelable() {
    106         final ColorStateList c = ColorStateList.valueOf(Color.GRAY);
    107         final Parcel parcel = Parcel.obtain();
    108         c.writeToParcel(parcel, 0);
    109         parcel.setDataPosition(0);
    110 
    111         final ColorStateList actual = ColorStateList.CREATOR.createFromParcel(parcel);
    112         assertEquals(c.isStateful(), actual.isStateful());
    113         assertEquals(c.getDefaultColor(), actual.getDefaultColor());
    114     }
    115 
    116     @SmallTest
    117     public void testIsOpaque() {
    118         ColorStateList c;
    119 
    120         c = ColorStateList.valueOf(Color.GRAY);
    121         assertTrue(c.isOpaque());
    122 
    123         c = ColorStateList.valueOf(0x80FFFFFF);
    124         assertFalse(c.isOpaque());
    125 
    126         c = ColorStateList.valueOf(Color.TRANSPARENT);
    127         assertFalse(c.isOpaque());
    128     }
    129 }
    130