From 4a65b1a1d87a2920a611bc27e3eef14d5a87c349 Mon Sep 17 00:00:00 2001 From: Konstantin Tskhovrebov Date: Wed, 31 Jan 2024 15:54:03 +0100 Subject: [PATCH] Refactor compose library tests --- .../compose/resources/TestUtils.blocking.kt | 12 -- .../compose/resources/ComposeResourceTest.kt | 155 +++++++++--------- .../compose/resources/ResourceTest.kt | 4 +- .../jetbrains/compose/resources/TestUtils.kt | 6 - .../compose/resources/TestUtils.js.kt | 7 - .../compose/resources/ResourceReader.macos.kt | 1 + .../compose/resources/TestUtils.wasmJs.kt | 17 -- 7 files changed, 76 insertions(+), 126 deletions(-) delete mode 100644 components/resources/library/src/blockingTest/kotlin/org/jetbrains/compose/resources/TestUtils.blocking.kt delete mode 100644 components/resources/library/src/jsTest/kotlin/org/jetbrains/compose/resources/TestUtils.js.kt delete mode 100644 components/resources/library/src/wasmJsTest/kotlin/org/jetbrains/compose/resources/TestUtils.wasmJs.kt diff --git a/components/resources/library/src/blockingTest/kotlin/org/jetbrains/compose/resources/TestUtils.blocking.kt b/components/resources/library/src/blockingTest/kotlin/org/jetbrains/compose/resources/TestUtils.blocking.kt deleted file mode 100644 index 94a48bf768..0000000000 --- a/components/resources/library/src/blockingTest/kotlin/org/jetbrains/compose/resources/TestUtils.blocking.kt +++ /dev/null @@ -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() } -} - diff --git a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt b/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt index 206610ccdb..3160db93b2 100644 --- a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt +++ b/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() + 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 { readResourceBytes("missing.png") } @@ -142,7 +133,7 @@ class ComposeResourceTest { } @Test - fun testReadFileResource() = runBlockingTest { + fun testReadFileResource() = runTest { val bytes = readResourceBytes("strings.xml") assertEquals( """ diff --git a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ResourceTest.kt b/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ResourceTest.kt index 36fd4230c8..f2a516618a 100644 --- a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ResourceTest.kt +++ b/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")) } diff --git a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt b/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt index cd0658f53d..7675790707 100644 --- a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt +++ b/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", diff --git a/components/resources/library/src/jsTest/kotlin/org/jetbrains/compose/resources/TestUtils.js.kt b/components/resources/library/src/jsTest/kotlin/org/jetbrains/compose/resources/TestUtils.js.kt deleted file mode 100644 index 2d9dfb5498..0000000000 --- a/components/resources/library/src/jsTest/kotlin/org/jetbrains/compose/resources/TestUtils.js.kt +++ /dev/null @@ -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") \ No newline at end of file diff --git a/components/resources/library/src/macosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.macos.kt b/components/resources/library/src/macosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.macos.kt index 03d515621c..e226506458 100644 --- a/components/resources/library/src/macosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.macos.kt +++ b/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 { diff --git a/components/resources/library/src/wasmJsTest/kotlin/org/jetbrains/compose/resources/TestUtils.wasmJs.kt b/components/resources/library/src/wasmJsTest/kotlin/org/jetbrains/compose/resources/TestUtils.wasmJs.kt deleted file mode 100644 index 0cfd4d8b96..0000000000 --- a/components/resources/library/src/wasmJsTest/kotlin/org/jetbrains/compose/resources/TestUtils.wasmJs.kt +++ /dev/null @@ -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() -} \ No newline at end of file