package org.robolectric.util; import com.google.common.io.CharStreams; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.junit.Test; import org.junit.runners.model.InitializationError; import org.robolectric.R; import org.robolectric.RobolectricTestRunner; import org.robolectric.internal.SdkConfig; import org.robolectric.internal.dependency.DependencyResolver; import org.robolectric.res.Fs; import org.robolectric.res.FsFile; import org.robolectric.res.ResourcePath; public abstract class TestUtil { private static ResourcePath SYSTEM_RESOURCE_PATH; private static ResourcePath TEST_RESOURCE_PATH; public static final String TEST_PACKAGE = R.class.getPackage().getName(); private static File testDirLocation; public static FsFile resourcesBaseDir() { return Fs.newFile(resourcesBaseDirFile()); } private static File resourcesBaseDirFile() { if (testDirLocation == null) { String baseDir = System.getProperty("robolectric-tests.base-dir"); return testDirLocation = new File(baseDir, "src/test/resources"); } else { return testDirLocation; } } public static FsFile resourceFile(String... pathParts) { return resourcesBaseDir().join(pathParts); } public static ResourcePath testResources() { if (TEST_RESOURCE_PATH == null) { TEST_RESOURCE_PATH = new ResourcePath(R.class, resourceFile("res"), resourceFile("assets")); } return TEST_RESOURCE_PATH; } public static ResourcePath systemResources() { if (SYSTEM_RESOURCE_PATH == null) { SdkConfig sdkConfig = new SdkConfig(SdkConfig.MAX_SDK_VERSION); Fs fs = Fs.fromJar(getDependencyResolver().getLocalArtifactUrl(sdkConfig.getAndroidSdkDependency())); SYSTEM_RESOURCE_PATH = new ResourcePath(android.R.class, fs.join("raw-res/res"), fs.join("raw-res/assets")); } return SYSTEM_RESOURCE_PATH; } public static ResourcePath sdkResources(int apiLevel) { Fs sdkResFs = Fs.fromJar(getDependencyResolver().getLocalArtifactUrl(new SdkConfig(apiLevel).getAndroidSdkDependency())); return new ResourcePath(null, sdkResFs.join("raw-res/res"), null, null); } public static String readString(InputStream is) throws IOException { return CharStreams.toString(new InputStreamReader(is, "UTF-8")); } private static DependencyResolver getDependencyResolver() { try { return new MyRobolectricTestRunner().getJarResolver(); } catch (InitializationError initializationError) { throw new RuntimeException(initializationError); } } public static void resetSystemProperty(String name, String value) { if (value == null) { System.clearProperty(name); } else { System.setProperty(name, value); } } private static class MyRobolectricTestRunner extends RobolectricTestRunner { MyRobolectricTestRunner() throws InitializationError { super(FakeTest.class); } @Override protected DependencyResolver getJarResolver() { return super.getJarResolver(); } public static class FakeTest { @Test public void fakeTest() { } } } }