summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com>2024-03-26 08:42:40 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2024-03-26 08:42:40 +0000
commit48dde924d2d53a2d90a223a2f5e3f0b5c84277ba (patch)
tree77ba7371d495bdd5e73b1105376e95db8dc3a02f
parentac5316ff37f87a03c0d6c34ee871de77cef714c9 (diff)
parent67689e928b6b48e0731aea69bef7916ceb7d33aa (diff)
downloadplatform_testing-48dde924d2d53a2d90a223a2f5e3f0b5c84277ba.tar.gz
Merge "Added a unit test for DynamicRuleChain that tests the failed() callback." into main
-rw-r--r--libraries/health/rules/tests/src/android/platform/test/rule/DynamicRuleChainTest.java45
1 files changed, 35 insertions, 10 deletions
diff --git a/libraries/health/rules/tests/src/android/platform/test/rule/DynamicRuleChainTest.java b/libraries/health/rules/tests/src/android/platform/test/rule/DynamicRuleChainTest.java
index 0fd988c2e..cede191ef 100644
--- a/libraries/health/rules/tests/src/android/platform/test/rule/DynamicRuleChainTest.java
+++ b/libraries/health/rules/tests/src/android/platform/test/rule/DynamicRuleChainTest.java
@@ -43,7 +43,7 @@ public class DynamicRuleChainTest {
// DynamicRuleChain.
private static final List<String> sLogs = new ArrayList<String>();
- private static final Statement mStatement =
+ private static final Statement PASSING_STATEMENT =
new Statement() {
@Override
public void evaluate() {
@@ -51,6 +51,15 @@ public class DynamicRuleChainTest {
}
};
+ private static final Statement FAILING_STATEMENT =
+ new Statement() {
+ @Override
+ public void evaluate() {
+ sLogs.add("Test execution");
+ throw new RuntimeException("Test failed");
+ }
+ };
+
@Rule public ExpectedException expectedException = ExpectedException.none();
@Before
@@ -62,16 +71,27 @@ public class DynamicRuleChainTest {
@Test
public void testAppliesRuleBySimpleClassNameForRulesInKnownPackage() throws Throwable {
DynamicRuleChain chain = createWithRuleNames("DynamicRuleChainTest$Rule1");
- Statement applied = chain.apply(mStatement, DESCRIPTION);
+ Statement applied = chain.apply(PASSING_STATEMENT, DESCRIPTION);
applied.evaluate();
assertThat(sLogs).containsExactly("Rule1 starting", "Test execution", "Rule1 finished");
}
@Test
+ public void testRuleCallbacks() throws Throwable {
+ DynamicRuleChain chain = createWithRuleNames("DynamicRuleChainTest$Rule1");
+ Statement applied = chain.apply(FAILING_STATEMENT, DESCRIPTION);
+ expectedException.expectMessage("Test failed");
+ applied.evaluate();
+ assertThat(sLogs)
+ .containsExactly(
+ "Rule1 starting", "Test execution", "Rule1 on failure", "Rule1 finished");
+ }
+
+ @Test
public void testAppliesRuleByFullyQualifiedClassName() throws Throwable {
DynamicRuleChain chain =
createWithRuleNames("android.platform.test.rule.DynamicRuleChainTest$Rule1");
- Statement applied = chain.apply(mStatement, DESCRIPTION);
+ Statement applied = chain.apply(PASSING_STATEMENT, DESCRIPTION);
applied.evaluate();
assertThat(sLogs).containsExactly("Rule1 starting", "Test execution", "Rule1 finished");
}
@@ -83,7 +103,7 @@ public class DynamicRuleChainTest {
String.format(
"Failed to dynamically load rule with simple class name %s.", badName));
DynamicRuleChain chain = createWithRuleNames(badName);
- chain.apply(mStatement, DESCRIPTION);
+ chain.apply(PASSING_STATEMENT, DESCRIPTION);
}
@Test
@@ -93,7 +113,7 @@ public class DynamicRuleChainTest {
String.format(
"Failed to dynamically load rule with fully qualified name %s.", badName));
DynamicRuleChain chain = createWithRuleNames(badName);
- chain.apply(mStatement, DESCRIPTION);
+ chain.apply(PASSING_STATEMENT, DESCRIPTION);
}
@Test
@@ -102,7 +122,7 @@ public class DynamicRuleChainTest {
createWithRuleNames(
"DynamicRuleChainTest$Rule2",
"android.platform.test.rule.DynamicRuleChainTest$Rule1");
- Statement applied = chain.apply(mStatement, DESCRIPTION);
+ Statement applied = chain.apply(PASSING_STATEMENT, DESCRIPTION);
applied.evaluate();
// Rule2's logs should be outside, and Rule1's logs should be inside.
assertThat(sLogs)
@@ -123,7 +143,7 @@ public class DynamicRuleChainTest {
DynamicRuleChain chain =
createWithRuleNames(
badName, "android.platform.test.rule.DynamicRuleChainTest$Rule1");
- chain.apply(mStatement, DESCRIPTION);
+ chain.apply(PASSING_STATEMENT, DESCRIPTION);
}
@Test
@@ -133,7 +153,7 @@ public class DynamicRuleChainTest {
"DynamicRuleChainTest$Rule2",
"android.platform.test.rule.DynamicRuleChainTest$Rule1",
"DynamicRuleChainTest$Rule2");
- Statement applied = chain.apply(mStatement, DESCRIPTION);
+ Statement applied = chain.apply(PASSING_STATEMENT, DESCRIPTION);
applied.evaluate();
// Rule2's logs should be outside, and Rule1's logs should be inside.
assertThat(sLogs)
@@ -154,7 +174,7 @@ public class DynamicRuleChainTest {
"android.platform.test.rule.DynamicRuleChainTest$Rule1",
"DynamicRuleChainTest$Rule2",
"android.platform.test.rule.DynamicRuleChainTest$Rule1");
- Statement applied = chain.apply(mStatement, DESCRIPTION);
+ Statement applied = chain.apply(PASSING_STATEMENT, DESCRIPTION);
applied.evaluate();
// Rule2's logs should be outside, and Rule1's logs should be inside.
assertThat(sLogs)
@@ -175,7 +195,7 @@ public class DynamicRuleChainTest {
"override-rules",
"DynamicRuleChainTest$Rule2",
"android.platform.test.rule.DynamicRuleChainTest$Rule1");
- Statement applied = chain.apply(mStatement, DESCRIPTION);
+ Statement applied = chain.apply(PASSING_STATEMENT, DESCRIPTION);
applied.evaluate();
assertThat(sLogs)
.containsExactly(
@@ -229,6 +249,11 @@ public class DynamicRuleChainTest {
public void finished(Description description) {
sLogs.add("Rule1 finished");
}
+
+ @Override
+ public void failed(Throwable e, Description description) {
+ sLogs.add("Rule1 on failure");
+ }
}
public static class Rule2 extends TestWatcher {