1package org.robolectric.shadows;
2
3import static java.nio.charset.StandardCharsets.UTF_8;
4import static org.assertj.core.api.Assertions.assertThat;
5import static org.robolectric.Shadows.shadowOf;
6
7import android.content.res.Resources;
8import android.graphics.Bitmap;
9import android.graphics.Canvas;
10import android.graphics.ColorMatrix;
11import android.graphics.ColorMatrixColorFilter;
12import android.graphics.Shader;
13import android.graphics.drawable.BitmapDrawable;
14import android.graphics.drawable.Drawable;
15import java.io.ByteArrayInputStream;
16import java.io.InputStream;
17import org.junit.Test;
18import org.junit.runner.RunWith;
19import org.robolectric.R;
20import org.robolectric.RobolectricTestRunner;
21import org.robolectric.RuntimeEnvironment;
22import org.robolectric.Shadows;
23import org.robolectric.shadow.api.Shadow;
24
25@RunWith(RobolectricTestRunner.class)
26public class ShadowBitmapDrawableTest {
27  private final Resources resources = RuntimeEnvironment.application.getResources();
28
29  @Test
30  public void constructors_shouldSetBitmap() throws Exception {
31    Bitmap bitmap = Shadow.newInstanceOf(Bitmap.class);
32    BitmapDrawable drawable = new BitmapDrawable(bitmap);
33    assertThat(drawable.getBitmap()).isEqualTo(bitmap);
34
35    drawable = new BitmapDrawable(resources, bitmap);
36    assertThat(drawable.getBitmap()).isEqualTo(bitmap);
37  }
38
39  @Test
40  public void getBitmap_shouldReturnBitmapUsedToDraw() throws Exception {
41    BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
42    assertThat(shadowOf(drawable.getBitmap()).getDescription()).isEqualTo("Bitmap for resource:org.robolectric:drawable/an_image");
43  }
44
45  @Test
46  public void mutate_createsDeepCopy() throws Exception {
47    BitmapDrawable original = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
48    Drawable mutated = original.mutate();
49    assertThat(original).isNotSameAs(mutated);
50    assertThat(mutated instanceof BitmapDrawable).isTrue();
51    assertThat(original).isEqualTo(mutated);
52  }
53
54  @Test
55  public void draw_shouldCopyDescriptionToCanvas() throws Exception {
56    BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
57    Canvas canvas = new Canvas();
58    drawable.draw(canvas);
59
60    assertThat(shadowOf(canvas).getDescription()).isEqualTo("Bitmap for resource:org.robolectric:drawable/an_image");
61  }
62
63  @Test
64  public void shouldInheritSourceStringFromDrawableDotCreateFromStream() throws Exception {
65    InputStream emptyInputStream = new ByteArrayInputStream("".getBytes(UTF_8));
66    BitmapDrawable drawable = (BitmapDrawable) Drawable.createFromStream(emptyInputStream, "source string value");
67    assertThat(shadowOf(drawable).getSource()).isEqualTo("source string value");
68  }
69
70  @Test
71  public void withColorFilterSet_draw_shouldCopyDescriptionToCanvas() throws Exception {
72    BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
73    drawable.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix()));
74    Canvas canvas = new Canvas();
75    drawable.draw(canvas);
76
77    assertThat(shadowOf(canvas).getDescription()).isEqualTo("Bitmap for resource:org.robolectric:drawable/an_image with ColorMatrixColorFilter<1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0>");
78  }
79
80  @Test
81  public void shouldStillHaveShadow() throws Exception {
82    Drawable drawable = resources.getDrawable(R.drawable.an_image);
83    assertThat(Shadows.shadowOf(drawable).getCreatedFromResId()).isEqualTo(R.drawable.an_image);
84  }
85
86  @Test
87  public void shouldSetTileModeXY() throws Exception {
88    BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
89    drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
90    assertThat(drawable.getTileModeX()).isEqualTo(Shader.TileMode.REPEAT);
91    assertThat(drawable.getTileModeY()).isEqualTo(Shader.TileMode.MIRROR);
92  }
93
94  @Test
95  public void constructor_shouldSetTheIntrinsicWidthAndHeightToTheWidthAndHeightOfTheBitmap() throws Exception {
96    Bitmap bitmap = Bitmap.createBitmap(5, 10, Bitmap.Config.ARGB_8888);
97    BitmapDrawable drawable = new BitmapDrawable(RuntimeEnvironment.application.getResources(), bitmap);
98    assertThat(drawable.getIntrinsicWidth()).isEqualTo(5);
99    assertThat(drawable.getIntrinsicHeight()).isEqualTo(10);
100  }
101
102  @Test
103  public void constructor_shouldAcceptNullBitmap() throws Exception {
104    assertThat(new BitmapDrawable(RuntimeEnvironment.application.getResources(), (Bitmap) null)).isNotNull();
105  }
106}
107