Skip to content

Commit

Permalink
Fix loading assets via uri. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite authored Aug 14, 2019
1 parent e4a4023 commit 64f4474
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,58 @@ import coil.bitmappool.BitmapPool
import coil.bitmappool.FakeBitmapPool
import coil.size.PixelSize
import coil.util.createOptions
import coil.util.createTestMainDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.resetMain
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.shadow.api.Shadow
import org.robolectric.shadows.ShadowContentResolver
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@RunWith(RobolectricTestRunner::class)
class UriFetcherTest {

private val context: Context = ApplicationProvider.getApplicationContext()

private lateinit var mainDispatcher: TestCoroutineDispatcher
private lateinit var loader: UriFetcher
private lateinit var pool: BitmapPool

@Before
fun before() {
mainDispatcher = createTestMainDispatcher()
loader = UriFetcher(context)
pool = FakeBitmapPool()
}

@After
fun after() {
Dispatchers.resetMain()
}

@Test
fun `basic asset load`() {
fun basicAssetLoad() {
val uri = Uri.parse("file:///android_asset/normal.jpg")
assertTrue(loader.handles(uri))
assertEquals(uri.toString(), loader.key(uri))

val contentResolver: ShadowContentResolver = Shadow.extract(context.contentResolver)
contentResolver.registerInputStream(uri, context.assets.open("normal.jpg"))

val result = runBlocking {
loader.fetch(pool, uri, PixelSize(100, 100), createOptions())
}

assertTrue(result is SourceResult)
assertFalse(result.source.exhausted())
}

@Test
fun basicExtractFileName() {
val uri = Uri.parse("file:///android_asset/something.jpg")
val result = loader.extractAssetFileName(uri)
assertEquals("something.jpg", result)
}

@Test
fun emptyExtractFileName() {
val uri = Uri.parse("file:///android_asset/")
val result = loader.extractAssetFileName(uri)
assertEquals(null, result)
}

@Test
fun nonAssetUriExtractFileName() {
val uri = Uri.parse("file:///fake/file/path")
val result = loader.extractAssetFileName(uri)
assertEquals(null, result)
}
}
30 changes: 29 additions & 1 deletion coil-base/src/main/java/coil/fetch/UriFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package coil.fetch

import android.content.ContentResolver
import android.content.Context
import android.content.res.AssetManager
import android.net.Uri
import androidx.annotation.VisibleForTesting
import androidx.collection.arraySetOf
import coil.bitmappool.BitmapPool
import coil.decode.DataSource
Expand All @@ -16,6 +18,8 @@ internal class UriFetcher(
) : Fetcher<Uri> {

companion object {
private const val ASSET_FILE_PATH_SEGMENT = "android_asset"

private val SUPPORTED_SCHEMES = arraySetOf(
ContentResolver.SCHEME_ANDROID_RESOURCE,
ContentResolver.SCHEME_CONTENT,
Expand All @@ -33,10 +37,34 @@ internal class UriFetcher(
size: Size,
options: Options
): FetchResult {
val assetFileName = extractAssetFileName(data)
val inputStream = if (assetFileName != null) {
context.assets.open(assetFileName)
} else {
checkNotNull(context.contentResolver.openInputStream(data))
}

return SourceResult(
source = checkNotNull(context.contentResolver.openInputStream(data)).source().buffer(),
source = inputStream.source().buffer(),
mimeType = context.contentResolver.getType(data),
dataSource = DataSource.DISK
)
}

/** Return the asset's filename if [uri] must be handled by [AssetManager]. Else, return null. */
@VisibleForTesting
internal fun extractAssetFileName(uri: Uri): String? {
if (uri.scheme != ContentResolver.SCHEME_FILE) {
return null
}

val segments = uri.pathSegments
return if (segments.count() == 2 &&
segments[0] == ASSET_FILE_PATH_SEGMENT &&
segments[1].isNotBlank()) {
segments[1]
} else {
null
}
}
}

0 comments on commit 64f4474

Please sign in to comment.