Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 77efd35f authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Hook up "instrumentation resources"

- Also rename "test context" to "inst context" because "test context"
is a bit confusing.

Bug: 339614874
Flag: EXEMPT host test change only
Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh
Change-Id: If924ac03bca36409c0a45121e5165389dc62b9bc
parent e1034f1a
Loading
Loading
Loading
Loading
+16 −19
Original line number Diff line number Diff line
@@ -16,12 +16,11 @@

package android.platform.test.ravenwood;

import static com.android.ravenwood.common.RavenwoodCommonUtils.RAVENWOOD_INST_RESOURCE_APK;
import static com.android.ravenwood.common.RavenwoodCommonUtils.RAVENWOOD_RESOURCE_APK;
import static com.android.ravenwood.common.RavenwoodCommonUtils.RAVENWOOD_VERBOSE_LOGGING;
import static com.android.ravenwood.common.RavenwoodCommonUtils.RAVENWOOD_VERSION_JAVA_SYSPROP;

import static org.junit.Assert.fail;

import android.app.ActivityManager;
import android.app.Instrumentation;
import android.app.ResourcesManager;
@@ -211,23 +210,21 @@ public class RavenwoodRuntimeEnvironmentController {
            var file = new File(RAVENWOOD_RESOURCE_APK);
            return config.mState.loadResources(file.exists() ? file : null);
        };
        // Set up test context's resources.

        // Set up test context's (== instrumentation context's) resources.
        // If the target package name == test package name, then we use the main resources.
        // Otherwise, we don't simulate loading resources from the test APK yet.
        // (we need to add `test_resource_apk` to `android_ravenwood_test`)
        final Supplier<Resources> testResourcesLoader;
        final Supplier<Resources> instResourcesLoader;
        if (isSelfInstrumenting) {
            testResourcesLoader = targetResourcesLoader;
            instResourcesLoader = targetResourcesLoader;
        } else {
            testResourcesLoader = () -> {
                fail("Cannot load resources from the test context (yet)."
                        + " Use target context's resources instead.");
                return null; // unreachable.
            instResourcesLoader = () -> {
                var file = new File(RAVENWOOD_INST_RESOURCE_APK);
                return config.mState.loadResources(file.exists() ? file : null);
            };
        }

        var testContext = new RavenwoodContext(
                config.mTestPackageName, main, testResourcesLoader);
        var instContext = new RavenwoodContext(
                config.mTestPackageName, main, instResourcesLoader);
        var targetContext = new RavenwoodContext(
                config.mTargetPackageName, main, targetResourcesLoader);

@@ -236,18 +233,18 @@ public class RavenwoodRuntimeEnvironmentController {
                config.mTargetPackageName, main, targetResourcesLoader);
        appContext.setApplicationContext(appContext);
        if (isSelfInstrumenting) {
            testContext.setApplicationContext(appContext);
            instContext.setApplicationContext(appContext);
            targetContext.setApplicationContext(appContext);
        } else {
            // When instrumenting into another APK, the test context doesn't have an app context.
            targetContext.setApplicationContext(appContext);
        }
        config.mTestContext = testContext;
        config.mInstContext = instContext;
        config.mTargetContext = targetContext;

        // Prepare other fields.
        config.mInstrumentation = new Instrumentation();
        config.mInstrumentation.basicInit(config.mTestContext, config.mTargetContext);
        config.mInstrumentation.basicInit(config.mInstContext, config.mTargetContext);
        InstrumentationRegistry.registerInstance(config.mInstrumentation, Bundle.EMPTY);

        RavenwoodSystemServer.init(config);
@@ -284,13 +281,13 @@ public class RavenwoodRuntimeEnvironmentController {

        InstrumentationRegistry.registerInstance(null, Bundle.EMPTY);
        config.mInstrumentation = null;
        if (config.mTestContext != null) {
            ((RavenwoodContext) config.mTestContext).cleanUp();
        if (config.mInstContext != null) {
            ((RavenwoodContext) config.mInstContext).cleanUp();
        }
        if (config.mTargetContext != null) {
            ((RavenwoodContext) config.mTargetContext).cleanUp();
        }
        config.mTestContext = null;
        config.mInstContext = null;
        config.mTargetContext = null;

        if (config.mProvideMainThread) {
+1 −1
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@ public class RavenwoodSystemServer {

        sStartedServices = new ArraySet<>();
        sTimings = new TimingsTraceAndSlog();
        sServiceManager = new SystemServiceManager(config.mTestContext);
        sServiceManager = new SystemServiceManager(config.mInstContext);
        sServiceManager.setStartInfo(false,
                SystemClock.elapsedRealtime(),
                SystemClock.uptimeMillis());
+1 −1
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ public final class RavenwoodConfig {

    final List<Class<?>> mServicesRequired = new ArrayList<>();

    volatile Context mTestContext;
    volatile Context mInstContext;
    volatile Context mTargetContext;
    volatile Instrumentation mInstrumentation;

+1 −1
Original line number Diff line number Diff line
@@ -216,7 +216,7 @@ public final class RavenwoodRule implements TestRule {
     */
    @Deprecated
    public Context getContext() {
        return Objects.requireNonNull(mConfiguration.mTestContext,
        return Objects.requireNonNull(mConfiguration.mInstContext,
                "Context is only available during @Test execution");
    }

+2 −0
Original line number Diff line number Diff line
@@ -63,6 +63,8 @@ public class RavenwoodCommonUtils {
    public static final String RAVENWOOD_SYSPROP = "ro.is_on_ravenwood";

    public static final String RAVENWOOD_RESOURCE_APK = "ravenwood-res-apks/ravenwood-res.apk";
    public static final String RAVENWOOD_INST_RESOURCE_APK =
            "ravenwood-res-apks/ravenwood-inst-res.apk";

    public static final String RAVENWOOD_EMPTY_RESOURCES_APK =
            RAVENWOOD_RUNTIME_PATH + "ravenwood-data/ravenwood-empty-res.apk";
Loading