|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.android.example.flow.test |
| 18 | + |
| 19 | +import app.cash.turbine.test |
| 20 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 21 | +import kotlinx.coroutines.flow.Flow |
| 22 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 23 | +import kotlinx.coroutines.flow.first |
| 24 | +import kotlinx.coroutines.flow.flow |
| 25 | +import kotlinx.coroutines.flow.map |
| 26 | +import kotlinx.coroutines.flow.toList |
| 27 | +import kotlinx.coroutines.launch |
| 28 | +import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 29 | +import kotlinx.coroutines.test.runTest |
| 30 | +import org.junit.Assert.assertEquals |
| 31 | +import org.junit.Test |
| 32 | + |
| 33 | +private const val ITEM_1 = "item1" |
| 34 | +private val ALL_MESSAGES = listOf("message1", "message2") |
| 35 | + |
| 36 | +interface MyRepository { |
| 37 | + fun observeCount(): Flow<String> |
| 38 | + fun observeChatMessages(): Flow<List<String>> |
| 39 | +} |
| 40 | + |
| 41 | +// [START android_snippets_kotlin_flow_test_fake_repository] |
| 42 | +class MyFakeRepository : MyRepository { |
| 43 | + override fun observeCount() = flow { |
| 44 | + emit(ITEM_1) |
| 45 | + } |
| 46 | + |
| 47 | + override fun observeChatMessages(): Flow<List<String>> = flow { |
| 48 | + emit(ALL_MESSAGES) |
| 49 | + } |
| 50 | +} |
| 51 | +// [END android_snippets_kotlin_flow_test_fake_repository] |
| 52 | + |
| 53 | +class MyUnitUnderTest(private val myRepository: MyRepository) |
| 54 | + |
| 55 | +class RepositoryTest { |
| 56 | + |
| 57 | + @Test |
| 58 | + fun myTest() { |
| 59 | + // [START android_snippets_kotlin_flow_test_my_test] |
| 60 | + // Given a class with fake dependencies: |
| 61 | + val sut = MyUnitUnderTest(MyFakeRepository()) |
| 62 | + // Trigger and verify |
| 63 | + // ... |
| 64 | + // [END android_snippets_kotlin_flow_test_my_test] |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun myRepositoryTest_first() = runTest { |
| 69 | + // [START android_snippets_kotlin_flow_test_repository_test_first] |
| 70 | + // Given a repository that combines values from two data sources: |
| 71 | + val repository = MyFakeRepository() |
| 72 | + |
| 73 | + // When the repository emits a value |
| 74 | + val firstItem = repository.observeCount().first() // Returns the first item in the flow |
| 75 | + |
| 76 | + // Then check it's the expected item |
| 77 | + assertEquals(ITEM_1, firstItem) |
| 78 | + // [END android_snippets_kotlin_flow_test_repository_test_first] |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun myRepositoryTest_toList() = runTest { |
| 83 | + // [START android_snippets_kotlin_flow_test_repository_test_to_list] |
| 84 | + // Given a repository with a fake data source that emits ALL_MESSAGES |
| 85 | + val repository = MyFakeRepository() |
| 86 | + val messages = repository.observeChatMessages().toList() |
| 87 | + |
| 88 | + // When all messages are emitted then they should be ALL_MESSAGES |
| 89 | + assertEquals(ALL_MESSAGES, messages[0]) |
| 90 | + // [END android_snippets_kotlin_flow_test_repository_test_to_list] |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// [START android_snippets_kotlin_flow_test_repository_and_datasource] |
| 95 | +interface DataSource { |
| 96 | + fun counts(): Flow<Int> |
| 97 | +} |
| 98 | + |
| 99 | +class Repository(private val dataSource: DataSource) { |
| 100 | + fun scores(): Flow<Int> { |
| 101 | + return dataSource.counts().map { it * 10 } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +class FakeDataSource : DataSource { |
| 106 | + private val flow = MutableSharedFlow<Int>() |
| 107 | + suspend fun emit(value: Int) = flow.emit(value) |
| 108 | + override fun counts(): Flow<Int> = flow |
| 109 | +} |
| 110 | +// [END android_snippets_kotlin_flow_test_repository_and_datasource] |
| 111 | + |
| 112 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 113 | +class ContinuousCollectionTest { |
| 114 | + @Test |
| 115 | + fun continuouslyCollect() = runTest { |
| 116 | + // [START android_snippets_kotlin_flow_test_continuously_collect] |
| 117 | + val dataSource = FakeDataSource() |
| 118 | + val repository = Repository(dataSource) |
| 119 | + |
| 120 | + val values = mutableListOf<Int>() |
| 121 | + backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { |
| 122 | + repository.scores().toList(values) |
| 123 | + } |
| 124 | + |
| 125 | + dataSource.emit(1) |
| 126 | + assertEquals(10, values[0]) // Assert on the list contents |
| 127 | + |
| 128 | + dataSource.emit(2) |
| 129 | + dataSource.emit(3) |
| 130 | + assertEquals(30, values[2]) |
| 131 | + |
| 132 | + assertEquals(3, values.size) // Assert the number of items collected |
| 133 | + // [END android_snippets_kotlin_flow_test_continuously_collect] |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + fun usingTurbine() = runTest { |
| 138 | + // [START android_snippets_kotlin_flow_test_using_turbine] |
| 139 | + val dataSource = FakeDataSource() |
| 140 | + val repository = Repository(dataSource) |
| 141 | + |
| 142 | + repository.scores().test { |
| 143 | + // Make calls that will trigger value changes only within test{} |
| 144 | + dataSource.emit(1) |
| 145 | + assertEquals(10, awaitItem()) |
| 146 | + |
| 147 | + dataSource.emit(2) |
| 148 | + awaitItem() // Ignore items if needed, can also use skip(n) |
| 149 | + |
| 150 | + dataSource.emit(3) |
| 151 | + assertEquals(30, awaitItem()) |
| 152 | + } |
| 153 | + // [END android_snippets_kotlin_flow_test_using_turbine] |
| 154 | + } |
| 155 | +} |
0 commit comments