diff options
author | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2019-12-19 23:48:38 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2019-12-19 23:48:38 +0000 |
commit | c4b517ebe5d22e9e7eb5d1d306cb7378313c62d7 (patch) | |
tree | 9a8d4aa0793309c075c0833e9c21cfd8a48adb96 | |
parent | 3d75632a1ac220d4bb39fe7ad97bcb6226a09d48 (diff) | |
parent | ac82a7cfac9f8483e41c63cfd8ab802cbc882e65 (diff) | |
download | platform_testing-c4b517ebe5d22e9e7eb5d1d306cb7378313c62d7.tar.gz |
Merge "Enabled custom runner selection in HealthRunnerBuilder." into qt-dev am: 4be1f65ea3 am: ac82a7cfac
Change-Id: Id7a2181e6cd360f851353165a0f554002cf824cb
9 files changed, 287 insertions, 70 deletions
diff --git a/libraries/health/runners/longevity/platform/src/android/platform/test/longevity/HealthRunnerBuilder.java b/libraries/health/runners/longevity/platform/src/android/platform/test/longevity/HealthRunnerBuilder.java deleted file mode 100644 index b6e2fdb6c..000000000 --- a/libraries/health/runners/longevity/platform/src/android/platform/test/longevity/HealthRunnerBuilder.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2019 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.longevity; - -import org.junit.runner.Runner; -import org.junit.runners.model.RunnerBuilder; - -/** A {@link RunnerBuilder} that uses {@link LongevityClassRunner} to run tests in a suite. */ -public class HealthRunnerBuilder extends RunnerBuilder { - @Override - public Runner runnerForClass(Class<?> testClass) throws Throwable { - return new LongevityClassRunner(testClass); - } -} diff --git a/libraries/health/runners/longevity/platform/tests/src/android/platform/test/longevity/HealthRunnerBuilderTest.java b/libraries/health/runners/longevity/platform/tests/src/android/platform/test/longevity/HealthRunnerBuilderTest.java deleted file mode 100644 index c83348f19..000000000 --- a/libraries/health/runners/longevity/platform/tests/src/android/platform/test/longevity/HealthRunnerBuilderTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2019 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.longevity; - -import static org.junit.Assert.assertTrue; - -import org.junit.Test; -import org.junit.runner.Runner; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link HealthRunnerBuilder}. */ -@RunWith(JUnit4.class) -public class HealthRunnerBuilderTest { - // A dummy test class. - @RunWith(JUnit4.class) - public static class SampleTest { - @Test - public void testMethod() { - // No-op so passes. - } - } - - /** Test that the runner builder returns a {@link LongevityClassRunner}. */ - @Test - public void testUsesLongevityRunner() throws Throwable { - Runner runner = (new HealthRunnerBuilder()).runnerForClass(SampleTest.class); - assertTrue(runner instanceof LongevityClassRunner); - } -} diff --git a/libraries/health/utils/Android.bp b/libraries/health/utils/Android.bp new file mode 100644 index 000000000..cf532fe49 --- /dev/null +++ b/libraries/health/utils/Android.bp @@ -0,0 +1,24 @@ +// Copyright (C) 2019 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. + +java_library_static { + name: "health-testing-utils", + srcs: [ + "src/**/*.java", + ], + libs: [ + "androidx.test.runner", + ], + min_sdk_version: "26", +} diff --git a/libraries/health/utils/OWNERS b/libraries/health/utils/OWNERS new file mode 100644 index 000000000..44741843a --- /dev/null +++ b/libraries/health/utils/OWNERS @@ -0,0 +1,12 @@ +# Use this reviewer by default. +android-crystalball-eng+reviews@google.com + +# If direct review needed. +gelanchezhian@google.com +harrytczhang@google.com +mrosenfeld@google.com + +# Don't send reviews here. +guangzhu@google.com +jdesprez@google.com +jwg@google.com diff --git a/libraries/health/utils/src/android/platform/test/util/HealthRunnerBuilder.java b/libraries/health/utils/src/android/platform/test/util/HealthRunnerBuilder.java new file mode 100644 index 000000000..76a23c460 --- /dev/null +++ b/libraries/health/utils/src/android/platform/test/util/HealthRunnerBuilder.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2019 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.util; + +import android.os.Bundle; +import androidx.annotation.VisibleForTesting; +import androidx.test.InstrumentationRegistry; + +import org.junit.runners.JUnit4; +import org.junit.runner.Runner; +import org.junit.runners.model.RunnerBuilder; + +import java.lang.reflect.Constructor; + +/** + * A {@link RunnerBuilder} that enables using a specified runner to run tests in a suite. + * + * <p>Usage: When instrumenting with AndroidJUnitRunner, use + * + * <pre>-e runnerBuilder android.platform.test.longevity.HealthRunnerBuilder -e use-runner + * (name_of_runner) + * + * <p>The runner name can be supplied with the fully qualified class name of the runner. It also has + * to be a {@link Runner} class and statically included in the APK. + */ +public class HealthRunnerBuilder extends RunnerBuilder { + @VisibleForTesting static final String RUNNER_OPTION = "use-runner"; + // The runner class to use as specified by the option. Defaults to the JUnit4 runner. + private Class<?> mRunnerClass = JUnit4.class; + + public HealthRunnerBuilder() { + this(InstrumentationRegistry.getArguments()); + } + + @VisibleForTesting + HealthRunnerBuilder(Bundle args) { + String runnerName = args.getString(RUNNER_OPTION); + if (runnerName != null) { + Class<?> loadedClass = null; + try { + loadedClass = HealthRunnerBuilder.class.getClassLoader().loadClass(runnerName); + } catch (ClassNotFoundException e) { + throw new RuntimeException( + String.format( + "Could not find class with fully qualified name %s.", runnerName)); + } + // Ensure that the class found is a Runner. + if (loadedClass != null && Runner.class.isAssignableFrom(loadedClass)) { + mRunnerClass = loadedClass; + } else { + throw new RuntimeException(String.format("Class %s is not a runner.", loadedClass)); + } + } + } + + @Override + public Runner runnerForClass(Class<?> testClass) throws Throwable { + try { + Constructor<?> runnerConstructor = mRunnerClass.getConstructor(Class.class); + // Cast is safe as getRunnerClass() ensures that instantiations of its return type are + // subclasses of Runner. + return (Runner) runnerConstructor.newInstance(testClass); + } catch (NoSuchMethodException e) { + throw new RuntimeException( + String.format( + "Runner %s cannot be instantiated with the test class alone.", + mRunnerClass), + e); + } catch (SecurityException e) { + throw new RuntimeException(e); + } + } +} diff --git a/libraries/health/utils/tests/Android.bp b/libraries/health/utils/tests/Android.bp new file mode 100644 index 000000000..7b25a2cd4 --- /dev/null +++ b/libraries/health/utils/tests/Android.bp @@ -0,0 +1,27 @@ +// +// Copyright (C) 2019 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. +// + +android_test { + name: "HealthTestingUtilsTests", + + min_sdk_version: "26", + static_libs: [ + "androidx.test.runner", + "health-testing-utils", + ], + srcs: ["src/**/*.java"], + test_suites: ["device-tests"], +} diff --git a/libraries/health/utils/tests/AndroidManifest.xml b/libraries/health/utils/tests/AndroidManifest.xml new file mode 100644 index 000000000..c84f996d6 --- /dev/null +++ b/libraries/health/utils/tests/AndroidManifest.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2019 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. +--> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="android.platform.test.util.tests"> + <uses-sdk android:minSdkVersion="24" android:targetSdkVersion="24" /> + <application> + <uses-library android:name="android.test.runner"/> + </application> + <instrumentation + android:name="androidx.test.runner.AndroidJUnitRunner" + android:targetPackage="android.platform.test.util.tests" + android:label="Health Testing Utils Tests" /> +</manifest> diff --git a/libraries/health/utils/tests/AndroidTest.xml b/libraries/health/utils/tests/AndroidTest.xml new file mode 100644 index 000000000..8d92ce59c --- /dev/null +++ b/libraries/health/utils/tests/AndroidTest.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2019 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. +--> +<configuration description="Config for health testing utils test cases"> + <option name="test-suite-tag" value="apct" /> + <option name="test-suite-tag" value="apct-instrumentation" /> + <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller"> + <option name="cleanup-apks" value="true" /> + <option name="test-file-name" value="HealthTestingUtilsTests.apk" /> + </target_preparer> + <test class="com.android.tradefed.testtype.AndroidJUnitTest" > + <option name="package" value="android.platform.test.util.tests" /> + <option name="runtime-hint" value="1m30s" /> + </test> +</configuration> diff --git a/libraries/health/utils/tests/src/android/platform/test/util/HealthRunnerBuilderTest.java b/libraries/health/utils/tests/src/android/platform/test/util/HealthRunnerBuilderTest.java new file mode 100644 index 000000000..144dd3984 --- /dev/null +++ b/libraries/health/utils/tests/src/android/platform/test/util/HealthRunnerBuilderTest.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2019 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.util; + +import static org.junit.Assert.assertTrue; + +import android.os.Bundle; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.Runner; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.JUnit4; +import org.junit.runners.model.InitializationError; + +/** Unit tests for {@link HealthRunnerBuilder}. */ +@RunWith(JUnit4.class) +public class HealthRunnerBuilderTest { + // A dummy test class. + @RunWith(JUnit4.class) + public static class SampleTest { + @Test + public void testMethod() { + // No-op so passes. + } + } + + // A dummy runner class. + public static class SampleRunner extends BlockJUnit4ClassRunner { + public SampleRunner(Class<?> testClass) throws InitializationError { + super(testClass); + } + } + + @Rule public ExpectedException mExpectedException = ExpectedException.none(); + + /** Test that the runner builder returns a {@link JUnit4} runner by default. */ + @Test + public void testDefaultsToJUnit4() throws Throwable { + Runner runner = (new HealthRunnerBuilder(new Bundle())).runnerForClass(SampleTest.class); + assertTrue(runner instanceof JUnit4); + } + + /** Test that the runner builder returns the specified, valid runner. */ + @Test + public void testUsingSpecifiedRunner_valid() throws Throwable { + Bundle args = new Bundle(); + args.putString(HealthRunnerBuilder.RUNNER_OPTION, SampleRunner.class.getName()); + Runner runner = (new HealthRunnerBuilder(args)).runnerForClass(SampleTest.class); + assertTrue(runner instanceof SampleRunner); + } + + /** Test that the runner throws when the supplied argument is not a class. */ + @Test + public void testUsingSpecifiedRunner_invalid_notClass() throws Throwable { + mExpectedException.expectMessage("Could not find class"); + Bundle args = new Bundle(); + args.putString(HealthRunnerBuilder.RUNNER_OPTION, "notAClass"); + Runner runner = (new HealthRunnerBuilder(args)).runnerForClass(SampleTest.class); + } + + /** Test that the runner throws when the supplied argument is not a {@link Runner}. */ + @Test + public void testUsingSpecifiedRunner_invalid_notRunner() throws Throwable { + mExpectedException.expectMessage("not a runner"); + Bundle args = new Bundle(); + args.putString(HealthRunnerBuilder.RUNNER_OPTION, HealthRunnerBuilderTest.class.getName()); + Runner runner = (new HealthRunnerBuilder(args)).runnerForClass(SampleTest.class); + } +} |