1/*
2 * Copyright (C) 2016 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 androidx.appcompat.widget;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21
22import android.content.res.Resources;
23import android.graphics.drawable.Drawable;
24import android.support.test.filters.SmallTest;
25import android.support.test.rule.ActivityTestRule;
26import android.support.test.runner.AndroidJUnit4;
27
28import androidx.appcompat.app.AppCompatActivity;
29
30import org.junit.Rule;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34@SmallTest
35@RunWith(AndroidJUnit4.class)
36public class TintResourcesTest {
37    @Rule
38    public final ActivityTestRule<AppCompatActivity> mActivityTestRule =
39            new ActivityTestRule<>(AppCompatActivity.class);
40
41    @Test
42    public void testTintResourcesDelegateBackToOriginalResources() {
43        final TestResources testResources =
44                new TestResources(mActivityTestRule.getActivity().getResources());
45        // First make sure that the flag is false
46        assertFalse(testResources.wasGetDrawableCalled());
47
48        // Now wrap in a TintResources instance and get a Drawable
49        final Resources tintResources =
50                new TintResources(mActivityTestRule.getActivity(), testResources);
51        tintResources.getDrawable(android.R.drawable.ic_delete);
52
53        // ...and assert that the flag was flipped
54        assertTrue(testResources.wasGetDrawableCalled());
55    }
56
57    /**
58     * Special Resources class which returns a known Drawable instance from a special ID
59     */
60    private static class TestResources extends Resources {
61        private boolean mGetDrawableCalled;
62
63        public TestResources(Resources res) {
64            super(res.getAssets(), res.getDisplayMetrics(), res.getConfiguration());
65        }
66
67        @Override
68        public Drawable getDrawable(int id) throws NotFoundException {
69            mGetDrawableCalled = true;
70            return super.getDrawable(id);
71        }
72
73        public boolean wasGetDrawableCalled() {
74            return mGetDrawableCalled;
75        }
76    }
77
78}
79