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