1 package org.robolectric.android; 2 3 import static android.os.Build.VERSION_CODES.KITKAT_WATCH; 4 import static android.os.Build.VERSION_CODES.LOLLIPOP; 5 import static com.google.common.truth.Truth.assertThat; 6 import static org.junit.Assert.assertEquals; 7 import static org.junit.Assert.assertNotNull; 8 import static org.junit.Assume.assumeTrue; 9 import static org.robolectric.RuntimeEnvironment.application; 10 import static org.robolectric.shadows.ShadowAssetManager.useLegacy; 11 12 import android.animation.Animator; 13 import android.animation.AnimatorInflater; 14 import android.content.res.Resources; 15 import android.graphics.drawable.BitmapDrawable; 16 import android.graphics.drawable.ColorDrawable; 17 import android.graphics.drawable.LayerDrawable; 18 import android.graphics.drawable.NinePatchDrawable; 19 import android.graphics.drawable.VectorDrawable; 20 import androidx.test.core.app.ApplicationProvider; 21 import androidx.test.ext.junit.runners.AndroidJUnit4; 22 import org.junit.Before; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.robolectric.R; 26 import org.robolectric.annotation.Config; 27 28 @RunWith(AndroidJUnit4.class) 29 public class DrawableResourceLoaderTest { 30 private Resources resources; 31 32 @Before setup()33 public void setup() throws Exception { 34 assumeTrue(useLegacy()); 35 resources = ApplicationProvider.getApplicationContext().getResources(); 36 } 37 38 @Test testGetDrawable_rainbow()39 public void testGetDrawable_rainbow() throws Exception { 40 assertNotNull( 41 ApplicationProvider.getApplicationContext().getResources().getDrawable(R.drawable.rainbow)); 42 } 43 44 @Test testGetDrawableBundle_shouldWorkWithSystem()45 public void testGetDrawableBundle_shouldWorkWithSystem() throws Exception { 46 assertNotNull(resources.getDrawable(android.R.drawable.ic_popup_sync)); 47 } 48 49 @Test testGetDrawable_red()50 public void testGetDrawable_red() throws Exception { 51 assertNotNull(Resources.getSystem().getDrawable(android.R.drawable.ic_menu_help)); 52 } 53 54 @Test testDrawableTypes()55 public void testDrawableTypes() { 56 assertThat(resources.getDrawable(R.drawable.l7_white)).isInstanceOf(BitmapDrawable.class); 57 assertThat(resources.getDrawable(R.drawable.l0_red)).isInstanceOf(BitmapDrawable.class); 58 assertThat(resources.getDrawable(R.drawable.nine_patch_drawable)).isInstanceOf(NinePatchDrawable.class); 59 assertThat(resources.getDrawable(R.drawable.rainbow)).isInstanceOf(LayerDrawable.class); 60 } 61 62 @Test @Config(maxSdk = KITKAT_WATCH) testVectorDrawableType_preVectors()63 public void testVectorDrawableType_preVectors() { 64 assertThat(resources.getDrawable(R.drawable.an_image_or_vector)).isInstanceOf(BitmapDrawable.class); 65 } 66 67 @Test @Config(minSdk = LOLLIPOP) testVectorDrawableType()68 public void testVectorDrawableType() { 69 assertThat(resources.getDrawable(R.drawable.an_image_or_vector)).isInstanceOf(VectorDrawable.class); 70 } 71 72 @Test 73 @Config(qualifiers = "land") testLayerDrawable_xlarge()74 public void testLayerDrawable_xlarge() { 75 assertEquals( 76 6, 77 ((LayerDrawable) 78 ApplicationProvider.getApplicationContext() 79 .getResources() 80 .getDrawable(R.drawable.rainbow)) 81 .getNumberOfLayers()); 82 } 83 84 @Test testLayerDrawable()85 public void testLayerDrawable() { 86 assertEquals( 87 8, 88 ((LayerDrawable) 89 ApplicationProvider.getApplicationContext() 90 .getResources() 91 .getDrawable(R.drawable.rainbow)) 92 .getNumberOfLayers()); 93 } 94 95 @Test shouldCreateAnimators()96 public void shouldCreateAnimators() throws Exception { 97 Animator animator = AnimatorInflater.loadAnimator(application, R.animator.spinning); 98 assertThat(animator).isInstanceOf((Class<? extends Animator>) Animator.class); 99 } 100 101 @Test shouldCreateAnimsAndColors()102 public void shouldCreateAnimsAndColors() throws Exception { 103 assertThat(resources.getDrawable(R.color.grey42)).isInstanceOf((Class<? extends android.graphics.drawable.Drawable>) ColorDrawable.class); 104 } 105 } 106