Skip to content

Commit 66a14ec

Browse files
committed
refactor: Remove runBlockingAll from UI tests
- The `runBlockingAll` utility function has been removed from several UI test cases (`SignInTestCase`, `EditTitleAfterCreateTestCase`, `LocaleTestCase`, `EditTitleAfterSaveTestCase`). - String resource fetching within tests now happens directly in the coroutine scope provided by `runTest`, simplifying the code. - `DbTestEncryptor` has been updated to be a `suspend` function, removing the need for `runBlockingAll`. - In `CustomActivityScenarioRule` for Android tests, the `beforeActivityLaunched` lambda is now a `suspend` function and is invoked within a `runBlocking` block, aligning with the removal of the custom blocking utility.
1 parent 9d62a12 commit 66a14ec

File tree

6 files changed

+13
-17
lines changed

6 files changed

+13
-17
lines changed

app/android/src/androidTest/java/com/softartdev/notedelight/ui/CustomActivityScenarioRule.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ import android.app.Activity
44
import androidx.activity.ComponentActivity
55
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
66
import androidx.test.core.app.ActivityScenario
7+
import kotlinx.coroutines.runBlocking
78
import org.junit.rules.ExternalResource
89

910
inline fun <reified A : ComponentActivity> customAndroidComposeRule(
10-
noinline beforeActivityLaunched: () -> Unit
11+
noinline beforeActivityLaunched: suspend () -> Unit
1112
): AndroidComposeTestRule<CustomActivityScenarioRule<A>, A> {
1213
return customAndroidComposeRule(A::class.java, beforeActivityLaunched)
1314
}
1415

