BitmapDrawableTest.java revision 4ac725f9b4cebbf46805fc5e9b2f0eaf3fdd9b29
1package com.xtremelabs.robolectric.shadows;
2
3import android.app.Application;
4import android.content.res.Resources;
5import android.graphics.Canvas;
6import android.graphics.ColorMatrix;
7import android.graphics.ColorMatrixColorFilter;
8import android.graphics.drawable.BitmapDrawable;
9import android.graphics.drawable.Drawable;
10import com.xtremelabs.robolectric.R;
11import com.xtremelabs.robolectric.Robolectric;
12import com.xtremelabs.robolectric.WithTestDefaultsRunner;
13import org.junit.Before;
14import org.junit.Test;
15import org.junit.runner.RunWith;
16
17import static android.test.MoreAsserts.assertNotEqual;
18import static com.xtremelabs.robolectric.Robolectric.shadowOf;
19import static org.junit.Assert.assertEquals;
20
21@RunWith(WithTestDefaultsRunner.class)
22public class BitmapDrawableTest {
23    private Resources resources;
24
25    @Before
26    public void setUp() throws Exception {
27        Robolectric.bindDefaultShadowClasses();
28
29        Application application = new Application();
30        resources = application.getResources();
31    }
32
33    @Test
34    public void getBitmap_shouldReturnBitmapUsedToDraw() throws Exception {
35        BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
36        assertEquals("Bitmap for resource:drawable/an_image", shadowOf(drawable.getBitmap()).getDescription());
37    }
38
39    @Test
40    public void draw_shouldCopyDescriptionToCanvas() throws Exception {
41        BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
42        Canvas canvas = new Canvas();
43        drawable.draw(canvas);
44
45        assertEquals("Bitmap for resource:drawable/an_image", shadowOf(canvas).getDescription());
46    }
47
48    @Test
49    public void withColorFilterSet_draw_shouldCopyDescriptionToCanvas() throws Exception {
50        BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
51        drawable.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix()));
52        Canvas canvas = new Canvas();
53        drawable.draw(canvas);
54
55        assertEquals("Bitmap for resource: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>",
56                shadowOf(canvas).getDescription());
57    }
58
59    @Test
60    public void equals_shouldTestResourceId() throws Exception {
61        Drawable drawable1a = resources.getDrawable(R.drawable.an_image);
62        Drawable drawable1b = resources.getDrawable(R.drawable.an_image);
63        Drawable drawable2 = resources.getDrawable(R.drawable.an_other_image);
64
65        assertEquals(drawable1a, drawable1b);
66        assertNotEqual(drawable1a, drawable2);
67    }
68
69    @Test
70    public void equals_shouldTestBounds() throws Exception {
71        Drawable drawable1a = resources.getDrawable(R.drawable.an_image);
72        Drawable drawable1b = resources.getDrawable(R.drawable.an_image);
73
74        drawable1a.setBounds(1, 2, 3, 4);
75        drawable1b.setBounds(1, 2, 3, 4);
76
77        assertEquals(drawable1a, drawable1b);
78
79        drawable1b.setBounds(1, 2, 3, 5);
80        assertNotEqual(drawable1a, drawable1b);
81    }
82
83    @Test
84    public void shouldStillHaveShadow() throws Exception {
85        Drawable drawable = resources.getDrawable(R.drawable.an_image);
86        assertEquals(R.drawable.an_image, ((ShadowBitmapDrawable) Robolectric.shadowOf(drawable)).getLoadedFromResourceId());
87    }
88}
89