1 /* 2 * Copyright 2019 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 17 package androidx.appcompat.widget; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 22 import android.app.Activity; 23 import android.content.res.Resources; 24 import android.graphics.drawable.Drawable; 25 import android.graphics.drawable.LayerDrawable; 26 27 import androidx.appcompat.resources.test.R; 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 import androidx.test.filters.SmallTest; 30 import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat; 31 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @SuppressWarnings("deprecation") 37 @SmallTest 38 @RunWith(AndroidJUnit4.class) 39 public class VectorEnabledTintResourcesTest { 40 @Rule 41 public final androidx.test.rule.ActivityTestRule<Activity> mActivityTestRule = 42 new androidx.test.rule.ActivityTestRule<>(Activity.class); 43 44 /** 45 * Ensures that VectorEnabledTintResources delegates calls to the wrapped Resources object. 46 */ 47 @Test testVectorEnabledTintResourcesDelegateBackToOriginalResources()48 public void testVectorEnabledTintResourcesDelegateBackToOriginalResources() { 49 final TestResources testResources = 50 new TestResources(mActivityTestRule.getActivity().getResources()); 51 52 // First make sure that the flag is false 53 testResources.resetGetDrawableCalled(); 54 assertFalse(testResources.wasGetDrawableCalled()); 55 56 // Now wrap in a TintResources instance and get a Drawable 57 final VectorEnabledTintResources tintResources = 58 new VectorEnabledTintResources(mActivityTestRule.getActivity(), testResources); 59 tintResources.getDrawable(android.R.drawable.ic_delete); 60 61 // We can't delegate to the wrapped Resource object's getDrawable() because it will break 62 // support for nested vector-enabled tinted resources. 63 assertFalse(testResources.wasGetDrawableCalled()); 64 65 tintResources.getString(android.R.string.ok); 66 67 // However, we can still delegate other calls. 68 assertTrue(testResources.wasGetStringCalled()); 69 } 70 testNestedVectorEnabledTintResources()71 public void testNestedVectorEnabledTintResources() { 72 Resources tintResources = new VectorEnabledTintResources(mActivityTestRule.getActivity(), 73 mActivityTestRule.getActivity().getResources()); 74 Drawable d = tintResources.getDrawable(R.drawable.vector_nested); 75 assertTrue(d instanceof LayerDrawable); 76 77 // Nested drawable is loaded using VectorEnabledTintResources. 78 LayerDrawable ld = (LayerDrawable) d; 79 assertTrue(ld.getDrawable(0) instanceof VectorDrawableCompat); 80 } 81 } 82