ShadowBitmapFactory/ShadowBitmapDrawable: Support the non-deprecated BitmapDrawable(Resources, Bitmap) constructor and use it be default.
diff --git a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapDrawable.java b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapDrawable.java
index c7cc9e7..da4c4e1 100644
--- a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapDrawable.java
+++ b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapDrawable.java
@@ -1,17 +1,19 @@
package com.xtremelabs.robolectric.shadows;
+import static com.xtremelabs.robolectric.Robolectric.shadowOf;
+
+import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
+import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
+
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;
-import static android.graphics.Shader.TileMode;
-import static com.xtremelabs.robolectric.Robolectric.shadowOf;
-
@SuppressWarnings({"UnusedDeclaration"})
@Implements(BitmapDrawable.class)
public class ShadowBitmapDrawable extends ShadowDrawable {
@@ -28,6 +30,10 @@
this.bitmap = bitmap;
}
+ public void __constructor__(Resources resources, Bitmap bitmap) {
+ this.bitmap = bitmap;
+ }
+
/**
* Draws the contained bitmap onto the canvas at 0,0 with a default {@code Paint}
*
@@ -57,6 +63,7 @@
* @return resource id from which this {@code BitmapDrawable} was loaded
* @deprecated use com.xtremelabs.robolectric.shadows.ShadowBitmap#getLoadedFromResourceId() instead.
*/
+ @Deprecated
@Override
public int getLoadedFromResourceId() {
return shadowOf(bitmap).getLoadedFromResourceId();
@@ -99,7 +106,7 @@
public TileMode getTileModeY() {
return tileModeY;
}
-
+
@Implementation
public void setTileModeXY(TileMode modeX, TileMode modeY) {
setTileModeX(modeX);
diff --git a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapFactory.java b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapFactory.java
index 642553d..5687045 100644
--- a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapFactory.java
+++ b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapFactory.java
@@ -31,6 +31,13 @@
return bitmap;
}
+ @Implementation
+ public static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts) {
+ Bitmap bitmap = create("resource:" + getResourceName(id), opts);
+ shadowOf(bitmap).setLoadedFromResourceId(id);
+ return bitmap;
+ }
+
private static String getResourceName(int id) {
return shadowOf(Robolectric.application).getResourceLoader().getNameForId(id);
}
diff --git a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowResources.java b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowResources.java
index 82bde5a..a9323ad 100644
--- a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowResources.java
+++ b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowResources.java
@@ -123,7 +123,7 @@
public CharSequence getText(int id) throws Resources.NotFoundException {
return getString(id);
}
-
+
public void setDensity(float density) {
this.density = density;
}
@@ -166,12 +166,12 @@
if (colorDrawable != null) {
return colorDrawable;
}
-
+
if (resLoader.isNinePatchDrawable(drawableResourceId)) {
return new NinePatchDrawable(realResources, null);
}
- return new BitmapDrawable(BitmapFactory.decodeResource(realResources, drawableResourceId));
+ return new BitmapDrawable(realResources, BitmapFactory.decodeResource(realResources, drawableResourceId));
}
@Implementation
@@ -183,7 +183,7 @@
public int getInteger(int id) throws Resources.NotFoundException {
return resourceLoader.getIntegerValue( id );
}
-
+
@Implementation
public int getDimensionPixelSize(int id) throws Resources.NotFoundException {
// The int value returned from here is probably going to be handed to TextView.setTextSize(),
diff --git a/src/test/java/com/xtremelabs/robolectric/shadows/BitmapFactoryTest.java b/src/test/java/com/xtremelabs/robolectric/shadows/BitmapFactoryTest.java
index 85d3ac1..a24b6c4 100644
--- a/src/test/java/com/xtremelabs/robolectric/shadows/BitmapFactoryTest.java
+++ b/src/test/java/com/xtremelabs/robolectric/shadows/BitmapFactoryTest.java
@@ -26,6 +26,17 @@
}
@Test
+ public void decodeResourceWithOpts_shouldSetDescription() throws Exception {
+ BitmapFactory.Options opts = new BitmapFactory.Options();
+ Bitmap bitmap = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.an_image, opts);
+ assertEquals("Bitmap for resource:drawable/an_image", shadowOf(bitmap).getDescription());
+ assertEquals(100, bitmap.getWidth());
+ assertEquals(100, bitmap.getHeight());
+ assertEquals(100, opts.outWidth);
+ assertEquals(100, opts.outHeight);
+ }
+
+ @Test
public void decodeFile_shouldSetDescription() throws Exception {
Bitmap bitmap = BitmapFactory.decodeFile("/some/file.jpg");
assertEquals("Bitmap for file:/some/file.jpg", shadowOf(bitmap).getDescription());
@@ -53,6 +64,19 @@
}
@Test
+ public void decodeResourceWithOpts_shouldGetWidthAndHeightFromHints() throws Exception {
+ ShadowBitmapFactory.provideWidthAndHeightHints(R.drawable.an_image, 123, 456);
+
+ BitmapFactory.Options opts = new BitmapFactory.Options();
+ Bitmap bitmap = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.an_image, opts);
+ assertEquals("Bitmap for resource:drawable/an_image", shadowOf(bitmap).getDescription());
+ assertEquals(123, bitmap.getWidth());
+ assertEquals(456, bitmap.getHeight());
+ assertEquals(123, opts.outWidth);
+ assertEquals(456, opts.outHeight);
+ }
+
+ @Test
public void decodeFile_shouldGetWidthAndHeightFromHints() throws Exception {
ShadowBitmapFactory.provideWidthAndHeightHints("/some/file.jpg", 123, 456);