1 package com.xtremelabs.robolectric.shadows; 2 3 import android.graphics.*; 4 import com.xtremelabs.robolectric.Robolectric; 5 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 6 import org.junit.Before; 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 10 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 11 import static org.hamcrest.CoreMatchers.equalTo; 12 import static org.junit.Assert.assertEquals; 13 import static org.junit.Assert.assertThat; 14 15 @RunWith(WithTestDefaultsRunner.class) 16 public class CanvasTest { 17 private Bitmap targetBitmap; 18 private Bitmap imageBitmap; 19 20 @Before 21 public void setUp() throws Exception { 22 targetBitmap = Robolectric.newInstanceOf(Bitmap.class); 23 imageBitmap = BitmapFactory.decodeFile("/an/image.jpg"); 24 } 25 26 @Test 27 public void shouldDescribeBitmapDrawing() throws Exception { 28 Canvas canvas = new Canvas(targetBitmap); 29 canvas.drawBitmap(imageBitmap, 1, 2, new Paint()); 30 canvas.drawBitmap(imageBitmap, 100, 200, new Paint()); 31 32 assertEquals("Bitmap for file:/an/image.jpg at (1,2)\n" + 33 "Bitmap for file:/an/image.jpg at (100,200)", shadowOf(canvas).getDescription()); 34 35 assertEquals("Bitmap for file:/an/image.jpg at (1,2)\n" + 36 "Bitmap for file:/an/image.jpg at (100,200)", shadowOf(targetBitmap).getDescription()); 37 } 38 39 @Test 40 public void shouldDescribeBitmapDrawing_WithMatrix() throws Exception { 41 Canvas canvas = new Canvas(targetBitmap); 42 canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); 43 canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); 44 45 assertEquals("Bitmap for file:/an/image.jpg transformed by matrix\n" + 46 "Bitmap for file:/an/image.jpg transformed by matrix", shadowOf(canvas).getDescription()); 47 48 assertEquals("Bitmap for file:/an/image.jpg transformed by matrix\n" + 49 "Bitmap for file:/an/image.jpg transformed by matrix", shadowOf(targetBitmap).getDescription()); 50 } 51 52 @Test 53 public void visualize_shouldReturnDescription() throws Exception { 54 Canvas canvas = new Canvas(targetBitmap); 55 canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); 56 canvas.drawBitmap(imageBitmap, new Matrix(), new Paint()); 57 58 assertEquals("Bitmap for file:/an/image.jpg transformed by matrix\n" + 59 "Bitmap for file:/an/image.jpg transformed by matrix", Robolectric.visualize(canvas)); 60 61 } 62 63 @Test 64 public void drawColor_shouldReturnDescription() throws Exception { 65 Canvas canvas = new Canvas(targetBitmap); 66 canvas.drawColor(Color.WHITE); 67 canvas.drawColor(Color.GREEN); 68 canvas.drawColor(Color.TRANSPARENT); 69 assertEquals("draw color -1draw color -16711936draw color 0", 70 shadowOf(canvas).getDescription()); 71 } 72 73 @Test 74 public void drawPath_shouldRecordThePathAndThePaint() throws Exception { 75 Canvas canvas = new Canvas(targetBitmap); 76 Path path = new Path(); 77 path.lineTo(10, 10); 78 79 Paint paint = new Paint(); 80 paint.setAlpha(7); 81 canvas.drawPath(path, paint); 82 83 ShadowCanvas shadow = shadowOf(canvas); 84 assertThat(shadow.getPathPaintHistoryCount(), equalTo(1)); 85 assertThat(shadow.getDrawnPath(0), equalTo(path)); 86 assertThat(shadow.getDrawnPathPaint(0), equalTo(paint)); 87 } 88 89 @Test 90 public void drawPath_shouldAppendDescriptionToBitmap() throws Exception { 91 Canvas canvas = new Canvas(targetBitmap); 92 Path path1 = new Path(); 93 path1.lineTo(10, 10); 94 path1.moveTo(20, 15); 95 Path path2 = new Path(); 96 path2.moveTo(100, 100); 97 path2.lineTo(150, 140); 98 99 Paint paint = new Paint(); 100 canvas.drawPath(path1, paint); 101 canvas.drawPath(path2, paint); 102 103 assertEquals("Path " + shadowOf(path1).getPoints().toString() + "\n" 104 + "Path " + shadowOf(path2).getPoints().toString(), shadowOf(canvas).getDescription()); 105 106 assertEquals("Path " + shadowOf(path1).getPoints().toString() + "\n" 107 + "Path " + shadowOf(path2).getPoints().toString(), shadowOf(targetBitmap).getDescription()); 108 } 109 } 110