xref: /aosp_15_r20/external/robolectric/shadows/framework/src/main/java/org/robolectric/shadows/ShadowCanvas.java (revision e6ba16074e6af37d123cb567d575f496bf0a58ee)
1 package org.robolectric.shadows;
2 
3 import android.graphics.Canvas;
4 import android.graphics.Paint;
5 import android.graphics.Path;
6 import android.graphics.RectF;
7 import org.robolectric.annotation.Implements;
8 import org.robolectric.shadow.api.Shadow;
9 import org.robolectric.shadows.ShadowCanvas.Picker;
10 
11 /** Base class for {@link Canvas} shadow classes. Mainly contains public shadow API signatures. */
12 @Implements(value = Canvas.class, shadowPicker = Picker.class)
13 public abstract class ShadowCanvas {
14 
visualize(Canvas canvas)15   public static String visualize(Canvas canvas) {
16     if (Shadow.extract(canvas) instanceof ShadowLegacyCanvas) {
17       ShadowCanvas shadowCanvas = Shadow.extract(canvas);
18       return shadowCanvas.getDescription();
19     } else {
20       throw new UnsupportedOperationException(
21           "ShadowCanvas.visualize is only supported in legacy Canvas");
22     }
23   }
24 
appendDescription(String s)25   public abstract void appendDescription(String s);
26 
getDescription()27   public abstract String getDescription();
28 
getPathPaintHistoryCount()29   public abstract int getPathPaintHistoryCount();
30 
getCirclePaintHistoryCount()31   public abstract int getCirclePaintHistoryCount();
32 
getArcPaintHistoryCount()33   public abstract int getArcPaintHistoryCount();
34 
hasDrawnPath()35   public abstract boolean hasDrawnPath();
36 
hasDrawnCircle()37   public abstract boolean hasDrawnCircle();
38 
getDrawnPathPaint(int i)39   public abstract Paint getDrawnPathPaint(int i);
40 
getDrawnPath(int i)41   public abstract Path getDrawnPath(int i);
42 
getDrawnCircle(int i)43   public abstract CirclePaintHistoryEvent getDrawnCircle(int i);
44 
getDrawnArc(int i)45   public abstract ArcPaintHistoryEvent getDrawnArc(int i);
46 
resetCanvasHistory()47   public abstract void resetCanvasHistory();
48 
getDrawnPaint()49   public abstract Paint getDrawnPaint();
50 
setHeight(int height)51   public abstract void setHeight(int height);
52 
setWidth(int width)53   public abstract void setWidth(int width);
54 
getDrawnTextEvent(int i)55   public abstract TextHistoryEvent getDrawnTextEvent(int i);
56 
getTextHistoryCount()57   public abstract int getTextHistoryCount();
58 
getDrawnRect(int i)59   public abstract RectPaintHistoryEvent getDrawnRect(int i);
60 
getLastDrawnRect()61   public abstract RectPaintHistoryEvent getLastDrawnRect();
62 
getRectPaintHistoryCount()63   public abstract int getRectPaintHistoryCount();
64 
getDrawnRoundRect(int i)65   public abstract RoundRectPaintHistoryEvent getDrawnRoundRect(int i);
66 
getLastDrawnRoundRect()67   public abstract RoundRectPaintHistoryEvent getLastDrawnRoundRect();
68 
getRoundRectPaintHistoryCount()69   public abstract int getRoundRectPaintHistoryCount();
70 
getDrawnLine(int i)71   public abstract LinePaintHistoryEvent getDrawnLine(int i);
72 
getLinePaintHistoryCount()73   public abstract int getLinePaintHistoryCount();
74 
getOvalPaintHistoryCount()75   public abstract int getOvalPaintHistoryCount();
76 
getDrawnOval(int i)77   public abstract OvalPaintHistoryEvent getDrawnOval(int i);
78 
79   public static class LinePaintHistoryEvent {
80     public Paint paint;
81     public float startX;
82     public float startY;
83     public float stopX;
84     public float stopY;
85 
LinePaintHistoryEvent(float startX, float startY, float stopX, float stopY, Paint paint)86     LinePaintHistoryEvent(float startX, float startY, float stopX, float stopY, Paint paint) {
87       this.paint = new Paint(paint);
88       this.paint.setColor(paint.getColor());
89       this.paint.setStrokeWidth(paint.getStrokeWidth());
90       this.startX = startX;
91       this.startY = startY;
92       this.stopX = stopX;
93       this.stopY = stopY;
94     }
95   }
96 
97   public static class OvalPaintHistoryEvent {
98     public final RectF oval;
99     public final Paint paint;
100 
OvalPaintHistoryEvent(RectF oval, Paint paint)101     OvalPaintHistoryEvent(RectF oval, Paint paint) {
102       this.oval = new RectF(oval);
103       this.paint = new Paint(paint);
104       this.paint.setColor(paint.getColor());
105       this.paint.setStrokeWidth(paint.getStrokeWidth());
106     }
107   }
108 
109   public static class RectPaintHistoryEvent {
110     public final Paint paint;
111     public final RectF rect;
112     public final float left;
113     public final float top;
114     public final float right;
115     public final float bottom;
116 
RectPaintHistoryEvent(float left, float top, float right, float bottom, Paint paint)117     RectPaintHistoryEvent(float left, float top, float right, float bottom, Paint paint) {
118       this.rect = new RectF(left, top, right, bottom);
119       this.paint = new Paint(paint);
120       this.paint.setColor(paint.getColor());
121       this.paint.setStrokeWidth(paint.getStrokeWidth());
122       this.paint.setTextSize(paint.getTextSize());
123       this.paint.setStyle(paint.getStyle());
124       this.left = left;
125       this.top = top;
126       this.right = right;
127       this.bottom = bottom;
128     }
129   }
130 
131   /** Captures round rectangle drawing events */
132   public static class RoundRectPaintHistoryEvent {
133     public final Paint paint;
134     public final RectF rect;
135     public final float left;
136     public final float top;
137     public final float right;
138     public final float bottom;
139     public final float rx;
140     public final float ry;
141 
RoundRectPaintHistoryEvent( float left, float top, float right, float bottom, float rx, float ry, Paint paint)142     RoundRectPaintHistoryEvent(
143         float left, float top, float right, float bottom, float rx, float ry, Paint paint) {
144       this.rect = new RectF(left, top, right, bottom);
145       this.paint = new Paint(paint);
146       this.paint.setColor(paint.getColor());
147       this.paint.setStrokeWidth(paint.getStrokeWidth());
148       this.paint.setTextSize(paint.getTextSize());
149       this.paint.setStyle(paint.getStyle());
150       this.left = left;
151       this.top = top;
152       this.right = right;
153       this.bottom = bottom;
154       this.rx = rx;
155       this.ry = ry;
156     }
157   }
158 
159   public static class CirclePaintHistoryEvent {
160     public final float centerX;
161     public final float centerY;
162     public final float radius;
163     public final Paint paint;
164 
CirclePaintHistoryEvent(float centerX, float centerY, float radius, Paint paint)165     CirclePaintHistoryEvent(float centerX, float centerY, float radius, Paint paint) {
166       this.centerX = centerX;
167       this.centerY = centerY;
168       this.radius = radius;
169       this.paint = paint;
170     }
171   }
172 
173   public static class ArcPaintHistoryEvent {
174     public final RectF oval;
175     public final float startAngle;
176     public final float sweepAngle;
177     public final boolean useCenter;
178     public final Paint paint;
179 
ArcPaintHistoryEvent( RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)180     public ArcPaintHistoryEvent(
181         RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint) {
182       this.oval = oval;
183       this.startAngle = startAngle;
184       this.sweepAngle = sweepAngle;
185       this.useCenter = useCenter;
186       this.paint = paint;
187     }
188   }
189 
190   public static class TextHistoryEvent {
191     public final float x;
192     public final float y;
193     public final Paint paint;
194     public final String text;
195 
TextHistoryEvent(float x, float y, Paint paint, String text)196     TextHistoryEvent(float x, float y, Paint paint, String text) {
197       this.x = x;
198       this.y = y;
199       this.paint = paint;
200       this.text = text;
201     }
202   }
203 
204   /** Shadow picker for {@link Canvas}. */
205   public static final class Picker extends GraphicsShadowPicker<Object> {
Picker()206     public Picker() {
207       super(ShadowLegacyCanvas.class, ShadowNativeCanvas.class);
208     }
209   }
210 }
211