• 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 org.junit.Test;
9 import org.junit.runners.model.InitializationError;
10 import org.robolectric.R;
11 import org.robolectric.RobolectricTestRunner;
12 import org.robolectric.internal.SdkConfig;
13 import org.robolectric.internal.dependency.DependencyResolver;
14 import org.robolectric.res.Fs;
15 import org.robolectric.res.FsFile;
16 import org.robolectric.res.ResourcePath;
17 
18 public abstract class TestUtil {
19   private static ResourcePath SYSTEM_RESOURCE_PATH;
20   private static ResourcePath TEST_RESOURCE_PATH;
21   public static final String TEST_PACKAGE = R.class.getPackage().getName();
22   private static File testDirLocation;
23 
resourcesBaseDir()24   public static FsFile resourcesBaseDir() {
25     return Fs.newFile(resourcesBaseDirFile());
26   }
27 
resourcesBaseDirFile()28   private static File resourcesBaseDirFile() {
29     if (testDirLocation == null) {
30       String baseDir = System.getProperty("robolectric-tests.base-dir");
31       return testDirLocation = new File(baseDir, "src/test/resources");
32     } else {
33       return testDirLocation;
34     }
35   }
36 
resourceFile(String... pathParts)37   public static FsFile resourceFile(String... pathParts) {
38     return resourcesBaseDir().join(pathParts);
39   }
40 
testResources()41   public static ResourcePath testResources() {
42     if (TEST_RESOURCE_PATH == null) {
43       TEST_RESOURCE_PATH = new ResourcePath(R.class, resourceFile("res"), resourceFile("assets"));
44     }
45     return TEST_RESOURCE_PATH;
46   }
47 
systemResources()48   public static ResourcePath systemResources() {
49     if (SYSTEM_RESOURCE_PATH == null) {
50       SdkConfig sdkConfig = new SdkConfig(SdkConfig.MAX_SDK_VERSION);
51       Fs fs = Fs.fromJar(getDependencyResolver().getLocalArtifactUrl(sdkConfig.getAndroidSdkDependency()));
52       SYSTEM_RESOURCE_PATH = new ResourcePath(android.R.class, fs.join("raw-res/res"), fs.join("raw-res/assets"));
53     }
54     return SYSTEM_RESOURCE_PATH;
55   }
56 
sdkResources(int apiLevel)57   public static ResourcePath sdkResources(int apiLevel) {
58     Fs sdkResFs = Fs.fromJar(getDependencyResolver().getLocalArtifactUrl(new SdkConfig(apiLevel).getAndroidSdkDependency()));
59     return new ResourcePath(null, sdkResFs.join("raw-res/res"), null, null);
60   }
61 
readString(InputStream is)62   public static String readString(InputStream is) throws IOException {
63     return CharStreams.toString(new InputStreamReader(is, "UTF-8"));
64   }
65 
getDependencyResolver()66   private static DependencyResolver getDependencyResolver() {
67     try {
68       return new MyRobolectricTestRunner().getJarResolver();
69     } catch (InitializationError initializationError) {
70       throw new RuntimeException(initializationError);
71     }
72   }
73 
resetSystemProperty(String name, String value)74   public static void resetSystemProperty(String name, String value) {
75     if (value == null) {
76       System.clearProperty(name);
77     } else {
78       System.setProperty(name, value);
79     }
80   }
81 
82   private static class MyRobolectricTestRunner extends RobolectricTestRunner {
MyRobolectricTestRunner()83     MyRobolectricTestRunner() throws InitializationError {
84       super(FakeTest.class);
85     }
86 
87     @Override
getJarResolver()88     protected DependencyResolver getJarResolver() {
89       return super.getJarResolver();
90     }
91 
92     public static class FakeTest {
fakeTest()93       @Test public void fakeTest() {
94       }
95     }
96   }
97 }
98