Skip to content

Instantly share code, notes, and snippets.

View JoseAlcerreca's full-sized avatar

Jose Alcérreca JoseAlcerreca

View GitHub Profile
// 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
// 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
}
composeTestRule.onNodeWithText("Continue¨) .performClick()
Thread.sleep(2000) // don't do this
composeTestRule.onNodeWithText("Welcome").assertExists()
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
}
}
val wrapped: IdlingResourceScheduler = Rx3Idler.wrap(myScheduler, "My Scheduler")
IdlingRegistry.getInstance().register(wrapped)
@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>()
@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>()
onCreateView(...) {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(STARTED) {
myViewModel.myUiState.collect { ... }
}
}
}
class MyViewModel(...) : ViewModel() {
val result = userId.mapLatest { newUserId ->
repository.observeItem(newUserId)
}.stateIn(
scope = viewModelScope,
started = WhileSubscribed(5000),
initialValue = Result.Loading
)
}
val result: StateFlow<Result<UiState>> = someFlow
.stateIn(
scope = viewModelScope,
started = WhileSubscribed(5000),
initialValue = Result.Loading
)