Skip to content

Commit

Permalink
[resources] Add compile only dependency on "androidx.test:monitor" to…
Browse files Browse the repository at this point in the history
… configure test context
  • Loading branch information
terrakok committed Aug 28, 2024
1 parent 662f4fd commit dd83199
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
3 changes: 2 additions & 1 deletion components/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
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" }
Expand Down
4 changes: 4 additions & 0 deletions components/resources/library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit dd83199

Please sign in to comment.