ColorStateListTest.java revision 045a6fa479180d6a6e85ad1913afe1a04bb5e4f6
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 */
16package android.content.res.cts;
17
18import java.io.IOException;
19
20import org.xmlpull.v1.XmlPullParser;
21import org.xmlpull.v1.XmlPullParserException;
22
23import android.content.res.ColorStateList;
24import android.content.res.Resources;
25import android.content.res.Resources.NotFoundException;
26import android.graphics.Color;
27import android.os.Parcel;
28import android.test.AndroidTestCase;
29
30import com.android.cts.content.R;
31
32
33public class ColorStateListTest extends AndroidTestCase {
34    public void testColorStateList() throws NotFoundException, XmlPullParserException, IOException {
35        final int[][] state = new int[][] { { 0 }, { 0 } };
36        final int[] colors = new int[] { Color.RED, Color.BLUE };
37        ColorStateList c = new ColorStateList(state, colors);
38        assertTrue(c.isStateful());
39        assertEquals(Color.RED, c.getDefaultColor());
40
41        final int alpha = 36;
42        final ColorStateList c1 = c.withAlpha(alpha);
43        assertNotSame(Color.RED, c1.getDefaultColor());
44        // check alpha
45        assertEquals(alpha, c1.getDefaultColor() >>> 24);
46        assertEquals(Color.RED & 0x00FF0000, c1.getDefaultColor() & 0x00FF0000);
47
48        final int xmlId = R.drawable.testcolor;
49        final int colorInXml = 0xFFA6C839;// this color value is define in testcolor.xml file.
50        final Resources res = getContext().getResources();
51        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        c = ColorStateList.valueOf(Color.GRAY);
59        assertEquals(Color.GRAY, c.getDefaultColor());
60
61        final Parcel parcel = Parcel.obtain();
62        c.writeToParcel(parcel, 0);
63        parcel.setDataPosition(0);
64        ColorStateList actual = ColorStateList.CREATOR.createFromParcel(parcel);
65        // can only compare the state and the default color. because no API to
66        // get every color of ColorStateList
67        assertEquals(c.isStateful(), actual.isStateful());
68        assertEquals(c.getDefaultColor(), actual.getDefaultColor());
69    }
70}
71