1package com.xtremelabs.robolectric.shadows; 2 3import android.graphics.Bitmap; 4import com.xtremelabs.robolectric.Robolectric; 5import com.xtremelabs.robolectric.internal.Implementation; 6import com.xtremelabs.robolectric.internal.Implements; 7import com.xtremelabs.robolectric.internal.RealObject; 8 9import java.io.IOException; 10import java.io.OutputStream; 11 12import static com.xtremelabs.robolectric.Robolectric.shadowOf; 13 14@SuppressWarnings({"UnusedDeclaration"}) 15@Implements(Bitmap.class) 16public 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 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 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 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 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 71 public void recycle() { 72 recycled = true; 73 } 74 75 @Implementation 76 public final boolean isRecycled() { 77 return recycled; 78 } 79 80 @Implementation 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 89 public final Bitmap.Config getConfig() { 90 return config; 91 } 92 93 public void setConfig(Bitmap.Config config) { 94 this.config = config; 95 } 96 97 @Implementation 98 public final boolean isMutable() { 99 return mutable; 100 } 101 102 public void setMutable(boolean mutable) { 103 this.mutable = mutable; 104 } 105 106 public void appendDescription(String s) { 107 description += s; 108 } 109 110 public void setDescription(String s) { 111 description = s; 112 } 113 114 public String getDescription() { 115 return description; 116 } 117 118 public static Bitmap create(String name) { 119 Bitmap bitmap = Robolectric.newInstanceOf(Bitmap.class); 120 shadowOf(bitmap).appendDescription(name); 121 return bitmap; 122 } 123 124 public void setLoadedFromResourceId(int loadedFromResourceId) { 125 this.loadedFromResourceId = loadedFromResourceId; 126 } 127 128 public int getLoadedFromResourceId() { 129 if (loadedFromResourceId == -1) { 130 throw new IllegalStateException("not loaded from a resource"); 131 } 132 return loadedFromResourceId; 133 } 134 135 public void setWidth(int width) { 136 this.width = width; 137 } 138 139 @Implementation 140 public int getWidth() { 141 return width; 142 } 143 144 public void setHeight(int height) { 145 this.height = height; 146 } 147 148 @Implementation 149 public int getHeight() { 150 return height; 151 } 152 153 @Override @Implementation 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 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 176 public String toString() { 177 return "ShadowBitmap{" + 178 "description='" + description + '\'' + 179 ", width=" + width + 180 ", height=" + height + 181 '}'; 182 } 183 184 public Bitmap getRealBitmap() { 185 return realBitmap; 186 } 187 188 189} 190