ShadowWranglerTest.java revision b5d56e95f27d484ad3098ac18867262e7c8826c1
1b5d56e95f27d484ad3098ac18867262e7c8826c1Christian Williams & Phil Goodwinpackage com.xtremelabs.robolectric.bytecode;
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;
7b5d56e95f27d484ad3098ac18867262e7c8826c1Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.Robolectric;
8b5d56e95f27d484ad3098ac18867262e7c8826c1Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.WithoutTestDefaultsRunner;
922c22c9aa4ca68c2deac6164edc1d82bc9645310Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.internal.Implements;
1022c22c9aa4ca68c2deac6164edc1d82bc9645310Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.internal.RealObject;
116eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.Before;
126eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.Test;
136eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.runner.RunWith;
146eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
153a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richardimport static org.hamcrest.Matchers.instanceOf;
163a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richardimport static org.junit.Assert.*;
176eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1807257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwin@RunWith(WithoutTestDefaultsRunner.class)
19cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williamspublic class ShadowWranglerTest {
206eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    private Context context;
216eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
226eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Before
236eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void setUp() throws Exception {
246eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        context = new MockContext();
256eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
266eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
276eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
28cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williams    public void testConstructorInvocation_WithDefaultConstructorAndNoConstructorDelegateOnShadowClass() throws Exception {
29237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(TestShadowView_WithDefaultConstructorAndNoConstructorDelegate.class);
306eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
316eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        View view = new View(context);
32e7a41c9b39233781c97dd599ff61024f0510e308Christian Williams        assertEquals(TestShadowView_WithDefaultConstructorAndNoConstructorDelegate.class, Robolectric.shadowOf_(view).getClass());
336eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
346eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
356eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
366eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void testConstructorInvocation() throws Exception {
37237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(TestShadowView.class);
386eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
396eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        View view = new View(context);
40433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(context, shadowOf(view).context);
41433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(view, shadowOf(view).realViewCtor);
421bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams    }
431bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
441bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams    @Test
451bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams    public void testRealObjectAnnotatedFieldsAreSetBeforeConstructorIsCalled() throws Exception {
46237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(TestShadowView.class);
471bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
481bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        View view = new View(context);
49433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(context, shadowOf(view).context);
50433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(view, shadowOf(view).realViewField);
511bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
52433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(view, shadowOf(view).realViewInConstructor);
53433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(view, shadowOf(view).realViewInParentConstructor);
546eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
556eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
566eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
576eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void testMethodDelegation() throws Exception {
58237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(TestShadowView.class);
596eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
606eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        View view = new View(context);
616eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        assertSame(context, view.getContext());
626eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
636eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
646eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
653fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    public void testEqualsMethodDelegation() throws Exception {
66237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(WithEquals.class);
673fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
683fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        View view1 = new View(context);
693fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        View view2 = new View(context);
703fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        assertEquals(view1, view2);
713fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
723fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
733fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    @Test
743fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    public void testHashCodeMethodDelegation() throws Exception {
75237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(WithEquals.class);
763fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
773fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        View view = new View(context);
783fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        assertEquals(42, view.hashCode());
793fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
803fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
813fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    @Test
823fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    public void testToStringMethodDelegation() throws Exception {
83237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(WithToString.class);
843fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
853fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        View view = new View(context);
863fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        assertEquals("the expected string", view.toString());
873fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
883fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
893fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    @Test
90cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williams    public void testShadowSelectionSearchesSuperclasses() throws Exception {
91237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(TestShadowView.class);
926eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
936eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        TextView textView = new TextView(context);
94e7a41c9b39233781c97dd599ff61024f0510e308Christian Williams        assertEquals(TestShadowView.class, Robolectric.shadowOf_(textView).getClass());
956eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
966eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
976eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
9807257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwin    public void shouldUseMostSpecificShadow() throws Exception {
99237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(TestShadowView.class);
100237888bb1e926aea523388de5d511a35f38506efPhil Goodwin & Tyler Schultz        Robolectric.bindShadowClass(TestShadowTextView.class);
1016eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1026eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        TextView textView = new TextView(context);
103433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertThat(shadowOf(textView), instanceOf(TestShadowTextView.class));
1046eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1056eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
106d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore    @Test
107d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore    public void testPrimitiveArrays() throws Exception {
108cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williams        Class<?> objArrayClass = ShadowWrangler.loadClass("java.lang.Object[]", getClass().getClassLoader());
109d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertTrue(objArrayClass.isArray());
110d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertEquals(Object.class, objArrayClass.getComponentType());
111d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore
112cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williams        Class<?> intArrayClass = ShadowWrangler.loadClass("int[]", getClass().getClassLoader());
113d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertTrue(intArrayClass.isArray());
114d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertEquals(Integer.TYPE, intArrayClass.getComponentType());
115d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore    }
116d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore
117433b46be67768332f764eab6879e0649993d79e3Christian Williams    private TestShadowView shadowOf(View view) {
118e7a41c9b39233781c97dd599ff61024f0510e308Christian Williams        return (TestShadowView) Robolectric.shadowOf_(view);
1196eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1206eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
121433b46be67768332f764eab6879e0649993d79e3Christian Williams    private TestShadowTextView shadowOf(TextView view) {
122e7a41c9b39233781c97dd599ff61024f0510e308Christian Williams        return (TestShadowTextView) Robolectric.shadowOf_(view);
1236eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1246eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
125537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
126537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    public static class WithEquals {
1273fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        @Override
1283fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        public boolean equals(Object o) {
1293fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin            return true;
1303fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        }
1313fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
1323fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        @Override
1333fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        public int hashCode() {
1343fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin            return 42;
1353fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        }
1363fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
1373fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
1383fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
139537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
140537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    public static class WithToString {
1413fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        @Override
1423fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        public String toString() {
1433fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin            return "the expected string";
1443fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        }
1453fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
1463fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
147537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
148afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams    public static class TestShadowView extends TestShadowViewParent {
1491bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        @RealObject
1501bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        private View realViewField;
1511bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        private View realViewInConstructor;
1521bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
1531bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        private View realViewCtor;
1541bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
1556eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        private Context context;
1566eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
157afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams        public TestShadowView(View view) {
1581bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams            this.realViewCtor = view;
1596eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        }
1606eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1611bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        @Override
1626eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        @SuppressWarnings({"UnusedDeclaration"})
1636eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        public void __constructor__(Context context) {
1641bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams            super.__constructor__(context);
1656eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz            this.context = context;
1661bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams            realViewInConstructor = realViewField;
1676eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        }
1686eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1696eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        @SuppressWarnings({"UnusedDeclaration"})
1706eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        public Context getContext() {
1716eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz            return context;
1726eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        }
1736eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1741bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
175537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
176afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams    public static class TestShadowViewParent {
1771bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        @RealObject
1781bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        private View realView;
1791bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        View realViewInParentConstructor;
1801bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
1811bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        public void __constructor__(Context context) {
1821bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams            realViewInParentConstructor = realView;
1831bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        }
1841bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams    }
185adfaceabbda4c8c51f24a0def8926075bd7306adChristian Williams
186537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
187afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams    public static class TestShadowView_WithDefaultConstructorAndNoConstructorDelegate {
1886eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1896eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
190537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(TextView.class)
191afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams    public static class TestShadowTextView {
1926eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1936eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz}
194