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

Commit 6af4b17c authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Stop exposing Ravenwood internal implementation to public APIs" into main

parents 01b99d92 c1332897
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ package android.os;

import android.util.Log;

import com.android.ravenwood.common.RavenwoodCommonUtils;
import com.android.ravenwood.common.RavenwoodInternalUtils;

import java.io.File;
import java.util.Objects;
@@ -28,7 +28,7 @@ import java.util.Objects;
public class Environment_ravenwood {
    private static final String TAG = "Environment_ravenwood";

    private static final boolean VERBOSE = RavenwoodCommonUtils.RAVENWOOD_VERBOSE_LOGGING;
    private static final boolean VERBOSE = RavenwoodInternalUtils.RAVENWOOD_VERBOSE_LOGGING;

    private Environment_ravenwood() {
    }
+6 −8
Original line number Diff line number Diff line
@@ -81,12 +81,10 @@ filegroup {

// Common code that is shared across host and device.
java_library {
    name: "ravenwood-runtime-common",
    name: "ravenwood-common-utils",
    host_supported: true,
    sdk_version: "core_current",
    srcs: [
        "runtime-common-src/**/*.java",
    ],
    srcs: ["common-src/**/*.java"],
    static_libs: [
        "framework-annotations-lib", // should it be "libs" instead?
        "modules-utils-ravenwood",
@@ -103,7 +101,7 @@ java_library_host {
    ],
    libs: [
        "app-compat-annotations",
        "ravenwood-runtime-common",
        "ravenwood-common-utils",
    ],
    visibility: ["//visibility:private"],
}
@@ -129,7 +127,7 @@ java_library {
        "framework-configinfrastructure.ravenwood",
        "framework-minus-apex.ravenwood",
        "ravenwood-helper-libcore-runtime",
        "ravenwood-runtime-common",
        "ravenwood-common-utils",
    ],
    sdk_version: "core_current",
    visibility: ["//visibility:private"],
@@ -156,7 +154,7 @@ java_library {
    ],
    static_libs: [
        "androidx.test.monitor",
        "ravenwood-runtime-common",
        "ravenwood-common-utils",
    ],
    libs: [
        "android.test.mock.impl",
@@ -195,7 +193,7 @@ java_library {
    ],
    sdk_version: "module_current",
    static_libs: [
        "ravenwood-runtime-common",
        "ravenwood-common-utils",
    ],
    libs: [
        "flag-junit",
+8 −111
Original line number Diff line number Diff line
@@ -26,18 +26,16 @@ import java.io.FileInputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;

public class RavenwoodCommonUtils {
public class RavenwoodInternalUtils {
    public static final String TAG = "Ravenwood";

    private RavenwoodCommonUtils() {
    private RavenwoodInternalUtils() {
    }

    /**
@@ -48,19 +46,9 @@ public class RavenwoodCommonUtils {
    public static final boolean RAVENWOOD_VERBOSE_LOGGING = "1".equals(System.getenv(
            "RAVENWOOD_VERBOSE"));

    /**
     * Env var name for the runtime dir. When running a test locally, it'll contain
     * `/...(path).../out/host/linux-x86/testcases/ravenwood-runtime`.
     */
    private static final String RAVENWOOD_RUNTIME_DIR_ENV = "RAVENWOOD_RUNTIME_DIR";

    private static boolean sEnableExtraRuntimeCheck =
            "1".equals(System.getenv("RAVENWOOD_ENABLE_EXTRA_RUNTIME_CHECK"));

    private static final boolean IS_ON_RAVENWOOD = RavenwoodHelper.isRunningOnRavenwood();

    private static final String RAVENWOOD_RUNTIME_PATH = getRavenwoodRuntimePathInternal();

    public static final String RAVENWOOD_SYSPROP = "ro.is_on_ravenwood";

    public static final String RAVENWOOD_RESOURCE_APK = "ravenwood-res-apks/ravenwood-res.apk";
@@ -68,22 +56,13 @@ public class RavenwoodCommonUtils {
            "ravenwood-res-apks/ravenwood-inst-res.apk";

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

    /**
     * @return if we're running on Ravenwood.
     */
    public static boolean isOnRavenwood() {
        return IS_ON_RAVENWOOD;
    }

    /**
     * Throws if the runtime is not Ravenwood.
     */
    public static void ensureOnRavenwood() {
        if (!isOnRavenwood()) {
            throw new RavenwoodRuntimeException("This is only supposed to be used on Ravenwood");
        }
        return RavenwoodHelper.isRunningOnRavenwood();
    }

    /**
@@ -107,10 +86,10 @@ public class RavenwoodCommonUtils {

    /**
     * Internal implementation of
     * {@link android.platform.test.ravenwood.RavenwoodUtils#loadJniLibrary(String)}
     * {@link android.platform.test.ravenwood.RavenwoodRule#loadJniLibrary(String)}
     */
    public static void loadJniLibrary(String libname) {
        if (RavenwoodCommonUtils.isOnRavenwood()) {
        if (isOnRavenwood()) {
            System.load(getJniLibraryPath(libname));
        } else {
            System.loadLibrary(libname);
@@ -199,23 +178,7 @@ public class RavenwoodCommonUtils {
     * This method throws if called on the device side.
     */
    public static String getRavenwoodRuntimePath() {
        ensureOnRavenwood();
        return RAVENWOOD_RUNTIME_PATH;
    }

    private static String getRavenwoodRuntimePathInternal() {
        if (!isOnRavenwood()) {
            return null;
        }
        var dir = System.getenv(RAVENWOOD_RUNTIME_DIR_ENV);
        if (dir == null || dir.isEmpty()) {
            throw new IllegalStateException("$" + RAVENWOOD_RUNTIME_DIR_ENV + " not set");
        }
        if (!(new File(dir).isDirectory())) {
            throw new IllegalStateException("$" + RAVENWOOD_RUNTIME_DIR_ENV + " contains "
                    + dir + ", but it's not a directory");
        }
        return dir;
        return RavenwoodHelper.getRavenwoodRuntimePath();
    }

    /** Close an {@link AutoCloseable}. */
@@ -232,7 +195,7 @@ public class RavenwoodCommonUtils {
    /** Close a {@link FileDescriptor}. */
    public static void closeQuietly(FileDescriptor fd) {
        var is = new FileInputStream(fd);
        RavenwoodCommonUtils.closeQuietly(is);
        closeQuietly(is);
    }

    public static void ensureIsPublicVoidMethod(Method method, boolean isStatic) {
@@ -311,70 +274,4 @@ public class RavenwoodCommonUtils {
    public static <T> T withDefault(@Nullable T value, @Nullable T def) {
        return value != null ? value : def;
    }

    /**
     * Utility for calling a method with reflections. Used to call a method by name.
     * Note, this intentionally does _not_ support non-public methods, as we generally
     * shouldn't violate java visibility in ravenwood.
     *
     * @param <TTHIS> class owning the method.
     */
    public static class ReflectedMethod<TTHIS> {
        private final Class<TTHIS> mThisClass;
        private final Method mMethod;

        private ReflectedMethod(Class<TTHIS> thisClass, Method method) {
            mThisClass = thisClass;
            mMethod = method;
        }

        /** Factory method. */
        @SuppressWarnings("unchecked")
        public static <TTHIS> ReflectedMethod<TTHIS> reflectMethod(
                @NonNull Class<TTHIS> clazz, @NonNull String methodName,
                @NonNull Class<?>... argTypes) {
            try {
                return new ReflectedMethod(clazz, clazz.getMethod(methodName, argTypes));
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }

        /** Factory method. */
        @SuppressWarnings("unchecked")
        public static <TTHIS> ReflectedMethod<TTHIS> reflectMethod(
                @NonNull String className, @NonNull String methodName,
                @NonNull Class<?>... argTypes) {
            try {
                return reflectMethod((Class<TTHIS>) Class.forName(className), methodName, argTypes);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        /** Call the instance method */
        @SuppressWarnings("unchecked")
        public <RET> RET call(@NonNull TTHIS thisObject, @NonNull Object... args) {
            try {
                return (RET) mMethod.invoke(Objects.requireNonNull(thisObject), args);
            } catch (InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        /** Call the static method */
        @SuppressWarnings("unchecked")
        public <RET> RET callStatic(@NonNull Object... args) {
            try {
                return (RET) mMethod.invoke(null, args);
            } catch (InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /** Handy method to create an array */
    public static <T> T[] arr(@NonNull T... objects) {
        return objects;
    }
}
+5 −4
Original line number Diff line number Diff line
@@ -15,8 +15,8 @@
 */
package android.platform.test.ravenwood;

import static com.android.ravenwood.common.RavenwoodCommonUtils.RAVENWOOD_VERBOSE_LOGGING;
import static com.android.ravenwood.common.RavenwoodCommonUtils.ensureIsPublicVoidMethod;
import static com.android.ravenwood.common.RavenwoodInternalUtils.RAVENWOOD_VERBOSE_LOGGING;
import static com.android.ravenwood.common.RavenwoodInternalUtils.ensureIsPublicVoidMethod;

import static org.junit.Assume.assumeTrue;

@@ -26,7 +26,7 @@ import android.platform.test.annotations.RavenwoodTestRunnerInitializing;
import android.platform.test.annotations.internal.InnerRunner;
import android.util.Log;

import com.android.ravenwood.common.RavenwoodCommonUtils;
import com.android.ravenwood.common.RavenwoodInternalUtils;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
@@ -228,7 +228,8 @@ public final class RavenwoodAwareTestRunner extends RavenwoodAwareTestRunnerBase
            s.evaluate();
            onAfter(description, scope, order, null);
        } catch (Throwable t) {
            RavenwoodCommonUtils.runIgnoringException(() -> onAfter(description, scope, order, t));
            RavenwoodInternalUtils
                    .runIgnoringException(() -> onAfter(description, scope, order, t));
            throw t;
        }
    }
Loading