1package com.xtremelabs.robolectric.bytecode;
2
3import android.graphics.Bitmap;
4import android.graphics.Paint;
5import com.xtremelabs.robolectric.Robolectric;
6import com.xtremelabs.robolectric.WithCustomClassListTestRunner;
7import com.xtremelabs.robolectric.internal.Implementation;
8import com.xtremelabs.robolectric.internal.Implements;
9import org.junit.Test;
10import org.junit.runner.RunWith;
11
12import static org.hamcrest.CoreMatchers.equalTo;
13import static org.hamcrest.CoreMatchers.is;
14import static org.junit.Assert.assertThat;
15
16@RunWith(WithCustomClassListTestRunner.class)
17public class AndroidTranslatorClassIntrumentedTest {
18
19    @Test
20    public void testNativeMethodsAreDelegated() throws Exception {
21        Robolectric.bindShadowClass(ShadowPaintForTests.class);
22
23        Paint paint = new Paint();
24        paint.setColor(1234);
25
26        assertThat(paint.getColor(), is(1234));
27    }
28
29    @Test
30    public void testClassesWithPrivateDefaultConstructorsCanBeShadowed() {
31        Robolectric.bindShadowClass(ShadowClassWithPrivateConstructor.class);
32
33        ClassWithPrivateConstructor inst = new ClassWithPrivateConstructor();
34        assertThat(inst.getInt(), is(42));
35    }
36
37    @Test
38    public void testEnumConstructorsAreNotRewritten() {
39        // just referencing this enum value would blow up if we rewrite its constructor
40        Bitmap.Config alpha8 = Bitmap.Config.ALPHA_8;
41        assertThat(alpha8.toString(), equalTo("ALPHA_8"));
42    }
43
44    /*
45     * Test "foreign class" getting its methods shadowed whe it's
46     * in the RobolectricClassLoader CustomClassNames arrayList
47     */
48    @Test
49    public void testCustomMethodShadowed() throws Exception {
50        Robolectric.bindShadowClass(ShadowCustomPaint.class);
51
52        CustomPaint customPaint = new CustomPaint();
53        assertThat(customPaint.getColor(), equalTo(10));
54        assertThat(customPaint.getColorName(), equalTo("rainbow"));
55    }
56
57    /*
58     * Test "foreign class" not getting its methods shadowed when it's
59     * not in the RobolectricClassLoader CustomClassNames arrayList
60     */
61    @Test
62    public void testCustomMethodNotShadowed() throws Exception {
63        Robolectric.bindShadowClass(ShadowCustomXmasPaint.class);
64
65        CustomXmasPaint customXmasPaint = new CustomXmasPaint();
66        assertThat(customXmasPaint.getColor(), equalTo(999));
67        assertThat(customXmasPaint.getColorName(), equalTo("XMAS"));
68    }
69
70    public static class ClassWithPrivateConstructor {
71        private ClassWithPrivateConstructor() {
72        }
73
74        public int getInt() {
75            return 99;
76        }
77    }
78
79    @Implements(ClassWithPrivateConstructor.class)
80    public static class ShadowClassWithPrivateConstructor {
81        @Implementation
82        public int getInt() {
83            return 42;
84        }
85    }
86
87    @Implements(Paint.class)
88    public static class ShadowPaintForTests {
89        private int color;
90
91        @Implementation
92        public void setColor(int color) {
93            this.color = color;
94        }
95
96        @Implementation
97        public int getColor() {
98            return color;
99        }
100    }
101
102    @SuppressWarnings({"UnusedDeclaration"})
103    public static class CustomPaint extends Paint {
104        private int customColor;
105
106        @Override
107        public int getColor() {
108            return customColor;
109        }
110
111        public String getColorName() {
112            return Integer.toString(customColor);
113        }
114    }
115
116    @Implements(CustomPaint.class)
117    public static class ShadowCustomPaint {
118
119        @Implementation
120        public int getColor() {
121            return 10;
122        }
123
124        @Implementation
125        public String getColorName() {
126            return "rainbow";
127        }
128    }
129
130    @SuppressWarnings({"UnusedDeclaration"})
131    public static class CustomXmasPaint extends Paint {
132
133        @Override
134        public int getColor() {
135            return 999;
136        }
137
138        public String getColorName() {
139            return "XMAS";
140        }
141    }
142
143    @Implements(CustomXmasPaint.class)
144    public static class ShadowCustomXmasPaint {
145
146        @Implementation
147        public int getColor() {
148            return -999;
149        }
150
151        @Implementation
152        public String getColorName() {
153            return "XMAS Color Test";
154        }
155    }
156}
157