ShadowWranglerTest.java revision 1bd18692c4958c09a8daa416137a64ef0bb3d96f
1a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultzpackage com.xtremelabs.robolectric; 26eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 36eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport android.content.Context; 46eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport android.test.mock.MockContext; 56eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport android.view.View; 66eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport android.widget.TextView; 71bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williamsimport com.xtremelabs.robolectric.util.RealObject; 86eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.Before; 96eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.Test; 106eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.runner.RunWith; 116eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 123a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richardimport static org.hamcrest.Matchers.instanceOf; 133a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richardimport static org.junit.Assert.*; 146eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 15a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz@RunWith(RobolectricAndroidTestRunner.class) 166eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzpublic class ProxyDelegatingHandlerTest { 176eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz private Context context; 186eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 196eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz @Before 206eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public void setUp() throws Exception { 216eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz context = new MockContext(); 226eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 236eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 246eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz @Test 256eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public void testConstructorInvocation_WithDefaultConstructorAndNoConstructorDelegateOnProxyClass() throws Exception { 26a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView_WithDefaultConstructorAndNoConstructorDelegate.class); 276eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 286eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz View view = new View(context); 29a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz assertEquals(TestFakeView_WithDefaultConstructorAndNoConstructorDelegate.class, RobolectricAndroidTestRunner.proxyFor(view).getClass()); 306eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 316eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 326eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz @Test 336eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public void testConstructorInvocation() throws Exception { 34a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class); 356eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 366eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz View view = new View(context); 376eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz assertSame(context, proxyFor(view).context); 381bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams assertSame(view, proxyFor(view).realViewCtor); 391bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams } 401bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams 411bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams @Test 421bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams public void testRealObjectAnnotatedFieldsAreSetBeforeConstructorIsCalled() throws Exception { 431bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class); 441bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams 451bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams View view = new View(context); 461bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams assertSame(context, proxyFor(view).context); 471bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams assertSame(view, proxyFor(view).realViewField); 481bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams 491bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams assertSame(view, proxyFor(view).realViewInConstructor); 501bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams assertSame(view, proxyFor(view).realViewInParentConstructor); 516eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 526eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 536eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz @Test 546eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public void testMethodDelegation() throws Exception { 55a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class); 566eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 576eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz View view = new View(context); 586eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz assertSame(context, view.getContext()); 596eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 606eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 616eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz @Test 626eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public void testProxySelectionSearchesSuperclasses() throws Exception { 63a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class); 646eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 656eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz TextView textView = new TextView(context); 66a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz assertEquals(TestFakeView.class, RobolectricAndroidTestRunner.proxyFor(textView).getClass()); 676eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 686eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 696eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz @Test 706eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public void testWeirdness() throws Exception { 71a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class); 72a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz RobolectricAndroidTestRunner.addProxy(TextView.class, TestFakeTextView.class); 736eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 746eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz TextView textView = new TextView(context); 753a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richard assertThat(proxyFor(textView), instanceOf(TestFakeTextView.class)); 766eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 776eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 78d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore @Test 79d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore public void testPrimitiveArrays() throws Exception { 80d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore Class<?> objArrayClass = ProxyDelegatingHandler.loadClass("java.lang.Object[]", getClass().getClassLoader()); 81d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore assertTrue(objArrayClass.isArray()); 82d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore assertEquals(Object.class, objArrayClass.getComponentType()); 83d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore 84d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore Class<?> intArrayClass = ProxyDelegatingHandler.loadClass("int[]", getClass().getClassLoader()); 85d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore assertTrue(intArrayClass.isArray()); 86d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore assertEquals(Integer.TYPE, intArrayClass.getComponentType()); 87d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore } 88d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore 89d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore 906eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz private TestFakeView proxyFor(View view) { 91a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz return (TestFakeView) RobolectricAndroidTestRunner.proxyFor(view); 926eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 936eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 946eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz private TestFakeTextView proxyFor(TextView view) { 95a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultz return (TestFakeTextView) RobolectricAndroidTestRunner.proxyFor(view); 966eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 976eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 981bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams public static class TestFakeView extends TestFakeViewParent { 991bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams @RealObject 1001bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams private View realViewField; 1011bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams private View realViewInConstructor; 1021bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams 1031bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams private View realViewCtor; 1041bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams 1056eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz private Context context; 1066eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 1076eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public TestFakeView(View view) { 1081bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams this.realViewCtor = view; 1096eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 1106eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 1111bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams @Override 1126eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz @SuppressWarnings({"UnusedDeclaration"}) 1136eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public void __constructor__(Context context) { 1141bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams super.__constructor__(context); 1156eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz this.context = context; 1161bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams realViewInConstructor = realViewField; 1176eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 1186eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 1196eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz @SuppressWarnings({"UnusedDeclaration"}) 1206eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public Context getContext() { 1216eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz return context; 1226eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 1236eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 1241bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams 1251bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams public static class TestFakeViewParent { 1261bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams @RealObject 1271bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams private View realView; 1281bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams View realViewInParentConstructor; 1291bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams 1301bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams public void __constructor__(Context context) { 1311bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams realViewInParentConstructor = realView; 1321bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams } 1331bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams } 1346eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 1356eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public static class TestFakeView_WithDefaultConstructorAndNoConstructorDelegate { 1366eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 1376eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 1386eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public static class TestFakeTextView { 1396eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 1406eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz} 141