Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static java.nio.charset.StandardCharsets.UTF_8;
      4 import static org.assertj.core.api.Assertions.assertThat;
      5 import static org.robolectric.Shadows.shadowOf;
      6 
      7 import android.content.res.Resources;
      8 import android.graphics.Bitmap;
      9 import android.graphics.Canvas;
     10 import android.graphics.ColorMatrix;
     11 import android.graphics.ColorMatrixColorFilter;
     12 import android.graphics.Shader;
     13 import android.graphics.drawable.BitmapDrawable;
     14 import android.graphics.drawable.Drawable;
     15 import java.io.ByteArrayInputStream;
     16 import java.io.InputStream;
     17 import org.junit.Test;
     18 import org.junit.runner.RunWith;
     19 import org.robolectric.R;
     20 import org.robolectric.RobolectricTestRunner;
     21 import org.robolectric.RuntimeEnvironment;
     22 import org.robolectric.Shadows;
     23 import org.robolectric.shadow.api.Shadow;
     24 
     25 @RunWith(RobolectricTestRunner.class)
     26 public 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