ShadowWranglerTest.java revision adfaceabbda4c8c51f24a0def8926075bd7306ad
1a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultzpackage com.xtremelabs.robolectric;
26eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
36eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport android.content.Context;
43fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwinimport android.graphics.Rect;
56eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport android.test.mock.MockContext;
66eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport android.view.View;
76eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport android.widget.TextView;
8537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwinimport com.xtremelabs.robolectric.util.Implements;
91bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williamsimport com.xtremelabs.robolectric.util.RealObject;
106eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.Before;
116eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.Test;
126eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport org.junit.runner.RunWith;
136eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
143a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richardimport static org.hamcrest.Matchers.instanceOf;
153a15df058f1e3fd7fbff5c3b17126249f1ed909aChristian Williams & Ryan Richardimport static org.junit.Assert.*;
166eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1707257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwin@RunWith(WithoutTestDefaultsRunner.class)
18cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williamspublic class ShadowWranglerTest {
196eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    private Context context;
206eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
216eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Before
226eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void setUp() throws Exception {
236eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        context = new MockContext();
246eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
256eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
266eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
27cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williams    public void testConstructorInvocation_WithDefaultConstructorAndNoConstructorDelegateOnShadowClass() throws Exception {
28638a930898ff7ff4d62b8a3028439142af65e4b4Christian Williams        Robolectric.bindShadowClass(View.class, TestShadowView_WithDefaultConstructorAndNoConstructorDelegate.class);
296eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
306eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        View view = new View(context);
31e7a41c9b39233781c97dd599ff61024f0510e308Christian Williams        assertEquals(TestShadowView_WithDefaultConstructorAndNoConstructorDelegate.class, Robolectric.shadowOf_(view).getClass());
326eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
336eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
346eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
356eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void testConstructorInvocation() throws Exception {
36638a930898ff7ff4d62b8a3028439142af65e4b4Christian Williams        Robolectric.bindShadowClass(View.class, TestShadowView.class);
376eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
386eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        View view = new View(context);
39433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(context, shadowOf(view).context);
40433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(view, shadowOf(view).realViewCtor);
411bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams    }
421bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
431bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams    @Test
441bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams    public void testRealObjectAnnotatedFieldsAreSetBeforeConstructorIsCalled() throws Exception {
45638a930898ff7ff4d62b8a3028439142af65e4b4Christian Williams        Robolectric.bindShadowClass(View.class, TestShadowView.class);
461bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
471bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        View view = new View(context);
48433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(context, shadowOf(view).context);
49433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(view, shadowOf(view).realViewField);
501bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
51433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(view, shadowOf(view).realViewInConstructor);
52433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertSame(view, shadowOf(view).realViewInParentConstructor);
536eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
546eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
556eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
566eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    public void testMethodDelegation() throws Exception {
57638a930898ff7ff4d62b8a3028439142af65e4b4Christian Williams        Robolectric.bindShadowClass(View.class, TestShadowView.class);
586eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
596eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        View view = new View(context);
606eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        assertSame(context, view.getContext());
616eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
626eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
636eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
643fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    public void testEqualsMethodDelegation() throws Exception {
65537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin        Robolectric.bindShadowClass(View.class, WithEquals.class);
663fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
673fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        View view1 = new View(context);
683fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        View view2 = new View(context);
693fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        assertEquals(view1, view2);
703fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
71537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin        Robolectric.bindShadowClass(Rect.class, WithEquals.class);
723fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        Rect rect1 = new Rect();
733fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        Rect rect2 = new Rect();
743fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        assertEquals(rect1, rect2);
753fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
763fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
773fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    @Test
783fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    public void testHashCodeMethodDelegation() throws Exception {
79537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin        Robolectric.bindShadowClass(View.class, WithEquals.class);
803fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
813fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        View view = new View(context);
823fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        assertEquals(42, view.hashCode());
833fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
843fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
853fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    @Test
863fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    public void testToStringMethodDelegation() throws Exception {
87537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin        Robolectric.bindShadowClass(View.class, WithToString.class);
883fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
893fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        View view = new View(context);
903fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        assertEquals("the expected string", view.toString());
913fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
923fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
933fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    @Test
94cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williams    public void testShadowSelectionSearchesSuperclasses() throws Exception {
95638a930898ff7ff4d62b8a3028439142af65e4b4Christian Williams        Robolectric.bindShadowClass(View.class, TestShadowView.class);
966eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
976eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        TextView textView = new TextView(context);
98e7a41c9b39233781c97dd599ff61024f0510e308Christian Williams        assertEquals(TestShadowView.class, Robolectric.shadowOf_(textView).getClass());
996eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1006eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1016eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    @Test
10207257335f88da163910cc0bd039e6163edc38580Gregg Van Hove & Phil Goodwin    public void shouldUseMostSpecificShadow() throws Exception {
103638a930898ff7ff4d62b8a3028439142af65e4b4Christian Williams        Robolectric.bindShadowClass(View.class, TestShadowView.class);
104638a930898ff7ff4d62b8a3028439142af65e4b4Christian Williams        Robolectric.bindShadowClass(TextView.class, TestShadowTextView.class);
1056eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1066eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        TextView textView = new TextView(context);
107433b46be67768332f764eab6879e0649993d79e3Christian Williams        assertThat(shadowOf(textView), instanceOf(TestShadowTextView.class));
1086eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1096eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
110d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore    @Test
111d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore    public void testPrimitiveArrays() throws Exception {
112cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williams        Class<?> objArrayClass = ShadowWrangler.loadClass("java.lang.Object[]", getClass().getClassLoader());
113d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertTrue(objArrayClass.isArray());
114d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertEquals(Object.class, objArrayClass.getComponentType());
115d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore
116cee7ff89bfcb83d6cd65e6de9b7cafe2c1caf40aChristian Williams        Class<?> intArrayClass = ShadowWrangler.loadClass("int[]", getClass().getClassLoader());
117d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertTrue(intArrayClass.isArray());
118d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore        assertEquals(Integer.TYPE, intArrayClass.getComponentType());
119d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore    }
120d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore
121d6318af742756246573a88fe2844d59d3c7ee29cJoseph Moore
122433b46be67768332f764eab6879e0649993d79e3Christian Williams    private TestShadowView shadowOf(View view) {
123e7a41c9b39233781c97dd599ff61024f0510e308Christian Williams        return (TestShadowView) Robolectric.shadowOf_(view);
1246eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1256eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
126433b46be67768332f764eab6879e0649993d79e3Christian Williams    private TestShadowTextView shadowOf(TextView view) {
127e7a41c9b39233781c97dd599ff61024f0510e308Christian Williams        return (TestShadowTextView) Robolectric.shadowOf_(view);
1286eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1296eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
130537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
131537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    public static class WithEquals {
1323fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        @Override
1333fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        public boolean equals(Object o) {
1343fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin            return true;
1353fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        }
1363fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
1373fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        @Override
1383fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        public int hashCode() {
1393fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin            return 42;
1403fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        }
1413fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
1423fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
1433fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
144537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
145537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    public static class WithToString {
1463fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        @Override
1473fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        public String toString() {
1483fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin            return "the expected string";
1493fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin        }
1503fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin    }
1513fbe415c50867fd95a2d7c7b1e0b7a08871290b8Christian Williams & Phil Goodwin
152537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
153afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams    public static class TestShadowView extends TestShadowViewParent {
1541bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        @RealObject
1551bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        private View realViewField;
1561bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        private View realViewInConstructor;
1571bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
1581bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        private View realViewCtor;
1591bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
1606eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        private Context context;
1616eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
162afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams        public TestShadowView(View view) {
1631bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams            this.realViewCtor = view;
1646eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        }
1656eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1661bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        @Override
1676eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        @SuppressWarnings({"UnusedDeclaration"})
1686eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        public void __constructor__(Context context) {
1691bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams            super.__constructor__(context);
1706eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz            this.context = context;
1711bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams            realViewInConstructor = realViewField;
1726eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        }
1736eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
1746eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        @SuppressWarnings({"UnusedDeclaration"})
1756eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        public Context getContext() {
1766eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz            return context;
1776eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz        }
1786eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1791bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
180537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
181afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams    public static class TestShadowViewParent {
1821bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        @RealObject
1831bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        private View realView;
1841bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        View realViewInParentConstructor;
1851bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams
1861bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        public void __constructor__(Context context) {
1871bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams            realViewInParentConstructor = realView;
1881bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams        }
1891bd18692c4958c09a8daa416137a64ef0bb3d96fChristian Williams    }
190adfaceabbda4c8c51f24a0def8926075bd7306adChristian Williams
191537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(View.class)
192afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams    public static class TestShadowView_WithDefaultConstructorAndNoConstructorDelegate {
1936eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1946eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz
195537f38b99433fee0ee08d7c634763e7381942f58Phil Goodwin    @Implements(TextView.class)
196afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams    public static class TestShadowTextView {
1976eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz    }
1986eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz}
199