ShadowWranglerTest.java revision 1bd18692c4958c09a8daa416137a64ef0bb3d96f
1package com.xtremelabs.robolectric;
2
3import android.content.Context;
4import android.test.mock.MockContext;
5import android.view.View;
6import android.widget.TextView;
7import com.xtremelabs.robolectric.util.RealObject;
8import org.junit.Before;
9import org.junit.Test;
10import org.junit.runner.RunWith;
11
12import static org.hamcrest.Matchers.instanceOf;
13import static org.junit.Assert.*;
14
15@RunWith(RobolectricAndroidTestRunner.class)
16public class ProxyDelegatingHandlerTest {
17    private Context context;
18
19    @Before
20    public void setUp() throws Exception {
21        context = new MockContext();
22    }
23
24    @Test
25    public void testConstructorInvocation_WithDefaultConstructorAndNoConstructorDelegateOnProxyClass() throws Exception {
26        RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView_WithDefaultConstructorAndNoConstructorDelegate.class);
27
28        View view = new View(context);
29        assertEquals(TestFakeView_WithDefaultConstructorAndNoConstructorDelegate.class, RobolectricAndroidTestRunner.proxyFor(view).getClass());
30    }
31
32    @Test
33    public void testConstructorInvocation() throws Exception {
34        RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class);
35
36        View view = new View(context);
37        assertSame(context, proxyFor(view).context);
38        assertSame(view, proxyFor(view).realViewCtor);
39    }
40
41    @Test
42    public void testRealObjectAnnotatedFieldsAreSetBeforeConstructorIsCalled() throws Exception {
43        RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class);
44
45        View view = new View(context);
46        assertSame(context, proxyFor(view).context);
47        assertSame(view, proxyFor(view).realViewField);
48
49        assertSame(view, proxyFor(view).realViewInConstructor);
50        assertSame(view, proxyFor(view).realViewInParentConstructor);
51    }
52
53    @Test
54    public void testMethodDelegation() throws Exception {
55        RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class);
56
57        View view = new View(context);
58        assertSame(context, view.getContext());
59    }
60
61    @Test
62    public void testProxySelectionSearchesSuperclasses() throws Exception {
63        RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class);
64
65        TextView textView = new TextView(context);
66        assertEquals(TestFakeView.class, RobolectricAndroidTestRunner.proxyFor(textView).getClass());
67    }
68
69    @Test
70    public void testWeirdness() throws Exception {
71        RobolectricAndroidTestRunner.addProxy(View.class, TestFakeView.class);
72        RobolectricAndroidTestRunner.addProxy(TextView.class, TestFakeTextView.class);
73
74        TextView textView = new TextView(context);
75        assertThat(proxyFor(textView), instanceOf(TestFakeTextView.class));
76    }
77
78    @Test
79    public void testPrimitiveArrays() throws Exception {
80        Class<?> objArrayClass = ProxyDelegatingHandler.loadClass("java.lang.Object[]", getClass().getClassLoader());
81        assertTrue(objArrayClass.isArray());
82        assertEquals(Object.class, objArrayClass.getComponentType());
83
84        Class<?> intArrayClass = ProxyDelegatingHandler.loadClass("int[]", getClass().getClassLoader());
85        assertTrue(intArrayClass.isArray());
86        assertEquals(Integer.TYPE, intArrayClass.getComponentType());
87    }
88
89
90    private TestFakeView proxyFor(View view) {
91        return (TestFakeView) RobolectricAndroidTestRunner.proxyFor(view);
92    }
93
94    private TestFakeTextView proxyFor(TextView view) {
95        return (TestFakeTextView) RobolectricAndroidTestRunner.proxyFor(view);
96    }
97
98    public static class TestFakeView extends TestFakeViewParent {
99        @RealObject
100        private View realViewField;
101        private View realViewInConstructor;
102
103        private View realViewCtor;
104
105        private Context context;
106
107        public TestFakeView(View view) {
108            this.realViewCtor = view;
109        }
110
111        @Override
112        @SuppressWarnings({"UnusedDeclaration"})
113        public void __constructor__(Context context) {
114            super.__constructor__(context);
115            this.context = context;
116            realViewInConstructor = realViewField;
117        }
118
119        @SuppressWarnings({"UnusedDeclaration"})
120        public Context getContext() {
121            return context;
122        }
123    }
124
125    public static class TestFakeViewParent {
126        @RealObject
127        private View realView;
128        View realViewInParentConstructor;
129
130        public void __constructor__(Context context) {
131            realViewInParentConstructor = realView;
132        }
133    }
134
135    public static class TestFakeView_WithDefaultConstructorAndNoConstructorDelegate {
136    }
137
138    public static class TestFakeTextView {
139    }
140}
141