use of org.robolectric.util.TempDirectory in project robolectric by robolectric.
the class AndroidTestEnvironment method setUpApplicationState.
@Override
public void setUpApplicationState(Method method, Configuration configuration, AndroidManifest appManifest) {
for (TestEnvironmentLifecyclePlugin e : testEnvironmentLifecyclePlugins) {
e.onSetupApplicationState();
}
Config config = configuration.get(Config.class);
ConfigurationRegistry.instance = new ConfigurationRegistry(configuration.map());
clearEnvironment();
RuntimeEnvironment.setTempDirectory(new TempDirectory(createTestDataDirRootPath(method)));
if (ShadowLooper.looperMode() == LooperMode.Mode.LEGACY) {
RuntimeEnvironment.setMasterScheduler(new Scheduler());
RuntimeEnvironment.setMainThread(Thread.currentThread());
ShadowLegacyLooper.internalInitializeBackgroundThreadScheduler();
}
if (!loggingInitialized) {
ShadowLog.setupLogging();
loggingInitialized = true;
}
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
android.content.res.Configuration androidConfiguration = new android.content.res.Configuration();
DisplayMetrics displayMetrics = new DisplayMetrics();
Bootstrap.applyQualifiers(config.qualifiers(), apiLevel, androidConfiguration, displayMetrics);
Locale locale = apiLevel >= VERSION_CODES.N ? androidConfiguration.getLocales().get(0) : androidConfiguration.locale;
Locale.setDefault(locale);
// Looper needs to be prepared before the activity thread is created
if (Looper.myLooper() == null) {
Looper.prepareMainLooper();
}
if (ShadowLooper.looperMode() == LooperMode.Mode.LEGACY) {
ShadowLooper.getShadowMainLooper().resetScheduler();
} else {
RuntimeEnvironment.setMasterScheduler(new LooperDelegatingScheduler(Looper.getMainLooper()));
}
preloadClasses(apiLevel);
RuntimeEnvironment.setAndroidFrameworkJarPath(sdkJarPath);
Bootstrap.setDisplayConfiguration(androidConfiguration, displayMetrics);
RuntimeEnvironment.setActivityThread(ReflectionHelpers.newInstance(ActivityThread.class));
ReflectionHelpers.setStaticField(ActivityThread.class, "sMainThreadHandler", new Handler(Looper.myLooper()));
Instrumentation instrumentation = createInstrumentation();
InstrumentationRegistry.registerInstance(instrumentation, new Bundle());
Supplier<Application> applicationSupplier = createApplicationSupplier(appManifest, config, androidConfiguration, displayMetrics);
RuntimeEnvironment.setApplicationSupplier(applicationSupplier);
if (configuration.get(LazyLoad.class) == LazyLoad.ON) {
RuntimeEnvironment.setConfiguredApplicationClass(getApplicationClass(appManifest, config, new ApplicationInfo()));
} else {
// force eager load of the application
RuntimeEnvironment.getApplication();
}
}
use of org.robolectric.util.TempDirectory in project robolectric by robolectric.
the class LegacyDependencyResolverTest method setUp.
@Before
public void setUp() throws Exception {
tempDirectory = new TempDirectory();
properties = new Properties();
mockClassLoader = mock(DefinitelyNotAClassLoader.class);
}
use of org.robolectric.util.TempDirectory in project robolectric by robolectric.
the class ShadowSharedMemory method nCreate.
@Implementation
protected static FileDescriptor nCreate(String name, int size) throws ErrnoException {
maybeThrow(fakeCreateException);
TempDirectory tempDirectory = RuntimeEnvironment.getTempDirectory();
try {
// Give each instance its own private file.
File sharedMemoryFile = Files.createTempFile(tempDirectory.createIfNotExists("SharedMemory"), "shmem-" + name, ".tmp").toFile();
RandomAccessFile randomAccessFile = new RandomAccessFile(sharedMemoryFile, "rw");
randomAccessFile.setLength(0);
randomAccessFile.setLength(size);
FileDescriptor fileDescriptor = randomAccessFile.getFD();
int fd = ReflectionHelpers.getField(fileDescriptor, "fd");
filesByFd.put(fd, sharedMemoryFile);
return fileDescriptor;
} catch (IOException e) {
throw new RuntimeException("Unable to create file descriptior", e);
}
}
Aggregations