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. 89
      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() }
}

89
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.runtime.*
import androidx.compose.ui.test.ExperimentalTestApi import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.runComposeUiTest import androidx.compose.ui.test.runComposeUiTest
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.test.runTest
import kotlin.test.* import kotlin.test.*
@OptIn(ExperimentalTestApi::class, ExperimentalResourceApi::class, InternalResourceApi::class) @OptIn(ExperimentalTestApi::class, ExperimentalResourceApi::class, InternalResourceApi::class)
@ -19,109 +19,100 @@ class ComposeResourceTest {
@Test @Test
fun testCountRecompositions() = runComposeUiTest { fun testCountRecompositions() = runComposeUiTest {
runBlockingTest { var res by mutableStateOf(DrawableResource("1.png"))
val imagePathFlow = MutableStateFlow(DrawableResource("1.png"))
val recompositionsCounter = RecompositionsCounter() val recompositionsCounter = RecompositionsCounter()
setContent { setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) { CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
val res by imagePathFlow.collectAsState()
val imgRes = imageResource(res) val imgRes = imageResource(res)
recompositionsCounter.content { recompositionsCounter.content {
Image(bitmap = imgRes, contentDescription = null) Image(bitmap = imgRes, contentDescription = null)
} }
} }
} }
awaitIdle() waitForIdle()
imagePathFlow.emit(DrawableResource("2.png")) res = DrawableResource("2.png")
awaitIdle() waitForIdle()
assertEquals(2, recompositionsCounter.count) assertEquals(2, recompositionsCounter.count)
} }
}
@Test @Test
fun testImageResourceCache() = runComposeUiTest { fun testImageResourceCache() = runComposeUiTest {
runBlockingTest {
val testResourceReader = TestResourceReader() val testResourceReader = TestResourceReader()
val imagePathFlow = MutableStateFlow(DrawableResource("1.png")) var res by mutableStateOf(DrawableResource("1.png"))
setContent { setContent {
CompositionLocalProvider( CompositionLocalProvider(
LocalResourceReader provides testResourceReader, LocalResourceReader provides testResourceReader,
LocalComposeEnvironment provides TestComposeEnvironment LocalComposeEnvironment provides TestComposeEnvironment
) { ) {
val res by imagePathFlow.collectAsState()
Image(painterResource(res), null) Image(painterResource(res), null)
} }
} }
awaitIdle() waitForIdle()
imagePathFlow.emit(DrawableResource("2.png")) res = DrawableResource("2.png")
awaitIdle() waitForIdle()
imagePathFlow.emit(DrawableResource("1.png")) res = DrawableResource("1.png")
awaitIdle() waitForIdle()
assertEquals( assertEquals(
expected = listOf("1.png", "2.png"), //no second read of 1.png expected = listOf("1.png", "2.png"), //no second read of 1.png
actual = testResourceReader.readPaths actual = testResourceReader.readPaths
) )
} }
}
@Test @Test
fun testStringResourceCache() = runComposeUiTest { fun testStringResourceCache() = runComposeUiTest {
runBlockingTest {
val testResourceReader = TestResourceReader() val testResourceReader = TestResourceReader()
val stringIdFlow = MutableStateFlow(TestStringResource("app_name")) var res by mutableStateOf(TestStringResource("app_name"))
var str = ""
setContent { setContent {
CompositionLocalProvider( CompositionLocalProvider(
LocalResourceReader provides testResourceReader, LocalResourceReader provides testResourceReader,
LocalComposeEnvironment provides TestComposeEnvironment LocalComposeEnvironment provides TestComposeEnvironment
) { ) {
val res by stringIdFlow.collectAsState() str = stringResource(res)
Text(stringResource(res)) Text(str)
Text(stringArrayResource(TestStringResource("str_arr")).joinToString()) Text(stringArrayResource(TestStringResource("str_arr")).joinToString())
} }
} }
awaitIdle() waitForIdle()
stringIdFlow.emit(TestStringResource("hello")) assertEquals(str, "Compose Resources App")
awaitIdle() res = TestStringResource("hello")
stringIdFlow.emit(TestStringResource("app_name")) waitForIdle()
awaitIdle() assertEquals(str, "\uD83D\uDE0A Hello world!")
res = TestStringResource("app_name")
waitForIdle()
assertEquals(str, "Compose Resources App")
assertEquals( assertEquals(
expected = listOf("strings.xml"), //just one string.xml read expected = listOf("strings.xml"), //just one string.xml read
actual = testResourceReader.readPaths actual = testResourceReader.readPaths
) )
} }
}
@Test @Test
fun testReadStringResource() = runComposeUiTest { fun testReadStringResource() = runComposeUiTest {
runBlockingTest { var app_name = ""
var accentuated_characters = ""
var str_template = ""
var str_arr = emptyList<String>()
setContent { setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) { CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
assertEquals( app_name = stringResource(TestStringResource("app_name"))
"Compose Resources App", accentuated_characters = stringResource(TestStringResource("accentuated_characters"))
stringResource(TestStringResource("app_name")) str_template = stringResource(TestStringResource("str_template"), "test-name", 42)
) str_arr = stringArrayResource(TestStringResource("str_arr"))
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"))
)
}
} }
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 @Test
fun testLoadStringResource() = runBlockingTest { fun testLoadStringResource() = runTest {
assertEquals("Compose Resources App", getString(TestStringResource("app_name"))) assertEquals("Compose Resources App", getString(TestStringResource("app_name")))
assertEquals( assertEquals(
"Hello, test-name! You have 42 new messages.", "Hello, test-name! You have 42 new messages.",
@ -131,7 +122,7 @@ class ComposeResourceTest {
} }
@Test @Test
fun testMissingResource() = runBlockingTest { fun testMissingResource() = runTest {
assertFailsWith<MissingResourceException> { assertFailsWith<MissingResourceException> {
readResourceBytes("missing.png") readResourceBytes("missing.png")
} }
@ -142,7 +133,7 @@ class ComposeResourceTest {
} }
@Test @Test
fun testReadFileResource() = runBlockingTest { fun testReadFileResource() = runTest {
val bytes = readResourceBytes("strings.xml") val bytes = readResourceBytes("strings.xml")
assertEquals( 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) @OptIn(ExperimentalResourceApi::class, InternalResourceApi::class)
class ResourceTest { class ResourceTest {
@Test @Test
fun testResourceEquals() = runBlockingTest { fun testResourceEquals() {
assertEquals(DrawableResource("a"), DrawableResource("a")) assertEquals(DrawableResource("a"), DrawableResource("a"))
} }
@Test @Test
fun testResourceNotEquals() = runBlockingTest { fun testResourceNotEquals() {
assertNotEquals(DrawableResource("a"), DrawableResource("b")) 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 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) @OptIn(InternalResourceApi::class, ExperimentalResourceApi::class)
internal fun TestStringResource(key: String) = StringResource( internal fun TestStringResource(key: String) = StringResource(
"STRING:$key", "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) //todo in future bundle resources with app and use all sourceSets (skikoMain, nativeMain)
contentsAtPath("$currentDirectoryPath/src/macosMain/resources/$path") contentsAtPath("$currentDirectoryPath/src/macosMain/resources/$path")
?: contentsAtPath("$currentDirectoryPath/src/commonMain/resources/$path") ?: contentsAtPath("$currentDirectoryPath/src/commonMain/resources/$path")
?: contentsAtPath("$currentDirectoryPath/src/commonTest/resources/$path")
} ?: throw MissingResourceException(path) } ?: throw MissingResourceException(path)
return ByteArray(contentsAtPath.length.toInt()).apply { return ByteArray(contentsAtPath.length.toInt()).apply {
usePinned { 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