1 /*
2  * Copyright (C) 2024 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.systemui.kosmos
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import androidx.test.filters.SmallTest
21 import com.android.systemui.SysuiTestCase
22 import com.google.common.truth.Truth.assertThat
23 import kotlinx.coroutines.flow.MutableStateFlow
24 import kotlinx.coroutines.flow.SharingStarted
25 import kotlinx.coroutines.flow.flow
26 import kotlinx.coroutines.flow.map
27 import kotlinx.coroutines.flow.stateIn
28 import kotlinx.coroutines.test.runTest
29 import org.junit.Test
30 import org.junit.runner.RunWith
31 
32 @SmallTest
33 @RunWith(AndroidJUnit4::class)
34 class GeneralKosmosTest : SysuiTestCase() {
35     @Test
<lambda>null36     fun stateCurrentValueMutableStateFlow() = runTest {
37         val source = MutableStateFlow(1)
38         val mapped =
39             source
40                 .map { it * 2 }
41                 .stateIn(
42                     scope = backgroundScope,
43                     started = SharingStarted.WhileSubscribed(),
44                     initialValue = source.value * 2,
45                 )
46         assertThat(currentValue(mapped)).isEqualTo(2)
47 
48         source.value = 3
49         assertThat(currentValue(mapped)).isEqualTo(6)
50     }
51 
52     @Test
<lambda>null53     fun stateCurrentValueOnEmittedFlow() = runTest {
54         val source = flow {
55             emit(1)
56             emit(2)
57         }
58         val mapped =
59             source
60                 .map { it * 2 }
61                 .stateIn(
62                     scope = backgroundScope,
63                     started = SharingStarted.WhileSubscribed(),
64                     initialValue = 2,
65                 )
66         assertThat(currentValue(mapped)).isEqualTo(4)
67     }
68 
69     @Test
<lambda>null70     fun currentValueIsNull() = runTest {
71         val source = MutableStateFlow<Int?>(null)
72         assertThat(currentValue(source)).isEqualTo(null)
73     }
74 }
75