1 package org.robolectric.shadows; 2 3 import static org.robolectric.util.reflector.Reflector.reflector; 4 5 import android.graphics.Bitmap; 6 import android.graphics.Matrix; 7 import com.google.common.base.Preconditions; 8 import java.io.InputStream; 9 import org.robolectric.annotation.Implementation; 10 import org.robolectric.annotation.Implements; 11 import org.robolectric.annotation.RealObject; 12 import org.robolectric.shadow.api.Shadow; 13 import org.robolectric.shadows.ShadowBitmap.Picker; 14 import org.robolectric.util.reflector.Accessor; 15 import org.robolectric.util.reflector.Direct; 16 import org.robolectric.util.reflector.ForType; 17 import org.robolectric.versioning.AndroidVersions.U; 18 19 /** Base class for {@link Bitmap} shadows. */ 20 @Implements(value = Bitmap.class, shadowPicker = Picker.class, looseSignatures = true) 21 public abstract class ShadowBitmap { 22 23 @RealObject protected Bitmap realBitmap; 24 25 /** 26 * Returns a textual representation of the appearance of the object. 27 * 28 * @param bitmap the bitmap to visualize 29 * @return Textual representation of the appearance of the object. 30 */ visualize(Bitmap bitmap)31 public static String visualize(Bitmap bitmap) { 32 ShadowBitmap shadowBitmap = Shadow.extract(bitmap); 33 return shadowBitmap.getDescription(); 34 } 35 36 /** 37 * Reference to original Bitmap from which this Bitmap was created. {@code null} if this Bitmap 38 * was not copied from another instance. 39 * 40 * @return Original Bitmap from which this Bitmap was created. 41 */ getCreatedFromBitmap()42 public abstract Bitmap getCreatedFromBitmap(); 43 44 /** 45 * Resource ID from which this Bitmap was created. {@code 0} if this Bitmap was not created from a 46 * resource. 47 * 48 * @return Resource ID from which this Bitmap was created. 49 */ getCreatedFromResId()50 public abstract int getCreatedFromResId(); 51 52 /** 53 * Path from which this Bitmap was created. {@code null} if this Bitmap was not create from a 54 * path. 55 * 56 * @return Path from which this Bitmap was created. 57 */ getCreatedFromPath()58 public abstract String getCreatedFromPath(); 59 60 /** 61 * {@link InputStream} from which this Bitmap was created. {@code null} if this Bitmap was not 62 * created from a stream. 63 * 64 * @return InputStream from which this Bitmap was created. 65 */ getCreatedFromStream()66 public abstract InputStream getCreatedFromStream(); 67 68 /** 69 * Bytes from which this Bitmap was created. {@code null} if this Bitmap was not created from 70 * bytes. 71 * 72 * @return Bytes from which this Bitmap was created. 73 */ getCreatedFromBytes()74 public abstract byte[] getCreatedFromBytes(); 75 76 /** 77 * Horizontal offset within {@link #getCreatedFromBitmap()} of this Bitmap's content, or -1. 78 * 79 * @return Horizontal offset within {@link #getCreatedFromBitmap()}. 80 */ getCreatedFromX()81 public abstract int getCreatedFromX(); 82 83 /** 84 * Vertical offset within {@link #getCreatedFromBitmap()} of this Bitmap's content, or -1. 85 * 86 * @return Vertical offset within {@link #getCreatedFromBitmap()} of this Bitmap's content, or -1. 87 */ getCreatedFromY()88 public abstract int getCreatedFromY(); 89 90 /** 91 * Width from {@link #getCreatedFromX()} within {@link #getCreatedFromBitmap()} of this Bitmap's 92 * content, or -1. 93 * 94 * @return Width from {@link #getCreatedFromX()} within {@link #getCreatedFromBitmap()} of this 95 * Bitmap's content, or -1. 96 */ getCreatedFromWidth()97 public abstract int getCreatedFromWidth(); 98 99 /** 100 * Height from {@link #getCreatedFromX()} within {@link #getCreatedFromBitmap()} of this Bitmap's 101 * content, or -1. 102 * 103 * @return Height from {@link #getCreatedFromX()} within {@link #getCreatedFromBitmap()} of this 104 * Bitmap's content, or -1. 105 */ getCreatedFromHeight()106 public abstract int getCreatedFromHeight(); 107 108 /** 109 * Color array from which this Bitmap was created. {@code null} if this Bitmap was not created 110 * from a color array. 111 * 112 * @return Color array from which this Bitmap was created. 113 */ getCreatedFromColors()114 public abstract int[] getCreatedFromColors(); 115 116 /** 117 * Matrix from which this Bitmap's content was transformed, or {@code null}. 118 * 119 * @return Matrix from which this Bitmap's content was transformed, or {@code null}. 120 */ getCreatedFromMatrix()121 public abstract Matrix getCreatedFromMatrix(); 122 123 /** 124 * {@code true} if this Bitmap was created with filtering. 125 * 126 * @return {@code true} if this Bitmap was created with filtering. 127 */ getCreatedFromFilter()128 public abstract boolean getCreatedFromFilter(); 129 setMutable(boolean mutable)130 public abstract void setMutable(boolean mutable); 131 appendDescription(String s)132 public abstract void appendDescription(String s); 133 getDescription()134 public abstract String getDescription(); 135 setDescription(String s)136 public abstract void setDescription(String s); 137 138 @Implementation(minSdk = U.SDK_INT) setGainmap(Object gainmap)139 protected void setGainmap(Object gainmap) { 140 Preconditions.checkState(!realBitmap.isRecycled(), "Bitmap is recycled"); 141 reflector(BitmapReflector.class, realBitmap).setGainmap(gainmap); 142 } 143 144 @Implementation(minSdk = U.SDK_INT) hasGainmap()145 protected boolean hasGainmap() { 146 Preconditions.checkState(!realBitmap.isRecycled(), "Bitmap is recycled"); 147 return reflector(BitmapReflector.class, realBitmap).getGainmap() != null; 148 } 149 150 /** Reflector for {@link Bitmap}. */ 151 @ForType(Bitmap.class) 152 protected interface BitmapReflector { checkRecycled(String errorMessage)153 void checkRecycled(String errorMessage); 154 155 @Accessor("mNativePtr") getNativePtr()156 long getNativePtr(); 157 158 @Accessor("mGainmap") setGainmap(Object gainmap)159 void setGainmap(Object gainmap); 160 161 @Direct getGainmap()162 Object getGainmap(); 163 } 164 165 /** Shadow picker for {@link Bitmap}. */ 166 public static final class Picker extends GraphicsShadowPicker<Object> { Picker()167 public Picker() { 168 super(ShadowLegacyBitmap.class, ShadowNativeBitmap.class); 169 } 170 } 171 } 172