14c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes/*
24c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes * Copyright (C) 2015 The Android Open Source Project
34c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes *
44c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes * Licensed under the Apache License, Version 2.0 (the "License");
54c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes * you may not use this file except in compliance with the License.
64c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes * You may obtain a copy of the License at
74c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes *
84c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes *      http://www.apache.org/licenses/LICENSE-2.0
94c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes *
104c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes * Unless required by applicable law or agreed to in writing, software
114c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes * distributed under the License is distributed on an "AS IS" BASIS,
124c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes * See the License for the specific language governing permissions and
144c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes * limitations under the License.
154c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes */
164c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
174c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banespackage android.support.v7.res.content;
184c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
194c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport static org.junit.Assert.assertEquals;
204c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport static org.junit.Assert.assertNotNull;
214c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
224c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.app.Activity;
234c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.content.res.ColorStateList;
244c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.graphics.Color;
25754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport android.support.test.filters.SmallTest;
264c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.support.v4.graphics.ColorUtils;
274c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.support.v7.app.AppCompatActivity;
284c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.support.v7.app.BaseInstrumentationTestCase;
294c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.support.v7.appcompat.test.R;
304c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.support.v7.content.res.AppCompatResources;
314c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport android.support.v7.testutils.TestUtils;
324c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
334c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banesimport org.junit.Test;
344c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
354c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes@SmallTest
364c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banespublic class AppCompatResourcesTestCase extends BaseInstrumentationTestCase<AppCompatActivity> {
374c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
384c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes    public AppCompatResourcesTestCase() {
394c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        super(AppCompatActivity.class);
404c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes    }
414c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
424c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes    @Test
434c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes    public void testGetColorStateListWithThemedAttributes() {
444c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        final Activity context = getActivity();
454c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
464c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        final int colorForegound = TestUtils.getThemeAttrColor(
474c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes                context, android.R.attr.colorForeground);
484c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
494c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        final ColorStateList result = AppCompatResources.getColorStateList(
504c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes                context, R.color.color_state_list_themed_attrs);
514c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
524c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        assertNotNull(result);
534c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
544c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        // Now check the state colors
554c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
564c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        // Disabled color should equals colorForeground with 50% of its alpha
574c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        final int expectedDisabled = ColorUtils.setAlphaComponent(colorForegound,
584c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes                Math.round(Color.alpha(colorForegound) * 0.5f));
594c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        assertEquals(expectedDisabled, result.getColorForState(
604c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes                new int[]{-android.R.attr.state_enabled}, 0));
614c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
624c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        // Default color should equal colorForeground
634c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        assertEquals(colorForegound, result.getDefaultColor());
644c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes    }
654c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
664c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes    @Test
674c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes    public void testGetDrawableVectorResource() {
684c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        final Activity context = getActivity();
694c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        assertNotNull(AppCompatResources.getDrawable(context, R.drawable.test_vector_off));
704c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes    }
714c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes
724c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes}
73