From b4881ffe010673ede358e7e5c9edfdf32c0aecff Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 30 Jan 2024 21:02:36 +0100 Subject: [PATCH] Fix native xml parser and add ios native tests (#4207) Co-authored-by: Da Risk --- components/resources/library/build.gradle.kts | 4 + .../compose/resources/ComposeResourceTest.kt} | 28 ++- .../src/commonTest/resources/strings.xml | 1 + .../resources/ComposeResourceTest.desktop.kt | 169 ------------------ .../resources/vector/xmldom/DomXmlParser.kt | 4 +- components/test.sh | 6 +- 6 files changed, 21 insertions(+), 191 deletions(-) rename components/resources/library/src/{androidInstrumentedTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.android.kt => commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt} (90%) delete mode 100644 components/resources/library/src/desktopTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.desktop.kt diff --git a/components/resources/library/build.gradle.kts b/components/resources/library/build.gradle.kts index 56c2eaad08..4834fb33e5 100644 --- a/components/resources/library/build.gradle.kts +++ b/components/resources/library/build.gradle.kts @@ -1,3 +1,4 @@ +import org.jetbrains.compose.ExperimentalComposeLibrary import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl @@ -76,6 +77,9 @@ kotlin { dependencies { implementation(libs.kotlinx.coroutines.test) implementation(kotlin("test")) + implementation(compose.material3) + @OptIn(ExperimentalComposeLibrary::class) + implementation(compose.uiTest) } } val blockingMain by creating { diff --git a/components/resources/library/src/androidInstrumentedTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.android.kt b/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt similarity index 90% rename from components/resources/library/src/androidInstrumentedTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.android.kt rename to components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt index 6783007f16..206610ccdb 100644 --- a/components/resources/library/src/androidInstrumentedTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.android.kt +++ b/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt @@ -6,23 +6,14 @@ import androidx.compose.runtime.* import androidx.compose.ui.test.ExperimentalTestApi import androidx.compose.ui.test.runComposeUiTest import kotlinx.coroutines.flow.MutableStateFlow -import org.junit.Assert.assertEquals -import org.junit.Before -import org.junit.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith +import kotlin.test.* @OptIn(ExperimentalTestApi::class, ExperimentalResourceApi::class, InternalResourceApi::class) class ComposeResourceTest { - @Before - fun dropCaches() { + init { dropStringsCache() dropImageCache() - } - - @Before - fun configureTestEnvironment() { getResourceEnvironment = ::getTestEnvironment } @@ -111,6 +102,10 @@ class ComposeResourceTest { "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) @@ -127,12 +122,12 @@ class ComposeResourceTest { @Test fun testLoadStringResource() = runBlockingTest { - kotlin.test.assertEquals("Compose Resources App", getString(TestStringResource("app_name"))) - kotlin.test.assertEquals( + assertEquals("Compose Resources App", getString(TestStringResource("app_name"))) + assertEquals( "Hello, test-name! You have 42 new messages.", getString(TestStringResource("str_template"), "test-name", 42) ) - kotlin.test.assertEquals(listOf("item 1", "item 2", "item 3"), getStringArray(TestStringResource("str_arr"))) + assertEquals(listOf("item 1", "item 2", "item 3"), getStringArray(TestStringResource("str_arr"))) } @Test @@ -143,17 +138,18 @@ class ComposeResourceTest { val error = assertFailsWith { getString(TestStringResource("unknown_id")) } - kotlin.test.assertEquals("String ID=`unknown_id` is not found!", error.message) + assertEquals("String ID=`unknown_id` is not found!", error.message) } @Test fun testReadFileResource() = runBlockingTest { val bytes = readResourceBytes("strings.xml") - kotlin.test.assertEquals( + assertEquals( """ Compose Resources App 😊 Hello world! + Créer une table Hello, %1${'$'}s! You have %2${'$'}d new messages. item 1 diff --git a/components/resources/library/src/commonTest/resources/strings.xml b/components/resources/library/src/commonTest/resources/strings.xml index fd2f9a9bca..2f19269c97 100644 --- a/components/resources/library/src/commonTest/resources/strings.xml +++ b/components/resources/library/src/commonTest/resources/strings.xml @@ -1,6 +1,7 @@ Compose Resources App 😊 Hello world! + Créer une table Hello, %1$s! You have %2$d new messages. item 1 diff --git a/components/resources/library/src/desktopTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.desktop.kt b/components/resources/library/src/desktopTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.desktop.kt deleted file mode 100644 index 6783007f16..0000000000 --- a/components/resources/library/src/desktopTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.desktop.kt +++ /dev/null @@ -1,169 +0,0 @@ -package org.jetbrains.compose.resources - -import androidx.compose.foundation.Image -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 org.junit.Assert.assertEquals -import org.junit.Before -import org.junit.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith - -@OptIn(ExperimentalTestApi::class, ExperimentalResourceApi::class, InternalResourceApi::class) -class ComposeResourceTest { - - @Before - fun dropCaches() { - dropStringsCache() - dropImageCache() - } - - @Before - fun configureTestEnvironment() { - getResourceEnvironment = ::getTestEnvironment - } - - @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) - } - } - } - awaitIdle() - imagePathFlow.emit(DrawableResource("2.png")) - awaitIdle() - 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) - } - } - 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 - ) - } - } - - @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()) - } - } - 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 - ) - } - } - - @Test - fun testReadStringResource() = runComposeUiTest { - runBlockingTest { - setContent { - CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) { - assertEquals( - "Compose Resources App", - stringResource(TestStringResource("app_name")) - ) - 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() - } - } - - @Test - fun testLoadStringResource() = runBlockingTest { - kotlin.test.assertEquals("Compose Resources App", getString(TestStringResource("app_name"))) - kotlin.test.assertEquals( - "Hello, test-name! You have 42 new messages.", - getString(TestStringResource("str_template"), "test-name", 42) - ) - kotlin.test.assertEquals(listOf("item 1", "item 2", "item 3"), getStringArray(TestStringResource("str_arr"))) - } - - @Test - fun testMissingResource() = runBlockingTest { - assertFailsWith { - readResourceBytes("missing.png") - } - val error = assertFailsWith { - getString(TestStringResource("unknown_id")) - } - kotlin.test.assertEquals("String ID=`unknown_id` is not found!", error.message) - } - - @Test - fun testReadFileResource() = runBlockingTest { - val bytes = readResourceBytes("strings.xml") - kotlin.test.assertEquals( - """ - - Compose Resources App - 😊 Hello world! - Hello, %1${'$'}s! You have %2${'$'}d new messages. - - item 1 - item 2 - item 3 - - - - """.trimIndent(), - bytes.decodeToString() - ) - } -} diff --git a/components/resources/library/src/nativeMain/kotlin/org/jetbrains/compose/resources/vector/xmldom/DomXmlParser.kt b/components/resources/library/src/nativeMain/kotlin/org/jetbrains/compose/resources/vector/xmldom/DomXmlParser.kt index bdc7aeda03..3d2a94a47b 100644 --- a/components/resources/library/src/nativeMain/kotlin/org/jetbrains/compose/resources/vector/xmldom/DomXmlParser.kt +++ b/components/resources/library/src/nativeMain/kotlin/org/jetbrains/compose/resources/vector/xmldom/DomXmlParser.kt @@ -94,7 +94,9 @@ private class DomXmlParser : NSObject(), NSXMLParserDelegateProtocol { } override fun parser(parser: NSXMLParser, foundCharacters: String) { - nodeStack.lastOrNull()?.textContent = foundCharacters + nodeStack.lastOrNull()?.let { node -> + node.textContent = node.textContent.orEmpty() + foundCharacters + } } override fun parser( diff --git a/components/test.sh b/components/test.sh index a16a91fcc0..03985cc26e 100755 --- a/components/test.sh +++ b/components/test.sh @@ -2,10 +2,6 @@ cd "$(dirname "$0")" # Run always in current dir set -euo pipefail # Fail fast -# Unit tests -./gradlew :resources:library:test ./gradlew :resources:library:desktopTest - -# Android integration tests ./gradlew :resources:library:pixel5DebugAndroidTest - +./gradlew :resources:library:iosSimulatorArm64Test