133873d2b41a5cd2597b20a4e88eb8942944c0f23Tyler Schultzpackage com.xtremelabs.robolectric.shadows;
2a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
3a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richardimport android.content.res.Resources;
4e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williamsimport android.graphics.Canvas;
5e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williamsimport android.graphics.ColorMatrix;
6e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williamsimport android.graphics.ColorMatrixColorFilter;
71ce4df3adb4f8ca5ae910430ee281eb920c98030Phil Goodwin & Ryan Richardimport android.graphics.Shader;
8b01474a3329222965d31068f5c33e59c6e6aa6a4Josh Lauer & Christian Williamsimport android.graphics.drawable.BitmapDrawable;
9a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richardimport android.graphics.drawable.Drawable;
10a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richardimport com.xtremelabs.robolectric.R;
11e7a41c9b39233781c97dd599ff61024f0510e308Christian Williamsimport com.xtremelabs.robolectric.Robolectric;
1207257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwinimport com.xtremelabs.robolectric.WithTestDefaultsRunner;
13a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richardimport org.junit.Before;
14a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richardimport org.junit.Test;
15a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richardimport org.junit.runner.RunWith;
16a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
1797ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richardimport java.io.ByteArrayInputStream;
1897ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richardimport java.io.InputStream;
1997ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richard
20b01474a3329222965d31068f5c33e59c6e6aa6a4Josh Lauer & Christian Williamsimport static com.xtremelabs.robolectric.Robolectric.shadowOf;
21a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richardimport static org.junit.Assert.assertEquals;
22f58c6545761402d3ad215566a49b0add7f6192d3Christian Williams & Phil Goodwinimport static org.junit.Assert.assertFalse;
23a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
2407257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwin@RunWith(WithTestDefaultsRunner.class)
25a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richardpublic class BitmapDrawableTest {
26a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    private Resources resources;
27a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
28a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    @Before
29a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    public void setUp() throws Exception {
30c321c406b82547d87398c0922410f31e90a77993Christian Williams        resources = Robolectric.application.getResources();
31a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    }
32a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
33a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    @Test
34b01474a3329222965d31068f5c33e59c6e6aa6a4Josh Lauer & Christian Williams    public void getBitmap_shouldReturnBitmapUsedToDraw() throws Exception {
35b01474a3329222965d31068f5c33e59c6e6aa6a4Josh Lauer & Christian Williams        BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
3622ae0c422c4f1bb6878f624fbf2531eea4da0187Christian Williams & Phil Goodwin        assertEquals("Bitmap for resource:drawable/an_image", shadowOf(drawable.getBitmap()).getDescription());
37b01474a3329222965d31068f5c33e59c6e6aa6a4Josh Lauer & Christian Williams    }
38e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams
39e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams    @Test
40e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams    public void draw_shouldCopyDescriptionToCanvas() throws Exception {
41e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams        BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
42e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams        Canvas canvas = new Canvas();
43e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams        drawable.draw(canvas);
44e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams
4522ae0c422c4f1bb6878f624fbf2531eea4da0187Christian Williams & Phil Goodwin        assertEquals("Bitmap for resource:drawable/an_image", shadowOf(canvas).getDescription());
46e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams    }
4740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
48b01474a3329222965d31068f5c33e59c6e6aa6a4Josh Lauer & Christian Williams    @Test
4997ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richard    public void shouldInheritSourceStringFromDrawableDotCreateFromStream() throws Exception {
5097ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richard        InputStream emptyInputStream = new ByteArrayInputStream("".getBytes());
5197ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richard        BitmapDrawable drawable = (BitmapDrawable) Drawable.createFromStream(emptyInputStream, "source string value");
5297ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richard        assertEquals("source string value", shadowOf(drawable).getSource());
5397ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richard    }
5497ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richard
5597ffcf00dfda4a2c5c67264df1ce9ee64c925823Ryan Richard    @Test
56e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams    public void withColorFilterSet_draw_shouldCopyDescriptionToCanvas() throws Exception {
57e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams        BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
58e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams        drawable.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix()));
59e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams        Canvas canvas = new Canvas();
60e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams        drawable.draw(canvas);
61e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams
6222ae0c422c4f1bb6878f624fbf2531eea4da0187Christian Williams & Phil Goodwin        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>",
63e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams                shadowOf(canvas).getDescription());
64e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams    }
65e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams
66e3bf0c0e84dd3464de0a508f9c03512d23c11bd8Josh Lauer & Christian Williams    @Test
67a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    public void equals_shouldTestResourceId() throws Exception {
68a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        Drawable drawable1a = resources.getDrawable(R.drawable.an_image);
69a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        Drawable drawable1b = resources.getDrawable(R.drawable.an_image);
70a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        Drawable drawable2 = resources.getDrawable(R.drawable.an_other_image);
71a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
72a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        assertEquals(drawable1a, drawable1b);
73f58c6545761402d3ad215566a49b0add7f6192d3Christian Williams & Phil Goodwin        assertFalse(drawable1a.equals(drawable2));
74a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    }
75a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
76a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    @Test
77a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    public void equals_shouldTestBounds() throws Exception {
78a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        Drawable drawable1a = resources.getDrawable(R.drawable.an_image);
79a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        Drawable drawable1b = resources.getDrawable(R.drawable.an_image);
80a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
81a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        drawable1a.setBounds(1, 2, 3, 4);
82a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        drawable1b.setBounds(1, 2, 3, 4);
83a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
84a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        assertEquals(drawable1a, drawable1b);
85a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
86a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        drawable1b.setBounds(1, 2, 3, 5);
87f58c6545761402d3ad215566a49b0add7f6192d3Christian Williams & Phil Goodwin        assertFalse(drawable1a.equals(drawable1b));
88a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    }
89a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard
90a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    @Test
91cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williams    public void shouldStillHaveShadow() throws Exception {
92a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard        Drawable drawable = resources.getDrawable(R.drawable.an_image);
93cc54a84f7c9f42028529e545e50b043e83f53c1cChristian Williams & Phil Goodwin        assertEquals(R.drawable.an_image, ((ShadowBitmapDrawable) Robolectric.shadowOf(drawable)).getLoadedFromResourceId());
94a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard    }
951ce4df3adb4f8ca5ae910430ee281eb920c98030Phil Goodwin & Ryan Richard
961ce4df3adb4f8ca5ae910430ee281eb920c98030Phil Goodwin & Ryan Richard    @Test
971ce4df3adb4f8ca5ae910430ee281eb920c98030Phil Goodwin & Ryan Richard    public void shouldSetTileModeXY() throws Exception {
981ce4df3adb4f8ca5ae910430ee281eb920c98030Phil Goodwin & Ryan Richard        BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image);
991ce4df3adb4f8ca5ae910430ee281eb920c98030Phil Goodwin & Ryan Richard        drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
1001ce4df3adb4f8ca5ae910430ee281eb920c98030Phil Goodwin & Ryan Richard        assertEquals(Shader.TileMode.REPEAT, drawable.getTileModeX());
1011ce4df3adb4f8ca5ae910430ee281eb920c98030Phil Goodwin & Ryan Richard        assertEquals(Shader.TileMode.MIRROR, drawable.getTileModeY());
1021ce4df3adb4f8ca5ae910430ee281eb920c98030Phil Goodwin & Ryan Richard    }
103a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard}
104