1 package com.xtremelabs.robolectric.shadows;
2 
3 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
4 
5 import android.content.res.Resources;
6 import android.graphics.Bitmap;
7 import android.graphics.Canvas;
8 import android.graphics.ColorFilter;
9 import android.graphics.Paint;
10 import android.graphics.Shader.TileMode;
11 import android.graphics.drawable.BitmapDrawable;
12 
13 import com.xtremelabs.robolectric.internal.Implementation;
14 import com.xtremelabs.robolectric.internal.Implements;
15 import com.xtremelabs.robolectric.internal.RealObject;
16 
17 @SuppressWarnings({"UnusedDeclaration"})
18 @Implements(BitmapDrawable.class)
19 public class ShadowBitmapDrawable extends ShadowDrawable {
20     private Bitmap bitmap;
21     private ColorFilter colorFilter;
22     private String drawableCreateFromStreamSource;
23     private String drawableCreateFromPath;
24 
25     @RealObject private BitmapDrawable realBitmapDrawable;
26     private TileMode tileModeX;
27     private TileMode tileModeY;
28 
__constructor__(Bitmap bitmap)29     public void __constructor__(Bitmap bitmap) {
30         this.bitmap = bitmap;
31     }
32 
__constructor__(Resources resources, Bitmap bitmap)33     public void __constructor__(Resources resources, Bitmap bitmap) {
34         this.bitmap = bitmap;
35     }
36 
37     /**
38      * Draws the contained bitmap onto the canvas at 0,0 with a default {@code Paint}
39      *
40      * @param canvas the canvas to draw on
41      */
42     @Implementation
draw(Canvas canvas)43     public void draw(Canvas canvas) {
44         Paint paint = new Paint();
45         paint.setColorFilter(colorFilter);
46         canvas.drawBitmap(realBitmapDrawable.getBitmap(), 0, 0, paint);
47     }
48 
49     @Implementation
setColorFilter(android.graphics.ColorFilter colorFilter)50     public void setColorFilter(android.graphics.ColorFilter colorFilter) {
51         this.colorFilter = colorFilter;
52     }
53 
54     @Implementation
getBitmap()55     public android.graphics.Bitmap getBitmap() {
56         return bitmap;
57     }
58 
59     /**
60      * Non-Android accessor that tells you the resource id that this {@code BitmapDrawable} was loaded from. This lets
61      * your tests assert that the bitmap is correct without having to actually load the bitmap.
62      *
63      * @return resource id from which this {@code BitmapDrawable} was loaded
64      * @deprecated use com.xtremelabs.robolectric.shadows.ShadowBitmap#getLoadedFromResourceId() instead.
65      */
66     @Deprecated
67     @Override
getLoadedFromResourceId()68     public int getLoadedFromResourceId() {
69         return shadowOf(bitmap).getLoadedFromResourceId();
70     }
71 
72     // Used by ShadowDrawable.createFromStream()
setSource(String drawableCreateFromStreamSource)73     public void setSource(String drawableCreateFromStreamSource) {
74         this.drawableCreateFromStreamSource = drawableCreateFromStreamSource;
75     }
76 
getSource()77     public String getSource() {
78         return drawableCreateFromStreamSource;
79     }
80 
81     //Used by ShadowDrawable.createFromPath()
setPath(String drawableCreateFromPath)82     public void setPath(String drawableCreateFromPath) {
83         this.drawableCreateFromPath = drawableCreateFromPath;
84     }
85 
getPath()86     public String getPath() {
87         return drawableCreateFromPath;
88     }
89 
90     @Implementation
setTileModeX(TileMode mode)91     public void setTileModeX(TileMode mode) {
92         tileModeX = mode;
93     }
94 
95     @Implementation
getTileModeX()96     public TileMode getTileModeX() {
97         return tileModeX;
98     }
99 
100     @Implementation
setTileModeY(TileMode mode)101     public void setTileModeY(TileMode mode) {
102         tileModeY = mode;
103     }
104 
105     @Implementation
getTileModeY()106     public TileMode getTileModeY() {
107         return tileModeY;
108     }
109 
110     @Implementation
setTileModeXY(TileMode modeX, TileMode modeY)111     public void setTileModeXY(TileMode modeX, TileMode modeY) {
112         setTileModeX(modeX);
113         setTileModeY(modeY);
114     }
115 
116     @Override
117     @Implementation
equals(Object o)118     public boolean equals(Object o) {
119         if (this == o) return true;
120         if (o == null || getClass() != ShadowBitmapDrawable.class) return false;
121 
122         ShadowBitmapDrawable that = shadowOf((BitmapDrawable) o);
123 
124         if (bitmap != null ? !bitmap.equals(that.bitmap) : that.bitmap != null) return false;
125 
126         return super.equals(o);
127     }
128 
129     @Override
130     @Implementation
hashCode()131     public int hashCode() {
132         return bitmap != null ? bitmap.hashCode() : 0;
133     }
134 
135     @Override
136     @Implementation
toString()137     public String toString() {
138         return "ShadowBitmapDrawable{" +
139                 "bitmap=" + bitmap +
140                 '}';
141     }
142 }
143