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

Commit 89fac794 authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Introduce RavenwoodConfig

- Now RavenwoodRule is deprecated, and we use RavenwoodConfig
instead.

- If a test has a @RavenwoodConfig.Config field,
we automatically extract the configuration from it.
```
    @RavenwoodConfig.Config
    public static RavenwoodConfig sConfig =
            new RavenwoodConfig.Builder()
                    .build();
```

- RC doesn't provide getContext() / getInstrumentation(). Tests
will now use InstrumentationRegistry.

- Previously, with RavenwoodRule, the environment was initialized
and reset for each test method, but now we do so at the class level,
before the inner runner starts.

This makes each test less hermetic, but it's closer to how the real
test works.

- We can introduce a hermetic mode later, if needed, which would be
a RavenwoodConfig parameter.

- So far, "bad config tests" can't be done automatically. I'll work
on the tests as a follow-up.

Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh
Flag: EXEMPT host test change only
Bug: 356918135

Change-Id: I6727ef814e8b8c643e6c79f6d9fc8c7f749e6fea
parent 00f16abc
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -141,6 +141,10 @@
      "name": "RavenwoodBivalentTest",
      "host": true
    },
    {
      "name": "RavenwoodCoreTest",
      "host": true
    },
    {
      "name": "RavenwoodMinimumTest",
      "host": true
+50 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 * 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.
@@ -13,25 +13,38 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ravenwoodtest.bivalenttest;

package android.platform.test.ravenwood;
import static android.platform.test.ravenwood.RavenwoodConfig.isOnRavenwood;

import org.junit.runner.Description;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

public class RavenwoodRuleImpl {
    public static void init(RavenwoodRule rule) {
        // No-op when running on a real device
    }
import android.platform.test.ravenwood.RavenwoodConfig;

    public static void reset(RavenwoodRule rule) {
        // No-op when running on a real device
    }
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

    public static void logTestRunner(String label, Description description) {
        // No-op when running on a real device
    }
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Test to make sure the config field is used.
 */
@RunWith(AndroidJUnit4.class)
public class RavenwoodConfigTest {
    private static final String PACKAGE_NAME = "com.test";

    @RavenwoodConfig.Config
    public static RavenwoodConfig sConfig =
            new RavenwoodConfig.Builder()
                    .setPackageName(PACKAGE_NAME)
                    .build();

    public static long realCurrentTimeMillis() {
        return System.currentTimeMillis();
    @Test
    public void testConfig() {
        assumeTrue(isOnRavenwood());
        assertEquals(PACKAGE_NAME,
                InstrumentationRegistry.getInstrumentation().getContext().getPackageName());
    }
}
+57 −0
Original line number Diff line number Diff line
@@ -13,54 +13,45 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ravenwoodtest.bivalenttest.listenertests;
package com.android.ravenwoodtest.bivalenttest;

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

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

import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
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.
 * Make sure having multiple RavenwoodRule's is detected.
 * (But only when running on ravenwod. Otherwise it'll be ignored.)
 */
@RunWith(ParameterizedAndroidJunit4.class)
public class RavenwoodBeforeClassAssumptionFailureTest {
    public RavenwoodBeforeClassAssumptionFailureTest(String param) {
    }
@RunWith(AndroidJUnit4.class)
public class RavenwoodMultipleRuleTest {

    @BeforeClass
    public static void beforeClass() {
        if (!isOnRavenwood()) return; // Don't do anything on real device.
    @Rule(order = Integer.MIN_VALUE)
    public final ExpectedException mExpectedException = ExpectedException.none();

        Assume.assumeTrue(false);
    }
    @Rule
    public final RavenwoodRule mRavenwood1 = new RavenwoodRule();

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

    @Test
    public void test1() {
    public RavenwoodMultipleRuleTest() {
        // We can't call it within the test method because the exception happens before
        // calling the method, so set it up here.
        if (RavenwoodConfig.isOnRavenwood()) {
            mExpectedException.expectMessage("Multiple nesting RavenwoodRule");
        }
    }

    @Test
    public void test2() {
    public void testMultipleRulesNotAllowed() {
        Assume.assumeTrue(RavenwoodConfig.isOnRavenwood());
    }
}
+0 −70
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() {
    }
}
+0 −71
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() {
    }
}
Loading