1 package org.robolectric.android; 2 3 import static org.assertj.core.api.Assertions.assertThat; 4 5 import android.graphics.Bitmap; 6 import android.graphics.Paint; 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 import org.robolectric.RobolectricTestRunner; 10 import org.robolectric.annotation.Config; 11 import org.robolectric.annotation.Implementation; 12 import org.robolectric.annotation.Implements; 13 import org.robolectric.annotation.internal.Instrument; 14 15 @RunWith(RobolectricTestRunner.class) 16 @Config(sdk = Config.NEWEST_SDK) 17 public class AndroidTranslatorClassInstrumentedTest { 18 19 @Test 20 @Config(shadows = ShadowPaintForTests.class) 21 public void testNativeMethodsAreDelegated() throws Exception { 22 Paint paint = new Paint(); 23 paint.setColor(1234); 24 25 assertThat(paint.getColor()).isEqualTo(1234); 26 } 27 28 @Test 29 @Config(shadows = ShadowClassWithPrivateConstructor.class) 30 public void testClassesWithPrivateDefaultConstructorsCanBeShadowed() { 31 ClassWithPrivateConstructor inst = new ClassWithPrivateConstructor(); 32 assertThat(inst.getInt()).isEqualTo(42); 33 } 34 35 @Test 36 public void testEnumConstructorsAreNotRewritten() { 37 // just referencing this enum value would blow up if we rewrite its constructor 38 Bitmap.Config alpha8 = Bitmap.Config.ALPHA_8; 39 assertThat(alpha8.toString()).isEqualTo("ALPHA_8"); 40 } 41 42 /* 43 * Test "foreign class" getting its methods shadowed whe it's 44 * in the SandboxClassLoader CustomClassNames arrayList 45 */ 46 @Test 47 @Config(shadows = {ShadowCustomPaint.class, ShadowPaintForTests.class}) 48 public void testCustomMethodShadowed() throws Exception { 49 CustomPaint customPaint = new CustomPaint(); 50 assertThat(customPaint.getColor()).isEqualTo(10); 51 assertThat(customPaint.getColorName()).isEqualTo("rainbow"); 52 } 53 54 @Instrument 55 public static class ClassWithPrivateConstructor { 56 private ClassWithPrivateConstructor() { 57 } 58 59 public int getInt() { 60 return 99; 61 } 62 } 63 64 @Implements(ClassWithPrivateConstructor.class) 65 public static class ShadowClassWithPrivateConstructor { 66 @Implementation 67 protected int getInt() { 68 return 42; 69 } 70 } 71 72 @Implements(Paint.class) 73 public static class ShadowPaintForTests { 74 private int color; 75 76 @Implementation 77 protected void setColor(int color) { 78 this.color = color; 79 } 80 81 @Implementation 82 protected int getColor() { 83 return color; 84 } 85 } 86 87 @SuppressWarnings({"UnusedDeclaration"}) 88 @Instrument 89 public static class CustomPaint extends Paint { 90 private int customColor; 91 92 @Override 93 public int getColor() { 94 return customColor; 95 } 96 97 public String getColorName() { 98 return Integer.toString(customColor); 99 } 100 } 101 102 @Implements(CustomPaint.class) 103 public static class ShadowCustomPaint extends ShadowPaintForTests { 104 105 @Override 106 @Implementation 107 protected int getColor() { 108 return 10; 109 } 110 111 @Implementation 112 protected String getColorName() { 113 return "rainbow"; 114 } 115 } 116 } 117