From 3b91723cec7d6340e452b91cec015f1ac99a2c3f Mon Sep 17 00:00:00 2001 From: sebaslogen Date: Sun, 19 Nov 2023 12:07:12 +0100 Subject: [PATCH] Try to fix flaky test --- .../sample/ComposeActivityRecreationTests.kt | 22 ++++++++----------- .../sample/utils/ComposeTestUtils.kt | 2 +- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/sample/src/test/java/com/sebaslogen/resacaapp/sample/ComposeActivityRecreationTests.kt b/sample/src/test/java/com/sebaslogen/resacaapp/sample/ComposeActivityRecreationTests.kt index 94740d36..44869e21 100644 --- a/sample/src/test/java/com/sebaslogen/resacaapp/sample/ComposeActivityRecreationTests.kt +++ b/sample/src/test/java/com/sebaslogen/resacaapp/sample/ComposeActivityRecreationTests.kt @@ -15,8 +15,6 @@ import com.sebaslogen.resacaapp.sample.ui.main.rememberScopedDestination import com.sebaslogen.resacaapp.sample.ui.main.showSingleScopedViewModel import com.sebaslogen.resacaapp.sample.ui.main.viewModelScopedDestination import com.sebaslogen.resacaapp.sample.utils.ComposeTestUtils -import kotlinx.coroutines.delay -import kotlinx.coroutines.runBlocking import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -94,17 +92,15 @@ class ComposeActivityRecreationTests : ComposeTestUtils { showSingleScopedViewModel = false // This is a fake night-mode change but it will remove Composable after Activity re-creation activity.recreate() printComposeUiTreeToLog() - runBlocking { - delay(COMPOSITION_RESUMED_TIMEOUT_IN_SECONDS * 1000) // Wait for the ViewModel to be cleared - printComposeUiTreeToLog() // Second print is needed to push the main thread forward - val finalAmountOfViewModelsCleared = viewModelsClearedGloballySharedCounter.get() - - // Then the scoped ViewModel disappears - onNodeWithTestTag("FakeInjectedViewModel Scoped", assertDisplayed = false).assertDoesNotExist() - assert(finalAmountOfViewModelsCleared == initialAmountOfViewModelsCleared + 1) { - "The amount of FakeInjectedViewModel that were cleared after key change ($finalAmountOfViewModelsCleared) " + - "was not higher that the amount before the key change ($initialAmountOfViewModelsCleared)" - } + Thread.sleep(COMPOSITION_RESUMED_TIMEOUT_IN_SECONDS * 1000) // Wait for the ViewModel to be cleared + printComposeUiTreeToLog() // Second print is needed to push the main thread forward + val finalAmountOfViewModelsCleared = viewModelsClearedGloballySharedCounter.get() + + // Then the scoped ViewModel disappears + onNodeWithTestTag("FakeInjectedViewModel Scoped", assertDisplayed = false).assertDoesNotExist() + assert(finalAmountOfViewModelsCleared == initialAmountOfViewModelsCleared + 1) { + "The amount of FakeInjectedViewModel that were cleared after key change ($finalAmountOfViewModelsCleared) " + + "was not higher that the amount before the key change ($initialAmountOfViewModelsCleared)" } } } diff --git a/sample/src/test/java/com/sebaslogen/resacaapp/sample/utils/ComposeTestUtils.kt b/sample/src/test/java/com/sebaslogen/resacaapp/sample/utils/ComposeTestUtils.kt index 55c795c5..0b75cab7 100644 --- a/sample/src/test/java/com/sebaslogen/resacaapp/sample/utils/ComposeTestUtils.kt +++ b/sample/src/test/java/com/sebaslogen/resacaapp/sample/utils/ComposeTestUtils.kt @@ -26,13 +26,13 @@ interface ComposeTestUtils { */ fun preTestInitializationToEmptyComposeDestination() { ComposeActivity.defaultDestination = emptyDestination + showSingleScopedViewModel = null } @Before @Throws(Exception::class) fun setUp() { ShadowLog.stream = System.out // Redirect Logcat to console output to read printToLog Compose debug messages - showSingleScopedViewModel = null } @After