Call setUp() and cleanUp() on metric collectors in TestMetricRule.

Original change: https://android-review.googlesource.com/c/platform/platform_testing/+/1919178

Bug: 206682311
Test: atest PlatformRuleTests:TestMetricRuleTest

Change-Id: Id8d8490c5c13839ec79ca1277ba2856c7cca6855
Merged-In: Ic568a85c869618705279e87d74db411f1d4ea6eb
Merged-In: I0275da385555a4cb50620b84f54a580a707df243
Merged-In: I46a23ed62f73e58b6116769afea17872cd090ee1
diff --git a/libraries/health/rules/src/android/platform/test/rule/TestMetricRule.java b/libraries/health/rules/src/android/platform/test/rule/TestMetricRule.java
index 3989728..e27389f 100644
--- a/libraries/health/rules/src/android/platform/test/rule/TestMetricRule.java
+++ b/libraries/health/rules/src/android/platform/test/rule/TestMetricRule.java
@@ -112,13 +112,15 @@
         // Initialize each listener.
         for (BaseMetricListener listener : mMetricListeners) {
             listener.setInstrumentation(InstrumentationRegistry.getInstrumentation());
-            listener.setupAdditionalArgs();
         }
     }
 
     @Override
     protected void starting(Description description) {
         for (BaseMetricListener listener : mMetricListeners) {
+            listener.setUp();
+        }
+        for (BaseMetricListener listener : mMetricListeners) {
             try {
                 listener.testStarted(description);
             } catch (Exception e) {
@@ -146,6 +148,9 @@
                         e);
             }
         }
+        for (BaseMetricListener listener : mMetricListeners) {
+            listener.cleanUp();
+        }
     }
 
     @Override
diff --git a/libraries/health/rules/tests/src/android/platform/test/rule/TestMetricRuleTest.java b/libraries/health/rules/tests/src/android/platform/test/rule/TestMetricRuleTest.java
index b8ebe1c..4f16e92 100644
--- a/libraries/health/rules/tests/src/android/platform/test/rule/TestMetricRuleTest.java
+++ b/libraries/health/rules/tests/src/android/platform/test/rule/TestMetricRuleTest.java
@@ -80,9 +80,11 @@
                 .containsExactly(
                         "TestableCollector1#setInstrumentation",
                         "TestableCollector1#setupAdditionalArgs",
+                        "TestableCollector1#onSetUp",
                         String.format("Test %s: TestableCollector1#onTestStart", DESCRIPTION),
                         "Test execution",
-                        String.format("Test %s: TestableCollector1#onTestEnd", DESCRIPTION))
+                        String.format("Test %s: TestableCollector1#onTestEnd", DESCRIPTION),
+                        "TestableCollector1#onCleanUp")
                 .inOrder();
     }
 
@@ -98,6 +100,7 @@
                 .containsExactly(
                         "TestableCollector1#setInstrumentation",
                         "TestableCollector1#setupAdditionalArgs",
+                        "TestableCollector1#onSetUp",
                         String.format("Test %s: TestableCollector1#onTestStart", DESCRIPTION),
                         "Test execution",
                         String.format(
@@ -105,7 +108,8 @@
                                 DESCRIPTION,
                                 new Failure(
                                         DESCRIPTION, new RuntimeException(TEST_FAILURE_MESSAGE))),
-                        String.format("Test %s: TestableCollector1#onTestEnd", DESCRIPTION))
+                        String.format("Test %s: TestableCollector1#onTestEnd", DESCRIPTION),
+                        "TestableCollector1#onCleanUp")
                 .inOrder();
     }
 
@@ -121,9 +125,11 @@
         assertThat(sLogs)
                 .containsExactly(
                         "TestableCollector1#setInstrumentation",
-                        "TestableCollector1#setupAdditionalArgs",
                         "TestableCollector2#setInstrumentation",
+                        "TestableCollector1#setupAdditionalArgs",
+                        "TestableCollector1#onSetUp",
                         "TestableCollector2#setupAdditionalArgs",
+                        "TestableCollector2#onSetUp",
                         String.format("Test %s: TestableCollector1#onTestStart", DESCRIPTION),
                         String.format("Test %s: TestableCollector2#onTestStart", DESCRIPTION),
                         "Test execution",
@@ -134,7 +140,9 @@
                                 "Test %s: TestableCollector2#onTestFail with failure %s",
                                 DESCRIPTION, failure),
                         String.format("Test %s: TestableCollector1#onTestEnd", DESCRIPTION),
-                        String.format("Test %s: TestableCollector2#onTestEnd", DESCRIPTION))
+                        String.format("Test %s: TestableCollector2#onTestEnd", DESCRIPTION),
+                        "TestableCollector1#onCleanUp",
+                        "TestableCollector2#onCleanUp")
                 .inOrder();
     }
 
@@ -187,6 +195,16 @@
         }
 
         @Override
+        public void onSetUp() {
+            sLogs.add(String.format("%s#%s", mName, "onSetUp"));
+        }
+
+        @Override
+        public void onCleanUp() {
+            sLogs.add(String.format("%s#%s", mName, "onCleanUp"));
+        }
+
+        @Override
         public void onTestStart(DataRecord testData, Description description) {
             sLogs.add(String.format("Test %s: %s#%s", description, mName, "onTestStart"));
         }