1/*
2 * Copyright (C) 2015 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
17package android.support.v7.res.content;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNotNull;
21
22import android.app.Activity;
23import android.content.res.ColorStateList;
24import android.graphics.Color;
25import android.support.test.filters.SmallTest;
26import android.support.test.rule.ActivityTestRule;
27import android.support.test.runner.AndroidJUnit4;
28import android.support.v4.graphics.ColorUtils;
29import android.support.v7.app.AppCompatActivity;
30import android.support.v7.appcompat.test.R;
31import android.support.v7.content.res.AppCompatResources;
32import android.support.v7.testutils.TestUtils;
33
34import org.junit.Rule;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37
38@SmallTest
39@RunWith(AndroidJUnit4.class)
40public class AppCompatResourcesTestCase {
41    @Rule
42    public final ActivityTestRule<AppCompatActivity> mActivityTestRule;
43
44    public AppCompatResourcesTestCase() {
45        mActivityTestRule = new ActivityTestRule<>(AppCompatActivity.class);
46    }
47
48    @Test
49    public void testGetColorStateListWithThemedAttributes() {
50        final Activity context = mActivityTestRule.getActivity();
51
52        final int colorForegound = TestUtils.getThemeAttrColor(
53                context, android.R.attr.colorForeground);
54
55        final ColorStateList result = AppCompatResources.getColorStateList(
56                context, R.color.color_state_list_themed_attrs);
57
58        assertNotNull(result);
59
60        // Now check the state colors
61
62        // Disabled color should equals colorForeground with 50% of its alpha
63        final int expectedDisabled = ColorUtils.setAlphaComponent(colorForegound,
64                Math.round(Color.alpha(colorForegound) * 0.5f));
65        assertEquals(expectedDisabled, result.getColorForState(
66                new int[]{-android.R.attr.state_enabled}, 0));
67
68        // Default color should equal colorForeground
69        assertEquals(colorForegound, result.getDefaultColor());
70    }
71
72    @Test
73    public void testGetDrawableVectorResource() {
74        final Activity context = mActivityTestRule.getActivity();
75        assertNotNull(AppCompatResources.getDrawable(context, R.drawable.test_vector_off));
76    }
77
78}
79