1 package com.xtremelabs.robolectric.shadows; 2 3 import android.graphics.Bitmap; 4 import com.xtremelabs.robolectric.Robolectric; 5 import com.xtremelabs.robolectric.internal.Implementation; 6 import com.xtremelabs.robolectric.internal.Implements; 7 import com.xtremelabs.robolectric.internal.RealObject; 8 9 import java.io.IOException; 10 import java.io.OutputStream; 11 12 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 13 14 @SuppressWarnings({"UnusedDeclaration"}) 15 @Implements(Bitmap.class) 16 public class ShadowBitmap { 17 @RealObject private Bitmap realBitmap; 18 19 private int width; 20 private int height; 21 private Bitmap.Config config; 22 private boolean mutable; 23 private String description = ""; 24 private int loadedFromResourceId = -1; 25 private boolean recycled = false; 26 27 @Implementation compress(Bitmap.CompressFormat format, int quality, OutputStream stream)28 public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) { 29 try { 30 stream.write((description + " compressed as " + format + " with quality " + quality).getBytes()); 31 } catch (IOException e) { 32 throw new RuntimeException(e); 33 } 34 35 return true; 36 } 37 38 @Implementation createBitmap(int width, int height, Bitmap.Config config)39 public static Bitmap createBitmap(int width, int height, Bitmap.Config config) { 40 Bitmap scaledBitmap = Robolectric.newInstanceOf(Bitmap.class); 41 ShadowBitmap shadowBitmap = shadowOf(scaledBitmap); 42 shadowBitmap.appendDescription("Bitmap (" + width + " x " + height + ")"); 43 shadowBitmap.setWidth(width); 44 shadowBitmap.setHeight(height); 45 shadowBitmap.setConfig(config); 46 return scaledBitmap; 47 } 48 49 @Implementation createBitmap(Bitmap bitmap)50 public static Bitmap createBitmap(Bitmap bitmap) { 51 ShadowBitmap shadowBitmap = shadowOf(bitmap); 52 shadowBitmap.appendDescription(" created from Bitmap object"); 53 return bitmap; 54 } 55 56 @Implementation createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)57 public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) { 58 Bitmap scaledBitmap = Robolectric.newInstanceOf(Bitmap.class); 59 ShadowBitmap shadowBitmap = shadowOf(scaledBitmap); 60 shadowBitmap.appendDescription(shadowOf(src).getDescription()); 61 shadowBitmap.appendDescription(" scaled to " + dstWidth + " x " + dstHeight); 62 if (filter) { 63 shadowBitmap.appendDescription(" with filter " + filter); 64 } 65 shadowBitmap.setWidth(dstWidth); 66 shadowBitmap.setHeight(dstHeight); 67 return scaledBitmap; 68 } 69 70 @Implementation recycle()71 public void recycle() { 72 recycled = true; 73 } 74 75 @Implementation isRecycled()76 public final boolean isRecycled() { 77 return recycled; 78 } 79 80 @Implementation copy(Bitmap.Config config, boolean isMutable)81 public Bitmap copy(Bitmap.Config config, boolean isMutable) { 82 ShadowBitmap shadowBitmap = shadowOf(realBitmap); 83 shadowBitmap.setConfig(config); 84 shadowBitmap.setMutable(isMutable); 85 return realBitmap; 86 } 87 88 @Implementation getConfig()89 public final Bitmap.Config getConfig() { 90 return config; 91 } 92 setConfig(Bitmap.Config config)93 public void setConfig(Bitmap.Config config) { 94 this.config = config; 95 } 96 97 @Implementation isMutable()98 public final boolean isMutable() { 99 return mutable; 100 } 101 setMutable(boolean mutable)102 public void setMutable(boolean mutable) { 103 this.mutable = mutable; 104 } 105 appendDescription(String s)106 public void appendDescription(String s) { 107 description += s; 108 } 109 setDescription(String s)110 public void setDescription(String s) { 111 description = s; 112 } 113 getDescription()114 public String getDescription() { 115 return description; 116 } 117 create(String name)118 public static Bitmap create(String name) { 119 Bitmap bitmap = Robolectric.newInstanceOf(Bitmap.class); 120 shadowOf(bitmap).appendDescription(name); 121 return bitmap; 122 } 123 setLoadedFromResourceId(int loadedFromResourceId)124 public void setLoadedFromResourceId(int loadedFromResourceId) { 125 this.loadedFromResourceId = loadedFromResourceId; 126 } 127 getLoadedFromResourceId()128 public int getLoadedFromResourceId() { 129 if (loadedFromResourceId == -1) { 130 throw new IllegalStateException("not loaded from a resource"); 131 } 132 return loadedFromResourceId; 133 } 134 setWidth(int width)135 public void setWidth(int width) { 136 this.width = width; 137 } 138 139 @Implementation getWidth()140 public int getWidth() { 141 return width; 142 } 143 setHeight(int height)144 public void setHeight(int height) { 145 this.height = height; 146 } 147 148 @Implementation getHeight()149 public int getHeight() { 150 return height; 151 } 152 153 @Override @Implementation equals(Object o)154 public boolean equals(Object o) { 155 if (this == o) return true; 156 if (o == null || getClass() != ShadowBitmap.class) return false; 157 158 ShadowBitmap that = shadowOf((Bitmap) o); 159 160 if (height != that.height) return false; 161 if (width != that.width) return false; 162 if (description != null ? !description.equals(that.description) : that.description != null) return false; 163 164 return true; 165 } 166 167 @Override @Implementation hashCode()168 public int hashCode() { 169 int result = width; 170 result = 31 * result + height; 171 result = 31 * result + (description != null ? description.hashCode() : 0); 172 return result; 173 } 174 175 @Override @Implementation toString()176 public String toString() { 177 return "ShadowBitmap{" + 178 "description='" + description + '\'' + 179 ", width=" + width + 180 ", height=" + height + 181 '}'; 182 } 183 getRealBitmap()184 public Bitmap getRealBitmap() { 185 return realBitmap; 186 } 187 188 189 } 190