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
|
package org.robolectric.shadows;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
import static org.junit.Assert.assertNotNull;
import static org.robolectric.Shadows.shadowOf;
import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Build;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.R;
import org.robolectric.annotation.Config;
@RunWith(AndroidJUnit4.class)
public class ShadowDrawableTest {
private Application context;
@Before
public void setUp() throws Exception {
context = ApplicationProvider.getApplicationContext();
}
@Test
public void createFromResourceStream_shouldWorkWithoutSourceName() {
Drawable drawable =
Drawable.createFromResourceStream(
context.getResources(), null, createImageStream(), null, new BitmapFactory.Options());
assertNotNull(drawable);
}
@Test
public void copyBoundsWithPassedRect() {
Drawable drawable = Drawable.createFromStream(createImageStream(), "my_source");
drawable.setBounds(1, 2, 3, 4);
Rect r = new Rect();
drawable.copyBounds(r);
assertThat(r.left).isEqualTo(1);
assertThat(r.top).isEqualTo(2);
assertThat(r.right).isEqualTo(3);
assertThat(r.bottom).isEqualTo(4);
}
@Test
public void copyBoundsToReturnedRect() {
Drawable drawable = Drawable.createFromStream(createImageStream(), "my_source");
drawable.setBounds(1, 2, 3, 4);
Rect r = drawable.copyBounds();
assertThat(r.left).isEqualTo(1);
assertThat(r.top).isEqualTo(2);
assertThat(r.right).isEqualTo(3);
assertThat(r.bottom).isEqualTo(4);
}
@Test
public void testGetLoadedFromResourceId_shouldDefaultToNegativeOne() {
Drawable drawable = new TestDrawable();
assertThat(shadowOf(drawable).getCreatedFromResId()).isEqualTo(-1);
}
@Test
public void testCreateFromResourceId_shouldSetTheId() {
Drawable drawable = ShadowDrawable.createFromResourceId(34758);
ShadowDrawable shadowDrawable = shadowOf(drawable);
assertThat(shadowDrawable.getCreatedFromResId()).isEqualTo(34758);
}
@Test
public void testWasSelfInvalidated() {
Drawable drawable = ShadowDrawable.createFromResourceId(34758);
ShadowDrawable shadowDrawable = shadowOf(drawable);
assertThat(shadowDrawable.wasInvalidated()).isFalse();
drawable.invalidateSelf();
assertThat(shadowDrawable.wasInvalidated()).isTrue();
}
@Test
public void shouldLoadNinePatchFromDrawableXml() {
assertThat(context.getResources().getDrawable(R.drawable.drawable_with_nine_patch)).isNotNull();
}
@Test public void settingBoundsShouldInvokeCallback() {
TestDrawable drawable = new TestDrawable();
assertThat(drawable.boundsChanged).isFalse();
drawable.setBounds(0, 0, 10, 10);
assertThat(drawable.boundsChanged).isTrue();
}
@Test
public void drawableIntrinsicWidthAndHeightShouldBeCorrect() {
final Drawable anImage = context.getResources().getDrawable(R.drawable.an_image);
assertThat(anImage.getIntrinsicHeight()).isEqualTo(53);
assertThat(anImage.getIntrinsicWidth()).isEqualTo(64);
}
@Test
@Config(qualifiers = "mdpi")
public void drawableShouldLoadImageOfCorrectSizeWithMdpiQualifier() {
final Drawable anImage = context.getResources().getDrawable(R.drawable.robolectric);
assertThat(anImage.getIntrinsicHeight()).isEqualTo(167);
assertThat(anImage.getIntrinsicWidth()).isEqualTo(198);
}
@Test
@Config(qualifiers = "hdpi")
public void drawableShouldLoadImageOfCorrectSizeWithHdpiQualifier() {
if (Build.VERSION.SDK_INT >= 28) {
// getDrawable depends on ImageDecoder, which depends on binary resources
assume().that(ShadowAssetManager.useLegacy()).isFalse();
}
final Drawable anImage = context.getResources().getDrawable(R.drawable.robolectric);
assertThat(anImage.getIntrinsicHeight()).isEqualTo(251);
assertThat(anImage.getIntrinsicWidth()).isEqualTo(297);
}
@Test
public void testGetBitmapOrVectorDrawableAt21() {
final Drawable aDrawable = context.getResources().getDrawable(R.drawable.an_image_or_vector);
assertThat(aDrawable).isInstanceOf(VectorDrawable.class);
}
private static class TestDrawable extends Drawable {
public boolean boundsChanged;
@Override
public void draw(Canvas canvas) {
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return 0;
}
@Override protected void onBoundsChange(Rect bounds) {
boundsChanged = true;
super.onBoundsChange(bounds);
}
}
private static ByteArrayInputStream createImageStream() {
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, outputStream);
return new ByteArrayInputStream(outputStream.toByteArray());
}
}
|