1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.graphics.Bitmap;
4 import android.graphics.BitmapFactory;
5 import android.net.Uri;
6 import android.provider.MediaStore;
7 import com.xtremelabs.robolectric.R;
8 import com.xtremelabs.robolectric.Robolectric;
9 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 
13 import java.io.InputStream;
14 
15 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
16 import static org.junit.Assert.assertEquals;
17 
18 @RunWith(WithTestDefaultsRunner.class)
19 public class BitmapFactoryTest {
20     @Test
decodeResource_shouldSetDescription()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
decodeResourceWithOpts_shouldSetDescription()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
decodeFile_shouldSetDescription()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
decodeStream_shouldSetDescription()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
decodeResource_shouldGetWidthAndHeightFromHints()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
decodeResourceWithOpts_shouldGetWidthAndHeightFromHints()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
decodeResource_canTakeOptions()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
decodeFile_shouldGetWidthAndHeightFromHints()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
decodeFileEtc_shouldSetOptionsOutWidthAndOutHeightFromHints()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
decodeUri_shouldGetWidthAndHeightFromHints()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
decodeStream_shouldGetWidthAndHeightFromHints()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