ShadowWranglerTest.java revision 3a15df058f1e3fd7fbff5c3b17126249f1ed909a
16eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzpackage com.xtremelabs.droidsugar;
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;
76eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.Before;
86eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.Test;
96eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.runner.RunWith;
106eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
113a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richardimport static org.hamcrest.Matchers.instanceOf;
123a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richardimport static org.junit.Assert.*;
136eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
146eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz@RunWith(DroidSugarAndroidTestRunner.class)
156eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzpublic class ProxyDelegatingHandlerTest {
166eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    private Context context;
176eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
186eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Before
196eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void setUp() throws Exception {
206eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        context = new MockContext();
216eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
226eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
236eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
246eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void testConstructorInvocation_WithDefaultConstructorAndNoConstructorDelegateOnProxyClass() throws Exception {
256eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        DroidSugarAndroidTestRunner.addProxy(View.class, TestFakeView_WithDefaultConstructorAndNoConstructorDelegate.class);
266eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
276eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        View view = new View(context);
286eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        assertEquals(TestFakeView_WithDefaultConstructorAndNoConstructorDelegate.class, DroidSugarAndroidTestRunner.proxyFor(view).getClass());
296eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
306eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
316eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
326eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void testConstructorInvocation() throws Exception {
336eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        DroidSugarAndroidTestRunner.addProxy(View.class, TestFakeView.class);
346eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
356eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        View view = new View(context);
366eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        assertSame(context, proxyFor(view).context);
376eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        assertSame(view, proxyFor(view).realView);
386eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
396eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
406eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
416eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void testMethodDelegation() throws Exception {
426eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        DroidSugarAndroidTestRunner.addProxy(View.class, TestFakeView.class);
436eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
446eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        View view = new View(context);
456eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        assertSame(context, view.getContext());
466eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
476eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
486eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
496eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void testProxySelectionSearchesSuperclasses() throws Exception {
506eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        DroidSugarAndroidTestRunner.addProxy(View.class, TestFakeView.class);
516eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
526eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        TextView textView = new TextView(context);
536eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        assertEquals(TestFakeView.class, DroidSugarAndroidTestRunner.proxyFor(textView).getClass());
546eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
556eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
566eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
576eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void testWeirdness() throws Exception {
586eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        DroidSugarAndroidTestRunner.addProxy(View.class, TestFakeView.class);
596eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        DroidSugarAndroidTestRunner.addProxy(TextView.class, TestFakeTextView.class);
606eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
616eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        TextView textView = new TextView(context);
623a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richard        assertThat(proxyFor(textView), instanceOf(TestFakeTextView.class));
636eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
646eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
65d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore    @Test
66d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore    public void testPrimitiveArrays() throws Exception {
67d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        Class<?> objArrayClass = ProxyDelegatingHandler.loadClass("java.lang.Object[]", getClass().getClassLoader());
68d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertTrue(objArrayClass.isArray());
69d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertEquals(Object.class, objArrayClass.getComponentType());
70d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore
71d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        Class<?> intArrayClass = ProxyDelegatingHandler.loadClass("int[]", getClass().getClassLoader());
72d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertTrue(intArrayClass.isArray());
73d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertEquals(Integer.TYPE, intArrayClass.getComponentType());
74d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore    }
75d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore
76d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore
776eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    private TestFakeView proxyFor(View view) {
786eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        return (TestFakeView) DroidSugarAndroidTestRunner.proxyFor(view);
796eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
806eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
816eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    private TestFakeTextView proxyFor(TextView view) {
826eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        return (TestFakeTextView) DroidSugarAndroidTestRunner.proxyFor(view);
836eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
846eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
856eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public static class TestFakeView {
866eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        private View realView;
876eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        private Context context;
886eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
896eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        public TestFakeView(View view) {
906eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz            this.realView = view;
916eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        }
926eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
936eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        @SuppressWarnings({"UnusedDeclaration"})
946eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        public void __constructor__(Context context) {
956eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz            this.context = context;
966eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        }
976eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
986eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        @SuppressWarnings({"UnusedDeclaration"})
996eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        public Context getContext() {
1006eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz            return context;
1016eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        }
1026eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1036eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1046eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public static class TestFakeView_WithDefaultConstructorAndNoConstructorDelegate {
1056eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1066eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1076eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public static class TestFakeTextView {
1086eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1096eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz}
110