diff --git a/library/robolectric/src/com/bumptech/glide/load/resource/LocalUriFetcherTest.java b/library/robolectric/src/com/bumptech/glide/load/resource/LocalUriFetcherTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2b76f2229a9a08fbb9f43b09eef012221c5cf024 --- /dev/null +++ b/library/robolectric/src/com/bumptech/glide/load/resource/LocalUriFetcherTest.java @@ -0,0 +1,66 @@ +package com.bumptech.glide.load.resource; + +import android.content.ContentResolver; +import android.content.Context; +import android.net.Uri; +import com.bumptech.glide.Priority; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; + +import java.io.Closeable; +import java.io.FileNotFoundException; +import java.io.IOException; + +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +@RunWith(RobolectricTestRunner.class) +public class LocalUriFetcherTest { + private TestLocalUriFetcher fetcher; + + @Before + public void setUp() { + fetcher = new TestLocalUriFetcher(Robolectric.application, Uri.parse("content://empty")); + } + + @Test + public void testClosesDataOnCleanup() throws Exception { + Closeable closeable = fetcher.loadResource(Priority.NORMAL); + fetcher.cleanup(); + + verify(closeable).close(); + } + + @Test + public void testDoesNotCLoseNullData() throws IOException { + fetcher.cleanup(); + + verify(fetcher.closeable, never()).close(); + } + + @Test + public void testHandlesExceptionOnClose() throws Exception { + Closeable closeable = fetcher.loadResource(Priority.NORMAL); + + doThrow(new IOException("Test")).when(closeable).close(); + fetcher.cleanup(); + verify(closeable).close(); + } + + private static class TestLocalUriFetcher extends LocalUriFetcher { + public Closeable closeable = mock(Closeable.class); + public TestLocalUriFetcher(Context context, Uri uri) { + super(context, uri); + } + + @Override + protected Closeable loadResource(Uri uri, ContentResolver contentResolver) throws FileNotFoundException { + return closeable; + } + } +} diff --git a/library/src/com/bumptech/glide/load/resource/LocalUriFetcher.java b/library/src/com/bumptech/glide/load/resource/LocalUriFetcher.java index 459e3da7bdbd71db75a98f08c75f16f4844e1862..078e23cbd9d53d02dbd5d100a2fb930ed22bf265 100644 --- a/library/src/com/bumptech/glide/load/resource/LocalUriFetcher.java +++ b/library/src/com/bumptech/glide/load/resource/LocalUriFetcher.java @@ -3,17 +3,22 @@ package com.bumptech.glide.load.resource; import android.content.ContentResolver; import android.content.Context; import android.net.Uri; +import android.util.Log; import com.bumptech.glide.Priority; +import java.io.Closeable; import java.io.FileNotFoundException; +import java.io.IOException; import java.lang.ref.WeakReference; /** * */ -public abstract class LocalUriFetcher implements ResourceFetcher { +public abstract class LocalUriFetcher implements ResourceFetcher { + private static final String TAG = "LocalUriFetcher"; private final WeakReference contextRef; private final Uri uri; + private T data; /** * Opens an input stream for a uri pointing to a local asset. Only certain uris are supported @@ -37,14 +42,23 @@ public abstract class LocalUriFetcher implements ResourceFetcher { throw new NullPointerException("Context has been cleared in LocalUriFetcher uri: " + uri); } ContentResolver contentResolver = context.getContentResolver(); - return loadResource(uri, contentResolver); + data = loadResource(uri, contentResolver); + return data; } @Override public void cleanup() { - // Do nothing. - } + if (data != null) { + try { + data.close(); + } catch (IOException e) { + if (Log.isLoggable(TAG, Log.VERBOSE)) { + Log.v(TAG, "failed to close data", e); + } + } + } + } @Override public void cancel() {