Do tearDown iff we did setUp

This makes sure we don't try to clean up a device where our tests
aren't supported, while also ensuring we leave the device in a working
state with AVF enabled if it was to begin with.

Bug: 268688303
Test: TH (atest AVFHostTestCase doesn't work remotely)
Change-Id: I21a1ca5730ce9cd8c6e430568a4ca2469bb2cfc7
diff --git a/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java b/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java
index 4dbf4ba..9c8714f 100644
--- a/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java
+++ b/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java
@@ -83,9 +83,12 @@
     private MetricsProcessor mMetricsProcessor;
     @Rule public TestMetrics mMetrics = new TestMetrics();
 
+    private boolean mNeedTearDown = false;
+
     @Before
     public void setUp() throws Exception {
         testIfDeviceIsCapable(getDevice());
+        mNeedTearDown = true;
 
         getDevice().installPackage(findTestFile(APK_NAME), /* reinstall */ false);
 
@@ -94,12 +97,10 @@
 
     @After
     public void tearDown() throws Exception {
-        try {
-            testIfDeviceIsCapable(getDevice());
-        } catch (Exception e) {
-            // Suppress execption here.
-            // If we throw exceptions in both setUp() and tearDown(),
-            // then test is reported as fail with org.junit.TestCouldNotBeSkippedException.
+        if (!mNeedTearDown) {
+            // If we skipped setUp, we don't need to undo it, and that avoids potential exceptions
+            // incompatible hardware. (Note that tests can change what testIfDeviceIsCapable()
+            // sees, so we can't rely on that - b/268688303.)
             return;
         }
         // Set PKVM enable and reboot to prevent previous staged session.