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