MediaMetadataRetrieverTest: fix MediaDataSource finalizer crash

MediaDataSource impelments Closeable interface, so the finalizer
attempts to close the object. testExceptionWhileClosingMediaDataSource()
creates a MediaDataSource object that throws exception at close
to verify the behavior. Stop throwing the exception from the object
after verification so that the finalizer does not throw an exception
that brings down the test process.

Bug: 279658810
Test: atest CtsMediaMiscTestCases
(cherry picked from https://android-review.googlesource.com/q/commit:7d6276433dd9dd129302730467eb3384acb310e1)
Merged-In: I9157c930e7eaa316801291eaca9042aa616fd5cf
Change-Id: I9157c930e7eaa316801291eaca9042aa616fd5cf
diff --git a/tests/tests/media/misc/src/android/media/misc/cts/MediaMetadataRetrieverTest.java b/tests/tests/media/misc/src/android/media/misc/cts/MediaMetadataRetrieverTest.java
index e519115..01e3caf 100644
--- a/tests/tests/media/misc/src/android/media/misc/cts/MediaMetadataRetrieverTest.java
+++ b/tests/tests/media/misc/src/android/media/misc/cts/MediaMetadataRetrieverTest.java
@@ -172,29 +172,44 @@
         return ds;
     }
 
+    private static class WrappedDataSource extends MediaDataSource {
+        private final MediaDataSource mBackingMediaDataSource;
+        private boolean mFinished = false;
+
+        WrappedDataSource(MediaDataSource backingMediaDataSource) {
+            mBackingMediaDataSource = backingMediaDataSource;
+        }
+
+        @Override
+        public int readAt(long position, byte[] buffer, int offset, int size)
+                throws IOException {
+            return mBackingMediaDataSource.readAt(position, buffer, offset, size);
+        }
+
+        @Override
+        public long getSize() throws IOException {
+            return mBackingMediaDataSource.getSize();
+        }
+
+        @Override
+        public void close() throws IOException {
+            mBackingMediaDataSource.close();
+            if (!mFinished) {
+                throw new IOException();
+            }
+        }
+
+        public void finish() {
+            mFinished = true;
+        }
+    }
+
     @Test
     public void testExceptionWhileClosingMediaDataSource() throws IOException {
         MediaDataSource backingMediaDataSource =
                 TestMediaDataSource.fromAssetFd(
                         getAssetFileDescriptorFor("audio_with_metadata.mp3"));
-        MediaDataSource mediaDataSource = new MediaDataSource() {
-            @Override
-            public int readAt(long position, byte[] buffer, int offset, int size)
-                    throws IOException {
-                return backingMediaDataSource.readAt(position, buffer, offset, size);
-            }
-
-            @Override
-            public long getSize() throws IOException {
-                return backingMediaDataSource.getSize();
-            }
-
-            @Override
-            public void close() throws IOException {
-                backingMediaDataSource.close();
-                throw new IOException();
-            }
-        };
+        WrappedDataSource mediaDataSource = new WrappedDataSource(backingMediaDataSource);
         mRetriever.setDataSource(mediaDataSource);
         try {
             mRetriever.release();
@@ -202,6 +217,10 @@
         } catch (IOException e) {
             // Expected.
         }
+        // MediaDataSource implements Closeable interface, so the finalizer will
+        // try to close the object. If close() always throws an exception, the
+        // finalizer will bring down the test.
+        mediaDataSource.finish();
     }
 
     @Test