13b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov/*
23b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * Copyright (C) 2017 The Android Open Source Project
33b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov *
43b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
53b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * you may not use this file except in compliance with the License.
63b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * You may obtain a copy of the License at
73b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov *
83b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
93b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov *
103b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
113b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
123b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * See the License for the specific language governing permissions and
143b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * limitations under the License.
153b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov */
163b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
173b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovpackage android.support.v7.widget;
183b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
193b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport static android.support.test.espresso.Espresso.onView;
203b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport static android.support.test.espresso.matcher.ViewMatchers.withId;
213b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport static android.support.v7.testutils.TestUtilsActions.setEnabled;
223b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
233b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.content.res.ColorStateList;
243b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.content.res.Resources;
253b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.graphics.PorterDuff;
263b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.graphics.drawable.Drawable;
273b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.annotation.ColorInt;
283b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.annotation.IdRes;
293b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.annotation.NonNull;
303b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.test.filters.SmallTest;
313b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.v4.content.res.ResourcesCompat;
323b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.v4.graphics.ColorUtils;
333b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.v7.appcompat.test.R;
343b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.v7.testutils.AppCompatTintableViewActions;
353b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.v7.testutils.BaseTestActivity;
363b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.support.v7.testutils.TestUtils;
373b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport android.widget.ImageView;
383b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
393b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovimport org.junit.Test;
403b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
413b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov/**
423b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * In addition to all tinting-related tests done by the base class, this class provides
433b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * testing for tinting image resources on appcompat-v7 view classes that extend directly
443b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov * or indirectly the core {@link ImageView} class.
453b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov */
463b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikovpublic abstract class AppCompatBaseImageViewTest<T extends ImageView>
473b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        extends AppCompatBaseViewTest<BaseTestActivity, T> {
483b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    public AppCompatBaseImageViewTest(Class clazz) {
493b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        super(clazz);
503b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    }
513b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
523b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    private void verifyImageSourceIsColoredAs(String description, @NonNull ImageView imageView,
533b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov            @ColorInt int color, int allowedComponentVariance) {
543b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        Drawable imageSource = imageView.getDrawable();
553b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        TestUtils.assertAllPixelsOfColor(description,
563b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                imageSource, imageSource.getIntrinsicWidth(), imageSource.getIntrinsicHeight(),
573b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                true, color, allowedComponentVariance, false);
583b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    }
593b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
603b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    /**
613b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * This method tests that image tinting is applied to tintable image view
623b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * in enabled and disabled state across a number of <code>ColorStateList</code>s set as
633b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * image source tint lists on the same image source.
643b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     */
653b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    @Test
663b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    @SmallTest
673b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    public void testImageTintingAcrossStateChange() {
683b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final @IdRes int viewId = R.id.view_tinted_source;
698d3b808ab4720ef4e9d58f2bba4e31f741d3898cAurimas Liutikas        final Resources res = mActivity.getResources();
703b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final T view = (T) mContainer.findViewById(viewId);
713b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
723b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int lilacDefault = ResourcesCompat.getColor(res, R.color.lilac_default, null);
733b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int lilacDisabled = ResourcesCompat.getColor(res, R.color.lilac_disabled, null);
743b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int sandDefault = ResourcesCompat.getColor(res, R.color.sand_default, null);
753b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int sandDisabled = ResourcesCompat.getColor(res, R.color.sand_disabled, null);
763b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int oceanDefault = ResourcesCompat.getColor(res, R.color.ocean_default, null);
773b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int oceanDisabled = ResourcesCompat.getColor(res, R.color.ocean_disabled, null);
783b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
793b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Test the default state for tinting set up in the layout XML file.
803b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default lilac tinting in enabled state", view,
813b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                lilacDefault, 0);
823b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
833b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Disable the view and check that the image has switched to the matching entry
843b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the default color state list.
853b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(false));
863b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default lilac tinting in disabled state", view,
873b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                lilacDisabled, 0);
883b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
893b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Enable the view and check that the image has switched to the matching entry
903b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the default color state list.
913b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(true));
923b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default lilac tinting in re-enabled state", view,
933b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                lilacDefault, 0);
943b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
953b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Load a new color state list, set it on the view and check that the image has
963b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // switched to the matching entry in newly set color state list.
973b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final ColorStateList sandColor = ResourcesCompat.getColorStateList(
983b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.color_state_list_sand, null);
993b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(
1003b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                AppCompatTintableViewActions.setImageSourceTintList(sandColor));
1013b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New sand tinting in enabled state", view,
1023b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                sandDefault, 0);
1033b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1043b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Disable the view and check that the image has switched to the matching entry
1053b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the newly set color state list.
1063b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(false));
1073b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New sand tinting in disabled state", view,
1083b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                sandDisabled, 0);
1093b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1103b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Enable the view and check that the image has switched to the matching entry
1113b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the newly set color state list.
1123b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(true));
1133b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New sand tinting in re-enabled state", view,
1143b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                sandDefault, 0);
1153b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1163b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Load another color state list, set it on the view and check that the image has
1173b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // switched to the matching entry in newly set color state list.
1183b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final ColorStateList oceanColor = ResourcesCompat.getColorStateList(
1193b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.color_state_list_ocean, null);
1203b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(
1213b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                AppCompatTintableViewActions.setImageSourceTintList(oceanColor));
1223b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New ocean tinting in enabled state", view,
1233b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                oceanDefault, 0);
1243b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1253b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Disable the view and check that the image has switched to the matching entry
1263b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the newly set color state list.
1273b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(false));
1283b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New ocean tinting in disabled state", view,
1293b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                oceanDisabled, 0);
1303b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1313b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Enable the view and check that the image has switched to the matching entry
1323b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the newly set color state list.
1333b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(true));
1343b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New ocean tinting in re-enabled state", view,
1353b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                oceanDefault, 0);
1363b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    }
1373b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1383b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    /**
1393b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * This method tests that image tinting is applied to tintable image view
1403b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * in enabled and disabled state across the same image source respects the currently set
1413b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * image source tinting mode.
1423b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     */
1433b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    @Test
1443b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    @SmallTest
1453b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    public void testImageTintingAcrossModeChange() {
1463b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final @IdRes int viewId = R.id.view_untinted_source;
1478d3b808ab4720ef4e9d58f2bba4e31f741d3898cAurimas Liutikas        final Resources res = mActivity.getResources();
1483b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final T view = (T) mContainer.findViewById(viewId);
1493b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1503b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int emeraldDefault = ResourcesCompat.getColor(
1513b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.emerald_translucent_default, null);
1523b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int emeraldDisabled = ResourcesCompat.getColor(
1533b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.emerald_translucent_disabled, null);
1543b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // This is the fill color of R.drawable.test_drawable_blue set on our view
1553b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // that we'll be testing in this method
1563b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int sourceColor = ResourcesCompat.getColor(
1573b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.test_blue, null);
1583b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1593b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Test the default state for tinting set up in the layout XML file.
1603b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default no tinting in enabled state", view,
1613b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                sourceColor, 0);
1623b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1633b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // From this point on in this method we're allowing a margin of error in checking the
1643b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // color of the image source. This is due to both translucent colors being used
1653b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the color state list and off-by-one discrepancies of SRC_OVER when it's compositing
1663b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // translucent color on top of solid fill color. This is where the allowed variance
1673b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // value of 2 comes from - one for compositing and one for color translucency.
1683b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final int allowedComponentVariance = 2;
1693b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1703b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Set src_in tint mode on our view
1713b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(
1723b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                AppCompatTintableViewActions.setImageSourceTintMode(PorterDuff.Mode.SRC_IN));
1733b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1743b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Load a new color state list, set it on the view and check that the image has
1753b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // switched to the matching entry in newly set color state list.
1763b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final ColorStateList emeraldColor = ResourcesCompat.getColorStateList(
1773b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.color_state_list_emerald_translucent, null);
1783b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(
1793b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                AppCompatTintableViewActions.setImageSourceTintList(emeraldColor));
1803b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New emerald tinting in enabled state under src_in", view,
1813b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                emeraldDefault, allowedComponentVariance);
1823b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1833b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Disable the view and check that the image has switched to the matching entry
1843b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the newly set color state list.
1853b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(false));
1863b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New emerald tinting in disabled state under src_in", view,
1873b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                emeraldDisabled, allowedComponentVariance);
1883b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1893b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Set src_over tint mode on our view. As the currently set tint list is using
1903b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // translucent colors, we expect the actual image source of the view to be different under
1913b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // this new mode (unlike src_in and src_over that behave identically when the destination is
1923b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // a fully filled rectangle and the source is an opaque color).
1933b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(
1943b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                AppCompatTintableViewActions.setImageSourceTintMode(PorterDuff.Mode.SRC_OVER));
1953b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
1963b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Enable the view and check that the image has switched to the matching entry
1973b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the color state list.
1983b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(true));
1993b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New emerald tinting in enabled state under src_over", view,
2003b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                ColorUtils.compositeColors(emeraldDefault, sourceColor),
2013b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                allowedComponentVariance);
2023b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2033b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Disable the view and check that the image has switched to the matching entry
2043b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the newly set color state list.
2053b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(false));
2063b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("New emerald tinting in disabled state under src_over",
2073b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, ColorUtils.compositeColors(emeraldDisabled, sourceColor),
2083b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                allowedComponentVariance);
2093b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    }
2103b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2113b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    /**
2123b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * This method tests that opaque tinting applied to tintable image source
2133b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * is applied correctly after changing the image source itself.
2143b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     */
2153b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    @Test
2163b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    @SmallTest
2173b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    public void testImageOpaqueTintingAcrossImageChange() {
2183b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final @IdRes int viewId = R.id.view_tinted_no_source;
2198d3b808ab4720ef4e9d58f2bba4e31f741d3898cAurimas Liutikas        final Resources res = mActivity.getResources();
2203b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final T view = (T) mContainer.findViewById(viewId);
2213b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2223b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int lilacDefault = ResourcesCompat.getColor(res, R.color.lilac_default, null);
2233b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int lilacDisabled = ResourcesCompat.getColor(res, R.color.lilac_disabled, null);
2243b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2253b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Set image source on our view
2263b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(AppCompatTintableViewActions.setImageResource(
2273b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                R.drawable.test_drawable_green));
2283b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2293b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Test the default state for tinting set up in the layout XML file.
2303b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default lilac tinting in enabled state on green source",
2313b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, lilacDefault, 0);
2323b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2333b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Disable the view and check that the image has switched to the matching entry
2343b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the default color state list.
2353b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(false));
2363b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default lilac tinting in disabled state on green source",
2373b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, lilacDisabled, 0);
2383b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2393b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Enable the view and check that the image has switched to the matching entry
2403b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the default color state list.
2413b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(true));
2423b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default lilac tinting in re-enabled state on green source",
2433b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, lilacDefault, 0);
2443b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2453b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Set a different image source on our view based on resource ID
2463b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(AppCompatTintableViewActions.setImageResource(
2473b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                R.drawable.test_drawable_red));
2483b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2493b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Test the default state for tinting set up in the layout XML file.
2503b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default lilac tinting in enabled state on red source",
2513b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, lilacDefault, 0);
2523b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2533b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Disable the view and check that the image has switched to the matching entry
2543b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the default color state list.
2553b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(false));
2563b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default lilac tinting in disabled state on red source",
2573b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, lilacDisabled, 0);
2583b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2593b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Enable the view and check that the image has switched to the matching entry
2603b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the default color state list.
2613b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(true));
2623b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Default lilac tinting in re-enabled state on red source",
2633b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, lilacDefault, 0);
2643b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    }
2653b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2663b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    /**
2673b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * This method tests that translucent tinting applied to tintable image source
2683b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     * is applied correctly after changing the image source itself.
2693b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov     */
2703b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    @Test
2713b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    @SmallTest
2723b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    public void testImageTranslucentTintingAcrossImageChange() {
2733b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final @IdRes int viewId = R.id.view_untinted_no_source;
2748d3b808ab4720ef4e9d58f2bba4e31f741d3898cAurimas Liutikas        final Resources res = mActivity.getResources();
2753b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final T view = (T) mContainer.findViewById(viewId);
2763b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2773b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int emeraldDefault = ResourcesCompat.getColor(
2783b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.emerald_translucent_default, null);
2793b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int emeraldDisabled = ResourcesCompat.getColor(
2803b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.emerald_translucent_disabled, null);
2813b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // This is the fill color of R.drawable.test_drawable_green that will be set on our view
2823b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // that we'll be testing in this method
2833b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int colorGreen = ResourcesCompat.getColor(
2843b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.test_green, null);
2853b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // This is the fill color of R.drawable.test_drawable_red that will be set on our view
2863b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // that we'll be testing in this method
2873b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        @ColorInt int colorRed = ResourcesCompat.getColor(
2883b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.test_red, null);
2893b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
2903b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Set src_over tint mode on our view. As the currently set tint list is using
2913b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // translucent colors, we expect the actual image source of the view to be different under
2923b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // this new mode (unlike src_in and src_over that behave identically when the destination is
2933b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // a fully filled rectangle and the source is an opaque color).
2943b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(
2953b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                AppCompatTintableViewActions.setImageSourceTintMode(PorterDuff.Mode.SRC_OVER));
2963b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Load and set a translucent color state list as the image source tint list
2973b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final ColorStateList emeraldColor = ResourcesCompat.getColorStateList(
2983b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                res, R.color.color_state_list_emerald_translucent, null);
2993b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(
3003b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                AppCompatTintableViewActions.setImageSourceTintList(emeraldColor));
3013b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
3023b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Set image source on our view
3033b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(AppCompatTintableViewActions.setImageResource(
3043b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                R.drawable.test_drawable_green));
3053b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
3063b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // From this point on in this method we're allowing a margin of error in checking the
3073b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // color of the image source. This is due to both translucent colors being used
3083b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the color state list and off-by-one discrepancies of SRC_OVER when it's compositing
3093b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // translucent color on top of solid fill color. This is where the allowed variance
3103b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // value of 2 comes from - one for compositing and one for color translucency.
3113b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        final int allowedComponentVariance = 2;
3123b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
3133b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Test the default state for tinting set up with the just loaded tint list.
3143b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Emerald tinting in enabled state on green source",
3153b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, ColorUtils.compositeColors(emeraldDefault, colorGreen),
3163b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                allowedComponentVariance);
3173b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
3183b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Disable the view and check that the image has switched to the matching entry
3193b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the default color state list.
3203b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(false));
3213b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Emerald tinting in disabled state on green source",
3223b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, ColorUtils.compositeColors(emeraldDisabled, colorGreen),
3233b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                allowedComponentVariance);
3243b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
3253b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Enable the view and check that the image has switched to the matching entry
3263b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in the default color state list.
3273b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(true));
3283b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Emerald tinting in re-enabled state on green source",
3293b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, ColorUtils.compositeColors(emeraldDefault, colorGreen),
3303b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                allowedComponentVariance);
3313b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
3323b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Set a different image source on our view based on resource ID
3333b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(AppCompatTintableViewActions.setImageResource(
3343b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                R.drawable.test_drawable_red));
3353b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
3363b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Test the default state for tinting the new image with the same color state list
3373b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Emerald tinting in enabled state on red source",
3383b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, ColorUtils.compositeColors(emeraldDefault, colorRed),
3393b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                allowedComponentVariance);
3403b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
3413b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Disable the view and check that the image has switched to the matching entry
3423b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in our current color state list.
3433b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(false));
3443b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Emerald tinting in disabled state on red source",
3453b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, ColorUtils.compositeColors(emeraldDisabled, colorRed),
3463b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                allowedComponentVariance);
3473b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov
3483b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // Enable the view and check that the image has switched to the matching entry
3493b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        // in our current color state list.
3503b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        onView(withId(viewId)).perform(setEnabled(true));
3513b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov        verifyImageSourceIsColoredAs("Emerald tinting in re-enabled state on red source",
3523b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                view, ColorUtils.compositeColors(emeraldDefault, colorRed),
3533b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov                allowedComponentVariance);
3543b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov    }
3555788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov
3565788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov    /**
3575788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov     * This method tests that background tinting applied on a tintable image view does not
3585788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov     * affect the tinting of the image source.
3595788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov     */
3605788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov    @Test
3615788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov    @SmallTest
3625788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov    public void testImageTintingAcrossBackgroundTintingChange() {
3635788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        final @IdRes int viewId = R.id.view_untinted_source;
3648d3b808ab4720ef4e9d58f2bba4e31f741d3898cAurimas Liutikas        final Resources res = mActivity.getResources();
3655788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        final T view = (T) mContainer.findViewById(viewId);
3665788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov
3675788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        @ColorInt int lilacDefault = ResourcesCompat.getColor(res, R.color.lilac_default, null);
3685788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        @ColorInt int lilacDisabled = ResourcesCompat.getColor(res, R.color.lilac_disabled, null);
3695788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // This is the fill color of R.drawable.test_drawable_blue set on our view
3705788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // that we'll be testing in this method
3715788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        @ColorInt int sourceColor = ResourcesCompat.getColor(
3725788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                res, R.color.test_blue, null);
3735788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        @ColorInt int newSourceColor = ResourcesCompat.getColor(
3745788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                res, R.color.test_red, null);
3755788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov
3765788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // Test the default state for tinting set up in the layout XML file.
3775788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        verifyImageSourceIsColoredAs("Default no tinting in enabled state", view,
3785788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                sourceColor, 0);
3795788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov
3805788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // Change background tinting of our image
3815788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        final ColorStateList lilacColor = ResourcesCompat.getColorStateList(
3825788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                mResources, R.color.color_state_list_lilac, null);
3835788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        onView(withId(viewId)).perform(
3845788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                AppCompatTintableViewActions.setBackgroundResource(
3855788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                        R.drawable.test_background_green));
3865788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        onView(withId(viewId)).perform(
3875788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                AppCompatTintableViewActions.setBackgroundTintMode(PorterDuff.Mode.SRC_IN));
3885788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        onView(withId(viewId)).perform(
3895788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                AppCompatTintableViewActions.setBackgroundTintList(lilacColor));
3905788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov
3915788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // Verify that the image still has the original color (untinted)
3925788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        verifyImageSourceIsColoredAs("No image tinting after change in background tinting", view,
3935788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                sourceColor, 0);
3945788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov
3955788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // Now set a different image source
3965788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        onView(withId(viewId)).perform(
3975788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                AppCompatTintableViewActions.setImageResource(R.drawable.test_drawable_red));
3985788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // And verify that the image has the new color (untinted)
3995788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        verifyImageSourceIsColoredAs("No image tinting after change of image source", view,
4005788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                newSourceColor, 0);
4015788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov
4025788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // Change the background tinting again
4035788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        final ColorStateList sandColor = ResourcesCompat.getColorStateList(
4045788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                mResources, R.color.color_state_list_sand, null);
4055788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        onView(withId(viewId)).perform(
4065788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                AppCompatTintableViewActions.setBackgroundTintList(sandColor));
4075788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // And verify that the image still has the same new color (untinted)
4085788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        verifyImageSourceIsColoredAs("No image tinting after change in background tinting", view,
4095788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                newSourceColor, 0);
4105788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov
4115788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // Now set up image tinting on our view. We're using a color state list with fully
4125788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // opaque colors, and we expect the matching entry in that list to be applied on the
4135788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        // image source (ignoring the background tinting)
4145788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        onView(withId(viewId)).perform(
4155788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                AppCompatTintableViewActions.setImageSourceTintMode(PorterDuff.Mode.SRC_IN));
4165788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        onView(withId(viewId)).perform(
4175788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                AppCompatTintableViewActions.setImageSourceTintList(lilacColor));
4185788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov        verifyImageSourceIsColoredAs("New lilac image tinting", view,
4195788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov                lilacDefault, 0);
4205788552fbf5a44a86e49e17e5ce86f013a6f542eKirill Grouchnikov    }
4213b04742a1d5209de20b93732e490a354d442294dKirill Grouchnikov}
422