aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/RuntimeEnvironment.java
blob: a728c859b21a0ee17fe7252b00e1d37fd3c7a50d (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package org.robolectric;

import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static org.robolectric.annotation.LooperMode.Mode.LEGACY;
import static org.robolectric.shadows.ShadowLooper.assertLooperMode;

import android.app.Application;
import android.app.ResourcesManager;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.Display;
import com.google.common.base.Supplier;
import java.nio.file.Path;
import org.robolectric.android.Bootstrap;
import org.robolectric.android.ConfigurationV25;
import org.robolectric.res.ResourceTable;
import org.robolectric.shadows.ShadowDisplayManager;
import org.robolectric.shadows.ShadowInstrumentation;
import org.robolectric.shadows.ShadowView;
import org.robolectric.util.Scheduler;
import org.robolectric.util.TempDirectory;

public class RuntimeEnvironment {
  /**
   * @deprecated Use {@link #getApplication} instead. Note that unlike the alternative, this field
   *     is inherently incompatible with {@link
   *     org.robolectric.annotation.experimental.LazyApplication}. This field may be removed in a
   *     later release
   */
  @Deprecated public static Context systemContext;

  /**
   * @deprecated Please use {#getApplication} instead. Accessing this field directly is inherently
   *     incompatible with {@link org.robolectric.annotation.experimental.LazyApplication} and
   *     Robolectric makes no guarantees if a test *modifies* this field during execution.
   */
  @Deprecated public static volatile Application application;

  private static volatile Thread mainThread;
  private static volatile Object activityThread;
  private static int apiLevel;
  private static Scheduler masterScheduler;
  private static ResourceTable systemResourceTable;
  private static ResourceTable appResourceTable;
  private static ResourceTable compileTimeResourceTable;
  private static TempDirectory tempDirectory = new TempDirectory("no-test-yet");
  private static Path androidFrameworkJar;
  public static Path compileTimeSystemResourcesFile;

  private static Supplier<Application> applicationSupplier;
  private static final Object supplierLock = new Object();

  /**
   * Get a reference to the {@link Application} under test.
   *
   * <p>The Application may be created a test setup time or created lazily at call time, based on
   * the test's {@link org.robolectric.annotation.experimental.LazyApplication} setting. If lazy
   * loading is enabled, this method must be called on the main/test thread.
   *
   * <p>An alternate API outside of Robolectric is {@link
   * androidx.test.core.app.ApplicationProvider#getApplicationContext()}, which is preferable if you
   * desire cross platform tests that work on the JVM and real Android devices.
   */
  public static Application getApplication() {
    // IMPORTANT NOTE: Given the order in which these are nulled out when cleaning up in
    // AndroidTestEnvironment, the application null check must happen before the supplier null
    // check. Otherwise the get() call can try to load an application that has already been
    // loaded and cleaned up (as well as race with other threads trying to load the "correct"
    // application)
    if (application == null) {
      synchronized (supplierLock) {
        if (applicationSupplier != null) {
          ShadowInstrumentation.runOnMainSyncNoIdle(() -> application = applicationSupplier.get());
        }
      }
    }
    return application;
  }

  /** internal use only */
  public static void setApplicationSupplier(Supplier<Application> applicationSupplier) {
    synchronized (supplierLock) {
      RuntimeEnvironment.applicationSupplier = applicationSupplier;
    }
  }

  private static Class<? extends Application> applicationClass;

  public static Class<? extends Application> getConfiguredApplicationClass() {
    return applicationClass;
  }

  public static void setConfiguredApplicationClass(Class<? extends Application> clazz) {
    applicationClass = clazz;
  }

  /**
   * Tests if the given thread is currently set as the main thread.
   *
   * @param thread the thread to test.
   * @return true if the specified thread is the main thread, false otherwise.
   * @see #isMainThread()
   */
  public static boolean isMainThread(Thread thread) {
    assertLooperMode(LEGACY);
    return thread == mainThread;
  }

  /**
   * Tests if the current thread is currently set as the main thread.
   *
   * <p>Not supported in realistic looper mode.
   *
   * @return true if the current thread is the main thread, false otherwise.
   */
  public static boolean isMainThread() {
    assertLooperMode(LEGACY);
    return isMainThread(Thread.currentThread());
  }

  /**
   * Retrieves the main thread. The main thread is the thread to which the main looper is attached.
   * Defaults to the thread that initialises the {@link RuntimeEnvironment} class.
   *
   * <p>Not supported in realistic looper mode.
   *
   * @return The main thread.
   * @see #setMainThread(Thread)
   * @see #isMainThread()
   */
  public static Thread getMainThread() {
    assertLooperMode(LEGACY);
    return mainThread;
  }

  /**
   * Sets the main thread. The main thread is the thread to which the main looper is attached.
   * Defaults to the thread that initialises the {@link RuntimeEnvironment} class.
   *
   * <p>Not supported in realistic looper mode.
   *
   * @param newMainThread the new main thread.
   * @see #setMainThread(Thread)
   * @see #isMainThread()
   */
  public static void setMainThread(Thread newMainThread) {
    assertLooperMode(LEGACY);
    mainThread = newMainThread;
  }

  public static Object getActivityThread() {
    return activityThread;
  }

  public static void setActivityThread(Object newActivityThread) {
    activityThread = newActivityThread;
  }

  /**
   * Returns a qualifier string describing the current {@link Configuration} of the system
   * resources.
   *
   * @return a qualifier string as described
   *     (https://developer.android.com/guide/topics/resources/providing-resources.html#QualifierRules)[here].
   */
  public static String getQualifiers() {
    Resources systemResources = Resources.getSystem();
    return getQualifiers(systemResources.getConfiguration(), systemResources.getDisplayMetrics());
  }

  /**
   * Returns a qualifier string describing the given configuration and display metrics.
   *
   * @param configuration the configuration.
   * @param displayMetrics the display metrics.
   * @return a qualifier string as described
   *     (https://developer.android.com/guide/topics/resources/providing-resources.html#QualifierRules)[here].
   */
  public static String getQualifiers(Configuration configuration, DisplayMetrics displayMetrics) {
    return ConfigurationV25.resourceQualifierString(configuration, displayMetrics);
  }

  /**
   * Overrides the current device configuration.
   *
   * <p>If {@param newQualifiers} starts with a plus ('+'), the prior configuration is used as the
   * base configuration, with the given changes applied additively. Otherwise, default values are
   * used for unspecified properties, as described <a
   * href="http://robolectric.org/device-configuration/">here</a>.
   *
   * @param newQualifiers the qualifiers to apply
   */
  public static void setQualifiers(String newQualifiers) {
    ShadowDisplayManager.changeDisplay(Display.DEFAULT_DISPLAY, newQualifiers);

    Configuration configuration;
    DisplayMetrics displayMetrics = new DisplayMetrics();

    if (newQualifiers.startsWith("+")) {
      configuration = new Configuration(Resources.getSystem().getConfiguration());
      displayMetrics.setTo(Resources.getSystem().getDisplayMetrics());
    } else {
      configuration = new Configuration();
    }
    Bootstrap.applyQualifiers(newQualifiers, getApiLevel(), configuration, displayMetrics);
    if (ShadowView.useRealGraphics()) {
      Bitmap.setDefaultDensity(displayMetrics.densityDpi);
    }

    updateConfiguration(configuration, displayMetrics);
  }

  public static void setFontScale(float fontScale) {
    Resources systemResources = getApplication().getResources();
    DisplayMetrics displayMetrics = systemResources.getDisplayMetrics();
    Configuration configuration = systemResources.getConfiguration();

    displayMetrics.scaledDensity = displayMetrics.density * fontScale;
    configuration.fontScale = fontScale;

    updateConfiguration(configuration, displayMetrics);
  }

  public static float getFontScale() {
    Resources systemResources = getApplication().getResources();
    return systemResources.getConfiguration().fontScale;
  }

  private static void updateConfiguration(
      Configuration configuration, DisplayMetrics displayMetrics) {
    // Update the resources last so that listeners will have a consistent environment.
    // TODO(paulsowden): Can we call ResourcesManager.getInstance().applyConfigurationToResources()?
    if (ResourcesManager.getInstance().getConfiguration() != null) {
      ResourcesManager.getInstance().getConfiguration().updateFrom(configuration);
    }
    Resources.getSystem().updateConfiguration(configuration, displayMetrics);
    if (RuntimeEnvironment.application != null) {
      getApplication().getResources().updateConfiguration(configuration, displayMetrics);
    } else {
      // if application is not yet loaded, update the configuration in Bootstrap so that the
      // changes will be propagated once the application is finally loaded
      Bootstrap.updateDisplayResources(configuration, displayMetrics);
    }
  }

  public static int getApiLevel() {
    return apiLevel;
  }

  public static Number castNativePtr(long ptr) {
    // Weird, using a ternary here doesn't work, there's some auto promotion of boxed types
    // happening.
    if (getApiLevel() >= LOLLIPOP) {
      return ptr;
    } else {
      return (int) ptr;
    }
  }

  /**
   * Retrieves the current master scheduler. This scheduler is always used by the main {@link
   * android.os.Looper Looper}, and if the global scheduler option is set it is also used for the
   * background scheduler and for all other {@link android.os.Looper Looper}s
   *
   * @return The current master scheduler.
   * @see #setMasterScheduler(Scheduler) see
   *     org.robolectric.Robolectric#getForegroundThreadScheduler() see
   *     org.robolectric.Robolectric#getBackgroundThreadScheduler()
   */
  public static Scheduler getMasterScheduler() {
    return masterScheduler;
  }

  /**
   * Sets the current master scheduler. See {@link #getMasterScheduler()} for details. Note that
   * this method is primarily intended to be called by the Robolectric core setup code. Changing the
   * master scheduler during a test will have unpredictable results.
   *
   * @param masterScheduler the new master scheduler.
   * @see #getMasterScheduler() see org.robolectric.Robolectric#getForegroundThreadScheduler() see
   *     org.robolectric.Robolectric#getBackgroundThreadScheduler()
   */
  public static void setMasterScheduler(Scheduler masterScheduler) {
    RuntimeEnvironment.masterScheduler = masterScheduler;
  }

  public static void setSystemResourceTable(ResourceTable systemResourceTable) {
    RuntimeEnvironment.systemResourceTable = systemResourceTable;
  }

  public static void setAppResourceTable(ResourceTable appResourceTable) {
    RuntimeEnvironment.appResourceTable = appResourceTable;
  }

  public static ResourceTable getSystemResourceTable() {
    return systemResourceTable;
  }

  public static ResourceTable getAppResourceTable() {
    return appResourceTable;
  }

  public static void setCompileTimeResourceTable(ResourceTable compileTimeResourceTable) {
    RuntimeEnvironment.compileTimeResourceTable = compileTimeResourceTable;
  }

  public static ResourceTable getCompileTimeResourceTable() {
    return compileTimeResourceTable;
  }

  public static void setTempDirectory(TempDirectory tempDirectory) {
    RuntimeEnvironment.tempDirectory = tempDirectory;
  }

  public static TempDirectory getTempDirectory() {
    return tempDirectory;
  }

  public static void setAndroidFrameworkJarPath(Path localArtifactPath) {
    RuntimeEnvironment.androidFrameworkJar = localArtifactPath;
  }

  public static Path getAndroidFrameworkJarPath() {
    return RuntimeEnvironment.androidFrameworkJar;
  }

  /**
   * Internal only.
   *
   * @deprecated Do not use.
   */
  @Deprecated
  public static boolean useLegacyResources() {
    return false;
  }
}