Fix handling of Throwable in tryTest/catch
Throwable could be used as a type argument to catch(), but it would
possibly never be caught, because the method only catches Exception.
Notably, junit assertions fail with AssertionError, which is not a
subclass of Exception. Due to that, tests or utilities like
DeviceConfigRule that have failing assertions in catch() would not
run the subsequent cleanup steps.
Bug: 210377950
Test: atest CleanupTest
Change-Id: I54e2922cb466f077ba4d219f8c3c6f885316296c
diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/CleanupTest.kt b/staticlibs/tests/unit/src/com/android/net/module/util/CleanupTest.kt
index 649b30e..851d09a 100644
--- a/staticlibs/tests/unit/src/com/android/net/module/util/CleanupTest.kt
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/CleanupTest.kt
@@ -175,6 +175,25 @@
}
@Test
+ fun testAssertionErrorInCatch() {
+ var x = 1
+ val thrown = assertFailsWith<AssertionError> {
+ tryTest {
+ x = 2
+ throw TestException1()
+ }.catch<TestException1> {
+ x = 3
+ fail("Test failure in catch")
+ } cleanup {
+ assertTrue(x == 3)
+ x = 4
+ }
+ }
+ assertTrue(x == 4)
+ assertTrue(thrown.suppressedExceptions.isEmpty())
+ }
+
+ @Test
fun testMultipleCleanups() {
var x = 1
val thrown = assertFailsWith<TestException1> {
diff --git a/staticlibs/testutils/hostdevice/com/android/testutils/Cleanup.kt b/staticlibs/testutils/hostdevice/com/android/testutils/Cleanup.kt
index 45783d8..3db357b 100644
--- a/staticlibs/testutils/hostdevice/com/android/testutils/Cleanup.kt
+++ b/staticlibs/testutils/hostdevice/com/android/testutils/Cleanup.kt
@@ -90,7 +90,7 @@
if (originalException !is E) return this
return TryExpr(try {
Result.success(block(originalException))
- } catch (e: Exception) {
+ } catch (e: Throwable) {
Result.failure(e)
})
}