1package org.robolectric.android;
2
3import static android.os.Build.VERSION_CODES.KITKAT;
4import static android.os.Build.VERSION_CODES.LOLLIPOP;
5import static org.assertj.core.api.Assertions.assertThat;
6import static org.junit.Assert.assertEquals;
7import static org.junit.Assert.assertNotNull;
8import static org.robolectric.RuntimeEnvironment.application;
9
10import android.animation.Animator;
11import android.animation.AnimatorInflater;
12import android.content.res.Resources;
13import android.graphics.drawable.BitmapDrawable;
14import android.graphics.drawable.ColorDrawable;
15import android.graphics.drawable.LayerDrawable;
16import android.graphics.drawable.NinePatchDrawable;
17import android.graphics.drawable.VectorDrawable;
18import org.junit.Before;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.robolectric.R;
22import org.robolectric.RobolectricTestRunner;
23import org.robolectric.RuntimeEnvironment;
24import org.robolectric.annotation.Config;
25
26@RunWith(RobolectricTestRunner.class) // todo: @Config(sdk=ALL_SDKS) or something
27public class DrawableResourceLoaderTest {
28  private Resources resources;
29
30  @Before
31  public void setup() throws Exception {
32    resources = RuntimeEnvironment.application.getResources();
33  }
34
35  @Test
36  public void testGetDrawable_rainbow() throws Exception {
37    assertNotNull(RuntimeEnvironment.application.getResources().getDrawable(R.drawable.rainbow));
38  }
39
40  @Test
41  public void testGetDrawableBundle_shouldWorkWithSystem() throws Exception {
42    assertNotNull(resources.getDrawable(android.R.drawable.ic_popup_sync));
43  }
44
45  @Test
46  public void testGetDrawable_red() throws Exception {
47    assertNotNull(Resources.getSystem().getDrawable(android.R.drawable.ic_menu_help));
48  }
49
50  @Test
51  public void testDrawableTypes() {
52    assertThat(resources.getDrawable(R.drawable.l7_white)).isInstanceOf(BitmapDrawable.class);
53    assertThat(resources.getDrawable(R.drawable.l0_red)).isInstanceOf(BitmapDrawable.class);
54    assertThat(resources.getDrawable(R.drawable.nine_patch_drawable)).isInstanceOf(NinePatchDrawable.class);
55    assertThat(resources.getDrawable(R.drawable.rainbow)).isInstanceOf(LayerDrawable.class);
56  }
57
58  @Test @Config(maxSdk = KITKAT)
59  public void testVectorDrawableType_preVectors() {
60    assertThat(resources.getDrawable(R.drawable.an_image_or_vector)).isInstanceOf(BitmapDrawable.class);
61  }
62
63  @Test @Config(minSdk = LOLLIPOP)
64  public void testVectorDrawableType() {
65    assertThat(resources.getDrawable(R.drawable.an_image_or_vector)).isInstanceOf(VectorDrawable.class);
66  }
67
68  @Test
69  @Config(qualifiers = "xlarge")
70  public void testLayerDrawable_xlarge() {
71    assertEquals(6, ((LayerDrawable) RuntimeEnvironment.application.getResources().getDrawable(R.drawable.rainbow)).getNumberOfLayers());
72  }
73
74  @Test
75  public void testLayerDrawable() {
76    assertEquals(8, ((LayerDrawable) RuntimeEnvironment.application.getResources().getDrawable(R.drawable.rainbow)).getNumberOfLayers());
77  }
78
79  @Test
80  public void shouldCreateAnimators() throws Exception {
81    Animator animator = AnimatorInflater.loadAnimator(application, R.animator.spinning);
82    assertThat(animator).isInstanceOf((Class<? extends Animator>) Animator.class);
83  }
84
85  @Test
86  public void shouldCreateAnimsAndColors() throws Exception {
87    assertThat(resources.getDrawable(R.color.grey42)).isInstanceOf((Class<? extends android.graphics.drawable.Drawable>) ColorDrawable.class);
88  }
89}