aboutsummaryrefslogtreecommitdiff
path: root/v1/src/test/java/com/xtremelabs/robolectric/shadows/DrawableTest.java
blob: dd4940a9babf40cde1414ed197c4a3015b111df0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.xtremelabs.robolectric.shadows;

import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import com.xtremelabs.robolectric.WithTestDefaultsRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static junit.framework.Assert.assertFalse;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

@RunWith(WithTestDefaultsRunner.class)
public class DrawableTest {
    @Test
    public void createFromStream__shouldReturnNullWhenAskedToCreateADrawableFromACorruptedSourceStream() throws Exception {
        String corruptedStreamSource = "http://foo.com/image.jpg";
        ShadowDrawable.addCorruptStreamSource(corruptedStreamSource);
        assertNull(ShadowDrawable.createFromStream(new ByteArrayInputStream(new byte[0]), corruptedStreamSource));
    }

    @Test
    public void createFromStream__shouldReturnDrawableWithSpecificSource() throws Exception {
        Drawable drawable = ShadowDrawable.createFromStream(new ByteArrayInputStream(new byte[0]), "my_source");
        assertNotNull(drawable);
        assertEquals("my_source", ((ShadowBitmapDrawable) shadowOf(drawable)).getSource());
    }

    @Test
    public void reset__shouldClearStaticState() throws Exception {
        String src = "source1";
        ShadowDrawable.addCorruptStreamSource(src);
        assertTrue(ShadowDrawable.corruptStreamSources.contains(src));
        ShadowDrawable.reset();
        assertFalse(ShadowDrawable.corruptStreamSources.contains(src));
    }

    @Test
    public void testCreateFromStream_shouldSetTheInputStreamOnTheReturnedDrawable() throws Exception {
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(new byte[0]);
        Drawable drawable = Drawable.createFromStream(byteInputStream, "src name");
        assertThat(shadowOf(drawable).getInputStream(), equalTo((InputStream) byteInputStream));
    }

    @Test
    public void copyBoundsWithPassedRect() {
        Drawable drawable = ShadowDrawable.createFromStream(new ByteArrayInputStream(new byte[0]), "my_source");
        drawable.setBounds(1, 2, 3, 4);
        Rect r = new Rect();
        drawable.copyBounds(r);
        assertThat(r.left, is(1));
        assertThat(r.top, is(2));
        assertThat(r.right, is(3));
        assertThat(r.bottom, is(4));
    }

    @Test
    public void copyBoundsToReturnedRect() {
        Drawable drawable = ShadowDrawable.createFromStream(new ByteArrayInputStream(new byte[0]), "my_source");
        drawable.setBounds(1, 2, 3, 4);
        Rect r = drawable.copyBounds();
        assertThat(r.left, is(1));
        assertThat(r.top, is(2));
        assertThat(r.right, is(3));
        assertThat(r.bottom, is(4));
    }

    @Test
    public void createFromPath__shouldReturnDrawableWithSpecificPath() throws Exception {
        Drawable drawable = ShadowDrawable.createFromPath("/foo");
        assertNotNull(drawable);
        assertEquals("/foo", ((ShadowBitmapDrawable) shadowOf(drawable)).getPath());
    }

    @Test
    public void testGetLoadedFromResourceId_shouldDefaultToNegativeOne() throws Exception {
        Drawable drawable = new TestDrawable();
        assertThat(shadowOf(drawable).getLoadedFromResourceId(), is(-1));
    }

    @Test
    public void testSetLoadedFromResourceId() throws Exception {
        Drawable drawable = new TestDrawable();
        ShadowDrawable shadowDrawable = shadowOf(drawable);
        shadowDrawable.setLoadedFromResourceId(99);
        assertThat(shadowDrawable.getLoadedFromResourceId(), is(99));
    }

    @Test
    public void testCreateFromResourceId_shouldSetTheId() throws Exception {
        Drawable drawable = ShadowDrawable.createFromResourceId(34758);
        ShadowDrawable shadowDrawable = shadowOf(drawable);
        assertThat(shadowDrawable.getLoadedFromResourceId(), is(34758));
    }

    @Test
    public void testWasSelfInvalidated() throws Exception {
        Drawable drawable = ShadowDrawable.createFromResourceId(34758);
        ShadowDrawable shadowDrawable = shadowOf(drawable);
        assertFalse(shadowDrawable.wasInvalidated());
        drawable.invalidateSelf();
        assertTrue(shadowDrawable.wasInvalidated());
    }
    
    @Test
    public void createFromResourceStream__shouldReturnNullWhenAskedToCreateADrawableFromACorruptedSourceStream() throws Exception {
        String corruptedStreamSource = "http://foo.com/image.jpg";
        ShadowDrawable.addCorruptStreamSource(corruptedStreamSource);
        assertNull(ShadowDrawable.createFromResourceStream(null, null, new ByteArrayInputStream(new byte[0]), corruptedStreamSource));
    }

    @Test
    public void createFromResourceStream__shouldReturnDrawableWithSpecificSource() throws Exception {
        Drawable drawable = ShadowDrawable.createFromResourceStream(null, null, new ByteArrayInputStream(new byte[0]), "my_source");
        assertNotNull(drawable);
        assertEquals("my_source", ((ShadowBitmapDrawable) shadowOf(drawable)).getSource());
    }

    @Test
    public void testCreateFromResourceStream_shouldSetTheInputStreamOnTheReturnedDrawable() throws Exception {
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(new byte[0]);
        Drawable drawable = Drawable.createFromResourceStream(null, null, byteInputStream, "src name");
        assertThat(shadowOf(drawable).getInputStream(), equalTo((InputStream) byteInputStream));
    }
    
    @Test
    public void createFromResourceStreamWithOptions__shouldReturnNullWhenAskedToCreateADrawableFromACorruptedSourceStream() throws Exception {
        String corruptedStreamSource = "http://foo.com/image.jpg";
        ShadowDrawable.addCorruptStreamSource(corruptedStreamSource);
        assertNull(ShadowDrawable.createFromResourceStream(null, null, new ByteArrayInputStream(new byte[0]), corruptedStreamSource, new BitmapFactory.Options()));
    }

    @Test
    public void createFromResourceStreamWithOptions__shouldReturnDrawableWithSpecificSource() throws Exception {
        Drawable drawable = ShadowDrawable.createFromResourceStream(null, null, new ByteArrayInputStream(new byte[0]), "my_source", new BitmapFactory.Options());
        assertNotNull(drawable);
        assertEquals("my_source", ((ShadowBitmapDrawable) shadowOf(drawable)).getSource());
    }

    @Test
    public void testCreateFromResourceStreamWithOptions_shouldSetTheInputStreamOnTheReturnedDrawable() throws Exception {
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(new byte[0]);
        Drawable drawable = Drawable.createFromResourceStream(null, null, byteInputStream, "src name", new BitmapFactory.Options());
        assertThat(shadowOf(drawable).getInputStream(), equalTo((InputStream) byteInputStream));
    }

    private static class TestDrawable extends Drawable {
        @Override
        public void draw(Canvas canvas) {
        }

        @Override
        public void setAlpha(int alpha) {
        }

        @Override
        public void setColorFilter(ColorFilter cf) {
        }

        @Override
        public int getOpacity() {
            return 0;
        }
    }
}