Skip to content

Commit

Permalink
Try to fix flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaslogen committed Nov 19, 2023
1 parent 8c98f98 commit 3b91723
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b91723

Please sign in to comment.