/** * Returns a textual representation of the appearance of the object. * * @param bitmap the bitmap to visualize * @return Textual representation of the appearance of the object. */ public static String visualize(Bitmap bitmap) { ShadowBitmap shadowBitmap = Shadow.extract(bitmap); return shadowBitmap.getDescription(); }
public String getDescription() { ShadowBitmap shadowBitmap = Shadow.extract(targetBitmap); return shadowBitmap.getDescription(); }
static void assertBitmapsEqual(Bitmap a, Bitmap b) { if (a.getHeight() != b.getHeight()) { fail(); } if (a.getWidth() != b.getWidth()) { fail(); } ShadowBitmap shadowA = shadowOf(a); ShadowBitmap shadowB = shadowOf(b); if (!Objects.equals(shadowA.getDescription(), shadowB.getDescription())) { fail(); } } }
@Test public void getBitmap_shouldReturnBitmapUsedToDraw() throws Exception { BitmapDrawable drawable = (BitmapDrawable) resources.getDrawable(R.drawable.an_image); assertThat(shadowOf(drawable.getBitmap()).getDescription()).isEqualTo("Bitmap for resource:org.robolectric:drawable/an_image"); }
@Test public void decodeStream_shouldGetWidthAndHeightFromActualImage() { InputStream inputStream = getClass().getClassLoader().getResourceAsStream("res/drawable/fourth_image.jpg"); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); assertEquals("Bitmap", shadowOf(bitmap).getDescription()); assertEquals(160, bitmap.getWidth()); assertEquals(107, bitmap.getHeight()); }
@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 shouldCreateScaledBitmap() throws Exception { Bitmap originalBitmap = create("Original bitmap"); Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, 100, 200, false); assertThat(shadowOf(scaledBitmap).getDescription()) .isEqualTo("Original bitmap scaled to 100 x 200"); assertThat(scaledBitmap.getWidth()).isEqualTo(100); assertThat(scaledBitmap.getHeight()).isEqualTo(200); scaledBitmap.getPixels(new int[20000], 0, 0, 0, 0, 100, 200); }
@Test public void decodeResource_canTakeOptions() { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 100; Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.an_image, options); assertEquals(true, shadowOf(bitmap).getDescription().contains("inSampleSize=100")); }
@Test public void decodeByteArray_shouldIncludeOffsets() { String data = "arbitrary bytes"; ShadowBitmapFactory.provideWidthAndHeightHints(Uri.parse(data), 123, 456); byte[] bytes = data.getBytes(UTF_8); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 1, bytes.length - 2); assertEquals("Bitmap for " + data + " bytes 1..13", shadowOf(bitmap).getDescription()); }
@Test public void testRotateImage() { Bitmap toRotate = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888); Bitmap zero = TransformationUtils.rotateImage(toRotate, 0); assertTrue(toRotate == zero); Bitmap ninety = TransformationUtils.rotateImage(toRotate, 90); assertThat(Shadows.shadowOf(ninety).getDescription()).contains("rotate=90.0"); assertEquals(toRotate.getWidth(), toRotate.getHeight()); }
@Test public void decodeStream_shouldSetDescriptionWithNullOptions() throws Exception { InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse("content:/path")); Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, null); assertEquals("Bitmap for content:/path", shadowOf(bitmap).getDescription()); assertEquals(100, bitmap.getWidth()); assertEquals(100, bitmap.getHeight()); }
@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 decodeByteArray_shouldGetWidthAndHeightFromHints() { String data = "arbitrary bytes"; ShadowBitmapFactory.provideWidthAndHeightHints(Uri.parse(data), 123, 456); byte[] bytes = data.getBytes(UTF_8); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); assertEquals("Bitmap for " + data, shadowOf(bitmap).getDescription()); assertEquals(123, bitmap.getWidth()); assertEquals(456, bitmap.getHeight()); }
@Test public void shouldCreateBitmapFromAnotherBitmap() { Bitmap originalBitmap = create("Original bitmap"); Bitmap newBitmap = Bitmap.createBitmap(originalBitmap); assertThat(shadowOf(newBitmap).getDescription()) .isEqualTo("Original bitmap created from Bitmap object"); }
@Test public void decodeResourceStream_canTakeOptions() throws Exception { BitmapFactory.Options options = new BitmapFactory.Options(); InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse("content:/path")); options.inSampleSize = 100; Bitmap bitmap = BitmapFactory.decodeResourceStream( context.getResources(), null, inputStream, null, options); assertEquals(true, shadowOf(bitmap).getDescription().contains("inSampleSize=100")); }
@Test public void decodeFile_shouldGetWidthAndHeightFromHints() { ShadowBitmapFactory.provideWidthAndHeightHints("/some/file.jpg", 123, 456); Bitmap bitmap = BitmapFactory.decodeFile("/some/file.jpg"); assertEquals("Bitmap for file:/some/file.jpg", shadowOf(bitmap).getDescription()); assertEquals(123, bitmap.getWidth()); assertEquals(456, bitmap.getHeight()); }
@Test public void decodeResource_shouldGetWidthAndHeightFromHints() { ShadowBitmapFactory.provideWidthAndHeightHints(R.drawable.an_image, 123, 456); Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.an_image); assertEquals("Bitmap for resource:org.robolectric:drawable/an_image", shadowOf(bitmap).getDescription()); assertEquals(123, bitmap.getWidth()); assertEquals(456, bitmap.getHeight()); }
@Test public void decodeUri_shouldGetWidthAndHeightFromHints() throws Exception { ShadowBitmapFactory.provideWidthAndHeightHints(Uri.parse("content:/path"), 123, 456); Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse("content:/path")); assertEquals("Bitmap for content:/path", shadowOf(bitmap).getDescription()); assertEquals(123, bitmap.getWidth()); assertEquals(456, bitmap.getHeight()); }
@Test public void decodeFile_shouldSetDescriptionAndCreatedFrom() { Bitmap bitmap = BitmapFactory.decodeFile("/some/file.jpg"); ShadowBitmap shadowBitmap = shadowOf(bitmap); assertEquals("Bitmap for file:/some/file.jpg", shadowBitmap.getDescription()); assertEquals("/some/file.jpg", shadowBitmap.getCreatedFromPath()); assertEquals(100, bitmap.getWidth()); assertEquals(100, bitmap.getHeight()); }
@Test public void decodeResource_shouldSetDescriptionAndCreatedFrom() { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.an_image); ShadowBitmap shadowBitmap = shadowOf(bitmap); assertEquals("Bitmap for resource:org.robolectric:drawable/an_image", shadowBitmap.getDescription()); assertEquals(R.drawable.an_image, shadowBitmap.getCreatedFromResId()); assertEquals(64, bitmap.getWidth()); assertEquals(53, bitmap.getHeight()); }