/** * Returns a textual representation of the appearance of the object. * * @param view the view to visualize * @return Textual representation of the appearance of the object. */ public static String visualize(View view) { Canvas canvas = new Canvas(); view.draw(canvas); ShadowCanvas shadowCanvas = Shadow.extract(canvas); return shadowCanvas.getDescription(); }
private void separateLines() { if (getDescription().length() != 0) { appendDescription("\n"); } }
/** * Returns a textual representation of the appearance of the object. * * @param canvas the canvas to visualize * @return The textual representation of the appearance of the object. */ public static String visualize(Canvas canvas) { ShadowCanvas shadowCanvas = Shadow.extract(canvas); return shadowCanvas.getDescription(); }
/** * Tries to extract the description of a drawn drawable from a canvas */ static String getDrawnDrawableDescription(final Drawable drawable) { final Canvas canvas = new Canvas(); drawable.draw(canvas); final ShadowCanvas shadowCanvas = Shadows.shadowOf(canvas); return shadowCanvas.getDescription(); }
/** * Tries to extract the description of a drawn view from a canvas * * Since Robolectric can screw up {@link View#draw}, this uses reflection to call * {@link View#onDraw} and give you a canvas that has all the information drawn into it. * This is useful for asserting some view draws something specific to a canvas. * * @param view the view to draw */ @TargetApi(Build.VERSION_CODES.KITKAT) private static String getDrawnViewDescription(View view) { final Canvas canvas = new Canvas(); view.draw(canvas); final ShadowCanvas shadowCanvas = Shadows.shadowOf(canvas); if (!shadowCanvas.getDescription().isEmpty()) { return shadowCanvas.getDescription(); } try { final Method onDraw = view.getClass().getMethod("onDraw", Canvas.class); onDraw.setAccessible(true); onDraw.invoke(view, canvas); final ShadowCanvas shadowCanvas2 = Shadows.shadowOf(canvas); return shadowCanvas2.getDescription(); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }
@Test public void shouldDescribeBitmapDrawing_withDestinationRect() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(imageBitmap, new Rect(1,2,3,4), new Rect(5,6,7,8), new Paint()); assertEquals("Bitmap for file:/an/image.jpg at (5,6) with height=2 and width=2 taken from Rect(1, 2 - 3, 4)", shadowOf(canvas).getDescription()); }
@Test public void shouldDescribeBitmapDrawing_withDestinationRectF() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(imageBitmap, new Rect(1,2,3,4), new RectF(5.0f,6.0f,7.5f,8.5f), new Paint()); assertEquals("Bitmap for file:/an/image.jpg at (5.0,6.0) with height=2.5 and width=2.5 taken from Rect(1, 2 - 3, 4)", shadowOf(canvas).getDescription()); }
@Test public void drawColor_shouldReturnDescription() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawColor(Color.WHITE); canvas.drawColor(Color.GREEN); canvas.drawColor(Color.TRANSPARENT); assertEquals("draw color -1draw color -16711936draw color 0", shadowOf(canvas).getDescription()); }
@Test public void shouldDescribeBitmapDrawing_WithMatrix() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); assertEquals("Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]\n" + "Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]", shadowOf(canvas).getDescription()); assertEquals("Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]\n" + "Bitmap for file:/an/image.jpg transformed by Matrix[pre=[], set={}, post=[]]", shadowOf(targetBitmap).getDescription()); }
@Test public void draw_shouldCopyDescriptionToCanvas() throws Exception { BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image); Canvas canvas = new Canvas(); drawable.draw(canvas); assertThat(shadowOf(canvas).getDescription()).isEqualTo("Bitmap for resource:org.robolectric:drawable/an_image"); }
@Test public void resetCanvasHistory_shouldClearTheHistoryAndDescription() throws Exception { Canvas canvas = new Canvas(); canvas.drawPath(new Path(), new Paint()); canvas.drawText("hi", 1, 2, new Paint()); ShadowCanvas shadow = shadowOf(canvas); shadow.resetCanvasHistory(); assertThat(shadow.getPathPaintHistoryCount()).isEqualTo(0); assertThat(shadow.getTextHistoryCount()).isEqualTo(0); assertEquals("", shadow.getDescription()); }
@Test public void shouldDescribeBitmapDrawing() throws Exception { Canvas canvas = new Canvas(targetBitmap); canvas.drawBitmap(imageBitmap, 1, 2, new Paint()); canvas.drawBitmap(imageBitmap, 100, 200, new Paint()); assertEquals("Bitmap for file:/an/image.jpg at (1,2)\n" + "Bitmap for file:/an/image.jpg at (100,200)", shadowOf(canvas).getDescription()); assertEquals("Bitmap for file:/an/image.jpg at (1,2)\n" + "Bitmap for file:/an/image.jpg at (100,200)", shadowOf(targetBitmap).getDescription()); }
@Test public void drawPath_shouldAppendDescriptionToBitmap() throws Exception { Canvas canvas = new Canvas(targetBitmap); Path path1 = new Path(); path1.lineTo(10, 10); path1.moveTo(20, 15); Path path2 = new Path(); path2.moveTo(100, 100); path2.lineTo(150, 140); Paint paint = new Paint(); canvas.drawPath(path1, paint); canvas.drawPath(path2, paint); assertEquals("Path " + shadowOf(path1).getPoints().toString() + "\n" + "Path " + shadowOf(path2).getPoints().toString(), shadowOf(canvas).getDescription()); assertEquals("Path " + shadowOf(path1).getPoints().toString() + "\n" + "Path " + shadowOf(path2).getPoints().toString(), shadowOf(targetBitmap).getDescription()); }
@Test public void withColorFilterSet_draw_shouldCopyDescriptionToCanvas() throws Exception { BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image); drawable.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix())); Canvas canvas = new Canvas(); drawable.draw(canvas); assertThat(shadowOf(canvas).getDescription()).isEqualTo("Bitmap for resource:org.robolectric:drawable/an_image with ColorMatrixColorFilter<1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0>"); }
/** * Returns a textual representation of the appearance of the object. * * @param canvas the canvas to visualize * @return The textual representation of the appearance of the object. */ public static String visualize(Canvas canvas) { return shadowOf(canvas).getDescription(); }
/** * Returns a textual representation of the appearance of the object. * * @param canvas the canvas to visualize * @return The textual representation of the appearance of the object. */ public static String visualize(Canvas canvas) { return shadowOf(canvas).getDescription(); }
private void separateLines() { if (getDescription().length() != 0) { appendDescription("\n"); } }
/** * Returns a textual representation of the appearance of the object. * * @param canvas the canvas to visualize * @return The textual representation of the appearance of the object. */ public static String visualize(Canvas canvas) { return shadowOf(canvas).getDescription(); }
/** * Returns a textual representation of the appearance of the object. * * @param view the view to visualize * @return Textual representation of the appearance of the object. */ public static String visualize(View view) { Canvas canvas = new Canvas(); view.draw(canvas); return shadowOf(canvas).getDescription(); }
/** * Returns a textual representation of the appearance of the object. * * @param canvas the canvas to visualize * @return The textual representation of the appearance of the object. */ public static String visualize(Canvas canvas) { ShadowCanvas shadowCanvas = Shadow.extract(canvas); return shadowCanvas.getDescription(); }