Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/asset-utils/src/AndroidPathUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ function getAndroidAssetSuffix(scale /*: number */) /*: string */ {
}

// See https://developer.android.com/guide/topics/resources/drawable-resource.html
// HEIC and HEIF are absent because the Android Gradle plugin fails the build on
// such a file in a drawable folder, so those images are packaged as raw
// resources.
const drawableFileTypes /*: Set<string> */ = new Set([
'gif',
'heic',
'heif',
'jpeg',
'jpg',
'ktx',
Expand Down
6 changes: 6 additions & 0 deletions packages/asset-utils/src/__tests__/AndroidPathUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ describe('getAndroidResourceFolderName', () => {
expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1)).toBe('raw');
expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1.25)).toBe('raw');
});

test.each(['heic', 'heif'])('returns "raw" for %s images', type => {
expect(getAndroidResourceFolderName({...DRAWABLE_ASSET, type}, 1)).toBe(
'raw',
);
});
});

describe('getAndroidResourceIdentifier', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,25 @@ import javax.annotation.concurrent.ThreadSafe
@ThreadSafe
public object ResourceDrawableIdHelper {
private val resourceDrawableIdMap: MutableMap<String, Int> = HashMap()
private val rawResourceIdMap: MutableMap<String, Int> = HashMap()

@Synchronized
@JvmStatic
public fun clear() {
resourceDrawableIdMap.clear()
rawResourceIdMap.clear()
}

@JvmStatic
public fun getResourceDrawableId(context: Context, name: String?): Int {
public fun getResourceDrawableId(context: Context, name: String?): Int =
getResourceId(context, name, "drawable", resourceDrawableIdMap)

private fun getResourceId(
context: Context,
name: String?,
type: String,
idMap: MutableMap<String, Int>,
): Int {
if (name.isNullOrEmpty()) {
return 0
}
Expand All @@ -38,16 +48,12 @@ public object ResourceDrawableIdHelper {
}

synchronized(this) {
return resourceDrawableIdMap.get(normalizedName) ?: addDrawableId(context, normalizedName)
return idMap.getOrPut(normalizedName) {
context.resources.getIdentifier(normalizedName, type, context.packageName)
}
}
}

private fun addDrawableId(context: Context, normalizedName: String): Int {
val newId = context.resources.getIdentifier(normalizedName, "drawable", context.packageName)
resourceDrawableIdMap[normalizedName] = newId
return newId
}

@JvmStatic
public fun getResourceDrawable(context: Context, name: String?): Drawable? {
val resId = getResourceDrawableId(context, name)
Expand All @@ -56,7 +62,11 @@ public object ResourceDrawableIdHelper {

@JvmStatic
public fun getResourceDrawableUri(context: Context, name: String?): Uri {
val resId = getResourceDrawableId(context, name)
val drawableId = getResourceDrawableId(context, name)
// An image in a format that cannot be packaged as a drawable, such as HEIC, is bundled as a
// raw resource.
val resId =
if (drawableId > 0) drawableId else getResourceId(context, name, "raw", rawResourceIdMap)
return if (resId > 0) {
Uri.Builder().scheme(LOCAL_RESOURCE_SCHEME).path(resId.toString()).build()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@
package com.facebook.react.views.imagehelper

import android.content.Context
import android.content.res.Resources
import android.net.Uri
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment

@RunWith(RobolectricTestRunner::class)
class ResourceDrawableIdHelperTest {

private lateinit var context: Context
private val mockContext = mock<Context>()
private val resources = mock<Resources>()

@Before
fun setUp() {
context = RuntimeEnvironment.getApplication()
ResourceDrawableIdHelper.clear()
whenever(mockContext.resources).thenReturn(resources)
whenever(mockContext.packageName).thenReturn("test.images")
}

@After
Expand Down Expand Up @@ -72,4 +80,29 @@ class ResourceDrawableIdHelperTest {
assertThat(ResourceDrawableIdHelper.getResourceDrawableId(context, "unknown_image"))
.isEqualTo(0)
}

@Test
fun uriFallsBackToRawResourceForNameWithoutADrawable() {
whenever(resources.getIdentifier("photo", "raw", "test.images")).thenReturn(42)

assertThat(ResourceDrawableIdHelper.getResourceDrawableUri(mockContext, "photo"))
.isEqualTo(Uri.parse("res:/42"))
}

@Test
fun uriPrefersDrawableOverRawResourceWithTheSameName() {
whenever(resources.getIdentifier("photo", "drawable", "test.images")).thenReturn(23)
whenever(resources.getIdentifier("photo", "raw", "test.images")).thenReturn(42)

assertThat(ResourceDrawableIdHelper.getResourceDrawableUri(mockContext, "photo"))
.isEqualTo(Uri.parse("res:/23"))
}

@Test
fun drawableIdIgnoresRawResources() {
whenever(resources.getIdentifier("photo", "raw", "test.images")).thenReturn(42)

assertThat(ResourceDrawableIdHelper.getResourceDrawableId(mockContext, "photo")).isEqualTo(0)
assertThat(ResourceDrawableIdHelper.getResourceDrawable(mockContext, "photo")).isNull()
}
}
Loading