aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowEnvironment.java
blob: 2ce293834728c1e5bfb6b3897978d44ea18ca8ec (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.shadows;

import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.robolectric.RuntimeEnvironment;
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.Accessor;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
import org.robolectric.util.reflector.Static;

@Implements(Environment.class)
@SuppressWarnings("NewApi")
public class ShadowEnvironment {
  private static String externalStorageState = Environment.MEDIA_REMOVED;
  private static final Map<File, Boolean> STORAGE_EMULATED = new HashMap<>();
  private static final Map<File, Boolean> STORAGE_REMOVABLE = new HashMap<>();
  private static boolean sIsExternalStorageEmulated;
  private static boolean isExternalStorageLegacy;
  private static Path tmpExternalFilesDirBase;
  private static final List<File> externalDirs = new ArrayList<>();
  private static Map<Path, String> storageState = new HashMap<>();
  private static Path rootStorageDirectory;

  static Path EXTERNAL_CACHE_DIR;
  static Path EXTERNAL_FILES_DIR;

  @Implementation
  protected static String getExternalStorageState() {
    return externalStorageState;
  }

  /**
   * Sets the return value of {@link #getExternalStorageState()}.
   *
   * @param externalStorageState Value to return from {@link #getExternalStorageState()}.
   */
  public static void setExternalStorageState(String externalStorageState) {
    ShadowEnvironment.externalStorageState = externalStorageState;
  }

  /**
   * Sets the return value of {@link #isExternalStorageEmulated()}.
   *
   * @param emulated Value to return from {@link #isExternalStorageEmulated()}.
   */
  public static void setIsExternalStorageEmulated(boolean emulated) {
    ShadowEnvironment.sIsExternalStorageEmulated = emulated;
  }

  /**
   * Sets the return value of {@link #isExternalStorageLegacy()} ()}.
   *
   * @param legacy Value to return from {@link #isExternalStorageLegacy()}.
   */
  public static void setIsExternalStorageLegacy(boolean legacy) {
    ShadowEnvironment.isExternalStorageLegacy = legacy;
  }

  /**
   * Sets the return value of {@link #getStorageDirectory()}. This can be used for example, when
   * testing code paths that need to perform regex matching on this directory.
   *
   * <p>Note that the default value provides a directory that is usable in the test environment. If
   * the test app uses this method to override that default directory, please clean up any files
   * written to that directory, as the Robolectric environment will not purge that directory when
   * the test ends.
   *
   * @param directory Path to return from {@link #getStorageDirectory()}.
   */
  public static void setStorageDirectory(Path directory) {
    rootStorageDirectory = directory;
  }

  @Implementation(minSdk = R)
  protected static File getStorageDirectory() {
    if (rootStorageDirectory == null) {
      return reflector(EnvironmentReflector.class).getStorageDirectory();
    }
    return rootStorageDirectory.toFile();
  }

  /**
   * Sets the return value of {@link #getExternalStorageDirectory()}. Note that the default value
   * provides a directory that is usable in the test environment. If the test app uses this method
   * to override that default directory, please clean up any files written to that directory, as the
   * Robolectric environment will not purge that directory when the test ends.
   *
   * @param directory Path to return from {@link #getExternalStorageDirectory()}.
   */
  public static void setExternalStorageDirectory(Path directory) {
    EXTERNAL_CACHE_DIR = directory;
  }

  @Implementation
  protected static File getExternalStorageDirectory() {
    if (EXTERNAL_CACHE_DIR == null) {

      EXTERNAL_CACHE_DIR =
          RuntimeEnvironment.getTempDirectory().createIfNotExists("external-cache");
    }
    return EXTERNAL_CACHE_DIR.toFile();
  }

  @Implementation
  protected static File[] buildExternalStorageAppCacheDirs(String packageName) {
    Path externalStorageDirectoryPath = getExternalStorageDirectory().toPath();
    // Add cache directory in path.
    String cacheDirectory = packageName + "-cache";
    Path path = externalStorageDirectoryPath.resolve(cacheDirectory);
    try {
      Files.createDirectory(path);
    } catch (FileAlreadyExistsException e) {
      // That's ok
      return new File[] {path.toFile()};
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return new File[] {path.toFile()};
  }

  @Implementation(maxSdk = JELLY_BEAN_MR2)
  protected static File getExternalStorageAppCacheDirectory(String packageName) {
    return buildExternalStorageAppCacheDirs(packageName)[0];
  }

  /**
   * Sets the return value of {@link #getExternalStoragePublicDirectory}. Note that the default
   * value provides a directory that is usable in the test environment. If the test app uses this
   * method to override that default directory, please clean up any files written to that directory,
   * as the Robolectric environment will not purge that directory when the test ends.
   *
   * @param directory Path to return from {@link #getExternalStoragePublicDirectory}.
   */
  public static void setExternalStoragePublicDirectory(Path directory) {
    EXTERNAL_FILES_DIR = directory;
  }

  @Implementation
  protected static File getExternalStoragePublicDirectory(String type) {
    if (externalStorageState.equals(Environment.MEDIA_UNKNOWN)) {
      return null;
    }
    if (EXTERNAL_FILES_DIR == null) {
      EXTERNAL_FILES_DIR =
          RuntimeEnvironment.getTempDirectory().createIfNotExists("external-files");
    }
    if (type == null) return EXTERNAL_FILES_DIR.toFile();
    Path path = EXTERNAL_FILES_DIR.resolve(type);
    try {
      Files.createDirectories(path);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return path.toFile();
  }

  @Resetter
  public static void reset() {

    rootStorageDirectory = null;
    EXTERNAL_CACHE_DIR = null;
    EXTERNAL_FILES_DIR = null;

    STORAGE_EMULATED.clear();
    STORAGE_REMOVABLE.clear();

    storageState = new HashMap<>();
    externalDirs.clear();
    externalStorageState = Environment.MEDIA_REMOVED;

    sIsExternalStorageEmulated = false;
    isExternalStorageLegacy = false;
  }

  @Implementation
  protected static boolean isExternalStorageRemovable() {
    final Boolean exists = STORAGE_REMOVABLE.get(getExternalStorageDirectory());
    return exists != null ? exists : false;
  }

  @Implementation
  protected static String getStorageState(File directory) {
    Path directoryPath = directory.toPath();
    for (Map.Entry<Path, String> entry : storageState.entrySet()) {
      if (directoryPath.startsWith(entry.getKey())) {
        return entry.getValue();
      }
    }
    return null;
  }

  @Implementation(minSdk = LOLLIPOP)
  protected static String getExternalStorageState(File directory) {
    Path directoryPath = directory.toPath();
    for (Map.Entry<Path, String> entry : storageState.entrySet()) {
      if (directoryPath.startsWith(entry.getKey())) {
        return entry.getValue();
      }
    }
    return null;
  }

  @Implementation(minSdk = LOLLIPOP)
  protected static boolean isExternalStorageRemovable(File path) {
    final Boolean exists = STORAGE_REMOVABLE.get(path);
    return exists != null ? exists : false;
  }

  @Implementation(minSdk = LOLLIPOP)
  protected static boolean isExternalStorageEmulated(File path) {
    final Boolean emulated = STORAGE_EMULATED.get(path);
    return emulated != null ? emulated : false;
  }

  @Implementation
  protected static boolean isExternalStorageEmulated() {
    return sIsExternalStorageEmulated;
  }

  @Implementation(minSdk = Q)
  protected static boolean isExternalStorageLegacy(File path) {
    return isExternalStorageLegacy;
  }

  @Implementation(minSdk = Q)
  protected static boolean isExternalStorageLegacy() {
    return isExternalStorageLegacy;
  }

  /**
   * Sets the "isRemovable" flag of a particular file.
   *
   * @param file Target file.
   * @param isRemovable True if the filesystem is removable.
   */
  public static void setExternalStorageRemovable(File file, boolean isRemovable) {
    STORAGE_REMOVABLE.put(file, isRemovable);
  }

  /**
   * Sets the "isEmulated" flag of a particular file.
   *
   * @param file Target file.
   * @param isEmulated True if the filesystem is emulated.
   */
  public static void setExternalStorageEmulated(File file, boolean isEmulated) {
    STORAGE_EMULATED.put(file, isEmulated);
  }

  /**
   * Adds a directory to list returned by {@link ShadowUserEnvironment#getExternalDirs()}.
   *
   * @param path the external dir to add
   */
  public static File addExternalDir(String path) {
    Path externalFileDir;
    if (path == null) {
      externalFileDir = null;
    } else {
      try {
        if (tmpExternalFilesDirBase == null) {
          tmpExternalFilesDirBase =
              RuntimeEnvironment.getTempDirectory().create("external-files-base");
        }
        externalFileDir = tmpExternalFilesDirBase.resolve(path);
        Files.createDirectories(externalFileDir);
        externalDirs.add(externalFileDir.toFile());
      } catch (IOException e) {
        throw new RuntimeException("Could not create external files dir", e);
      }
    }

    if (RuntimeEnvironment.getApiLevel() < M) {
      Environment.UserEnvironment userEnvironment =
          ReflectionHelpers.getStaticField(Environment.class, "sCurrentUser");
      reflector(_UserEnvironment_.class, userEnvironment)
          .setExternalDirsForApp(externalDirs.toArray(new File[0]));
    }

    if (externalFileDir == null) {
      return null;
    }
    return externalFileDir.toFile();
  }

  /**
   * Sets the {@link #getExternalStorageState(File)} for given directory.
   *
   * @param externalStorageState Value to return from {@link #getExternalStorageState(File)}.
   */
  public static void setExternalStorageState(File directory, String state) {
    storageState.put(directory.toPath(), state);
  }

  @Implements(className = "android.os.Environment$UserEnvironment", isInAndroidSdk = false)
  public static class ShadowUserEnvironment {

    @Implementation(minSdk = M)
    protected File[] getExternalDirs() {
      return externalDirs.toArray(new File[externalDirs.size()]);
    }
  }

  /** Accessor interface for Environment.UserEnvironment's internals. */
  @ForType(className = "android.os.Environment$UserEnvironment")
  interface _UserEnvironment_ {
    @Accessor("mExternalDirsForApp")
    void setExternalDirsForApp(File[] files);

    @Accessor("mExternalStorageAndroidData")
    void setExternalStorageAndroidData(File file);
  }

  @ForType(Environment.class)
  interface EnvironmentReflector {
    @Static
    @Direct
    File getStorageDirectory();
  }
}