1516
fun <A : ComponentActivity> customAndroidComposeRule(
1617
activityClass: Class<A>,
17-
beforeActivityLaunched: () -> Unit
18+
beforeActivityLaunched: suspend () -> Unit
1819
): AndroidComposeTestRule<CustomActivityScenarioRule<A>, A> = AndroidComposeTestRule(
1920
activityRule = CustomActivityScenarioRule(activityClass, beforeActivityLaunched),
2021
activityProvider = ::provideActivity
@@ -30,13 +31,13 @@ fun <A : Activity> provideActivity(customActivityScenarioRule: CustomActivitySce
3031

3132
class CustomActivityScenarioRule<A : Activity>(
3233
private val activityClass: Class<A>,
33-
private val beforeActivityLaunched: () -> Unit
34+
private val beforeActivityLaunched: suspend () -> Unit
3435
) : ExternalResource() {
3536

3637
internal lateinit var scenario: ActivityScenario<A>
3738

3839
override fun before() {
39-
beforeActivityLaunched()
40+
runBlocking { beforeActivityLaunched() }
4041
scenario = ActivityScenario.launch(activityClass)
4142
}
4243

ui/test/src/commonMain/kotlin/com/softartdev/notedelight/DbTestEncryptor.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ package com.softartdev.notedelight
33
import co.touchlab.kermit.Logger
44
import com.softartdev.notedelight.model.PlatformSQLiteState
55
import com.softartdev.notedelight.repository.SafeRepo
6-
import com.softartdev.notedelight.util.runBlockingAll
76
import kotlinx.coroutines.delay
87
import org.koin.mp.KoinPlatform
98
import kotlin.time.Duration.Companion.seconds
109

11-
object DbTestEncryptor : () -> Unit {
10+
object DbTestEncryptor : suspend () -> Unit {
1211
private val logger = Logger.withTag(this@DbTestEncryptor::class.simpleName.toString())
1312
const val PASSWORD = "password"
1413

15-
override fun invoke() = runBlockingAll {
14+
override suspend fun invoke() {
1615
val safeRepo: SafeRepo by KoinPlatform.getKoin().inject()
1716
while (safeRepo.databaseState == PlatformSQLiteState.DOES_NOT_EXIST) {
1817
safeRepo.buildDbIfNeed()

ui/test/src/commonMain/kotlin/com/softartdev/notedelight/ui/cases/EditTitleAfterCreateTestCase.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import androidx.compose.ui.test.performTextInput
1010
import androidx.compose.ui.test.performTextReplacement
1111
import com.softartdev.notedelight.ui.BaseTestCase
1212
import com.softartdev.notedelight.ui.screen.MainTestScreen.Companion.noteItemTitleText
13-
import com.softartdev.notedelight.util.runBlockingAll
1413
import com.softartdev.notedelight.waitUntilDisplayed
1514
import kotlinx.coroutines.test.TestResult
1615
import kotlinx.coroutines.test.runTest
@@ -41,7 +40,7 @@ class EditTitleAfterCreateTestCase(
4140
confirmDialogButtonSNI.performClick()
4241
}
4342
composeUiTest
44-
.onNodeWithContentDescription(label = runBlockingAll { getString(Res.string.enter_title) })
43+
.onNodeWithContentDescription(label = getString(Res.string.enter_title))
4544
.assertDoesNotExist()
4645
saveNoteMenuButtonSNI.performClick()
4746
backButtonSNI.performClick()

ui/test/src/commonMain/kotlin/com/softartdev/notedelight/ui/cases/EditTitleAfterSaveTestCase.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import androidx.compose.ui.test.performTextInput
1010
import androidx.compose.ui.test.performTextReplacement
1111
import com.softartdev.notedelight.ui.BaseTestCase
1212
import com.softartdev.notedelight.ui.screen.MainTestScreen.Companion.noteItemTitleText
13-
import com.softartdev.notedelight.util.runBlockingAll
1413
import com.softartdev.notedelight.waitUntilDisplayed
1514
import kotlinx.coroutines.test.TestResult
1615
import kotlinx.coroutines.test.runTest
@@ -47,7 +46,7 @@ class EditTitleAfterSaveTestCase(
4746
confirmDialogButtonSNI.performClick()
4847
}
4948
composeUiTest
50-
.onNodeWithContentDescription(label = runBlockingAll { getString(Res.string.enter_title) })
49+
.onNodeWithContentDescription(label = getString(Res.string.enter_title))
5150
.assertDoesNotExist()
5251
saveNoteMenuButtonSNI.performClick()
5352
backButtonSNI.performClick()

ui/test/src/commonMain/kotlin/com/softartdev/notedelight/ui/cases/LocaleTestCase.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import com.softartdev.notedelight.ASSERT_WAIT_TIMEOUT_MILLIS
1111
import com.softartdev.notedelight.interactor.LocaleInteractor
1212
import com.softartdev.notedelight.model.LanguageEnum
1313
import com.softartdev.notedelight.ui.BaseTestCase
14-
import com.softartdev.notedelight.util.runBlockingAll
1514
import com.softartdev.notedelight.waitUntilDisplayed
1615
import com.softartdev.notedelight.waitUntilSelected
1716
import kotlinx.coroutines.test.TestResult
@@ -43,7 +42,7 @@ class LocaleTestCase(
4342
confirmDialogButtonSNI.performClick()
4443
}
4544
composeUiTest.waitUntilDoesNotExist(
46-
matcher = hasText(runBlockingAll { getString(Res.string.choose_language) }),
45+
matcher = hasText(getString(Res.string.choose_language)),
4746
timeoutMillis = ASSERT_WAIT_TIMEOUT_MILLIS,
4847
)
4948
assertEquals(languageEnum, localeInteractor.languageEnum)

ui/test/src/commonMain/kotlin/com/softartdev/notedelight/ui/cases/SignInTestCase.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import com.softartdev.notedelight.DbTestEncryptor
1212
import com.softartdev.notedelight.ui.BaseTestCase
1313
import com.softartdev.notedelight.waitAssert
1414
import com.softartdev.notedelight.waitUntilDisplayed
15-
import com.softartdev.notedelight.util.runBlockingAll
1615
import kotlinx.coroutines.test.TestResult
1716
import kotlinx.coroutines.test.runTest
1817
import notedelight.ui.shared.generated.resources.Res
@@ -31,21 +30,21 @@ class SignInTestCase(
3130
composeUiTest.waitUntilDisplayed("passwordField", blockSNI = ::passwordFieldSNI)
3231

3332
passwordLabelSNI.assertIsDisplayed()
34-
.assertTextEquals(runBlockingAll { getString(Res.string.enter_password) })
33+
.assertTextEquals(getString(Res.string.enter_password))
3534

3635
passwordVisibilitySNI.assertIsDisplayed()
3736
.performClick()
3837

3938
signInButtonSNI.performClick()
40-
val emptyPassTitle = runBlockingAll { getString(Res.string.empty_password) }
39+
val emptyPassTitle = getString(Res.string.empty_password)
4140
composeUiTest.waitAssert("password label has empty pass text") {
4241
passwordLabelSNI.assertTextEquals(emptyPassTitle)
4342
}
4443
passwordFieldSNI.performTextReplacement(text = "incorrect password")
4544
closeSoftKeyboard()
4645
signInButtonSNI.performClick()
4746

48-
passwordLabelSNI.assertTextEquals(runBlockingAll { getString(Res.string.incorrect_password) })
47+
passwordLabelSNI.assertTextEquals(getString(Res.string.incorrect_password))
4948

5049
passwordFieldSNI.performTextReplacement(text = DbTestEncryptor.PASSWORD)
5150
closeSoftKeyboard()

0 commit comments

Comments
 (0)