aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBitmapDrawable.java
blob: de499e9e0a8c89afe41d7d7c44696a15d28db631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package org.robolectric.shadows;

import static org.robolectric.util.reflector.Reflector.reflector;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(BitmapDrawable.class)
public class ShadowBitmapDrawable extends ShadowDrawable {
  String drawableCreateFromStreamSource;
  String drawableCreateFromPath;

  @RealObject private BitmapDrawable realBitmapDrawable;

  /**
   * Draws the contained bitmap onto the canvas at 0,0 with a default {@code Paint}
   *
   * @param canvas the canvas to draw on
   */
  @Implementation
  protected void draw(Canvas canvas) {
    if (ShadowView.useRealGraphics()) {
      reflector(BitmapDrawableReflector.class, realBitmapDrawable).draw(canvas);
    } else {
      Bitmap bitmap = realBitmapDrawable.getBitmap();
      if (bitmap == null) {
        return;
      }
      canvas.drawBitmap(bitmap, 0, 0, realBitmapDrawable.getPaint());
    }
  }

  @Override
  protected void setCreatedFromResId(int createdFromResId, String resourceName) {
    super.setCreatedFromResId(createdFromResId, resourceName);
    Bitmap bitmap = realBitmapDrawable.getBitmap();
    if (bitmap != null && Shadow.extract(bitmap) instanceof ShadowBitmap) {
      ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
      if (shadowBitmap.createdFromResId == -1) {
        shadowBitmap.setCreatedFromResId(createdFromResId, resourceName);
      }
    }
  }

  /**
   * Returns the resource id that this {@code BitmapDrawable} was loaded from. This lets
   * your tests assert that the bitmap is correct without having to actually load the bitmap.
   *
   * @return resource id from which this {@code BitmapDrawable} was loaded
   * @deprecated use ShadowBitmap#getCreatedFromResId() instead.
   */
  @Deprecated
  @Override
  public int getCreatedFromResId() {
    ShadowBitmap shadowBitmap = Shadow.extract(realBitmapDrawable.getBitmap());
    return shadowBitmap.getCreatedFromResId();
  }

  public String getSource() {
    return drawableCreateFromStreamSource;
  }

  public String getPath() {
    return drawableCreateFromPath;
  }

  @ForType(BitmapDrawable.class)
  interface BitmapDrawableReflector {
    @Direct
    void draw(Canvas canvas);
  }
}