1package com.xtremelabs.robolectric.shadows; 2 3import android.graphics.Bitmap; 4import android.graphics.BitmapFactory; 5import android.net.Uri; 6import android.provider.MediaStore; 7import com.xtremelabs.robolectric.R; 8import com.xtremelabs.robolectric.Robolectric; 9import com.xtremelabs.robolectric.WithTestDefaultsRunner; 10import org.junit.Test; 11import org.junit.runner.RunWith; 12 13import java.io.InputStream; 14 15import static com.xtremelabs.robolectric.Robolectric.shadowOf; 16import static org.junit.Assert.assertEquals; 17 18@RunWith(WithTestDefaultsRunner.class) 19public class BitmapFactoryTest { 20 @Test 21 public void decodeResource_shouldSetDescription() throws Exception { 22 Bitmap bitmap = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.an_image); 23 assertEquals("Bitmap for resource:drawable/an_image", shadowOf(bitmap).getDescription()); 24 assertEquals(100, bitmap.getWidth()); 25 assertEquals(100, bitmap.getHeight()); 26 } 27 28 @Test 29 public void decodeResourceWithOpts_shouldSetDescription() throws Exception { 30 BitmapFactory.Options opts = new BitmapFactory.Options(); 31 Bitmap bitmap = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.an_image, opts); 32 assertEquals("Bitmap for resource:drawable/an_image", shadowOf(bitmap).getDescription()); 33 assertEquals(100, bitmap.getWidth()); 34 assertEquals(100, bitmap.getHeight()); 35 assertEquals(100, opts.outWidth); 36 assertEquals(100, opts.outHeight); 37 } 38 39 @Test 40 public void decodeFile_shouldSetDescription() throws Exception { 41 Bitmap bitmap = BitmapFactory.decodeFile("/some/file.jpg"); 42 assertEquals("Bitmap for file:/some/file.jpg", shadowOf(bitmap).getDescription()); 43 assertEquals(100, bitmap.getWidth()); 44 assertEquals(100, bitmap.getHeight()); 45 } 46 47 @Test 48 public void decodeStream_shouldSetDescription() throws Exception { 49 InputStream inputStream = Robolectric.application.getContentResolver().openInputStream(Uri.parse("content:/path")); 50 Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 51 assertEquals("Bitmap for content:/path", shadowOf(bitmap).getDescription()); 52 assertEquals(100, bitmap.getWidth()); 53 assertEquals(100, bitmap.getHeight()); 54 } 55 56 @Test 57 public void decodeResource_shouldGetWidthAndHeightFromHints() throws Exception { 58 ShadowBitmapFactory.provideWidthAndHeightHints(R.drawable.an_image, 123, 456); 59 60 Bitmap bitmap = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.an_image); 61 assertEquals("Bitmap for resource:drawable/an_image", shadowOf(bitmap).getDescription()); 62 assertEquals(123, bitmap.getWidth()); 63 assertEquals(456, bitmap.getHeight()); 64 } 65 66 @Test 67 public void decodeResourceWithOpts_shouldGetWidthAndHeightFromHints() throws Exception { 68 ShadowBitmapFactory.provideWidthAndHeightHints(R.drawable.an_image, 123, 456); 69 70 BitmapFactory.Options opts = new BitmapFactory.Options(); 71 Bitmap bitmap = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.an_image, opts); 72 assertEquals("Bitmap for resource:drawable/an_image", shadowOf(bitmap).getDescription()); 73 assertEquals(123, bitmap.getWidth()); 74 assertEquals(456, bitmap.getHeight()); 75 assertEquals(123, opts.outWidth); 76 assertEquals(456, opts.outHeight); 77 } 78 79 @Test 80 public void decodeResource_canTakeOptions() throws Exception { 81 BitmapFactory.Options options = new BitmapFactory.Options(); 82 options.inSampleSize = 100; 83 Bitmap bitmap = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.an_image, options); 84 assertEquals(true, shadowOf(bitmap).getDescription().contains("inSampleSize=100")); 85 } 86 87 @Test 88 public void decodeFile_shouldGetWidthAndHeightFromHints() throws Exception { 89 ShadowBitmapFactory.provideWidthAndHeightHints("/some/file.jpg", 123, 456); 90 91 Bitmap bitmap = BitmapFactory.decodeFile("/some/file.jpg"); 92 assertEquals("Bitmap for file:/some/file.jpg", shadowOf(bitmap).getDescription()); 93 assertEquals(123, bitmap.getWidth()); 94 assertEquals(456, bitmap.getHeight()); 95 } 96 97 @Test 98 public void decodeFileEtc_shouldSetOptionsOutWidthAndOutHeightFromHints() throws Exception { 99 ShadowBitmapFactory.provideWidthAndHeightHints("/some/file.jpg", 123, 456); 100 101 BitmapFactory.Options options = new BitmapFactory.Options(); 102 BitmapFactory.decodeFile("/some/file.jpg", options); 103 assertEquals(123, options.outWidth); 104 assertEquals(456, options.outHeight); 105 } 106 107 @Test 108 public void decodeUri_shouldGetWidthAndHeightFromHints() throws Exception { 109 ShadowBitmapFactory.provideWidthAndHeightHints(Uri.parse("content:/path"), 123, 456); 110 111 Bitmap bitmap = MediaStore.Images.Media.getBitmap(Robolectric.application.getContentResolver(), Uri.parse("content:/path")); 112 assertEquals("Bitmap for content:/path", shadowOf(bitmap).getDescription()); 113 assertEquals(123, bitmap.getWidth()); 114 assertEquals(456, bitmap.getHeight()); 115 } 116 117 @Test 118 public void decodeStream_shouldGetWidthAndHeightFromHints() throws Exception { 119 ShadowBitmapFactory.provideWidthAndHeightHints(Uri.parse("content:/path"), 123, 456); 120 121 InputStream inputStream = Robolectric.application.getContentResolver().openInputStream(Uri.parse("content:/path")); 122 Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 123 assertEquals("Bitmap for content:/path", shadowOf(bitmap).getDescription()); 124 assertEquals(123, bitmap.getWidth()); 125 assertEquals(456, bitmap.getHeight()); 126 } 127} 128