-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ShadowBuild.java
221 lines (192 loc) · 6.09 KB
/
ShadowBuild.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package org.robolectric.shadows;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.S;
import static org.robolectric.util.reflector.Reflector.reflector;
import android.annotation.TargetApi;
import android.os.Build;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
import org.robolectric.util.reflector.Static;
@Implements(value = Build.class)
public class ShadowBuild {
private static String radioVersionOverride = null;
/**
* Sets the value of the {@link Build#DEVICE} field.
*
* <p>It will be reset for the next test.
*/
public static void setDevice(String device) {
ReflectionHelpers.setStaticField(Build.class, "DEVICE", device);
}
/**
* Sets the value of the {@link Build#FINGERPRINT} field.
*
* <p>It will be reset for the next test.
*/
public static void setFingerprint(String fingerprint) {
ReflectionHelpers.setStaticField(Build.class, "FINGERPRINT", fingerprint);
}
/**
* Sets the value of the {@link Build#ID} field.
*
* <p>It will be reset for the next test.
*/
public static void setId(String id) {
ReflectionHelpers.setStaticField(Build.class, "ID", id);
}
/**
* Sets the value of the {@link Build#PRODUCT} field.
*
* <p>It will be reset for the next test.
*/
public static void setProduct(String product) {
ReflectionHelpers.setStaticField(Build.class, "PRODUCT", product);
}
/**
* Sets the value of the {@link Build#MODEL} field.
*
* <p>It will be reset for the next test.
*/
public static void setModel(String model) {
ReflectionHelpers.setStaticField(Build.class, "MODEL", model);
}
/**
* Sets the value of the {@link Build#MANUFACTURER} field.
*
* <p>It will be reset for the next test.
*/
public static void setManufacturer(String manufacturer) {
ReflectionHelpers.setStaticField(Build.class, "MANUFACTURER", manufacturer);
}
/**
* Sets the value of the {@link Build#BRAND} field.
*
* <p>It will be reset for the next test.
*/
public static void setBrand(String brand) {
ReflectionHelpers.setStaticField(Build.class, "BRAND", brand);
}
/**
* Sets the value of the {@link Build#HARDWARE} field.
*
* <p>It will be reset for the next test.
*/
public static void setHardware(String hardware) {
ReflectionHelpers.setStaticField(Build.class, "HARDWARE", hardware);
}
/**
* Sets the value of the {@link Build.VERSION#CODENAME} field.
*
* <p>It will be reset for the next test.
*/
public static void setVersionCodename(String versionCodename) {
ReflectionHelpers.setStaticField(Build.VERSION.class, "CODENAME", versionCodename);
}
/**
* Sets the value of the {@link Build.VERSION#INCREMENTAL} field.
*
* <p>It will be reset for the next test.
*/
public static void setVersionIncremental(String versionIncremental) {
ReflectionHelpers.setStaticField(Build.VERSION.class, "INCREMENTAL", versionIncremental);
}
/**
* Sets the value of the {@link Build.VERSION#MEDIA_PERFORMANCE_CLASS} field. Available in Android
* S+.
*
* <p>It will be reset for the next test.
*/
@TargetApi(S)
public static void setVersionMediaPerformanceClass(int performanceClass) {
ReflectionHelpers.setStaticField(
Build.VERSION.class, "MEDIA_PERFORMANCE_CLASS", performanceClass);
}
/**
* Sets the value of the {@link Build.VERSION#RELEASE} field.
*
* <p>It will be reset for the next test.
*/
public static void setVersionRelease(String release) {
ReflectionHelpers.setStaticField(Build.VERSION.class, "RELEASE", release);
}
/**
* Sets the value of the {@link Build.VERSION#SECURITY_PATCH} field. Available in Android M+.
*
* <p>It will be reset for the next test.
*/
@TargetApi(M)
public static void setVersionSecurityPatch(String securityPatch) {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SECURITY_PATCH", securityPatch);
}
/**
* Sets the value of the {@link Build#TAGS} field.
*
* <p>It will be reset for the next test.
*/
public static void setTags(String tags) {
ReflectionHelpers.setStaticField(Build.class, "TAGS", tags);
}
/**
* Sets the value of the {@link Build#TYPE} field.
*
* <p>It will be reset for the next test.
*/
public static void setType(String type) {
ReflectionHelpers.setStaticField(Build.class, "TYPE", type);
}
/**
* Sets the value of the {@link Build#SUPPORTED_64_BIT_ABIS} field. Available in Android L+.
*
* <p>It will be reset for the next test.
*/
@TargetApi(LOLLIPOP)
public static void setSupported64BitAbis(String[] supported64BitAbis) {
ReflectionHelpers.setStaticField(Build.class, "SUPPORTED_64_BIT_ABIS", supported64BitAbis);
}
/**
* Override return value from {@link Build#getRadioVersion()}
*
* @param radioVersion
*/
public static void setRadioVersion(String radioVersion) {
radioVersionOverride = radioVersion;
}
@Implementation
protected static String getRadioVersion() {
if (radioVersionOverride != null) {
return radioVersionOverride;
}
return reflector(_Build_.class).getRadioVersion();
}
@Implementation(minSdk = O)
protected static String getSerial() {
return Build.UNKNOWN;
}
@Resetter
public static synchronized void reset() {
radioVersionOverride = null;
reflector(_Build_.class).__staticInitializer__();
reflector(_VERSION_.class).__staticInitializer__();
}
/** Reflector interface for {@link Build}. */
@ForType(Build.class)
private interface _Build_ {
@Static
void __staticInitializer__();
@Static
@Direct
String getRadioVersion();
}
/** Reflector interface for {@link Build.VERSION}. */
@ForType(Build.VERSION.class)
private interface _VERSION_ {
@Static
void __staticInitializer__();
}
}