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

Commit 121d051b authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Set package name via the build file, not via Config.

Flag: EXEMPT host test change only
Bug: 377765941
Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh -t
Change-Id: Ied81b7c246a25ac376db93b2cd14b4d449359ca2
parent 34aa8a61
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import static com.android.ravenwood.common.RavenwoodCommonUtils.RAVENWOOD_INST_R
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 com.android.ravenwood.common.RavenwoodCommonUtils.parseNullableInt;
import static com.android.ravenwood.common.RavenwoodCommonUtils.withDefault;

import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
@@ -39,6 +41,7 @@ import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.os.Binder;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Looper;
@@ -154,6 +157,13 @@ public class RavenwoodRuntimeEnvironmentController {
    private static RavenwoodAwareTestRunner sRunner;
    private static RavenwoodSystemProperties sProps;

    private static final int DEFAULT_TARGET_SDK_LEVEL = VERSION_CODES.CUR_DEVELOPMENT;
    private static final String DEFAULT_PACKAGE_NAME = "com.android.ravenwoodtests.defaultname";

    private static int sTargetSdkLevel;
    private static String sTestPackageName;
    private static String sTargetPackageName;

    /**
     * Initialize the global environment.
     */
@@ -235,9 +245,22 @@ public class RavenwoodRuntimeEnvironmentController {
        System.setProperty("android.junit.runner",
                "androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner");

        loadRavenwoodProperties();

        assertMockitoVersion();
    }

    private static void loadRavenwoodProperties() {
        var props = RavenwoodSystemProperties.readProperties("ravenwood.properties");

        sTargetSdkLevel = withDefault(
                parseNullableInt(props.get("targetSdkVersionInt")), DEFAULT_TARGET_SDK_LEVEL);
        sTargetPackageName = withDefault(props.get("packageName"), DEFAULT_PACKAGE_NAME);
        sTestPackageName = withDefault(props.get("instPackageName"), sTargetPackageName);

        // TODO(b/377765941) Read them from the manifest too?
    }

    /**
     * Initialize the environment.
     */
@@ -267,6 +290,14 @@ public class RavenwoodRuntimeEnvironmentController {
            Thread.setDefaultUncaughtExceptionHandler(sUncaughtExceptionHandler);
        }

        config.mTargetPackageName = sTargetPackageName;
        config.mTestPackageName = sTestPackageName;
        config.mTargetSdkLevel = sTargetSdkLevel;

        Log.i(TAG, "TargetPackageName=" + sTargetPackageName);
        Log.i(TAG, "TestPackageName=" + sTestPackageName);
        Log.i(TAG, "TargetSdkLevel=" + sTargetSdkLevel);

        RavenwoodRuntimeState.sUid = config.mUid;
        RavenwoodRuntimeState.sPid = config.mPid;
        RavenwoodRuntimeState.sTargetSdkLevel = config.mTargetSdkLevel;
+10 −23
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.Instrumentation;
import android.content.Context;
import android.os.Build;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -30,16 +29,12 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Represents how to configure the ravenwood environment for a test class.
 *
 * If a ravenwood test class has a public static field with the {@link Config} annotation,
 * Ravenwood will extract the config from it and initializes the environment. The type of the
 * field must be of {@link RavenwoodConfig}.
 * @deprecated This class will be removed. Reach out to g/ravenwood if you need any features in it.
 */
@Deprecated
public final class RavenwoodConfig {
    /**
     * Use this to mark a field as the configuration.
@@ -66,7 +61,7 @@ public final class RavenwoodConfig {
    String mTestPackageName;
    String mTargetPackageName;

    int mTargetSdkLevel = Build.VERSION_CODES.CUR_DEVELOPMENT;
    int mTargetSdkLevel;

    final RavenwoodSystemProperties mSystemProperties = new RavenwoodSystemProperties();

@@ -91,12 +86,6 @@ public final class RavenwoodConfig {
        return RavenwoodRule.isOnRavenwood();
    }

    private void setDefaults() {
        if (mTargetPackageName == null) {
            mTargetPackageName = mTestPackageName;
        }
    }

    public static class Builder {
        private final RavenwoodConfig mConfig = new RavenwoodConfig();

@@ -120,28 +109,27 @@ public final class RavenwoodConfig {
        }

        /**
         * Configure the package name of the test, which corresponds to
         * {@link Instrumentation#getContext()}.
         * @deprecated no longer used. Package name is set in the build file. (for now)
         */
        @Deprecated
        public Builder setPackageName(@NonNull String packageName) {
            mConfig.mTestPackageName = Objects.requireNonNull(packageName);
            return this;
        }

        /**
         * Configure the package name of the target app, which corresponds to
         * {@link Instrumentation#getTargetContext()}. Defaults to {@link #setPackageName}.
         * @deprecated no longer used. Package name is set in the build file. (for now)
         */
        @Deprecated
        public Builder setTargetPackageName(@NonNull String packageName) {
            mConfig.mTargetPackageName = Objects.requireNonNull(packageName);
            return this;
        }


        /**
         * Configure the target SDK level of the test.
         * @deprecated no longer used. Target SDK level is set in the build file. (for now)
         */
        @Deprecated
        public Builder setTargetSdkLevel(int sdkLevel) {
            mConfig.mTargetSdkLevel = sdkLevel;
            return this;
        }

@@ -205,7 +193,6 @@ public final class RavenwoodConfig {
        }

        public RavenwoodConfig build() {
            mConfig.setDefaults();
            return mConfig;
        }
    }
+4 −7
Original line number Diff line number Diff line
@@ -36,10 +36,8 @@ import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @deprecated Use {@link RavenwoodConfig} to configure the ravenwood environment instead.
 * A {@link RavenwoodRule} is no longer needed for {@link DisabledOnRavenwood}. To get the
 * {@link Context} and {@link Instrumentation}, use
 * {@link androidx.test.platform.app.InstrumentationRegistry} instead.
 * @deprecated This class is undergoing a major change. Reach out to g/ravenwood if you need
 * any featues in it.
 */
@Deprecated
public final class RavenwoodRule implements TestRule {
@@ -128,11 +126,10 @@ public final class RavenwoodRule implements TestRule {
        }

        /**
         * Configure the identity of this process to be the given package name for the duration
         * of the test. Has no effect on non-Ravenwood environments.
         * @deprecated no longer used.
         */
        @Deprecated
        public Builder setPackageName(@NonNull String packageName) {
            mBuilder.setPackageName(packageName);
            return this;
        }

+2 −2
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ public class RavenwoodSystemProperties {
            "vendor_dlkm",
    };

    private static Map<String, String> readProperties(String propFile) {
    static Map<String, String> readProperties(String propFile) {
        // Use an ordered map just for cleaner dump log.
        final Map<String, String> ret = new LinkedHashMap<>();
        try {
@@ -60,7 +60,7 @@ public class RavenwoodSystemProperties {
                    .map(String::trim)
                    .filter(s -> !s.startsWith("#"))
                    .map(s -> s.split("\\s*=\\s*", 2))
                    .filter(a -> a.length == 2)
                    .filter(a -> a.length == 2 && a[1].length() > 0)
                    .forEach(a -> ret.put(a[0], a[1]));
        } catch (IOException e) {
            throw new RuntimeException(e);
+17 −0
Original line number Diff line number Diff line
@@ -284,4 +284,21 @@ public class RavenwoodCommonUtils {
        th.printStackTrace(writer);
        return stringWriter.toString();
    }

    /** Same as {@link Integer#parseInt(String)} but accepts null and returns null. */
    @Nullable
    public static Integer parseNullableInt(@Nullable String value) {
        if (value == null) {
            return null;
        }
        return Integer.parseInt(value);
    }

    /**
     * @return {@code value} if it's non-null. Otherwise, returns {@code def}.
     */
    @Nullable
    public static <T> T withDefault(@Nullable T value, @Nullable T def) {
        return value != null ? value : def;
    }
}
Loading