Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,15 @@ internal class BackgroundImageURLLoader(
ImageDecodeOptions.newBuilder().setForceStaticImage(true).build()
)
}
val dataSource =
Fresco.getImagePipeline().fetchDecodedImage(requestBuilder.build(), CALLER_CONTEXT)
val imageRequest =
try {
requestBuilder.build()
} catch (e: RuntimeException) {
// `build()` validates the uri, e.g. a `res:` uri must name a resource id.
FLog.w(ReactConstants.TAG, e, "Invalid background image uri: %s", uri)
return
}
val dataSource = Fresco.getImagePipeline().fetchDecodedImage(imageRequest, CALLER_CONTEXT)
this.dataSource = dataSource
dataSource.subscribe(
object : BaseDataSubscriber<CloseableReference<CloseableImage>>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
package com.facebook.react.uimanager.drawable

import android.graphics.Bitmap
import com.facebook.imagepipeline.core.ImagePipelineFactory
import com.facebook.react.bridge.JavaOnlyMap
import com.facebook.react.uimanager.style.BackgroundImageLayer
import org.assertj.core.api.Assertions.*
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.GraphicsMode

/** Tests for [BackgroundImageURLLoader.LoadedImage] */
/** Tests for [BackgroundImageURLLoader] */
@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class BackgroundImageURLLoaderTest {
Expand Down Expand Up @@ -50,4 +55,21 @@ class BackgroundImageURLLoaderTest {
assertThat(image.height).isEqualTo(40f)
}
}

@After
fun tearDown() {
ImagePipelineFactory.shutDown()
}

@Test
fun testTreatsInvalidResourceUriAsFailedLoad() {
val context = RuntimeEnvironment.getApplication()
ImagePipelineFactory.initialize(context)
val loader = BackgroundImageURLLoader(context) {}
for (uri in listOf("res:///icon", "res:icon")) {
val layer = BackgroundImageLayer.parse(JavaOnlyMap.of("type", "url", "uri", uri), context)
loader.loadBackgroundImages(listOfNotNull(layer))
assertThat(loader.imageForUri(uri)).isNull()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class BackgroundImageLayerTest {

@Test
fun testRejectsUnknownType() {
assertThat(BackgroundImageLayer.parse(JavaOnlyMap.of("type", "conic-gradient"), context))
assertThat(BackgroundImageLayer.parse(JavaOnlyMap.of("type", "unknown-gradient"), context))
.isNull()
assertThat(BackgroundImageLayer.parse(null, context)).isNull()
}
Expand Down
Loading