1 package com.xtremelabs.robolectric.shadows; 2 3 import android.content.res.Resources; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.graphics.Matrix; 7 import android.graphics.drawable.AnimationDrawable; 8 import android.graphics.drawable.Drawable; 9 import android.graphics.drawable.LayerDrawable; 10 import android.widget.ImageView; 11 import com.xtremelabs.robolectric.R; 12 import com.xtremelabs.robolectric.Robolectric; 13 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 14 import org.junit.Before; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 18 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 19 import static com.xtremelabs.robolectric.Robolectric.visualize; 20 import static org.hamcrest.CoreMatchers.equalTo; 21 import static org.hamcrest.CoreMatchers.is; 22 import static org.hamcrest.MatcherAssert.assertThat; 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertFalse; 25 import static org.junit.Assert.assertTrue; 26 27 @RunWith(WithTestDefaultsRunner.class) 28 public class ImageViewTest { 29 private ImageView imageView; 30 31 @Before setUp()32 public void setUp() throws Exception { 33 Resources resources = Robolectric.application.getResources(); 34 Bitmap bitmap = BitmapFactory.decodeResource(resources, 35 R.drawable.an_image); 36 imageView = new ImageView(Robolectric.application); 37 imageView.setImageBitmap(bitmap); 38 } 39 40 @Test shouldDrawWithImageMatrix()41 public void shouldDrawWithImageMatrix() throws Exception { 42 imageView.setImageMatrix(new Matrix()); 43 assertEquals("Bitmap for resource:drawable/an_image", 44 visualize(imageView)); 45 46 Matrix matrix = new Matrix(); 47 matrix.setTranslate(15, 20); 48 imageView.setImageMatrix(matrix); 49 assertEquals("Bitmap for resource:drawable/an_image at (15,20)", 50 visualize(imageView)); 51 } 52 53 @Test shouldCopyMatrixSetup()54 public void shouldCopyMatrixSetup() throws Exception { 55 Matrix matrix = new Matrix(); 56 matrix.setTranslate(15, 20); 57 imageView.setImageMatrix(matrix); 58 assertEquals("Bitmap for resource:drawable/an_image at (15,20)", 59 visualize(imageView)); 60 61 matrix.setTranslate(30, 40); 62 assertEquals("Bitmap for resource:drawable/an_image at (15,20)", 63 visualize(imageView)); 64 65 imageView.setImageMatrix(matrix); 66 assertEquals("Bitmap for resource:drawable/an_image at (30,40)", 67 visualize(imageView)); 68 } 69 70 @Test testSetImageResource_drawable()71 public void testSetImageResource_drawable() { 72 imageView.setImageResource(R.drawable.l0_red); 73 assertTrue("Drawable", imageView.getDrawable() instanceof Drawable); 74 assertFalse("LayerDrawable", 75 imageView.getDrawable() instanceof LayerDrawable); 76 } 77 78 @Test testSetAnimatedImage_drawable()79 public void testSetAnimatedImage_drawable() { 80 imageView.setImageResource(R.drawable.animation_list); 81 Drawable animation = imageView.getDrawable(); 82 assertTrue(animation instanceof Drawable); 83 assertTrue(animation instanceof AnimationDrawable); 84 } 85 86 @Test testSetAnimationItem()87 public void testSetAnimationItem() throws Exception { 88 imageView.setImageResource(R.drawable.animation_list); 89 AnimationDrawable animation = (AnimationDrawable) imageView.getDrawable(); 90 assertEquals(3, animation.getNumberOfFrames()); 91 } 92 93 @Test testSetImageResource_layerDrawable()94 public void testSetImageResource_layerDrawable() { 95 imageView.setImageResource(R.drawable.rainbow); 96 assertTrue("Drawable", imageView.getDrawable() instanceof Drawable); 97 assertTrue("LayerDrawable", 98 imageView.getDrawable() instanceof LayerDrawable); 99 assertThat(shadowOf(imageView.getDrawable()).getLoadedFromResourceId(), is(R.drawable.rainbow)); 100 } 101 102 @Test testSetImageLevel()103 public void testSetImageLevel() throws Exception { 104 imageView.setImageLevel(2); 105 assertThat(shadowOf(imageView).getImageLevel(), equalTo(2)); 106 } 107 } 108