diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-10-09 03:09:10 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-10-09 03:09:10 +0000 |
commit | 54d99857a5feac0164ee0dca50fdb872f68a8598 (patch) | |
tree | 9f2956fe766705b9adf8add71a49aee419fe3c12 | |
parent | dafe1005329d15cf201ae74674c772cfd75d1bfd (diff) | |
parent | 8fb119312e2bd0e6f09815b064f722eed7ba2888 (diff) | |
download | platform_testing-54d99857a5feac0164ee0dca50fdb872f68a8598.tar.gz |
Snap for 7808903 from 8fb119312e2bd0e6f09815b064f722eed7ba2888 to sc-qpr1-releaseandroid-12.0.0_r28android-12.0.0_r26android-12.0.0_r16android12-qpr1-release
Change-Id: I8632137965bbee431cd96eeb42d58242ab5ba586
2 files changed, 99 insertions, 21 deletions
diff --git a/libraries/health/runners/microbenchmark/src/android/platform/test/microbenchmark/Microbenchmark.java b/libraries/health/runners/microbenchmark/src/android/platform/test/microbenchmark/Microbenchmark.java index b6c6f134f..7ad17de98 100644 --- a/libraries/health/runners/microbenchmark/src/android/platform/test/microbenchmark/Microbenchmark.java +++ b/libraries/health/runners/microbenchmark/src/android/platform/test/microbenchmark/Microbenchmark.java @@ -75,8 +75,10 @@ public class Microbenchmark extends BlockJUnit4ClassRunner { @VisibleForTesting static final String MAX_BATTERY_DRAIN_OPTION = "max-battery-drain"; // Use these options to inject rules at runtime via the command line. For details, please see // documentation for DynamicRuleChain. - @VisibleForTesting static final String DYNAMIC_OUTER_RULES_OPTION = "outer-rules"; - @VisibleForTesting static final String DYNAMIC_INNER_RULES_OPTION = "inner-rules"; + @VisibleForTesting static final String DYNAMIC_OUTER_CLASS_RULES_OPTION = "outer-class-rules"; + @VisibleForTesting static final String DYNAMIC_INNER_CLASS_RULES_OPTION = "inner-class-rules"; + @VisibleForTesting static final String DYNAMIC_OUTER_TEST_RULES_OPTION = "outer-test-rules"; + @VisibleForTesting static final String DYNAMIC_INNER_TEST_RULES_OPTION = "inner-test-rules"; // Options for aligning with the battery charge (coulomb) counter for power tests. We want to // start microbenchmarks just after the coulomb counter has decremented to account for the @@ -283,9 +285,9 @@ public class Microbenchmark extends BlockJUnit4ClassRunner { Statement result = statement; List<TestRule> testRules = new ArrayList<>(); // Inner dynamic rules should be included first because RunRules applies rules inside-out. - testRules.add(new DynamicRuleChain(DYNAMIC_INNER_RULES_OPTION, mArguments)); + testRules.add(new DynamicRuleChain(DYNAMIC_INNER_TEST_RULES_OPTION, mArguments)); testRules.addAll(getTestRules(target)); - testRules.add(new DynamicRuleChain(DYNAMIC_OUTER_RULES_OPTION, mArguments)); + testRules.add(new DynamicRuleChain(DYNAMIC_OUTER_TEST_RULES_OPTION, mArguments)); // Apply legacy MethodRules, if they don't overlap with TestRules. for (org.junit.rules.MethodRule each : rules(target)) { if (!testRules.contains(each)) { @@ -297,6 +299,18 @@ public class Microbenchmark extends BlockJUnit4ClassRunner { return result; } + /** Add {@link DynamicRuleChain} to existing class rules. */ + @Override + protected List<TestRule> classRules() { + List<TestRule> classRules = new ArrayList<>(); + // Inner dynamic class rules should be included first because RunRules applies rules inside + // -out. + classRules.add(new DynamicRuleChain(DYNAMIC_INNER_CLASS_RULES_OPTION, mArguments)); + classRules.addAll(super.classRules()); + classRules.add(new DynamicRuleChain(DYNAMIC_OUTER_CLASS_RULES_OPTION, mArguments)); + return classRules; + } + /** * Combine the {@code #runChild}, {@code #methodBlock}, and final {@code #runLeaf} methods to * implement the specific {@code Microbenchmark} test behavior. In particular, (1) keep track of diff --git a/libraries/health/runners/microbenchmark/tests/src/android/platform/test/microbenchmark/MicrobenchmarkTest.java b/libraries/health/runners/microbenchmark/tests/src/android/platform/test/microbenchmark/MicrobenchmarkTest.java index 0b44d1aac..fa8e64a24 100644 --- a/libraries/health/runners/microbenchmark/tests/src/android/platform/test/microbenchmark/MicrobenchmarkTest.java +++ b/libraries/health/runners/microbenchmark/tests/src/android/platform/test/microbenchmark/MicrobenchmarkTest.java @@ -30,6 +30,7 @@ import android.platform.test.rule.TracePointRule; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestRule; @@ -418,53 +419,102 @@ public final class MicrobenchmarkTest { .inOrder(); } - /** - * Test successive iteration will be executed when the terminate on test fail option is - * disabled. - */ + /** Test dynamic test rule injection. */ @Test - public void testDynamicRuleInjection() throws InitializationError { + public void testDynamicTestRuleInjection() throws InitializationError { Bundle args = new Bundle(); args.putString("iterations", "2"); args.putString("rename-iterations", "false"); args.putString("terminate-on-test-fail", "false"); - args.putString(Microbenchmark.DYNAMIC_INNER_RULES_OPTION, LoggingRule1.class.getName()); - args.putString(Microbenchmark.DYNAMIC_OUTER_RULES_OPTION, LoggingRule2.class.getName()); + args.putString( + Microbenchmark.DYNAMIC_INNER_TEST_RULES_OPTION, LoggingRule1.class.getName()); + args.putString( + Microbenchmark.DYNAMIC_OUTER_TEST_RULES_OPTION, LoggingRule2.class.getName()); LoggingMicrobenchmark loggingRunner = - new LoggingMicrobenchmark(LoggingTestWithRule.class, args); + new LoggingMicrobenchmark(LoggingTestWithRules.class, args); loggingRunner.setOperationLog(sLogs); new JUnitCore().run(loggingRunner); assertThat(sLogs) .containsExactly( + "hardcoded class rule starting", "logging rule 2 starting", - "hardcoded rule starting", + "hardcoded test rule starting", "logging rule 1 starting", "before", "tight before", "begin: testMethod(" + "android.platform.test.microbenchmark.MicrobenchmarkTest" - + "$LoggingTestWithRule)", + + "$LoggingTestWithRules)", "test", "end", "tight after", "after", "logging rule 1 finished", - "hardcoded rule finished", + "hardcoded test rule finished", "logging rule 2 finished", "logging rule 2 starting", - "hardcoded rule starting", + "hardcoded test rule starting", "logging rule 1 starting", "before", "tight before", "begin: testMethod(" + "android.platform.test.microbenchmark.MicrobenchmarkTest" - + "$LoggingTestWithRule)", + + "$LoggingTestWithRules)", "test", "end", "tight after", "after", "logging rule 1 finished", - "hardcoded rule finished", + "hardcoded test rule finished", + "logging rule 2 finished", + "hardcoded class rule finished") + .inOrder(); + } + + /** Test dynamic class rule injection. */ + @Test + public void testDynamicClassRuleInjection() throws InitializationError { + Bundle args = new Bundle(); + args.putString("iterations", "2"); + args.putString("rename-iterations", "false"); + args.putString("terminate-on-test-fail", "false"); + args.putString( + Microbenchmark.DYNAMIC_INNER_CLASS_RULES_OPTION, LoggingRule1.class.getName()); + args.putString( + Microbenchmark.DYNAMIC_OUTER_CLASS_RULES_OPTION, LoggingRule2.class.getName()); + LoggingMicrobenchmark loggingRunner = + new LoggingMicrobenchmark(LoggingTestWithRules.class, args); + loggingRunner.setOperationLog(sLogs); + new JUnitCore().run(loggingRunner); + assertThat(sLogs) + .containsExactly( + "logging rule 2 starting", + "hardcoded class rule starting", + "logging rule 1 starting", + "hardcoded test rule starting", + "before", + "tight before", + "begin: testMethod(" + + "android.platform.test.microbenchmark.MicrobenchmarkTest" + + "$LoggingTestWithRules)", + "test", + "end", + "tight after", + "after", + "hardcoded test rule finished", + "hardcoded test rule starting", + "before", + "tight before", + "begin: testMethod(" + + "android.platform.test.microbenchmark.MicrobenchmarkTest" + + "$LoggingTestWithRules)", + "test", + "end", + "tight after", + "after", + "hardcoded test rule finished", + "logging rule 1 finished", + "hardcoded class rule finished", "logging rule 2 finished") .inOrder(); } @@ -565,18 +615,32 @@ public final class MicrobenchmarkTest { } } - public static class LoggingTestWithRule extends LoggingTest { + public static class LoggingTestWithRules extends LoggingTest { + @ClassRule + public static TestRule hardCodedClassRule = + new TestWatcher() { + @Override + public void starting(Description description) { + sLogs.add("hardcoded class rule starting"); + } + + @Override + public void finished(Description description) { + sLogs.add("hardcoded class rule finished"); + } + }; + @Rule public TestRule hardCodedRule = new TestWatcher() { @Override public void starting(Description description) { - sLogs.add("hardcoded rule starting"); + sLogs.add("hardcoded test rule starting"); } @Override public void finished(Description description) { - sLogs.add("hardcoded rule finished"); + sLogs.add("hardcoded test rule finished"); } }; } |