Merge "Added AbstractExtendedMockito.getClearInlineMethodsAtTheEnd()." into main am: b0af9c5dc4

Original change: https://android-review.googlesource.com/c/platform/frameworks/libs/modules-utils/+/2862586

Change-Id: Ie0e79f33be733db8525b08d917eee7c0f1b03006
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/java/com/android/modules/utils/testing/AbstractExtendedMockitoRule.java b/java/com/android/modules/utils/testing/AbstractExtendedMockitoRule.java
index 7a0f41b..5256d6a 100644
--- a/java/com/android/modules/utils/testing/AbstractExtendedMockitoRule.java
+++ b/java/com/android/modules/utils/testing/AbstractExtendedMockitoRule.java
@@ -133,8 +133,16 @@
         return Collections.unmodifiableSet(staticClasses);
     }
 
-
-
+    /**
+     * Gets whether the rule should clear the inline mocks after the given test.
+     *
+     * <p>By default, it returns {@code} (unless the rule was built with
+     * {@link AbstractBuilder#dontClearInlineMocks()}, but subclasses can override to change the
+     * behavior (for example, to decide based on custom annotations).
+     */
+    protected boolean getClearInlineMethodsAtTheEnd(Description description) {
+        return mClearInlineMocks;
+    }
 
     @Override
     public Statement apply(Statement base, Description description) {
@@ -247,12 +255,13 @@
                 }
             }
         } finally {
-            clearInlineMocks();
+            clearInlineMocks(description);
         }
     }
 
-    private void clearInlineMocks() {
-        if (!mClearInlineMocks) {
+    private void clearInlineMocks(Description description) {
+        boolean clearIt = getClearInlineMethodsAtTheEnd(description);
+        if (!clearIt) {
             Log.d(TAG, "NOT calling clearInlineMocks() as set on builder");
             return;
         }
diff --git a/javatests/com/android/modules/utils/testing/ExtendedMockitoRuleTest.java b/javatests/com/android/modules/utils/testing/ExtendedMockitoRuleTest.java
index 6a80268..765b318 100644
--- a/javatests/com/android/modules/utils/testing/ExtendedMockitoRuleTest.java
+++ b/javatests/com/android/modules/utils/testing/ExtendedMockitoRuleTest.java
@@ -639,6 +639,16 @@
         assertWithMessage("mockito framework cleared").that(mockitoFramework.called).isTrue();
     }
 
+    @Test
+    public void testGetClearInlineMethodsAtTheEnd() throws Throwable {
+        assertWithMessage("getClearInlineMethodsAtTheEnd() by default")
+                .that(mBuilder.build().getClearInlineMethodsAtTheEnd(mDescription)).isTrue();
+        assertWithMessage("getClearInlineMethodsAtTheEnd() when built with dontClearInlineMocks()")
+                .that(mBuilder.dontClearInlineMocks().build()
+                        .getClearInlineMethodsAtTheEnd(mDescription))
+                .isFalse();
+    }
+
     private void applyRuleOnTestThatDoesntUseExpectation(@Nullable Strictness strictness)
             throws Throwable {
         Log.d(TAG, "applyRuleOnTestThatDoesntUseExpectation(): strictness= " + strictness);