• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.graphics.Bitmap;
4 import android.graphics.Bitmap.Config;
5 import android.graphics.Canvas;
6 import android.graphics.ColorMatrix;
7 import android.graphics.ColorMatrixColorFilter;
8 import android.graphics.Matrix;
9 import android.graphics.Paint;
10 import com.xtremelabs.robolectric.Robolectric;
11 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 
15 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertTrue;
19 
20 @RunWith(WithTestDefaultsRunner.class)
21 public class BitmapTest {
22     @Test
shouldCreateScaledBitmap()23     public void shouldCreateScaledBitmap() throws Exception {
24         Bitmap originalBitmap = Robolectric.newInstanceOf(Bitmap.class);
25         shadowOf(originalBitmap).appendDescription("Original bitmap");
26         Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, 100, 200, false);
27         assertEquals("Original bitmap scaled to 100 x 200", shadowOf(scaledBitmap).getDescription());
28         assertEquals(100, scaledBitmap.getWidth());
29         assertEquals(200, scaledBitmap.getHeight());
30     }
31 
32     @Test
shouldCreateActiveBitmap()33     public void shouldCreateActiveBitmap() throws Exception {
34         Bitmap bitmap = Bitmap.createBitmap(100, 200, Config.ARGB_8888);
35         assertFalse(bitmap.isRecycled());
36     }
37 
38     @Test
shouldCreateBitmapWithCorrectConfig()39     public void shouldCreateBitmapWithCorrectConfig() throws Exception {
40         Bitmap bitmap = Bitmap.createBitmap(100, 200, Config.ARGB_8888);
41         assertEquals(bitmap.getConfig(), Config.ARGB_8888);
42     }
43 
44     @Test
shouldCreateBitmapFromAnotherBitmap()45     public void shouldCreateBitmapFromAnotherBitmap() {
46     	Bitmap originalBitmap = Robolectric.newInstanceOf(Bitmap.class);
47         shadowOf(originalBitmap).appendDescription("Original bitmap");
48         Bitmap newBitmap = Bitmap.createBitmap(originalBitmap);
49         assertEquals("Original bitmap created from Bitmap object", shadowOf(newBitmap).getDescription());
50     }
51 
52 
53     @Test
shouldRecycleBitmap()54     public void shouldRecycleBitmap() throws Exception {
55         Bitmap bitmap = Bitmap.createBitmap(100, 200, Config.ARGB_8888);
56         bitmap.recycle();
57         assertTrue( bitmap.isRecycled() );
58     }
59 
60     @Test
equals_shouldCompareDescriptions()61     public void equals_shouldCompareDescriptions() throws Exception {
62         assertFalse(ShadowBitmap.create("bitmap A").equals(ShadowBitmap.create("bitmap B")));
63 
64         assertTrue(ShadowBitmap.create("bitmap A").equals(ShadowBitmap.create("bitmap A")));
65     }
66 
67     @Test
equals_shouldCompareWidthAndHeight()68     public void equals_shouldCompareWidthAndHeight() throws Exception {
69         Bitmap bitmapA1 = ShadowBitmap.create("bitmap A");
70         shadowOf(bitmapA1).setWidth(100);
71         shadowOf(bitmapA1).setHeight(100);
72 
73         Bitmap bitmapA2 = ShadowBitmap.create("bitmap A");
74         shadowOf(bitmapA2).setWidth(101);
75         shadowOf(bitmapA2).setHeight(101);
76 
77         assertFalse(bitmapA1.equals(bitmapA2));
78     }
79 
80     @Test
shouldReceiveDescriptionWhenDrawingToCanvas()81     public void shouldReceiveDescriptionWhenDrawingToCanvas() throws Exception {
82         Bitmap bitmap1 = Robolectric.newInstanceOf(Bitmap.class);
83         shadowOf(bitmap1).appendDescription("Bitmap One");
84 
85         Bitmap bitmap2 = Robolectric.newInstanceOf(Bitmap.class);
86         shadowOf(bitmap2).appendDescription("Bitmap Two");
87 
88         Canvas canvas = new Canvas(bitmap1);
89         canvas.drawBitmap(bitmap2, 0, 0, null);
90 
91         assertEquals("Bitmap One\nBitmap Two", shadowOf(bitmap1).getDescription());
92     }
93 
94     @Test
shouldReceiveDescriptionWhenDrawingToCanvasWithBitmapAndMatrixAndPaint()95     public void shouldReceiveDescriptionWhenDrawingToCanvasWithBitmapAndMatrixAndPaint() throws Exception {
96         Bitmap bitmap1 = Robolectric.newInstanceOf(Bitmap.class);
97         shadowOf(bitmap1).appendDescription("Bitmap One");
98 
99         Bitmap bitmap2 = Robolectric.newInstanceOf(Bitmap.class);
100         shadowOf(bitmap2).appendDescription("Bitmap Two");
101 
102         Canvas canvas = new Canvas(bitmap1);
103         canvas.drawBitmap(bitmap2, new Matrix(), null);
104 
105         assertEquals("Bitmap One\nBitmap Two transformed by matrix", shadowOf(bitmap1).getDescription());
106     }
107 
108     @Test
shouldReceiveDescriptionWhenDrawABitmapToCanvasWithAPaintEffect()109     public void shouldReceiveDescriptionWhenDrawABitmapToCanvasWithAPaintEffect() throws Exception {
110         Bitmap bitmap1 = Robolectric.newInstanceOf(Bitmap.class);
111         shadowOf(bitmap1).appendDescription("Bitmap One");
112 
113         Bitmap bitmap2 = Robolectric.newInstanceOf(Bitmap.class);
114         shadowOf(bitmap2).appendDescription("Bitmap Two");
115 
116         Canvas canvas = new Canvas(bitmap1);
117         Paint paint = new Paint();
118         paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix()));
119         canvas.drawBitmap(bitmap2, new Matrix(), paint);
120 
121         assertEquals("Bitmap One\nBitmap Two with ColorMatrixColorFilter<1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0> transformed by matrix", shadowOf(bitmap1).getDescription());
122     }
123 
124     @Test
visualize_shouldReturnDescription()125     public void visualize_shouldReturnDescription() throws Exception {
126         Bitmap bitmap = Robolectric.newInstanceOf(Bitmap.class);
127         shadowOf(bitmap).appendDescription("Bitmap One");
128 
129         assertEquals("Bitmap One", Robolectric.visualize(bitmap));
130 
131     }
132 
133     @Test
shouldCopyBitmap()134     public void shouldCopyBitmap() {
135     	Bitmap bitmap = Robolectric.newInstanceOf(Bitmap.class);
136     	Bitmap bitmapCopy = bitmap.copy(Config.ARGB_8888, true);
137     	assertEquals(shadowOf(bitmapCopy).getConfig(), Config.ARGB_8888);
138     	assertTrue(shadowOf(bitmapCopy).isMutable());
139     }
140 }
141