Browse Source

Don't throw an error if a context is null in the DefaultAndroidResourceReader (#5433)

It fixes Robolectric tests where a context is available through
InstrumentationRegistry

Fixes https://youtrack.jetbrains.com/issue/CMP-6612

## Release Notes
### Fixes - Resources
- Fix resource access in a Robolectric test environment.
pull/5435/head v1.10.0+dev2978
Konstantin 3 months ago committed by GitHub
parent
commit
b0eb0916c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/ResourceReader.android.kt
  2. 4
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ResourceReader.kt

24
components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/ResourceReader.android.kt

@ -13,13 +13,7 @@ internal actual fun getPlatformResourceReader(): ResourceReader = DefaultAndroid @@ -13,13 +13,7 @@ internal actual fun getPlatformResourceReader(): ResourceReader = DefaultAndroid
@ExperimentalResourceApi
internal object DefaultAndroidResourceReader : ResourceReader {
private val assets: AssetManager by lazy {
val context = androidContext ?: error(
"Android context is not initialized. " +
"If it happens in the Preview mode then call PreviewContextConfigurationEffect() function."
)
context.assets
}
private val assets: AssetManager? get() = androidContext?.assets
private val instrumentedAssets: AssetManager?
get() = try {
@ -69,7 +63,7 @@ internal object DefaultAndroidResourceReader : ResourceReader { @@ -69,7 +63,7 @@ internal object DefaultAndroidResourceReader : ResourceReader {
Uri.parse("file:///android_asset/$path")
} else {
val classLoader = getClassLoader()
val resource = classLoader.getResource(path) ?: throw MissingResourceException(path)
val resource = classLoader.getResource(path) ?: throwMissingResourceException(path)
resource.toURI()
}
return uri.toString()
@ -83,11 +77,23 @@ internal object DefaultAndroidResourceReader : ResourceReader { @@ -83,11 +77,23 @@ internal object DefaultAndroidResourceReader : ResourceReader {
instrumentedAssets.open(path)
} catch (e: FileNotFoundException) {
val classLoader = getClassLoader()
classLoader.getResourceAsStream(path) ?: throw MissingResourceException(path)
classLoader.getResourceAsStream(path) ?: throwMissingResourceException(path)
}
}
}
private fun throwMissingResourceException(path: String): Nothing {
if (androidContext == null) {
throw MissingResourceException(
path = path,
message = "Android context is not initialized. " +
"If it happens in the Preview mode then call PreviewContextConfigurationEffect() function."
)
} else {
throw MissingResourceException(path)
}
}
private fun getClassLoader(): ClassLoader {
return this.javaClass.classLoader ?: error("Cannot find class loader")
}

4
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ResourceReader.kt

@ -4,7 +4,9 @@ import androidx.compose.runtime.Composable @@ -4,7 +4,9 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.staticCompositionLocalOf
class MissingResourceException(path: String) : Exception("Missing resource with path: $path")
class MissingResourceException(path: String) : Exception("Missing resource with path: $path") {
internal constructor(path: String, message: String) : this("$path. $message")
}
/**
* Reads the content of the resource file at the specified path and returns it as a byte array.

Loading…
Cancel
Save