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