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