• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.robolectric.annotation.Implementation;
8 import org.robolectric.annotation.Implements;
9 import org.robolectric.annotation.internal.Instrument;
10 import org.robolectric.internal.SandboxTestRunner;
11 import org.robolectric.internal.bytecode.SandboxConfig;
12 import org.robolectric.shadow.api.Shadow;
13 import org.robolectric.util.ReflectionHelpers.ClassParameter;
14 
15 @SandboxConfig(shadows={ RobolectricInternalsTest.ShadowConstructors.class })
16 @RunWith(SandboxTestRunner.class)
17 public class RobolectricInternalsTest {
18 
19   private static final String PARAM1 = "param1";
20   private static final Byte PARAM2 = (byte) 24;
21   private static final Long PARAM3 = (long) 10122345;
22 
23   @Test
getConstructor_withNoParams()24   public void getConstructor_withNoParams() {
25     Constructors a = new Constructors();
26     ShadowConstructors sa = shadowOf(a);
27 
28     assertThat(a.constructorCalled).isFalse();
29     assertThat(sa.shadowConstructorCalled).isTrue();
30 
31     Shadow.invokeConstructor(Constructors.class, a);
32     assertThat(a.constructorCalled).isTrue();
33   }
34 
35   @Test
getConstructor_withOneClassParam()36   public void getConstructor_withOneClassParam() {
37     Constructors a = new Constructors(PARAM1);
38     ShadowConstructors sa = shadowOf(a);
39 
40     assertThat(a.param11).isNull();
41     assertThat(sa.shadowParam11).isEqualTo(PARAM1);
42 
43     Shadow.invokeConstructor(Constructors.class, a, ClassParameter.from(String.class, PARAM1));
44     assertThat(a.param11).isEqualTo(PARAM1);
45   }
46 
47   @Test
getConstructor_withTwoClassParams()48   public void getConstructor_withTwoClassParams() {
49     Constructors a = new Constructors(PARAM1, PARAM2);
50     ShadowConstructors sa = shadowOf(a);
51 
52     assertThat(a.param21).isNull();
53     assertThat(a.param22).isNull();
54     assertThat(sa.shadowParam21).isEqualTo(PARAM1);
55     assertThat(sa.shadowParam22).isEqualTo(PARAM2);
56 
57     Shadow.invokeConstructor(Constructors.class, a, ClassParameter.from(String.class, PARAM1), ClassParameter.from(Byte.class, PARAM2));
58     assertThat(a.param21).isEqualTo(PARAM1);
59     assertThat(a.param22).isEqualTo(PARAM2);
60   }
61 
62   @Test
getConstructor_withThreeClassParams()63   public void getConstructor_withThreeClassParams() {
64     Constructors a = new Constructors(PARAM1, PARAM2, PARAM3);
65     ShadowConstructors sa = shadowOf(a);
66 
67     assertThat(a.param31).isNull();
68     assertThat(a.param32).isNull();
69     assertThat(a.param33).isNull();
70     assertThat(sa.shadowParam31).isEqualTo(PARAM1);
71     assertThat(sa.shadowParam32).isEqualTo(PARAM2);
72     assertThat(sa.shadowParam33).isEqualTo(PARAM3);
73 
74     Shadow.invokeConstructor(Constructors.class, a, ClassParameter.from(String.class, PARAM1), ClassParameter.from(Byte.class, PARAM2), ClassParameter.from(Long.class, PARAM3));
75     assertThat(a.param31).isEqualTo(PARAM1);
76     assertThat(a.param32).isEqualTo(PARAM2);
77     assertThat(a.param33).isEqualTo(PARAM3);
78   }
79 
shadowOf(Constructors realObject)80   private static ShadowConstructors shadowOf(Constructors realObject) {
81     Object shadow = Shadow.extract(realObject);
82     assertThat(shadow).isInstanceOf(ShadowConstructors.class);
83     return (ShadowConstructors) shadow;
84   }
85 
86   @Instrument
87   public static class Constructors {
88     public boolean constructorCalled = false;
89     public String param11 = null;
90 
91     public String param21 = null;
92     public Byte   param22 = null;
93 
94     public String param31 = null;
95     public Byte   param32 = null;
96     public Long   param33 = null;
97 
Constructors()98     public Constructors() {
99       constructorCalled = true;
100     }
101 
Constructors(String param)102     public Constructors(String param) {
103       param11 = param;
104     }
105 
Constructors(String param1, Byte param2)106     public Constructors(String param1, Byte param2) {
107       param21 = param1;
108       param22 = param2;
109     }
110 
Constructors(String param1, Byte param2, Long param3)111     public Constructors(String param1, Byte param2, Long param3) {
112       param31 = param1;
113       param32 = param2;
114       param33 = param3;
115     }
116   }
117 
118   @Implements(Constructors.class)
119   public static class ShadowConstructors {
120     public boolean shadowConstructorCalled = false;
121     public String shadowParam11 = null;
122 
123     public String shadowParam21 = null;
124     public Byte   shadowParam22 = null;
125 
126     public String shadowParam31 = null;
127     public Byte   shadowParam32 = null;
128     public Long   shadowParam33 = null;
129 
130     @Implementation
__constructor__()131     protected void __constructor__() {
132       shadowConstructorCalled = true;
133     }
134 
135     @Implementation
__constructor__(String param)136     protected void __constructor__(String param) {
137       shadowParam11 = param;
138     }
139 
140     @Implementation
__constructor__(String param1, Byte param2)141     protected void __constructor__(String param1, Byte param2) {
142       shadowParam21 = param1;
143       shadowParam22 = param2;
144     }
145 
146     @Implementation
__constructor__(String param1, Byte param2, Long param3)147     protected void __constructor__(String param1, Byte param2, Long param3) {
148       shadowParam31 = param1;
149       shadowParam32 = param2;
150       shadowParam33 = param3;
151     }
152   }
153 }
154