|
|
@ -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( |
|
|
|
""" |
|
|
|
""" |
|
|
|