Avoid catching AssertionError for actual failures.
ActivityManagerServiceUtilsTest was checking to make sure its test
method would actually validate input and correctly fail. Unfortunately,
it also caught the AssertionError for the test _actually_ failing.
Bug: 217923092
Test: errorprone build
ActivityManagerUtilsTest#testCheckShouldSamplePackage
Change-Id: Icd5a62c470b0f3d15de65f4b43d4355ba9c4661c
diff --git a/services/Android.bp b/services/Android.bp
index 4623ff9..378ad54 100644
--- a/services/Android.bp
+++ b/services/Android.bp
@@ -18,6 +18,7 @@
// "-Xep:AndroidFrameworkUid:ERROR",
"-Xep:SelfEquals:ERROR",
"-Xep:NullTernary:ERROR",
+ "-Xep:TryFailThrowable:ERROR",
// NOTE: only enable to generate local patchfiles
// "-XepPatchChecks:refaster:frameworks/base/errorprone/refaster/EfficientXml.java.refaster",
// "-XepPatchLocation:/tmp/refaster/",
diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityManagerUtilsTest.java b/services/tests/servicestests/src/com/android/server/am/ActivityManagerUtilsTest.java
index 96103e3..d95c9ac 100644
--- a/services/tests/servicestests/src/com/android/server/am/ActivityManagerUtilsTest.java
+++ b/services/tests/servicestests/src/com/android/server/am/ActivityManagerUtilsTest.java
@@ -17,7 +17,7 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertTrue;
import androidx.test.filters.SmallTest;
@@ -89,18 +89,20 @@
}
@Test
- public void testSheckShouldSamplePackage() {
+ public void testCheckShouldSamplePackage() {
// Just make sure checkShouldSamplePackage is actually working...
+ assertFailure(() -> checkShouldSamplePackage(0.3f, 0.6f, false, true));
+ assertFailure(() -> checkShouldSamplePackage(0.6f, 0.3f, true, false));
+ }
+
+ private static void assertFailure(Runnable r) {
+ boolean failed = false;
try {
- checkShouldSamplePackage(0.3f, 0.6f, false, true);
- fail();
- } catch (AssertionError expected) {
+ r.run();
+ } catch (AssertionError e) {
+ failed = true;
}
- try {
- checkShouldSamplePackage(0.6f, 0.3f, true, false);
- fail();
- } catch (AssertionError expected) {
- }
+ assertTrue(failed);
}
private void checkShouldSamplePackage(float inputSampleRate, float expectedRate,