From dd83199a94ff07927ea11503096414091987203f Mon Sep 17 00:00:00 2001 From: Konstantin Tskhovrebov Date: Wed, 28 Aug 2024 15:15:49 +0200 Subject: [PATCH] [resources] Add compile only dependency on "androidx.test:monitor" to configure test context --- components/gradle/libs.versions.toml | 3 ++- components/resources/library/build.gradle.kts | 4 +++ .../resources/AndroidContextProvider.kt | 26 ++----------------- .../resources/ResourceReader.android.kt | 9 ++++++- 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/components/gradle/libs.versions.toml b/components/gradle/libs.versions.toml index 6ecd0569a66..bd1a087c307 100644 --- a/components/gradle/libs.versions.toml +++ b/components/gradle/libs.versions.toml @@ -3,7 +3,7 @@ kotlinx-coroutines = "1.8.0" androidx-appcompat = "1.6.1" androidx-activity-compose = "1.8.2" androidx-test = "1.5.0" -androidx-compose = "1.6.0" +androidx-compose = "1.6.1" [libraries] kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } @@ -11,6 +11,7 @@ kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-t androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity-compose" } androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" } +androidx-test-monitor = { module = "androidx.test:monitor", version.ref = "androidx-test" } androidx-compose-ui-test = { module = "androidx.compose.ui:ui-test", version.ref = "androidx-compose" } androidx-compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "androidx-compose" } androidx-compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "androidx-compose" } diff --git a/components/resources/library/build.gradle.kts b/components/resources/library/build.gradle.kts index c53dcba6422..088f94323d9 100644 --- a/components/resources/library/build.gradle.kts +++ b/components/resources/library/build.gradle.kts @@ -113,6 +113,10 @@ kotlin { } val androidMain by getting { dependsOn(jvmAndAndroidMain) + dependencies { + //it will be called only in android instrumented tests where the library should be available + compileOnly(libs.androidx.test.monitor) + } } val androidInstrumentedTest by getting { dependsOn(jvmAndAndroidTest) diff --git a/components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/AndroidContextProvider.kt b/components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/AndroidContextProvider.kt index 1d348cf933c..fafb548b846 100644 --- a/components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/AndroidContextProvider.kt +++ b/components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/AndroidContextProvider.kt @@ -10,9 +10,10 @@ import android.net.Uri import androidx.compose.runtime.Composable import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalInspectionMode +import androidx.test.platform.app.InstrumentationRegistry internal val androidContext get() = AndroidContextProvider.ANDROID_CONTEXT -internal val androidInstrumentedContext get() = AndroidContextProvider.ANDROID_INSTRUMENTED_CONTEXT +internal val androidInstrumentedContext get() = InstrumentationRegistry.getInstrumentation().context /** * The function configures the android context @@ -38,34 +39,11 @@ fun PreviewContextConfigurationEffect() { } } -/** - * Sets the Android instrumented context. - * - * @param context The Android context to be set as the instrumented context. - * This context will be used by a ResourceReader to read test assets. - * - * Example usage: - * ``` - * fun configureTestEnvironment() { - * setAndroidInstrumentedContext( - * InstrumentationRegistry.getInstrumentation().context - * ) - * } - * ``` - */ -@ExperimentalResourceApi -fun setAndroidInstrumentedContext(context: Context) { - AndroidContextProvider.ANDROID_INSTRUMENTED_CONTEXT = context -} - //https://andretietz.com/2017/09/06/autoinitialise-android-library/ internal class AndroidContextProvider : ContentProvider() { companion object { @SuppressLint("StaticFieldLeak") var ANDROID_CONTEXT: Context? = null - - @SuppressLint("StaticFieldLeak") - var ANDROID_INSTRUMENTED_CONTEXT: Context? = null } override fun onCreate(): Boolean { diff --git a/components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/ResourceReader.android.kt b/components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/ResourceReader.android.kt index a4d259bebf9..c58ab133e26 100644 --- a/components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/ResourceReader.android.kt +++ b/components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/ResourceReader.android.kt @@ -2,6 +2,7 @@ package org.jetbrains.compose.resources import android.content.res.AssetManager import android.net.Uri +import android.util.Log import androidx.compose.runtime.Composable import androidx.compose.runtime.ProvidableCompositionLocal import java.io.FileNotFoundException @@ -16,7 +17,13 @@ internal actual fun getPlatformResourceReader(): ResourceReader = object : Resou context.assets } - private val instrumentedAssets: AssetManager? get() = androidInstrumentedContext?.assets + private val instrumentedAssets: AssetManager? + get() = try { + androidInstrumentedContext.assets + } catch (e: NoClassDefFoundError) { + Log.d("ResourceReader", "Android Instrumentation context is not available.") + null + } override suspend fun read(path: String): ByteArray { val resource = getResourceAsStream(path)