From dae9bb1e58b214ef5091a9410260fcf172e710e2 Mon Sep 17 00:00:00 2001 From: hoisie Date: Thu, 2 Dec 2021 15:14:17 -0800 Subject: [PATCH] Fix remaining CloseGuard warnings in Robolectric tests CloseGuard warnings and stack traces accounted for ~40% of log lines during Robolectric tests. This will remove around ~3.5k CloseGuard errors and stack traces in the Robolectric test suite. PiperOrigin-RevId: 413781118 --- .../ContentProviderControllerTest.java | 39 +++++++++++---- .../robolectric/shadows/SQLiteCursorTest.java | 2 + .../shadows/SQLiteDatabaseTest.java | 47 ++++++++++++++++-- .../shadows/SQLiteOpenHelperTest.java | 11 ++++- .../shadows/SQLiteStatementTest.java | 1 + .../shadows/ShadowAbstractCursorTest.java | 8 +++- .../shadows/ShadowActivityTest.java | 6 +-- .../ShadowBitmapRegionDecoderTest.java | 40 ++++++++-------- .../shadows/ShadowBugreportManagerTest.java | 18 ++++++- .../shadows/ShadowContentResolverTest.java | 5 +- .../shadows/ShadowCursorAdapterTest.java | 15 ++++-- .../shadows/ShadowCursorWindowTest.java | 5 ++ .../shadows/ShadowHardwareBufferTest.java | 22 ++++++--- .../shadows/ShadowMatrixCursorTest.java | 31 ++++++++---- .../shadows/ShadowMediaMuxerTest.java | 1 + .../shadows/ShadowMediaPlayerTest.java | 11 +++-- .../shadows/ShadowMergeCursorTest.java | 25 +++++++++- .../ShadowParcelFileDescriptorTest.java | 48 +++++++++---------- .../shadows/ShadowResourcesTest.java | 13 +++-- .../shadows/ShadowUsbManagerTest.java | 7 ++- .../shadows/ShadowWallpaperManagerTest.java | 10 ++-- .../shadows/ShadowWifiAwareManagerTest.java | 23 +++++++-- 22 files changed, 282 insertions(+), 106 deletions(-) diff --git a/robolectric/src/test/java/org/robolectric/android/controller/ContentProviderControllerTest.java b/robolectric/src/test/java/org/robolectric/android/controller/ContentProviderControllerTest.java index c6c1641d9e0..9cdff632c4f 100644 --- a/robolectric/src/test/java/org/robolectric/android/controller/ContentProviderControllerTest.java +++ b/robolectric/src/test/java/org/robolectric/android/controller/ContentProviderControllerTest.java @@ -1,6 +1,7 @@ package org.robolectric.android.controller; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import android.app.Application; import android.content.ContentProviderClient; @@ -8,12 +9,14 @@ import android.content.pm.PathPermission; import android.content.pm.ProviderInfo; import android.net.Uri; +import android.os.Build; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; +import org.robolectric.RuntimeEnvironment; import org.robolectric.shadows.testing.TestContentProvider1; import org.robolectric.shadows.testing.TestContentProvider3And4; @@ -42,7 +45,7 @@ public void shouldInitializeFromManifestProviderInfo() throws Exception { assertThat(myContentProvider.getReadPermission()).isEqualTo("READ_PERMISSION"); assertThat(myContentProvider.getWritePermission()).isEqualTo("WRITE_PERMISSION"); - assertThat(myContentProvider.getPathPermissions()).asList().hasSize(1); + assertThat(myContentProvider.getPathPermissions()).hasLength(1); PathPermission pathPermission = myContentProvider.getPathPermissions()[0]; assertThat(pathPermission.getPath()).isEqualTo("/path/*"); assertThat(pathPermission.getType()).isEqualTo(PathPermission.PATTERN_SIMPLE_GLOB); @@ -55,10 +58,10 @@ public void shouldRegisterWithContentResolver() throws Exception { controller.create().get(); ContentProviderClient client = - contentResolver.acquireContentProviderClient( - "org.robolectric.authority1"); - client.query(Uri.parse("something"), new String[]{"title"}, "*", new String[]{}, "created"); + contentResolver.acquireContentProviderClient("org.robolectric.authority1"); + client.query(Uri.parse("something"), new String[] {"title"}, "*", new String[] {}, "created"); assertThat(controller.get().transcript).containsExactly("onCreate", "query for something"); + close(client); } @Test @@ -70,6 +73,7 @@ public void shouldResolveProvidersWithMultipleAuthorities() throws Exception { contentResolver.acquireContentProviderClient("org.robolectric.authority3"); client.query(Uri.parse("something"), new String[] {"title"}, "*", new String[] {}, "created"); assertThat(contentProvider.transcript).containsExactly("onCreate", "query for something"); + close(client); } @Test @@ -98,9 +102,11 @@ public void withoutManifest_shouldRegisterWithContentResolver() throws Exception providerInfo.authority = "some-authority"; controller.create(providerInfo); - ContentProviderClient client = contentResolver.acquireContentProviderClient(providerInfo.authority); - client.query(Uri.parse("something"), new String[]{"title"}, "*", new String[]{}, "created"); + ContentProviderClient client = + contentResolver.acquireContentProviderClient(providerInfo.authority); + client.query(Uri.parse("something"), new String[] {"title"}, "*", new String[] {}, "created"); assertThat(controller.get().transcript).containsExactly("onCreate", "query for something"); + close(client); } @Test @@ -111,11 +117,17 @@ public void contentProviderShouldBeCreatedBeforeBeingRegistered() throws Excepti ContentProviderClient contentProviderClient = contentResolver.acquireContentProviderClient("x-authority"); assertThat(contentProviderClient.getLocalContentProvider()).isSameInstanceAs(xContentProvider); + close(contentProviderClient); } - @Test(expected = IllegalArgumentException.class) + @Test public void createContentProvider_nullAuthority() throws Exception { - Robolectric.buildContentProvider(XContentProvider.class).create(new ProviderInfo()).get(); + assertThrows( + IllegalArgumentException.class, + () -> + Robolectric.buildContentProvider(XContentProvider.class) + .create(new ProviderInfo()) + .get()); } static class XContentProvider extends TestContentProvider1 { @@ -129,9 +141,20 @@ public boolean onCreate() { contentProviderClient == null ? "x-authority" + " not registered" + " yet" : "x-authority" + " is registered"); + if (contentProviderClient != null) { + close(contentProviderClient); + } return false; } } static class NotInManifestContentProvider extends TestContentProvider1 {} + + private static void close(ContentProviderClient client) { + if (RuntimeEnvironment.getApiLevel() > Build.VERSION_CODES.M) { + client.close(); + } else { + client.release(); + } + } } diff --git a/robolectric/src/test/java/org/robolectric/shadows/SQLiteCursorTest.java b/robolectric/src/test/java/org/robolectric/shadows/SQLiteCursorTest.java index c862afe2a94..c648eb30b5d 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/SQLiteCursorTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/SQLiteCursorTest.java @@ -39,6 +39,7 @@ public void setUp() throws Exception { @After public void tearDown() { database.close(); + cursor.close(); } @Test @@ -482,6 +483,7 @@ private Cursor createCursor() { private void setupEmptyResult() { database.execSQL("DELETE FROM table_name;"); + cursor.close(); cursor = createCursor(); } diff --git a/robolectric/src/test/java/org/robolectric/shadows/SQLiteDatabaseTest.java b/robolectric/src/test/java/org/robolectric/shadows/SQLiteDatabaseTest.java index 0cc99dbc62b..b1c496cc01c 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/SQLiteDatabaseTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/SQLiteDatabaseTest.java @@ -122,6 +122,7 @@ public void testInsertAndQuery() { assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue); assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue); + cursor.close(); } @Test @@ -146,6 +147,7 @@ public void testInsertAndRawQuery() { assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue); assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue); + cursor.close(); } @Test(expected = android.database.SQLException.class) @@ -173,6 +175,7 @@ public void testInsertOrThrow() { String stringValueFromDatabase = cursor.getString(1); assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue); assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue); + cursor.close(); } @Test(expected = IllegalArgumentException.class) @@ -194,12 +197,14 @@ public void testRawQueryCountWithOneArgument() { "select second_column, first_column from rawtable WHERE" + " `id` = ?", new String[] {"1"}); assertThat(cursor.getCount()).isEqualTo(1); + cursor.close(); } @Test public void testRawQueryCountWithNullArgs() { Cursor cursor = database.rawQuery("select second_column, first_column from rawtable", null); assertThat(cursor.getCount()).isEqualTo(2); + cursor.close(); } @Test @@ -208,6 +213,7 @@ public void testRawQueryCountWithEmptyArguments() { database.rawQuery( "select second_column, first_column" + " from" + " rawtable", new String[] {}); assertThat(cursor.getCount()).isEqualTo(2); + cursor.close(); } @Test(expected = IllegalArgumentException.class) @@ -235,6 +241,7 @@ public void testEmptyTable() { null); assertThat(cursor.moveToFirst()).isFalse(); + cursor.close(); } @Test @@ -325,6 +332,7 @@ public void testUpdate() { assertThat(cursor.getCount()).isEqualTo(1); assertIdAndName(cursor, 1234L, "Buster"); + cursor.close(); } @Test @@ -339,6 +347,7 @@ public void testUpdateNoMatch() { assertThat(cursor.getCount()).isEqualTo(1); assertIdAndName(cursor, 1234L, "Chuck"); + cursor.close(); } @Test @@ -361,6 +370,7 @@ public void testUpdateAll() { assertThat(cursor.moveToNext()).isFalse(); assertThat(cursor.isAfterLast()).isTrue(); assertThat(cursor.moveToNext()).isFalse(); + cursor.close(); } @Test @@ -402,6 +412,7 @@ public void testExecSQL() { assertThat(cursor).isNotNull(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(1); + cursor.close(); cursor = database.rawQuery("SELECT * FROM table_name", null); assertThat(cursor).isNotNull(); @@ -409,6 +420,7 @@ public void testExecSQL() { assertThat(cursor.getInt(cursor.getColumnIndex("id"))).isEqualTo(1234); assertThat(cursor.getString(cursor.getColumnIndex("name"))).isEqualTo("Chuck"); + cursor.close(); } @Test @@ -429,6 +441,7 @@ public void testExecSQLParams() { assertThat(cursor).isNotNull(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(2); + cursor.close(); cursor = database.rawQuery("SELECT `id`, `name` ,`lastUsed` FROM `routine`", null); assertThat(cursor).isNotNull(); @@ -443,6 +456,7 @@ public void testExecSQLParams() { assertThat(cursor.getInt(cursor.getColumnIndex("id"))).isEqualTo(2); assertThat(cursor.getInt(cursor.getColumnIndex("lastUsed"))).isEqualTo(1); assertThat(cursor.getString(cursor.getColumnIndex("name"))).isEqualTo("Bench Press"); + cursor.close(); } @Test(expected = SQLiteException.class) @@ -487,6 +501,7 @@ public void testExecSQLInsertNull() { int nameIndex = cursor.getColumnIndex("name"); assertThat(cursor.getString(nameIndex)).isEqualTo(name); assertThat(cursor.getString(firstIndex)).isEqualTo(null); + cursor.close(); } @Test @@ -525,6 +540,7 @@ public void shouldStoreGreatBigHonkingIntegersCorrectly() { database.query("table_name", new String[] {"big_int"}, null, null, null, null, null); assertThat(cursor.moveToFirst()).isTrue(); assertEquals(1234567890123456789L, cursor.getLong(0)); + cursor.close(); } @Test @@ -537,6 +553,7 @@ public void testSuccessTransaction() { Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM table_name", null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(1); + cursor.close(); } @Test @@ -557,6 +574,7 @@ public void testFailureTransaction() { cursor = database.rawQuery(select, null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(0); + cursor.close(); } @Test @@ -573,6 +591,7 @@ public void testSuccessNestedTransaction() { Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM table_name", null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(2); + cursor.close(); } @Test @@ -588,6 +607,7 @@ public void testFailureNestedTransaction() { Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM table_name", null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(0); + cursor.close(); } @Test @@ -600,6 +620,8 @@ public void testTransactionAlreadySuccessful() { } catch (IllegalStateException e) { assertThat(e.getMessage()).contains("transaction"); assertThat(e.getMessage()).contains("successful"); + } finally { + database.endTransaction(); } } @@ -629,6 +651,7 @@ public void testReplace() { assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getString(cursor.getColumnIndex("name"))).isEqualTo("Norris"); + cursor.close(); } @Test @@ -657,21 +680,26 @@ public void testReplaceIsReplacing() { assertThat(secondId).isEqualTo(id); assertThat(firstCursor.getString(0)).isEqualTo(stringValueA); assertThat(secondCursor.getString(0)).isEqualTo(stringValueB); + firstCursor.close(); + secondCursor.close(); } @Test public void shouldCreateDefaultCursorFactoryWhenNullFactoryPassedToRawQuery() { - database.rawQueryWithFactory(null, ANY_VALID_SQL, null, null); + Cursor cursor = database.rawQueryWithFactory(null, ANY_VALID_SQL, null, null); + cursor.close(); } @Test public void shouldCreateDefaultCursorFactoryWhenNullFactoryPassedToQuery() { - database.queryWithFactory(null, false, "table_name", null, null, null, null, null, null, null); + Cursor cursor = + database.queryWithFactory( + null, false, "table_name", null, null, null, null, null, null, null); + cursor.close(); } @Test public void shouldOpenExistingDatabaseFromFileSystemIfFileExists() { - database.close(); SQLiteDatabase db = @@ -679,6 +707,7 @@ public void shouldOpenExistingDatabaseFromFileSystemIfFileExists() { Cursor c = db.rawQuery("select * from rawtable", null); assertThat(c).isNotNull(); assertThat(c.getCount()).isEqualTo(2); + c.close(); assertThat(db.isOpen()).isTrue(); db.close(); assertThat(db.isOpen()).isFalse(); @@ -687,6 +716,7 @@ public void shouldOpenExistingDatabaseFromFileSystemIfFileExists() { SQLiteDatabase.openDatabase(databasePath.getAbsolutePath(), null, OPEN_READWRITE); assertThat(reopened).isNotSameInstanceAs(db); assertThat(reopened.isOpen()).isTrue(); + reopened.close(); } @Test(expected = SQLiteException.class) @@ -701,6 +731,7 @@ public void shouldUseInMemoryDatabaseWhenCallingCreate() { SQLiteDatabase db = SQLiteDatabase.create(null); assertThat(db.isOpen()).isTrue(); assertThat(db.getPath()).isEqualTo(":memory:"); + db.close(); } @Test @@ -732,12 +763,14 @@ public void testTwoConcurrentDbConnections() { assertThat(c.getCount()).isEqualTo(1); assertThat(c.moveToNext()).isTrue(); assertThat(c.getString(c.getColumnIndex("data"))).isEqualTo("d1"); + c.close(); c = db2.rawQuery("select * from bar", null); assertThat(c).isNotNull(); assertThat(c.getCount()).isEqualTo(1); assertThat(c.moveToNext()).isTrue(); assertThat(c.getString(c.getColumnIndex("data"))).isEqualTo("d2"); + c.close(); } @Test(expected = SQLiteException.class) @@ -802,6 +835,7 @@ public void testDataInMemoryDatabaseIsPersistentAfterClose() { assertThat(c.getCount()).isEqualTo(1); assertThat(c.moveToNext()).isTrue(); assertThat(c.getString(c.getColumnIndex("data"))).isEqualTo("d1"); + c.close(); } @Test @@ -822,6 +856,7 @@ public void testRawQueryWithFactoryAndCancellationSignal() { } catch (OperationCanceledException e) { // expected } + cursor.close(); } @Test @@ -848,8 +883,7 @@ public void shouldBeAbleToBeUsedFromDifferentThread() { new Thread() { @Override public void run() { - try { - executeQuery("select * from table_name"); + try (Cursor c = executeQuery("select * from table_name")) { } catch (Throwable e) { e.printStackTrace(); error[0] = e; @@ -915,6 +949,7 @@ private void assertEmptyDatabase() { assertThat(cursor.moveToFirst()).isFalse(); assertThat(cursor.isClosed()).isFalse(); assertThat(cursor.getCount()).isEqualTo(0); + cursor.close(); } private void assertNonEmptyDatabase() { @@ -922,6 +957,7 @@ private void assertNonEmptyDatabase() { database.query("table_name", new String[] {"id", "name"}, null, null, null, null, null); assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.getCount()).isNotEqualTo(0); + cursor.close(); } @Test @@ -986,6 +1022,7 @@ public void shouldCorrectlyReturnNullValues() { assertThat(nullValuesCursor.getString(i)).isNull(); } assertThat(nullValuesCursor.getBlob(3)).isNull(); + nullValuesCursor.close(); } @Test diff --git a/robolectric/src/test/java/org/robolectric/shadows/SQLiteOpenHelperTest.java b/robolectric/src/test/java/org/robolectric/shadows/SQLiteOpenHelperTest.java index e809827a56a..574a7a2803e 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/SQLiteOpenHelperTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/SQLiteOpenHelperTest.java @@ -4,6 +4,7 @@ import android.content.ContentValues; import android.content.Context; +import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; @@ -35,6 +36,7 @@ public void testConstructorWithNullPathShouldCreateInMemoryDatabase() { SQLiteDatabase database = helper.getReadableDatabase(); assertDatabaseOpened(database, helper); assertInitialDB(database, helper); + helper.close(); } @Test @@ -106,6 +108,8 @@ public void testGetPath() { String expectedPath2 = ApplicationProvider.getApplicationContext().getDatabasePath(path2).getAbsolutePath(); assertThat(helper2.getReadableDatabase().getPath()).isEqualTo(expectedPath2); + helper1.close(); + helper2.close(); } @Test @@ -154,8 +158,9 @@ private void insertData(SQLiteDatabase db, String table, int[] values) { } private void verifyData(SQLiteDatabase db, String table, int expectedVals) { - assertThat(db.query(table, null, null, null, - null, null, null).getCount()).isEqualTo(expectedVals); + try (Cursor cursor = db.query(table, null, null, null, null, null, null)) { + assertThat(cursor.getCount()).isEqualTo(expectedVals); + } } @Test @@ -171,6 +176,7 @@ public void testMultipleDbsPreserveData() { insertData(db2, TABLE_NAME2, new int[]{4, 5, 6}); verifyData(db1, TABLE_NAME1, 2); verifyData(db2, TABLE_NAME2, 3); + helper2.close(); } @Test @@ -191,6 +197,7 @@ public void testCloseOneDbKeepsDataForOther() { db1 = helper.getWritableDatabase(); verifyData(db1, TABLE_NAME1, 2); verifyData(db2, TABLE_NAME2, 3); + helper2.close(); } @Test diff --git a/robolectric/src/test/java/org/robolectric/shadows/SQLiteStatementTest.java b/robolectric/src/test/java/org/robolectric/shadows/SQLiteStatementTest.java index eaf0dc099d0..987829b477c 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/SQLiteStatementTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/SQLiteStatementTest.java @@ -117,6 +117,7 @@ public void testExecuteUpdateDelete() { Cursor dataCursor = database.rawQuery("SELECT `name` FROM `routine`", null); assertThat(dataCursor.moveToNext()).isTrue(); assertThat(dataCursor.getString(0)).isEqualTo("Head Press"); + dataCursor.close(); } @Test diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowAbstractCursorTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowAbstractCursorTest.java index ef0cf35bdc7..100f840c5d6 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowAbstractCursorTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowAbstractCursorTest.java @@ -8,6 +8,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4; import java.util.ArrayList; import java.util.List; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -23,6 +24,11 @@ public void setUp() throws Exception { cursor = new TestCursor(); } + @After + public void tearDown() { + cursor.close(); + } + @Test public void testMoveToFirst() { cursor.theTable.add("Foobar"); @@ -269,4 +275,4 @@ public boolean isNull(int columnIndex) { throw new UnsupportedOperationException(); } } -} \ No newline at end of file +} diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowActivityTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowActivityTest.java index 42cf9c824fe..35572098c29 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowActivityTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowActivityTest.java @@ -43,7 +43,7 @@ import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.database.Cursor; -import android.database.sqlite.SQLiteCursor; +import android.database.MatrixCursor; import android.media.AudioManager; import android.net.Uri; import android.os.Bundle; @@ -72,7 +72,6 @@ import org.robolectric.annotation.Config; import org.robolectric.annotation.LooperMode; import org.robolectric.annotation.LooperMode.Mode; -import org.robolectric.shadow.api.Shadow; import org.robolectric.shadows.ShadowActivity.IntentSenderRequest; import org.robolectric.util.TestRunnable; @@ -756,7 +755,7 @@ public void startAndStopManagingCursorTracksCursors() throws Exception { assertThat(shadowOf(activity).getManagedCursors()).isNotNull(); assertThat(shadowOf(activity).getManagedCursors()).isEmpty(); - Cursor c = Shadow.newInstanceOf(SQLiteCursor.class); + Cursor c = new MatrixCursor(new String[] {"a"}); activity.startManagingCursor(c); assertThat(shadowOf(activity).getManagedCursors()).isNotNull(); @@ -767,6 +766,7 @@ public void startAndStopManagingCursorTracksCursors() throws Exception { assertThat(shadowOf(activity).getManagedCursors()).isNotNull(); assertThat(shadowOf(activity).getManagedCursors()).isEmpty(); + c.close(); } @Test diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowBitmapRegionDecoderTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowBitmapRegionDecoderTest.java index f1e645ff734..172cf5eac19 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowBitmapRegionDecoderTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowBitmapRegionDecoderTest.java @@ -2,6 +2,7 @@ import static com.google.common.truth.Truth.assertThat; +import android.content.res.AssetFileDescriptor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapRegionDecoder; @@ -11,7 +12,6 @@ import com.google.common.io.ByteStreams; import java.awt.image.BufferedImage; import java.io.File; -import java.io.FileDescriptor; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; @@ -32,10 +32,12 @@ public class ShadowBitmapRegionDecoderTest { public void testNewInstance() throws Exception { assertThat(BitmapRegionDecoder.newInstance(ByteStreams.toByteArray(getImageInputStream()), 0, 0, false)) .isNotNull(); - assertThat(BitmapRegionDecoder.newInstance(getImageFd(), false)) - .isNotNull(); - assertThat(BitmapRegionDecoder.newInstance(getImageInputStream(), false)) - .isNotNull(); + try (AssetFileDescriptor afd = getImageFd()) { + assertThat(BitmapRegionDecoder.newInstance(afd.getFileDescriptor(), false)).isNotNull(); + } + try (InputStream inputStream = getImageInputStream()) { + assertThat(BitmapRegionDecoder.newInstance(inputStream, false)).isNotNull(); + } assertThat(BitmapRegionDecoder.newInstance(getGeneratedImageFile(), false)) .isNotNull(); } @@ -59,17 +61,18 @@ public void testDecodeRegionReturnsExpectedSize() throws IOException { @Test public void testDecodeRegionReturnsExpectedConfig() throws IOException { - BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(getImageInputStream(), false); - - BitmapFactory.Options options = new BitmapFactory.Options(); - assertThat(bitmapRegionDecoder.decodeRegion(new Rect(0, 0, 1, 1), options).getConfig()) - .isEqualTo(Bitmap.Config.ARGB_8888); - options.inPreferredConfig = null; - assertThat(bitmapRegionDecoder.decodeRegion(new Rect(0, 0, 1, 1), options).getConfig()) - .isEqualTo(Bitmap.Config.ARGB_8888); - options.inPreferredConfig = Bitmap.Config.RGB_565; - assertThat(bitmapRegionDecoder.decodeRegion(new Rect(0, 0, 1, 1), options).getConfig()) - .isEqualTo(Bitmap.Config.RGB_565); + try (InputStream inputStream = getImageInputStream()) { + BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false); + BitmapFactory.Options options = new BitmapFactory.Options(); + assertThat(bitmapRegionDecoder.decodeRegion(new Rect(0, 0, 1, 1), options).getConfig()) + .isEqualTo(Bitmap.Config.ARGB_8888); + options.inPreferredConfig = null; + assertThat(bitmapRegionDecoder.decodeRegion(new Rect(0, 0, 1, 1), options).getConfig()) + .isEqualTo(Bitmap.Config.ARGB_8888); + options.inPreferredConfig = Bitmap.Config.RGB_565; + assertThat(bitmapRegionDecoder.decodeRegion(new Rect(0, 0, 1, 1), options).getConfig()) + .isEqualTo(Bitmap.Config.RGB_565); + } } private static InputStream getImageInputStream() { @@ -78,12 +81,11 @@ private static InputStream getImageInputStream() { .openRawResource(R.drawable.robolectric); } - private static FileDescriptor getImageFd() throws Exception { + private static AssetFileDescriptor getImageFd() throws Exception { return ApplicationProvider.getApplicationContext() .getResources() .getAssets() - .openFd("robolectric.png") - .getFileDescriptor(); + .openFd("robolectric.png"); } private String getGeneratedImageFile() throws Exception { diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowBugreportManagerTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowBugreportManagerTest.java index e8235b5f7ed..b5bddd4376b 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowBugreportManagerTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowBugreportManagerTest.java @@ -20,6 +20,9 @@ import androidx.test.ext.junit.runners.AndroidJUnit4; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -33,12 +36,20 @@ public final class ShadowBugreportManagerTest { private ShadowBugreportManager shadowBugreportManager; private final Context context = ApplicationProvider.getApplicationContext(); + private final List openFds = new ArrayList<>(); @Before public void setUp() { shadowBugreportManager = Shadow.extract(context.getSystemService(Context.BUGREPORT_SERVICE)); } + @After + public void tearDown() throws Exception { + for (ParcelFileDescriptor pfd : openFds) { + pfd.close(); + } + } + @Test public void requestBugreport() { shadowBugreportManager.requestBugreport( @@ -281,7 +292,10 @@ private ParcelFileDescriptor createWriteFile(String fileName) throws IOException f.delete(); } f.createNewFile(); - return ParcelFileDescriptor.open( - f, ParcelFileDescriptor.MODE_WRITE_ONLY | ParcelFileDescriptor.MODE_APPEND); + ParcelFileDescriptor pfd = + ParcelFileDescriptor.open( + f, ParcelFileDescriptor.MODE_WRITE_ONLY | ParcelFileDescriptor.MODE_APPEND); + openFds.add(pfd); + return pfd; } } diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowContentResolverTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowContentResolverTest.java index 420ab133c25..896e58a9d53 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowContentResolverTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowContentResolverTest.java @@ -912,12 +912,13 @@ public void getProvider_shouldNotReturnAnyProviderWhenManifestIsNull() { public void openTypedAssetFileDescriptor_shouldOpenDescriptor() throws IOException, RemoteException { Robolectric.setupContentProvider(MyContentProvider.class, AUTHORITY); - AssetFileDescriptor afd = + try (AssetFileDescriptor afd = contentResolver.openTypedAssetFileDescriptor( - Uri.parse("content://" + AUTHORITY + "/whatever"), "*/*", null); + Uri.parse("content://" + AUTHORITY + "/whatever"), "*/*", null)) { FileDescriptor descriptor = afd.getFileDescriptor(); assertThat(descriptor).isNotNull(); + } } @Test diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowCursorAdapterTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowCursorAdapterTest.java index d31912f26af..88c5987b638 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowCursorAdapterTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowCursorAdapterTest.java @@ -10,6 +10,7 @@ import android.widget.CursorAdapter; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,6 +44,12 @@ public void setUp() throws Exception { adapter = new TestAdapter(curs); } + @After + public void tearDown() { + database.close(); + curs.close(); + } + @Test public void testChangeCursor() { assertThat(adapter.getCursor()).isNotNull(); @@ -81,9 +88,11 @@ public void testGetItemId() { } @Test public void shouldNotErrorOnCursorChangeWhenNoFlagsAreSet() throws Exception { - adapter = new TestAdapterWithFlags(curs, 0); - adapter.changeCursor(database.rawQuery("SELECT * FROM table_name;", null)); - assertThat(adapter.getCursor()).isNotSameInstanceAs(curs); + try (Cursor newCursor = database.rawQuery("SELECT * FROM table_name;", null)) { + adapter = new TestAdapterWithFlags(curs, 0); + adapter.changeCursor(newCursor); + assertThat(adapter.getCursor()).isNotSameInstanceAs(curs); + } } private static class TestAdapter extends CursorAdapter { diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowCursorWindowTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowCursorWindowTest.java index bf0bfd37116..f21dcdf0d0c 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowCursorWindowTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowCursorWindowTest.java @@ -17,6 +17,7 @@ public class ShadowCursorWindowTest { public void shouldCreateWindowWithName() { CursorWindow window = new CursorWindow("name"); assertThat(window.getName()).isEqualTo("name"); + window.close(); } @Test @@ -45,6 +46,8 @@ public void shouldFillWindowWithCursor() { assertThat(window.getBlob(1, 3)).isEqualTo(null); assertThat(window.getBlob(2, 3)).isEqualTo(new byte[]{}); + testCursor.close(); + window.close(); } /** Real Android will crash in native code if putBlob is called with a null value. */ @@ -53,6 +56,7 @@ public void putBlobNullValueThrowsNPE() { CursorWindow cursorWindow = new CursorWindow("test"); cursorWindow.allocRow(); assertThrows(NullPointerException.class, () -> cursorWindow.putBlob(null, 0, 0)); + cursorWindow.close(); } /** Real Android will crash in native code if putString is called with a null value. */ @@ -61,5 +65,6 @@ public void putStringNullValueThrowsNPE() { CursorWindow cursorWindow = new CursorWindow("test"); cursorWindow.allocRow(); assertThrows(NullPointerException.class, () -> cursorWindow.putString(null, 0, 0)); + cursorWindow.close(); } } diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowHardwareBufferTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowHardwareBufferTest.java index fcbb21b1688..35d16b69252 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowHardwareBufferTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowHardwareBufferTest.java @@ -175,8 +175,10 @@ public void createWithBlobFormatInvalidHeightThrows() { public void createWithOFormatsAndFlagsSucceedsOnOAndLater() { for (int format : VALID_FORMATS_O) { int height = format == HardwareBuffer.BLOB ? 1 : VALID_HEIGHT; - assertNotNull( - HardwareBuffer.create(VALID_WIDTH, height, format, VALID_LAYERS, VALID_USAGE_FLAGS_O)); + try (HardwareBuffer buffer = + HardwareBuffer.create(VALID_WIDTH, height, format, VALID_LAYERS, VALID_USAGE_FLAGS_O)) { + assertNotNull(buffer); + } } } @@ -184,22 +186,26 @@ public void createWithOFormatsAndFlagsSucceedsOnOAndLater() { @Config(minSdk = P) public void createWithPFormatsAndFlagsSucceedsOnPAndLater() { for (int format : VALID_FORMATS_P) { - assertNotNull( + try (HardwareBuffer buffer = HardwareBuffer.create( - VALID_WIDTH, VALID_HEIGHT, format, VALID_LAYERS, VALID_USAGE_FLAGS_P)); + VALID_WIDTH, VALID_HEIGHT, format, VALID_LAYERS, VALID_USAGE_FLAGS_P)) { + assertNotNull(buffer); + } } } @Test @Config(minSdk = P) public void createWithPFlagsSucceedsOnPAndLater() { - assertNotNull( + try (HardwareBuffer buffer = HardwareBuffer.create( VALID_WIDTH, VALID_HEIGHT, HardwareBuffer.RGBA_8888, VALID_LAYERS, - VALID_USAGE_FLAGS_P)); + VALID_USAGE_FLAGS_P)) { + assertNotNull(buffer); + } } @Test @@ -214,6 +220,7 @@ public void gettersOnHardwareBufferAreCorrect() { assertEquals(HardwareBuffer.RGBA_8888, buffer.getFormat()); assertEquals(VALID_LAYERS, buffer.getLayers()); assertEquals(VALID_USAGE_FLAGS_O, buffer.getUsage()); + buffer.close(); } @Test @@ -267,11 +274,14 @@ public void gettersOnParceledBufferAreCorrect() { final Parcel parcel = Parcel.obtain(); buffer.writeToParcel(parcel, 0); parcel.setDataPosition(0); + HardwareBuffer otherBuffer = HardwareBuffer.CREATOR.createFromParcel(parcel); assertEquals(VALID_WIDTH, otherBuffer.getWidth()); assertEquals(VALID_HEIGHT, otherBuffer.getHeight()); assertEquals(HardwareBuffer.RGBA_8888, otherBuffer.getFormat()); assertEquals(VALID_LAYERS, otherBuffer.getLayers()); assertEquals(VALID_USAGE_FLAGS_O, otherBuffer.getUsage()); + buffer.close(); + otherBuffer.close(); } } diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowMatrixCursorTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowMatrixCursorTest.java index 4370896bd8e..019a9e64cf4 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowMatrixCursorTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowMatrixCursorTest.java @@ -2,12 +2,14 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import android.database.CursorIndexOutOfBoundsException; import android.database.MatrixCursor; import androidx.test.ext.junit.runners.AndroidJUnit4; import java.util.Arrays; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -24,6 +26,11 @@ public void setUp() throws Exception { singleColumnSingleNullValueMatrixCursor.moveToFirst(); } + @After + public void tearDown() { + singleColumnSingleNullValueMatrixCursor.close(); + } + @Test public void shouldAddObjectArraysAsRows() { MatrixCursor cursor = new MatrixCursor(new String[]{"a", "b", "c"}); @@ -44,6 +51,7 @@ public void shouldAddObjectArraysAsRows() { assertTrue(cursor.isNull(2)); assertFalse(cursor.moveToNext()); + cursor.close(); } @Test @@ -66,6 +74,7 @@ public void shouldAddIterablesAsRows() { assertTrue(cursor.isNull(2)); assertFalse(cursor.moveToNext()); + cursor.close(); } @Test @@ -82,6 +91,7 @@ public void shouldDefineColumnNames() { assertThat(cursor.getColumnIndex("b")).isEqualTo(1); assertThat(cursor.getColumnIndex("z")).isEqualTo(-1); + cursor.close(); } @Test @@ -116,34 +126,39 @@ public void shouldAllowTypeFlexibility() { assertThat(cursor.getDouble(1)).isEqualTo(3.3); assertThat(cursor.getString(2)).isEqualTo("a"); + cursor.close(); } - @Test(expected = IllegalArgumentException.class) + @Test public void shouldDefineGetColumnNameOrThrow() { MatrixCursor cursor = new MatrixCursor(new String[]{"a", "b", "c"}); - cursor.getColumnIndexOrThrow("z"); + assertThrows(IllegalArgumentException.class, () -> cursor.getColumnIndexOrThrow("z")); + cursor.close(); } - @Test(expected = CursorIndexOutOfBoundsException.class) + @Test public void shouldThrowIndexOutOfBoundsExceptionWithoutData() { MatrixCursor cursor = new MatrixCursor(new String[]{"a", "b", "c"}); - cursor.getString(0); + assertThrows(CursorIndexOutOfBoundsException.class, () -> cursor.getString(0)); + cursor.close(); } - @Test(expected = CursorIndexOutOfBoundsException.class) + @Test public void shouldThrowIndexOutOfBoundsExceptionForInvalidColumn() { MatrixCursor cursor = new MatrixCursor(new String[]{"a", "b", "c"}); cursor.addRow(new Object[]{"foo", 10L, 0.1f}); - cursor.getString(3); + assertThrows(CursorIndexOutOfBoundsException.class, () -> cursor.getString(3)); + cursor.close(); } - @Test(expected = CursorIndexOutOfBoundsException.class) + @Test public void shouldThrowIndexOutOfBoundsExceptionForInvalidColumnLastRow() { MatrixCursor cursor = new MatrixCursor(new String[]{"a", "b", "c"}); cursor.addRow(new Object[]{"foo", 10L, 0.1f}); cursor.moveToFirst(); cursor.moveToNext(); - cursor.getString(0); + assertThrows(CursorIndexOutOfBoundsException.class, () -> cursor.getString(0)); + cursor.close(); } @Test diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowMediaMuxerTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowMediaMuxerTest.java index 0279ba7d62c..6a8217340b4 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowMediaMuxerTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowMediaMuxerTest.java @@ -97,5 +97,6 @@ private void basicMuxingFlow(int bufInfoOffset, int bufOffset, int inputSize) th assertThat(outputBytes) .isEqualTo(Arrays.copyOfRange(inputBytes, bufInfoOffset, inputBytes.length)); new File(tempFilePath).deleteOnExit(); + muxer.release(); } } diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowMediaPlayerTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowMediaPlayerTest.java index 4d9873d8500..6553049c7ca 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowMediaPlayerTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowMediaPlayerTest.java @@ -240,11 +240,12 @@ public long getSize() { @Test public void testSetDataSourceAssetFileDescriptorDataSource() throws IOException { Application context = ApplicationProvider.getApplicationContext(); - AssetFileDescriptor fd = context.getResources().openRawResourceFd(R.drawable.an_image); - DataSource ds = toDataSource(fd); - ShadowMediaPlayer.addMediaInfo(ds, info); - mediaPlayer.setDataSource(fd); - assertWithMessage("dataSource").that(shadowMediaPlayer.getDataSource()).isEqualTo(ds); + try (AssetFileDescriptor fd = context.getResources().openRawResourceFd(R.drawable.an_image)) { + DataSource ds = toDataSource(fd); + ShadowMediaPlayer.addMediaInfo(ds, info); + mediaPlayer.setDataSource(fd); + assertWithMessage("dataSource").that(shadowMediaPlayer.getDataSource()).isEqualTo(ds); + } } @Test diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowMergeCursorTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowMergeCursorTest.java index e0036b41355..f8e3c50cf1f 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowMergeCursorTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowMergeCursorTest.java @@ -1,12 +1,15 @@ package org.robolectric.shadows; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import android.database.Cursor; import android.database.MergeCursor; import android.database.sqlite.SQLiteCursor; import android.database.sqlite.SQLiteDatabase; import androidx.test.ext.junit.runners.AndroidJUnit4; +import dalvik.system.CloseGuard; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,6 +55,16 @@ public void setUp() throws Exception { "SELECT * FROM table_2;"); } + @After + public void tearDown() throws Exception { + database.close(); + if (cursor != null) { + cursor.close(); + } + dbCursor1.close(); + dbCursor2.close(); + } + private SQLiteCursor setupTable(final String createSql, final String[] insertions, final String selectSql) { database.execSQL(createSql); @@ -65,9 +78,16 @@ private SQLiteCursor setupTable(final String createSql, final String[] insertion return (SQLiteCursor) cursor; } - @Test(expected = NullPointerException.class) + @Test public void shouldThrowIfConstructorArgumentIsNull() { - new MergeCursor(null); + CloseGuard.Reporter originalReporter = CloseGuard.getReporter(); + try { + // squelch spurious CloseGuard error + CloseGuard.setReporter((s, throwable) -> {}); + assertThrows(NullPointerException.class, () -> new MergeCursor(null)); + } finally { + CloseGuard.setReporter(originalReporter); + } } @Test @@ -77,6 +97,7 @@ public void testEmptyCursors() { assertThat(cursor.getCount()).isEqualTo(0); assertThat(cursor.moveToFirst()).isFalse(); assertThat(cursor.getColumnNames()).isNotNull(); + cursor.close(); // cursor list with partially null contents Cursor[] cursors = new Cursor[2]; diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowParcelFileDescriptorTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowParcelFileDescriptorTest.java index dacb95bc7cb..be3a35cf280 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowParcelFileDescriptorTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowParcelFileDescriptorTest.java @@ -11,6 +11,7 @@ import java.io.FileOutputStream; import java.io.IOException; import org.hamcrest.Matchers; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -22,6 +23,7 @@ public class ShadowParcelFileDescriptorTest { private File file; private File readOnlyFile; + private ParcelFileDescriptor pfd; @Before public void setUp() throws Exception { @@ -36,30 +38,32 @@ public void setUp() throws Exception { assertThat(readOnlyFile.setReadOnly()).isTrue(); } + @After + public void tearDown() throws Exception { + if (pfd != null) { + pfd.close(); + } + } + @Test public void testOpens() throws Exception { - ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); + pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); - pfd.close(); } @Test public void testOpens_canReadReadOnlyFile() throws Exception { - ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(readOnlyFile, ParcelFileDescriptor.MODE_READ_ONLY); + pfd = ParcelFileDescriptor.open(readOnlyFile, ParcelFileDescriptor.MODE_READ_ONLY); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); FileInputStream is = new FileInputStream(pfd.getFileDescriptor()); assertThat(is.read()).isEqualTo(READ_ONLY_FILE_CONTENTS); - pfd.close(); } @Test public void testOpens_canWriteWritableFile() throws Exception { - ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); + pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor()); @@ -69,18 +73,15 @@ public void testOpens_canWriteWritableFile() throws Exception { @Test public void testStatSize_emptyFile() throws Exception { - ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); + pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); assertThat(pfd.getStatSize()).isEqualTo(0); - pfd.close(); } @Test public void testStatSize_writtenFile() throws Exception { - ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); + pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor()); @@ -91,14 +92,14 @@ public void testStatSize_writtenFile() throws Exception { @Test public void testAppend() throws Exception { - ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); + pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor()); os.write(5); assertThat(pfd.getStatSize()).isEqualTo(1); // One byte. os.close(); + pfd.close(); pfd = ParcelFileDescriptor.open( @@ -113,8 +114,7 @@ public void testAppend() throws Exception { @Test public void testTruncate() throws Exception { - ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); + pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor()); @@ -131,6 +131,7 @@ public void testTruncate() throws Exception { assertThat(in.available()).isEqualTo(0); } + pfd.close(); pfd = ParcelFileDescriptor.open( file, ParcelFileDescriptor.MODE_READ_WRITE | ParcelFileDescriptor.MODE_TRUNCATE); @@ -149,8 +150,7 @@ public void testTruncate() throws Exception { @Test public void testWriteTwiceNoTruncate() throws Exception { - ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); + pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor()); @@ -166,6 +166,7 @@ public void testWriteTwiceNoTruncate() throws Exception { assertThat(buffer).isEqualTo(new byte[] {1, 2, 3}); assertThat(in.available()).isEqualTo(0); } + pfd.close(); pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); @@ -185,7 +186,7 @@ public void testWriteTwiceNoTruncate() throws Exception { @Test public void testCloses() throws Exception { - ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, -1); + pfd = ParcelFileDescriptor.open(file, -1); pfd.close(); assertThat(pfd.getFileDescriptor().valid()).isFalse(); assertThat(pfd.getFd()).isEqualTo(-1); @@ -193,14 +194,14 @@ public void testCloses() throws Exception { @Test public void testCloses_getStatSize_returnsInvalidLength() throws Exception { - ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, -1); + pfd = ParcelFileDescriptor.open(file, -1); pfd.close(); assertThat(pfd.getStatSize()).isEqualTo(-1); } @Test public void testAutoCloseInputStream() throws Exception { - ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, -1); + pfd = ParcelFileDescriptor.open(file, -1); ParcelFileDescriptor.AutoCloseInputStream is = new ParcelFileDescriptor.AutoCloseInputStream(pfd); is.close(); @@ -247,8 +248,7 @@ public void testGetFd_canRead() throws IOException { assumeThat("Windows is an affront to decency.", File.separator, Matchers.equalTo("/")); - ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(readOnlyFile, ParcelFileDescriptor.MODE_READ_ONLY); + pfd = ParcelFileDescriptor.open(readOnlyFile, ParcelFileDescriptor.MODE_READ_ONLY); int fd = pfd.getFd(); assertThat(fd).isGreaterThan(0); FileInputStream is = new FileInputStream(new File("/proc/self/fd/" + fd)); diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowResourcesTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowResourcesTest.java index 7b6e484f864..f3925429771 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowResourcesTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowResourcesTest.java @@ -4,6 +4,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.robolectric.shadows.ShadowAssetManager.useLegacy; +import android.content.res.AssetFileDescriptor; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; @@ -75,11 +76,13 @@ public void openRawResource_shouldLoadDrawableWithQualifiers() { } @Test - public void openRawResourceFd_returnsNull_todo_FIX() { - if (useLegacy()) { - assertThat(resources.openRawResourceFd(R.raw.raw_resource)).isNull(); - } else { - assertThat(resources.openRawResourceFd(R.raw.raw_resource)).isNotNull(); + public void openRawResourceFd_returnsNull_todo_FIX() throws Exception { + try (AssetFileDescriptor afd = resources.openRawResourceFd(R.raw.raw_resource)) { + if (useLegacy()) { + assertThat(afd).isNull(); + } else { + assertThat(afd).isNotNull(); + } } } diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowUsbManagerTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowUsbManagerTest.java index 71337a6e61f..8fba878a24d 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowUsbManagerTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowUsbManagerTest.java @@ -18,6 +18,7 @@ import android.hardware.usb.UsbPort; import android.hardware.usb.UsbPortStatus; import android.os.Build; +import android.os.ParcelFileDescriptor; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import java.util.Arrays; @@ -194,8 +195,10 @@ public void removeDevice() { } @Test - public void openAccessory() { - assertThat(usbManager.openAccessory(usbAccessory)).isNotNull(); + public void openAccessory() throws Exception { + try (ParcelFileDescriptor pfd = usbManager.openAccessory(usbAccessory)) { + assertThat(pfd).isNotNull(); + } } @Test diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowWallpaperManagerTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowWallpaperManagerTest.java index de43947298e..27f012eae1a 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowWallpaperManagerTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowWallpaperManagerTest.java @@ -241,10 +241,11 @@ public void getWallpaperFile_flagSystem_previouslyCached_shouldReturnParcelFileD /* allowBackup= */ false, WallpaperManager.FLAG_SYSTEM); - ParcelFileDescriptor parcelFileDescriptor = - manager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM); + try (ParcelFileDescriptor parcelFileDescriptor = + manager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM)) { assertThat(getBytesFromFileDescriptor(parcelFileDescriptor.getFileDescriptor())) .isEqualTo(getBytesFromBitmap(TEST_IMAGE_1)); + } } @Test @@ -263,10 +264,11 @@ public void getWallpaperFile_flagLock_previouslyCached_shouldReturnParcelFileDes /* allowBackup= */ false, WallpaperManager.FLAG_LOCK); - ParcelFileDescriptor parcelFileDescriptor = - manager.getWallpaperFile(WallpaperManager.FLAG_LOCK); + try (ParcelFileDescriptor parcelFileDescriptor = + manager.getWallpaperFile(WallpaperManager.FLAG_LOCK)) { assertThat(getBytesFromFileDescriptor(parcelFileDescriptor.getFileDescriptor())) .isEqualTo(getBytesFromBitmap(TEST_IMAGE_3)); + } } @Test diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowWifiAwareManagerTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowWifiAwareManagerTest.java index b18a9c0eba0..1c7ce687c17 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowWifiAwareManagerTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowWifiAwareManagerTest.java @@ -19,6 +19,7 @@ import android.os.Looper; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,6 +33,7 @@ public final class ShadowWifiAwareManagerTest { private Binder binder; private Handler handler; private Looper looper; + private WifiAwareSession session; private static final int CLIENT_ID = 1; @Before @@ -41,11 +43,15 @@ public void setUp() { binder = new Binder(); handler = new Handler(); looper = handler.getLooper(); - WifiAwareSession session = - ShadowWifiAwareManager.newWifiAwareSession(wifiAwareManager, binder, CLIENT_ID); + session = ShadowWifiAwareManager.newWifiAwareSession(wifiAwareManager, binder, CLIENT_ID); shadowOf(wifiAwareManager).setWifiAwareSession(session); } + @After + public void tearDown() { + session.close(); + } + @Test public void setAvailable_shouldUpdateWithAvailableStatus() { boolean available = false; @@ -86,6 +92,7 @@ public void publish_shouldPublishServiceIfWifiAwareAvailable() { shadowOf(wifiAwareManager).publish(CLIENT_ID, looper, config, testDiscoverySessionCallback); shadowMainLooper().idle(); assertThat(testDiscoverySessionCallback.publishSuccess).isTrue(); + publishDiscoverySession.close(); } @Test @@ -100,6 +107,7 @@ public void publish_shouldPublishServiceIfWifiAwareUnavailable() { wifiAwareManager.publish(CLIENT_ID, looper, config, testDiscoverySessionCallback); shadowMainLooper().idle(); assertThat(testDiscoverySessionCallback.publishSuccess).isFalse(); + publishDiscoverySession.close(); } @Test @@ -114,6 +122,7 @@ public void subscribe_shouldSubscribeIfWifiAwareAvailable() { wifiAwareManager.subscribe(CLIENT_ID, looper, config, testDiscoverySessionCallback); shadowMainLooper().idle(); assertThat(testDiscoverySessionCallback.subscribeSuccess).isTrue(); + subscribeDiscoverySession.close(); } @Test @@ -128,14 +137,16 @@ public void subscribe_shouldNotSubscribeIfWifiAwareUnavailable() { wifiAwareManager.subscribe(CLIENT_ID, looper, config, testDiscoverySessionCallback); shadowMainLooper().idle(); assertThat(testDiscoverySessionCallback.subscribeSuccess).isFalse(); + subscribeDiscoverySession.close(); } @Test public void canCreatePublishDiscoverySessionViaNewInstance() { int sessionId = 1; - PublishDiscoverySession publishDiscoverySession = - ShadowWifiAwareManager.newPublishDiscoverySession(wifiAwareManager, CLIENT_ID, sessionId); - assertThat(publishDiscoverySession).isNotNull(); + try (PublishDiscoverySession publishDiscoverySession = + ShadowWifiAwareManager.newPublishDiscoverySession(wifiAwareManager, CLIENT_ID, sessionId)) { + assertThat(publishDiscoverySession).isNotNull(); + } } @Test @@ -144,6 +155,7 @@ public void canCreateSubscribeDiscoverySessionViaNewInstance() { SubscribeDiscoverySession subscribeDiscoverySession = ShadowWifiAwareManager.newSubscribeDiscoverySession(wifiAwareManager, CLIENT_ID, sessionId); assertThat(subscribeDiscoverySession).isNotNull(); + subscribeDiscoverySession.close(); } @Test @@ -151,6 +163,7 @@ public void canCreateWifiAwareSessionViaNewInstance() { WifiAwareSession wifiAwareSession = ShadowWifiAwareManager.newWifiAwareSession(wifiAwareManager, binder, CLIENT_ID); assertThat(wifiAwareSession).isNotNull(); + wifiAwareSession.close(); } private static class TestAttachCallback extends AttachCallback {