This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2022 Google LLC. | |
// SPDX-License-Identifier: Apache-2.0 | |
fun ComposeContentTestRule.waitUntilNodeCount( | |
matcher: SemanticsMatcher, | |
count: Int, | |
timeoutMillis: Long = 1_000L | |
) { | |
this.waitUntil(timeoutMillis) { | |
this.onAllNodes(matcher).fetchSemanticsNodes().size == count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Click on a button | |
composeTestRule.onNodeWithText("Continue").performClick() | |
// Wait until there's one element with a "Welcome" text | |
composeTestRule.waitUntil { | |
composeTestRule | |
.onAllNodesWithText("Welcome") | |
.fetchSemanticsNodes().size == 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composeTestRule.onNodeWithText("Continue¨) .performClick() | |
Thread.sleep(2000) // don't do this | |
composeTestRule.onNodeWithText("Welcome").assertExists() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyRepo(dataSource: MyDataSource) { | |
suspend fun getItems() { | |
idlingResource.setBusy() // App is busy… test must wait. | |
val result = dataSource.getTasks() | |
idlingResource.setIdle() // App is idle, ready for next operation. | |
return result | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val wrapped: IdlingResourceScheduler = Rx3Idler.wrap(myScheduler, "My Scheduler") | |
IdlingRegistry.getInstance().register(wrapped) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun channelShareInTest() { | |
val scope = TestCoroutineScope() | |
val _channel = Channel<Int>(capacity = 2, onBufferOverflow = BufferOverflow.DROP_OLDEST) | |
val exposedFlow: SharedFlow<Int> = _channel | |
.consumeAsFlow().shareIn(scope, SharingStarted.WhileSubscribed()) | |
val testResults = mutableListOf<Int>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun channelConflatedTest() { | |
val _channel = Channel<Int>(capacity = CONFLATED) | |
val exposedFlow: Flow<Int> = _channel.receiveAsFlow() | |
val testResults = mutableListOf<Int>() | |
val testResults2 = mutableListOf<Int>() | |
val testResults3 = mutableListOf<Int>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onCreateView(...) { | |
viewLifecycleOwner.lifecycleScope.launch { | |
viewLifecycleOwner.lifecycle.repeatOnLifecycle(STARTED) { | |
myViewModel.myUiState.collect { ... } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel(...) : ViewModel() { | |
val result = userId.mapLatest { newUserId -> | |
repository.observeItem(newUserId) | |
}.stateIn( | |
scope = viewModelScope, | |
started = WhileSubscribed(5000), | |
initialValue = Result.Loading | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val result: StateFlow<Result<UiState>> = someFlow | |
.stateIn( | |
scope = viewModelScope, | |
started = WhileSubscribed(5000), | |
initialValue = Result.Loading | |
) |