Browse Source

Refactor compose library tests

pull/4215/head
Konstantin Tskhovrebov 10 months ago
parent
commit
4a65b1a1d8
  1. 12
      components/resources/library/src/blockingTest/kotlin/org/jetbrains/compose/resources/TestUtils.blocking.kt
  2. 155
      components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt
  3. 4
      components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ResourceTest.kt
  4. 6
      components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt
  5. 7
      components/resources/library/src/jsTest/kotlin/org/jetbrains/compose/resources/TestUtils.js.kt
  6. 1
      components/resources/library/src/macosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.macos.kt
  7. 17
      components/resources/library/src/wasmJsTest/kotlin/org/jetbrains/compose/resources/TestUtils.wasmJs.kt

12
components/resources/library/src/blockingTest/kotlin/org/jetbrains/compose/resources/TestUtils.blocking.kt

@ -1,12 +0,0 @@
package org.jetbrains.compose.resources
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
actual typealias TestReturnType = Unit
actual fun runBlockingTest(block: suspend CoroutineScope.() -> Unit): TestReturnType {
return runBlocking { block() }
}

155
components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt

@ -5,7 +5,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.runComposeUiTest
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import kotlin.test.*
@OptIn(ExperimentalTestApi::class, ExperimentalResourceApi::class, InternalResourceApi::class)
@ -19,109 +19,100 @@ class ComposeResourceTest {
@Test
fun testCountRecompositions() = runComposeUiTest {
runBlockingTest {
val imagePathFlow = MutableStateFlow(DrawableResource("1.png"))
val recompositionsCounter = RecompositionsCounter()
setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
val res by imagePathFlow.collectAsState()
val imgRes = imageResource(res)
recompositionsCounter.content {
Image(bitmap = imgRes, contentDescription = null)
}
var res by mutableStateOf(DrawableResource("1.png"))
val recompositionsCounter = RecompositionsCounter()
setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
val imgRes = imageResource(res)
recompositionsCounter.content {
Image(bitmap = imgRes, contentDescription = null)
}
}
awaitIdle()
imagePathFlow.emit(DrawableResource("2.png"))
awaitIdle()
assertEquals(2, recompositionsCounter.count)
}
waitForIdle()
res = DrawableResource("2.png")
waitForIdle()
assertEquals(2, recompositionsCounter.count)
}
@Test
fun testImageResourceCache() = runComposeUiTest {
runBlockingTest {
val testResourceReader = TestResourceReader()
val imagePathFlow = MutableStateFlow(DrawableResource("1.png"))
setContent {
CompositionLocalProvider(
LocalResourceReader provides testResourceReader,
LocalComposeEnvironment provides TestComposeEnvironment
) {
val res by imagePathFlow.collectAsState()
Image(painterResource(res), null)
}
val testResourceReader = TestResourceReader()
var res by mutableStateOf(DrawableResource("1.png"))
setContent {
CompositionLocalProvider(
LocalResourceReader provides testResourceReader,
LocalComposeEnvironment provides TestComposeEnvironment
) {
Image(painterResource(res), null)
}
awaitIdle()
imagePathFlow.emit(DrawableResource("2.png"))
awaitIdle()
imagePathFlow.emit(DrawableResource("1.png"))
awaitIdle()
assertEquals(
expected = listOf("1.png", "2.png"), //no second read of 1.png
actual = testResourceReader.readPaths
)
}
waitForIdle()
res = DrawableResource("2.png")
waitForIdle()
res = DrawableResource("1.png")
waitForIdle()
assertEquals(
expected = listOf("1.png", "2.png"), //no second read of 1.png
actual = testResourceReader.readPaths
)
}
@Test
fun testStringResourceCache() = runComposeUiTest {
runBlockingTest {
val testResourceReader = TestResourceReader()
val stringIdFlow = MutableStateFlow(TestStringResource("app_name"))
setContent {
CompositionLocalProvider(
LocalResourceReader provides testResourceReader,
LocalComposeEnvironment provides TestComposeEnvironment
) {
val res by stringIdFlow.collectAsState()
Text(stringResource(res))
Text(stringArrayResource(TestStringResource("str_arr")).joinToString())
}
val testResourceReader = TestResourceReader()
var res by mutableStateOf(TestStringResource("app_name"))
var str = ""
setContent {
CompositionLocalProvider(
LocalResourceReader provides testResourceReader,
LocalComposeEnvironment provides TestComposeEnvironment
) {
str = stringResource(res)
Text(str)
Text(stringArrayResource(TestStringResource("str_arr")).joinToString())
}
awaitIdle()
stringIdFlow.emit(TestStringResource("hello"))
awaitIdle()
stringIdFlow.emit(TestStringResource("app_name"))
awaitIdle()
assertEquals(
expected = listOf("strings.xml"), //just one string.xml read
actual = testResourceReader.readPaths
)
}
waitForIdle()
assertEquals(str, "Compose Resources App")
res = TestStringResource("hello")
waitForIdle()
assertEquals(str, "\uD83D\uDE0A Hello world!")
res = TestStringResource("app_name")
waitForIdle()
assertEquals(str, "Compose Resources App")
assertEquals(
expected = listOf("strings.xml"), //just one string.xml read
actual = testResourceReader.readPaths
)
}
@Test
fun testReadStringResource() = runComposeUiTest {
runBlockingTest {
setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
assertEquals(
"Compose Resources App",
stringResource(TestStringResource("app_name"))
)
assertEquals(
"Créer une table",
stringResource(TestStringResource("accentuated_characters"))
)
assertEquals(
"Hello, test-name! You have 42 new messages.",
stringResource(TestStringResource("str_template"), "test-name", 42)
)
assertEquals(
listOf("item 1", "item 2", "item 3"),
stringArrayResource(TestStringResource("str_arr"))
)
}
var app_name = ""
var accentuated_characters = ""
var str_template = ""
var str_arr = emptyList<String>()
setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
app_name = stringResource(TestStringResource("app_name"))
accentuated_characters = stringResource(TestStringResource("accentuated_characters"))
str_template = stringResource(TestStringResource("str_template"), "test-name", 42)
str_arr = stringArrayResource(TestStringResource("str_arr"))
}
awaitIdle()
}
waitForIdle()
assertEquals("Compose Resources App", app_name)
assertEquals("Créer une table", accentuated_characters)
assertEquals("Hello, test-name! You have 42 new messages.", str_template)
assertEquals(listOf("item 1", "item 2", "item 3"), str_arr)
}
@Test
fun testLoadStringResource() = runBlockingTest {
fun testLoadStringResource() = runTest {
assertEquals("Compose Resources App", getString(TestStringResource("app_name")))
assertEquals(
"Hello, test-name! You have 42 new messages.",
@ -131,7 +122,7 @@ class ComposeResourceTest {
}
@Test
fun testMissingResource() = runBlockingTest {
fun testMissingResource() = runTest {
assertFailsWith<MissingResourceException> {
readResourceBytes("missing.png")
}
@ -142,7 +133,7 @@ class ComposeResourceTest {
}
@Test
fun testReadFileResource() = runBlockingTest {
fun testReadFileResource() = runTest {
val bytes = readResourceBytes("strings.xml")
assertEquals(
"""

4
components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ResourceTest.kt

@ -13,12 +13,12 @@ import kotlin.test.*
@OptIn(ExperimentalResourceApi::class, InternalResourceApi::class)
class ResourceTest {
@Test
fun testResourceEquals() = runBlockingTest {
fun testResourceEquals() {
assertEquals(DrawableResource("a"), DrawableResource("a"))
}
@Test
fun testResourceNotEquals() = runBlockingTest {
fun testResourceNotEquals() {
assertNotEquals(DrawableResource("a"), DrawableResource("b"))
}

6
components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt

@ -1,11 +1,5 @@
package org.jetbrains.compose.resources
import kotlinx.coroutines.CoroutineScope
expect class TestReturnType
expect fun runBlockingTest(block: suspend CoroutineScope.() -> Unit): TestReturnType
@OptIn(InternalResourceApi::class, ExperimentalResourceApi::class)
internal fun TestStringResource(key: String) = StringResource(
"STRING:$key",

7
components/resources/library/src/jsTest/kotlin/org/jetbrains/compose/resources/TestUtils.js.kt

@ -1,7 +0,0 @@
package org.jetbrains.compose.resources
import kotlinx.coroutines.*
actual typealias TestReturnType = Any
actual fun runBlockingTest(block: suspend CoroutineScope.() -> Unit): TestReturnType =
TODO("Implement if necessary. We focus on k/wasm target for now")

1
components/resources/library/src/macosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.macos.kt

@ -13,6 +13,7 @@ actual suspend fun readResourceBytes(path: String): ByteArray {
//todo in future bundle resources with app and use all sourceSets (skikoMain, nativeMain)
contentsAtPath("$currentDirectoryPath/src/macosMain/resources/$path")
?: contentsAtPath("$currentDirectoryPath/src/commonMain/resources/$path")
?: contentsAtPath("$currentDirectoryPath/src/commonTest/resources/$path")
} ?: throw MissingResourceException(path)
return ByteArray(contentsAtPath.length.toInt()).apply {
usePinned {

17
components/resources/library/src/wasmJsTest/kotlin/org/jetbrains/compose/resources/TestUtils.wasmJs.kt

@ -1,17 +0,0 @@
package org.jetbrains.compose.resources
import kotlinx.coroutines.*
import kotlinx.coroutines.test.runTest
@JsFun("() => ''")
private external fun jsRef(): JsAny
actual typealias TestReturnType = Any
/**
* Runs the [block] in a coroutine.
*/
actual fun runBlockingTest(block: suspend CoroutineScope.() -> Unit): TestReturnType = MainScope().promise {
block()
jsRef()
}
Loading…
Cancel
Save