1package com.xtremelabs.robolectric.shadows;
2
3import android.graphics.BitmapFactory;
4import android.graphics.Canvas;
5import android.graphics.ColorFilter;
6import android.graphics.Rect;
7import android.graphics.drawable.Drawable;
8
9import com.xtremelabs.robolectric.WithTestDefaultsRunner;
10
11import org.junit.Test;
12import org.junit.runner.RunWith;
13
14import java.io.ByteArrayInputStream;
15import java.io.InputStream;
16
17import static com.xtremelabs.robolectric.Robolectric.shadowOf;
18import static junit.framework.Assert.assertFalse;
19import static org.hamcrest.CoreMatchers.equalTo;
20import static org.hamcrest.CoreMatchers.is;
21import static org.junit.Assert.*;
22
23@RunWith(WithTestDefaultsRunner.class)
24public class DrawableTest {
25    @Test
26    public void createFromStream__shouldReturnNullWhenAskedToCreateADrawableFromACorruptedSourceStream() throws Exception {
27        String corruptedStreamSource = "http://foo.com/image.jpg";
28        ShadowDrawable.addCorruptStreamSource(corruptedStreamSource);
29        assertNull(ShadowDrawable.createFromStream(new ByteArrayInputStream(new byte[0]), corruptedStreamSource));
30    }
31
32    @Test
33    public void createFromStream__shouldReturnDrawableWithSpecificSource() throws Exception {
34        Drawable drawable = ShadowDrawable.createFromStream(new ByteArrayInputStream(new byte[0]), "my_source");
35        assertNotNull(drawable);
36        assertEquals("my_source", ((ShadowBitmapDrawable) shadowOf(drawable)).getSource());
37    }
38
39    @Test
40    public void reset__shouldClearStaticState() throws Exception {
41        String src = "source1";
42        ShadowDrawable.addCorruptStreamSource(src);
43        assertTrue(ShadowDrawable.corruptStreamSources.contains(src));
44        ShadowDrawable.reset();
45        assertFalse(ShadowDrawable.corruptStreamSources.contains(src));
46    }
47
48    @Test
49    public void testCreateFromStream_shouldSetTheInputStreamOnTheReturnedDrawable() throws Exception {
50        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(new byte[0]);
51        Drawable drawable = Drawable.createFromStream(byteInputStream, "src name");
52        assertThat(shadowOf(drawable).getInputStream(), equalTo((InputStream) byteInputStream));
53    }
54
55    @Test
56    public void copyBoundsWithPassedRect() {
57        Drawable drawable = ShadowDrawable.createFromStream(new ByteArrayInputStream(new byte[0]), "my_source");
58        drawable.setBounds(1, 2, 3, 4);
59        Rect r = new Rect();
60        drawable.copyBounds(r);
61        assertThat(r.left, is(1));
62        assertThat(r.top, is(2));
63        assertThat(r.right, is(3));
64        assertThat(r.bottom, is(4));
65    }
66
67    @Test
68    public void copyBoundsToReturnedRect() {
69        Drawable drawable = ShadowDrawable.createFromStream(new ByteArrayInputStream(new byte[0]), "my_source");
70        drawable.setBounds(1, 2, 3, 4);
71        Rect r = drawable.copyBounds();
72        assertThat(r.left, is(1));
73        assertThat(r.top, is(2));
74        assertThat(r.right, is(3));
75        assertThat(r.bottom, is(4));
76    }
77
78    @Test
79    public void createFromPath__shouldReturnDrawableWithSpecificPath() throws Exception {
80        Drawable drawable = ShadowDrawable.createFromPath("/foo");
81        assertNotNull(drawable);
82        assertEquals("/foo", ((ShadowBitmapDrawable) shadowOf(drawable)).getPath());
83    }
84
85    @Test
86    public void testGetLoadedFromResourceId_shouldDefaultToNegativeOne() throws Exception {
87        Drawable drawable = new TestDrawable();
88        assertThat(shadowOf(drawable).getLoadedFromResourceId(), is(-1));
89    }
90
91    @Test
92    public void testSetLoadedFromResourceId() throws Exception {
93        Drawable drawable = new TestDrawable();
94        ShadowDrawable shadowDrawable = shadowOf(drawable);
95        shadowDrawable.setLoadedFromResourceId(99);
96        assertThat(shadowDrawable.getLoadedFromResourceId(), is(99));
97    }
98
99    @Test
100    public void testCreateFromResourceId_shouldSetTheId() throws Exception {
101        Drawable drawable = ShadowDrawable.createFromResourceId(34758);
102        ShadowDrawable shadowDrawable = shadowOf(drawable);
103        assertThat(shadowDrawable.getLoadedFromResourceId(), is(34758));
104    }
105
106    @Test
107    public void testWasSelfInvalidated() throws Exception {
108        Drawable drawable = ShadowDrawable.createFromResourceId(34758);
109        ShadowDrawable shadowDrawable = shadowOf(drawable);
110        assertFalse(shadowDrawable.wasInvalidated());
111        drawable.invalidateSelf();
112        assertTrue(shadowDrawable.wasInvalidated());
113    }
114
115    @Test
116    public void createFromResourceStream__shouldReturnNullWhenAskedToCreateADrawableFromACorruptedSourceStream() throws Exception {
117        String corruptedStreamSource = "http://foo.com/image.jpg";
118        ShadowDrawable.addCorruptStreamSource(corruptedStreamSource);
119        assertNull(ShadowDrawable.createFromResourceStream(null, null, new ByteArrayInputStream(new byte[0]), corruptedStreamSource));
120    }
121
122    @Test
123    public void createFromResourceStream__shouldReturnDrawableWithSpecificSource() throws Exception {
124        Drawable drawable = ShadowDrawable.createFromResourceStream(null, null, new ByteArrayInputStream(new byte[0]), "my_source");
125        assertNotNull(drawable);
126        assertEquals("my_source", ((ShadowBitmapDrawable) shadowOf(drawable)).getSource());
127    }
128
129    @Test
130    public void testCreateFromResourceStream_shouldSetTheInputStreamOnTheReturnedDrawable() throws Exception {
131        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(new byte[0]);
132        Drawable drawable = Drawable.createFromResourceStream(null, null, byteInputStream, "src name");
133        assertThat(shadowOf(drawable).getInputStream(), equalTo((InputStream) byteInputStream));
134    }
135
136    @Test
137    public void createFromResourceStreamWithOptions__shouldReturnNullWhenAskedToCreateADrawableFromACorruptedSourceStream() throws Exception {
138        String corruptedStreamSource = "http://foo.com/image.jpg";
139        ShadowDrawable.addCorruptStreamSource(corruptedStreamSource);
140        assertNull(ShadowDrawable.createFromResourceStream(null, null, new ByteArrayInputStream(new byte[0]), corruptedStreamSource, new BitmapFactory.Options()));
141    }
142
143    @Test
144    public void createFromResourceStreamWithOptions__shouldReturnDrawableWithSpecificSource() throws Exception {
145        Drawable drawable = ShadowDrawable.createFromResourceStream(null, null, new ByteArrayInputStream(new byte[0]), "my_source", new BitmapFactory.Options());
146        assertNotNull(drawable);
147        assertEquals("my_source", ((ShadowBitmapDrawable) shadowOf(drawable)).getSource());
148    }
149
150    @Test
151    public void testCreateFromResourceStreamWithOptions_shouldSetTheInputStreamOnTheReturnedDrawable() throws Exception {
152        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(new byte[0]);
153        Drawable drawable = Drawable.createFromResourceStream(null, null, byteInputStream, "src name", new BitmapFactory.Options());
154        assertThat(shadowOf(drawable).getInputStream(), equalTo((InputStream) byteInputStream));
155    }
156
157    private static class TestDrawable extends Drawable {
158        @Override
159        public void draw(Canvas canvas) {
160        }
161
162        @Override
163        public void setAlpha(int alpha) {
164        }
165
166        @Override
167        public void setColorFilter(ColorFilter cf) {
168        }
169
170        @Override
171        public int getOpacity() {
172            return 0;
173        }
174    }
175}
176