• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.util;
2 
3 import com.google.common.io.CharStreams;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.nio.file.Path;
9 import java.util.Properties;
10 import org.robolectric.R;
11 import org.robolectric.pluginapi.Sdk;
12 import org.robolectric.plugins.SdkCollection;
13 import org.robolectric.res.Fs;
14 import org.robolectric.res.ResourcePath;
15 import org.robolectric.util.inject.Injector;
16 
17 public abstract class TestUtil {
18   private static ResourcePath SYSTEM_RESOURCE_PATH;
19   private static ResourcePath TEST_RESOURCE_PATH;
20   private static File testDirLocation;
21   private static SdkCollection sdkCollection;
22   private static final Injector injector =
23       new Injector.Builder().bind(Properties.class, System.getProperties()).build();
24 
resourcesBaseDir()25   public static Path resourcesBaseDir() {
26     return resourcesBaseDirFile().toPath();
27   }
28 
resourcesBaseDirFile()29   private static File resourcesBaseDirFile() {
30     if (testDirLocation == null) {
31       String baseDir = System.getProperty("robolectric-tests.base-dir");
32       return testDirLocation = new File(baseDir, "src/test/resources");
33     } else {
34       return testDirLocation;
35     }
36   }
37 
resourceFile(String... pathParts)38   public static Path resourceFile(String... pathParts) {
39     return Fs.join(resourcesBaseDir(), pathParts);
40   }
41 
testResources()42   public static ResourcePath testResources() {
43     if (TEST_RESOURCE_PATH == null) {
44       TEST_RESOURCE_PATH = new ResourcePath(R.class, resourceFile("res"), resourceFile("assets"));
45     }
46     return TEST_RESOURCE_PATH;
47   }
48 
systemResources()49   public static ResourcePath systemResources() {
50     if (SYSTEM_RESOURCE_PATH == null) {
51       Sdk sdk = getSdkCollection().getMaxSupportedSdk();
52       Path path = sdk.getJarPath();
53       SYSTEM_RESOURCE_PATH =
54           new ResourcePath(
55               android.R.class, path.resolve("raw-res/res"), path.resolve("raw-res/assets"));
56     }
57     return SYSTEM_RESOURCE_PATH;
58   }
59 
sdkResources(int apiLevel)60   public static ResourcePath sdkResources(int apiLevel) {
61     Path path = getSdkCollection().getSdk(apiLevel).getJarPath();
62     return new ResourcePath(null, path.resolve("raw-res/res"), null, null);
63   }
64 
readString(InputStream is)65   public static String readString(InputStream is) throws IOException {
66     return CharStreams.toString(new InputStreamReader(is, "UTF-8"));
67   }
68 
getSdkCollection()69   public static synchronized SdkCollection getSdkCollection() {
70     if (sdkCollection == null) {
71       sdkCollection = getInjectedInstance(SdkCollection.class);
72     }
73     return sdkCollection;
74   }
75 
resetSystemProperty(String name, String value)76   public static void resetSystemProperty(String name, String value) {
77     if (value == null) {
78       System.clearProperty(name);
79     } else {
80       System.setProperty(name, value);
81     }
82   }
83 
getInjectedInstance(Class<T> clazz)84   private static <T> T getInjectedInstance(Class<T> clazz) {
85     return injector.getInstance(clazz);
86   }
87 }
88