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

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

Merge "Handle failures from Before/AfterClass in the runner" into main

parents 0dea5a3d d6632909
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -94,6 +94,9 @@ java_library {
    libs: [
        "ravenwood-runtime-common-ravenwood",
    ],
    static_libs: [
        "framework-annotations-lib", // should it be "libs" instead?
    ],
    visibility: ["//visibility:private"],
}

+70 −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.listenertests;

import static android.platform.test.ravenwood.RavenwoodRule.isOnRavenwood;

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

import java.util.ArrayList;
import java.util.List;

import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;

/**
 * Test that throws from @AfterClass.
 *
 * Tradefed would ignore it, so instead RavenwoodAwareTestRunner would detect it and kill
 * the self (test) process.
 *
 * Unfortunately, this behavior can't easily be tested from within this class, so for now
 * it's only used for a manual test, which you can run by removing the @Ignore.
 *
 * TODO(b/364948126) Improve the tests and automate it.
 */
@Ignore
@RunWith(ParameterizedAndroidJunit4.class)
public class RavenwoodAfterClassFailureTest {
    public RavenwoodAfterClassFailureTest(String param) {
    }

    @AfterClass
    public static void afterClass() {
        if (!isOnRavenwood()) return; // Don't do anything on real device.

        throw new RuntimeException("FAILURE");
    }

    @Parameters
    public static List<String> getParams() {
        var params =  new ArrayList<String>();
        params.add("foo");
        params.add("bar");
        return params;
    }

    @Test
    public void test1() {
    }

    @Test
    public void test2() {
    }
}
+66 −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.listenertests;

import static android.platform.test.ravenwood.RavenwoodRule.isOnRavenwood;

import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;

import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;

/**
 * Test that fails in assumption in @BeforeClass.
 *
 * This is only used for manual tests. Make sure `atest` shows 4 test results with
 * "ASSUMPTION_FAILED".
 *
 * TODO(b/364948126) Improve the tests and automate it.
 */
@RunWith(ParameterizedAndroidJunit4.class)
public class RavenwoodBeforeClassAssumptionFailureTest {
    public RavenwoodBeforeClassAssumptionFailureTest(String param) {
    }

    @BeforeClass
    public static void beforeClass() {
        if (!isOnRavenwood()) return; // Don't do anything on real device.

        Assume.assumeTrue(false);
    }

    @Parameters
    public static List<String> getParams() {
        var params =  new ArrayList<String>();
        params.add("foo");
        params.add("bar");
        return params;
    }

    @Test
    public void test1() {
    }

    @Test
    public void test2() {
    }
}
+71 −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.listenertests;

import static android.platform.test.ravenwood.RavenwoodRule.isOnRavenwood;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;

import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;

/**
 * Test that fails throws from @BeforeClass.
 *
 * This is only used for manual tests. Make sure `atest` shows 4 test results with
 * a "FAILURE" runtime exception.
 *
 * In order to run the test, you'll need to remove the @Ignore.
 *
 * TODO(b/364948126) Improve the tests and automate it.
 */
@Ignore
@RunWith(ParameterizedAndroidJunit4.class)
public class RavenwoodBeforeClassFailureTest {
    public static final String TAG = "RavenwoodBeforeClassFailureTest";

    public RavenwoodBeforeClassFailureTest(String param) {
    }

    @BeforeClass
    public static void beforeClass() {
        if (!isOnRavenwood()) return; // Don't do anything on real device.

        throw new RuntimeException("FAILURE");
    }

    @Parameters
    public static List<String> getParams() {
        var params =  new ArrayList<String>();
        params.add("foo");
        params.add("bar");
        return params;
    }

    @Test
    public void test1() {
    }

    @Test
    public void test2() {
    }
}
+78 −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.listenertests;

import static android.platform.test.ravenwood.RavenwoodRule.isOnRavenwood;

import static org.junit.Assume.assumeTrue;

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runners.model.Statement;

import java.util.ArrayList;
import java.util.List;

import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;

/**
 * Test that fails in assumption from a class rule.
 *
 * This is only used for manual tests. Make sure `atest` shows 4 test results with
 * "ASSUMPTION_FAILED".
 *
 * TODO(b/364948126) Improve the tests and automate it.
 */
@RunWith(ParameterizedAndroidJunit4.class)
public class RavenwoodClassRuleAssumptionFailureTest {
    public static final String TAG = "RavenwoodClassRuleFailureTest";

    @ClassRule
    public static final TestRule sClassRule = new TestRule() {
        @Override
        public Statement apply(Statement base, Description description) {
            if (!isOnRavenwood()) {
                return base; // Just run the test as-is on a real device.
            }

            assumeTrue(false);
            return null; // unreachable
        }
    };

    public RavenwoodClassRuleAssumptionFailureTest(String param) {
    }

    @Parameters
    public static List<String> getParams() {
        var params =  new ArrayList<String>();
        params.add("foo");
        params.add("bar");
        return params;
    }

    @Test
    public void test1() {
    }

    @Test
    public void test2() {
    }
}
Loading