xref: /aosp_15_r20/external/robolectric/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBuild.java (revision e6ba16074e6af37d123cb567d575f496bf0a58ee)
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.M;
4 import static android.os.Build.VERSION_CODES.O;
5 import static android.os.Build.VERSION_CODES.S;
6 import static org.robolectric.util.reflector.Reflector.reflector;
7 
8 import android.annotation.TargetApi;
9 import android.os.Build;
10 import org.robolectric.annotation.Implementation;
11 import org.robolectric.annotation.Implements;
12 import org.robolectric.annotation.Resetter;
13 import org.robolectric.util.ReflectionHelpers;
14 import org.robolectric.util.reflector.Accessor;
15 import org.robolectric.util.reflector.Direct;
16 import org.robolectric.util.reflector.ForType;
17 import org.robolectric.util.reflector.Static;
18 
19 @Implements(value = Build.class)
20 public class ShadowBuild {
21 
22   private static String radioVersionOverride = null;
23   private static String serialOverride = Build.UNKNOWN;
24 
25   /**
26    * Sets the value of the {@link Build#BOARD} field.
27    *
28    * <p>It will be reset for the next test.
29    */
setBoard(String board)30   public static void setBoard(String board) {
31     ReflectionHelpers.setStaticField(Build.class, "BOARD", board);
32   }
33 
34   /**
35    * Sets the value of the {@link Build#DEVICE} field.
36    *
37    * <p>It will be reset for the next test.
38    */
setDevice(String device)39   public static void setDevice(String device) {
40     ReflectionHelpers.setStaticField(Build.class, "DEVICE", device);
41   }
42 
43   /**
44    * Sets the value of the {@link Build#FINGERPRINT} field.
45    *
46    * <p>It will be reset for the next test.
47    */
setFingerprint(String fingerprint)48   public static void setFingerprint(String fingerprint) {
49     ReflectionHelpers.setStaticField(Build.class, "FINGERPRINT", fingerprint);
50   }
51 
52   /**
53    * Sets the value of the {@link Build#ID} field.
54    *
55    * <p>It will be reset for the next test.
56    */
setId(String id)57   public static void setId(String id) {
58     ReflectionHelpers.setStaticField(Build.class, "ID", id);
59   }
60 
61   /**
62    * Sets the value of the {@link Build#PRODUCT} field.
63    *
64    * <p>It will be reset for the next test.
65    */
setProduct(String product)66   public static void setProduct(String product) {
67     ReflectionHelpers.setStaticField(Build.class, "PRODUCT", product);
68   }
69 
70   /**
71    * Sets the value of the {@link Build#IS_DEBUGGABLE} field.
72    *
73    * <p>It will be reset for the next test.
74    */
setDebuggable(Boolean isDebuggable)75   public static void setDebuggable(Boolean isDebuggable) {
76     ReflectionHelpers.setStaticField(Build.class, "IS_DEBUGGABLE", isDebuggable);
77   }
78 
79   /**
80    * Sets the value of the {@link Build#MODEL} field.
81    *
82    * <p>It will be reset for the next test.
83    */
setModel(String model)84   public static void setModel(String model) {
85     ReflectionHelpers.setStaticField(Build.class, "MODEL", model);
86   }
87 
88   /**
89    * Sets the value of the {@link Build#MANUFACTURER} field.
90    *
91    * <p>It will be reset for the next test.
92    */
setManufacturer(String manufacturer)93   public static void setManufacturer(String manufacturer) {
94     ReflectionHelpers.setStaticField(Build.class, "MANUFACTURER", manufacturer);
95   }
96 
97   /**
98    * Sets the value of the {@link Build#BRAND} field.
99    *
100    * <p>It will be reset for the next test.
101    */
setBrand(String brand)102   public static void setBrand(String brand) {
103     ReflectionHelpers.setStaticField(Build.class, "BRAND", brand);
104   }
105 
106   /**
107    * Sets the value of the {@link Build#HARDWARE} field.
108    *
109    * <p>It will be reset for the next test.
110    */
setHardware(String hardware)111   public static void setHardware(String hardware) {
112     ReflectionHelpers.setStaticField(Build.class, "HARDWARE", hardware);
113   }
114 
115   /** Override return value from {@link Build#getSerial()}. */
setSerial(String serial)116   public static void setSerial(String serial) {
117     serialOverride = serial;
118   }
119 
120   /**
121    * Sets the value of the {@link Build.VERSION#CODENAME} field.
122    *
123    * <p>It will be reset for the next test.
124    */
setVersionCodename(String versionCodename)125   public static void setVersionCodename(String versionCodename) {
126     ReflectionHelpers.setStaticField(Build.VERSION.class, "CODENAME", versionCodename);
127   }
128 
129   /**
130    * Sets the value of the {@link Build.VERSION#INCREMENTAL} field.
131    *
132    * <p>It will be reset for the next test.
133    */
setVersionIncremental(String versionIncremental)134   public static void setVersionIncremental(String versionIncremental) {
135     ReflectionHelpers.setStaticField(Build.VERSION.class, "INCREMENTAL", versionIncremental);
136   }
137 
138   /**
139    * Sets the value of the {@link Build.VERSION#MEDIA_PERFORMANCE_CLASS} field. Available in Android
140    * S+.
141    *
142    * <p>It will be reset for the next test.
143    */
144   @TargetApi(S)
setVersionMediaPerformanceClass(int performanceClass)145   public static void setVersionMediaPerformanceClass(int performanceClass) {
146     ReflectionHelpers.setStaticField(
147         Build.VERSION.class, "MEDIA_PERFORMANCE_CLASS", performanceClass);
148   }
149 
150   /**
151    * Sets the value of the {@link Build.VERSION#RELEASE} field.
152    *
153    * <p>It will be reset for the next test.
154    */
setVersionRelease(String release)155   public static void setVersionRelease(String release) {
156     ReflectionHelpers.setStaticField(Build.VERSION.class, "RELEASE", release);
157   }
158 
159   /**
160    * Sets the value of the {@link Build.VERSION#SECURITY_PATCH} field. Available in Android M+.
161    *
162    * <p>It will be reset for the next test.
163    */
164   @TargetApi(M)
setVersionSecurityPatch(String securityPatch)165   public static void setVersionSecurityPatch(String securityPatch) {
166     ReflectionHelpers.setStaticField(Build.VERSION.class, "SECURITY_PATCH", securityPatch);
167   }
168 
169   /**
170    * Sets the value of the {@link Build#TAGS} field.
171    *
172    * <p>It will be reset for the next test.
173    */
setTags(String tags)174   public static void setTags(String tags) {
175     ReflectionHelpers.setStaticField(Build.class, "TAGS", tags);
176   }
177 
178   /**
179    * Sets the value of the {@link Build#TYPE} field.
180    *
181    * <p>It will be reset for the next test.
182    */
setType(String type)183   public static void setType(String type) {
184     ReflectionHelpers.setStaticField(Build.class, "TYPE", type);
185   }
186 
187   /**
188    * Sets the value of the {@link Build#SUPPORTED_32_BIT_ABIS} field.
189    *
190    * <p>It will be reset for the next test.
191    */
setSupported32BitAbis(String[] supported32BitAbis)192   public static void setSupported32BitAbis(String[] supported32BitAbis) {
193     ReflectionHelpers.setStaticField(Build.class, "SUPPORTED_32_BIT_ABIS", supported32BitAbis);
194   }
195 
196   /**
197    * Sets the value of the {@link Build#SUPPORTED_64_BIT_ABIS} field.
198    *
199    * <p>It will be reset for the next test.
200    */
setSupported64BitAbis(String[] supported64BitAbis)201   public static void setSupported64BitAbis(String[] supported64BitAbis) {
202     ReflectionHelpers.setStaticField(Build.class, "SUPPORTED_64_BIT_ABIS", supported64BitAbis);
203   }
204 
205   /**
206    * Sets the value of the {@link Build#SUPPORTED_ABIS} field.
207    *
208    * <p>It will be reset for the next test.
209    */
setSupportedAbis(String[] supportedAbis)210   public static void setSupportedAbis(String[] supportedAbis) {
211     ReflectionHelpers.setStaticField(Build.class, "SUPPORTED_ABIS", supportedAbis);
212   }
213 
214   /**
215    * Override return value from {@link Build#getRadioVersion()}
216    *
217    * @param radioVersion
218    */
setRadioVersion(String radioVersion)219   public static void setRadioVersion(String radioVersion) {
220     radioVersionOverride = radioVersion;
221   }
222 
223   /**
224    * Sets the value of the {@link Build#SOC_MANUFACTURER} field.
225    *
226    * <p>It will be reset for the next test.
227    *
228    * <p>Added in API level 31.
229    */
setSystemOnChipManufacturer(String systemOnChipManufacturer)230   public static void setSystemOnChipManufacturer(String systemOnChipManufacturer) {
231     ReflectionHelpers.setStaticField(Build.class, "SOC_MANUFACTURER", systemOnChipManufacturer);
232   }
233 
234   /**
235    * Sets the value of the {@link Build#SOC_MODEL} field.
236    *
237    * <p>It will be reset for the next test.
238    *
239    * <p>Added in API level 31.
240    */
setSystemOnChipModel(String systemOnChipModel)241   public static void setSystemOnChipModel(String systemOnChipModel) {
242     ReflectionHelpers.setStaticField(Build.class, "SOC_MODEL", systemOnChipModel);
243   }
244 
245   /**
246    * Sets the value of the {@link Build#ODM_SKU} field.
247    *
248    * <p>It will be reset for the next test.
249    *
250    * <p>Added in API level 31.
251    */
setOdmSku(String odmSku)252   public static void setOdmSku(String odmSku) {
253     reflector(_Build_.class).setOdmSku(odmSku);
254   }
255 
256   @Implementation
getRadioVersion()257   protected static String getRadioVersion() {
258     if (radioVersionOverride != null) {
259       return radioVersionOverride;
260     }
261     return reflector(_Build_.class).getRadioVersion();
262   }
263 
264   @Implementation(minSdk = O)
getSerial()265   protected static String getSerial() {
266     return serialOverride;
267   }
268 
269   @Resetter
reset()270   public static synchronized void reset() {
271     radioVersionOverride = null;
272     serialOverride = Build.UNKNOWN;
273     reflector(_Build_.class).__staticInitializer__();
274     reflector(_VERSION_.class).__staticInitializer__();
275   }
276 
277   /** Reflector interface for {@link Build}. */
278   @ForType(Build.class)
279   private interface _Build_ {
280 
281     @Static
__staticInitializer__()282     void __staticInitializer__();
283 
284     @Static
285     @Accessor("ODM_SKU")
setOdmSku(String odmSku)286     void setOdmSku(String odmSku);
287 
288     @Static
289     @Direct
getRadioVersion()290     String getRadioVersion();
291   }
292 
293   /** Reflector interface for {@link Build.VERSION}. */
294   @ForType(Build.VERSION.class)
295   private interface _VERSION_ {
296 
297     @Static
__staticInitializer__()298     void __staticInitializer__();
299   }
300 }
301