/src/test/java/com/xtremelabs/robolectric/shadows/BitmapFactoryTest.java

http://github.com/pivotal/robolectric · Java · 163 lines · 135 code · 28 blank · 0 comment · 0 complexity · 04e84dd849271a30fc19af65f28bd721 MD5 · raw file

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