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

Commit 29f70f3a authored by Makoto Onuki's avatar Makoto Onuki Committed by Android (Google) Code Review
Browse files

Merge "Move @DisabledOnRavenwood logic to the runner side" into main

parents a2d433e4 1d417245
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -160,6 +160,7 @@ java_library {
        "ravenwood-framework",
        "services.core.ravenwood",
        "junit",
        "framework-annotations-lib",
    ],
    sdk_version: "core_current",
    visibility: ["//frameworks/base"],
+4 −0
Original line number Diff line number Diff line
@@ -26,6 +26,10 @@ import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Test to ensure @DisabledOnRavenwood works. Note, now the DisabledOnRavenwood annotation
 * is handled by the test runner, so it won't really need the class rule.
 */
@RunWith(AndroidJUnit4.class)
@DisabledOnRavenwood
public class RavenwoodClassRuleDeviceOnlyTest {
+10 −2
Original line number Diff line number Diff line
@@ -34,8 +34,13 @@ public class RavenwoodImplicitClassRuleDeviceOnlyTest {

    @BeforeClass
    public static void beforeClass() {
        // This method shouldn't be called -- unless RUN_DISABLED_TESTS is enabled.

        // If we're doing RUN_DISABLED_TESTS, don't throw here, because that'd confuse junit.
        if (!RavenwoodRule.private$ravenwood().isRunningDisabledTests()) {
            Assert.assertFalse(RavenwoodRule.isOnRavenwood());
        }
    }

    @Test
    public void testDeviceOnly() {
@@ -46,7 +51,10 @@ public class RavenwoodImplicitClassRuleDeviceOnlyTest {
    public static void afterClass() {
        if (RavenwoodRule.isOnRavenwood()) {
            Log.e(TAG, "Even @AfterClass shouldn't be executed!");

            if (!RavenwoodRule.private$ravenwood().isRunningDisabledTests()) {
                System.exit(1);
            }
        }
    }
}
+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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 com.android.ravenwoodtest.bivalenttest.ravenizer;

import static org.junit.Assert.fail;

import android.platform.test.annotations.DisabledOnRavenwood;
import android.platform.test.ravenwood.RavenwoodAwareTestRunner.RavenwoodTestRunnerInitializing;
import android.platform.test.ravenwood.RavenwoodRule;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Test for "RAVENWOOD_RUN_DISABLED_TESTS" with "REALLY_DISABLED" set.
 *
 * This test is only executed on Ravenwood.
 */
@RunWith(AndroidJUnit4.class)
public class RavenwoodRunDisabledTestsReallyDisabledTest {
    private static final String TAG = "RavenwoodRunDisabledTestsTest";

    private static final CallTracker sCallTracker = new CallTracker();

    @RavenwoodTestRunnerInitializing
    public static void ravenwoodRunnerInitializing() {
        RavenwoodRule.private$ravenwood().overrideRunDisabledTest(true,
                "\\#testReallyDisabled$");
    }

    /**
     * This test gets to run with RAVENWOOD_RUN_DISABLED_TESTS set.
     */
    @Test
    @DisabledOnRavenwood
    public void testDisabledTestGetsToRun() {
        if (!RavenwoodRule.isOnRavenwood()) {
            return;
        }
        sCallTracker.incrementMethodCallCount();

        fail("This test won't pass on Ravenwood.");
    }

    /**
     * This will still not be executed due to the "really disabled" pattern.
     */
    @Test
    @DisabledOnRavenwood
    public void testReallyDisabled() {
        if (!RavenwoodRule.isOnRavenwood()) {
            return;
        }
        sCallTracker.incrementMethodCallCount();

        fail("This test won't pass on Ravenwood.");
    }

    @AfterClass
    public static void afterClass() {
        if (!RavenwoodRule.isOnRavenwood()) {
            return;
        }
        Log.i(TAG, "afterClass called");

        sCallTracker.assertCallsOrDie(
                "testDisabledTestGetsToRun", 1,
                "testReallyDisabled", 0
        );
    }
}
+87 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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 com.android.ravenwoodtest.bivalenttest.ravenizer;

import static org.junit.Assert.fail;

import android.platform.test.annotations.DisabledOnRavenwood;
import android.platform.test.ravenwood.RavenwoodAwareTestRunner.RavenwoodTestRunnerInitializing;
import android.platform.test.ravenwood.RavenwoodRule;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.AfterClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

/**
 * Test for "RAVENWOOD_RUN_DISABLED_TESTS". (with no "REALLY_DISABLED" set.)
 *
 * This test is only executed on Ravenwood.
 */
@RunWith(AndroidJUnit4.class)
public class RavenwoodRunDisabledTestsTest {
    private static final String TAG = "RavenwoodRunDisabledTestsTest";

    @Rule
    public ExpectedException mExpectedException = ExpectedException.none();

    private static final CallTracker sCallTracker = new CallTracker();

    @RavenwoodTestRunnerInitializing
    public static void ravenwoodRunnerInitializing() {
        RavenwoodRule.private$ravenwood().overrideRunDisabledTest(true, null);
    }

    @Test
    @DisabledOnRavenwood
    public void testDisabledTestGetsToRun() {
        if (!RavenwoodRule.isOnRavenwood()) {
            return;
        }
        sCallTracker.incrementMethodCallCount();

        fail("This test won't pass on Ravenwood.");
    }

    @Test
    @DisabledOnRavenwood
    public void testDisabledButPass() {
        if (!RavenwoodRule.isOnRavenwood()) {
            return;
        }
        sCallTracker.incrementMethodCallCount();

        // When a @DisabledOnRavenwood actually passed, the runner should make fail().
        mExpectedException.expectMessage("it actually passed under Ravenwood");
    }

    @AfterClass
    public static void afterClass() {
        if (!RavenwoodRule.isOnRavenwood()) {
            return;
        }
        Log.i(TAG, "afterClass called");

        sCallTracker.assertCallsOrDie(
                "testDisabledTestGetsToRun", 1,
                "testDisabledButPass", 1
        );
    }
}
Loading