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

Commit 41cfbb3f authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Build `ravenwood-junit` against `test_current`.

Some CTS tests desire to link against `test_current` to prove to
themselves that they're not touching hidden APIs.  This means if they
also want to use our `ravenwood-junit` rules, that dependency also
needs to link against `test_current`.

Since we're reaching down into plenty of hidden details when we're
actually running on a Ravenwood runtime, we split into a `stub` and
an `impl` library, so that we give a cleanly compiling library to
to clients, which is then replaced under the Ravenwood runtime.

Bug: 292141694
Test: atest-dev CtsTextTestCasesRavenwood CtsTextTestCases
Change-Id: I96dbc9643324a7853a992e97b51a88f70eb721f3
parent 36b2f512
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ android_ravenwood_libgroup {
        "hoststubgen-helper-framework-runtime.ravenwood",
        "junit",
        "truth",
        "ravenwood-junit",
        "ravenwood-junit-impl",
        "android.test.mock",
    ],
}
+22 −2
Original line number Diff line number Diff line
@@ -25,12 +25,32 @@ java_library {
}

java_library {
    name: "ravenwood-junit",
    srcs: ["junit-src/**/*.java"],
    name: "ravenwood-junit-impl",
    srcs: [
        "junit-src/**/*.java",
        "junit-impl-src/**/*.java",
    ],
    libs: [
        "framework-minus-apex.ravenwood",
        "junit",
    ],
    visibility: ["//frameworks/base"],
}

// Carefully compiles against only test_current to support tests that
// want to verify they're unbundled.  The "impl" library above is what
// ships inside the Ravenwood environment to actually drive any API
// access to implementation details.
java_library {
    name: "ravenwood-junit",
    srcs: [
        "junit-src/**/*.java",
        "junit-stub-src/**/*.java",
    ],
    sdk_version: "test_current",
    libs: [
        "junit",
    ],
    visibility: ["//visibility:public"],
}

+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.platform.test.ravenwood;

public class RavenwoodRuleImpl {
    public static void init(RavenwoodRule rule) {
        android.os.Process.init$ravenwood(rule.mUid, rule.mPid);
        android.os.Binder.init$ravenwood();
    }

    public static void reset(RavenwoodRule rule) {
        android.os.Process.reset$ravenwood();
        android.os.Binder.reset$ravenwood();
    }
}
+10 −17
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package android.platform.test.ravenwood;

import android.os.Process;
import android.platform.test.annotations.IgnoreUnderRavenwood;

import org.junit.Assume;
@@ -35,12 +34,16 @@ import java.util.concurrent.atomic.AtomicInteger;
public class RavenwoodRule implements TestRule {
    private static AtomicInteger sNextPid = new AtomicInteger(100);

    private static final int SYSTEM_UID = 1000;
    private static final int NOBODY_UID = 9999;
    private static final int FIRST_APPLICATION_UID = 10000;

    /**
     * Unless the test author requests differently, run as "nobody", and give each collection of
     * tests its own unique PID.
     */
    private int mUid = android.os.Process.NOBODY_UID;
    private int mPid = sNextPid.getAndIncrement();
    int mUid = NOBODY_UID;
    int mPid = sNextPid.getAndIncrement();

    public RavenwoodRule() {
    }
@@ -56,7 +59,7 @@ public class RavenwoodRule implements TestRule {
         * test. Has no effect under non-Ravenwood environments.
         */
        public Builder setProcessSystem() {
            mRule.mUid = android.os.Process.SYSTEM_UID;
            mRule.mUid = SYSTEM_UID;
            return this;
        }

@@ -65,7 +68,7 @@ public class RavenwoodRule implements TestRule {
         * test. Has no effect under non-Ravenwood environments.
         */
        public Builder setProcessApp() {
            mRule.mUid = android.os.Process.FIRST_APPLICATION_UID;
            mRule.mUid = FIRST_APPLICATION_UID;
            return this;
        }

@@ -82,16 +85,6 @@ public class RavenwoodRule implements TestRule {
        return System.getProperty("java.class.path").contains("ravenwood");
    }

    private void init() {
        android.os.Process.init$ravenwood(mUid, mPid);
        android.os.Binder.init$ravenwood();
    }

    private void reset() {
        android.os.Process.reset$ravenwood();
        android.os.Binder.reset$ravenwood();
    }

    @Override
    public Statement apply(Statement base, Description description) {
        return new Statement() {
@@ -102,13 +95,13 @@ public class RavenwoodRule implements TestRule {
                    Assume.assumeFalse(isUnderRavenwood);
                }
                if (isUnderRavenwood) {
                    init();
                    RavenwoodRuleImpl.init(RavenwoodRule.this);
                }
                try {
                    base.evaluate();
                } finally {
                    if (isUnderRavenwood) {
                        reset();
                        RavenwoodRuleImpl.reset(RavenwoodRule.this);
                    }
                }
            }
+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.platform.test.ravenwood;

public class RavenwoodRuleImpl {
    public static void init(RavenwoodRule rule) {
        // Must be provided by impl to reference runtime internals
        throw new UnsupportedOperationException();
    }

    public static void reset(RavenwoodRule rule) {
        // Must be provided by impl to reference runtime internals
        throw new UnsupportedOperationException();
    }
}