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.lifecycle
18 
19 import androidx.compose.foundation.layout.Column
20 import androidx.compose.material3.Text
21 import androidx.compose.runtime.getValue
22 import androidx.compose.runtime.mutableStateOf
23 import androidx.compose.ui.Modifier
24 import androidx.compose.ui.platform.testTag
25 import androidx.compose.ui.test.assertTextEquals
26 import androidx.compose.ui.test.hasTestTag
27 import androidx.compose.ui.test.junit4.createComposeRule
28 import androidx.test.ext.junit.runners.AndroidJUnit4
29 import androidx.test.filters.SmallTest
30 import com.android.systemui.SysuiTestCase
31 import kotlinx.coroutines.flow.MutableStateFlow
32 import kotlinx.coroutines.flow.map
33 import org.junit.Rule
34 import org.junit.Test
35 import org.junit.runner.RunWith
36 
37 @SmallTest
38 @RunWith(AndroidJUnit4::class)
39 class HydratorTest : SysuiTestCase() {
40 
41     @get:Rule val composeRule = createComposeRule()
42 
43     @Test
hydratedStateOfnull44     fun hydratedStateOf() {
45         val keepAliveMutable = mutableStateOf(true)
46         val upstreamStateFlow = MutableStateFlow(true)
47         val upstreamFlow = upstreamStateFlow.map { !it }
48         composeRule.setContent {
49             val keepAlive by keepAliveMutable
50             if (keepAlive) {
51                 val viewModel =
52                     rememberViewModel("test") {
53                         FakeSysUiViewModel(
54                             upstreamFlow = upstreamFlow,
55                             upstreamStateFlow = upstreamStateFlow,
56                         )
57                     }
58 
59                 Column {
60                     Text(
61                         "upstreamStateFlow=${viewModel.stateBackedByStateFlow}",
62                         Modifier.testTag("upstreamStateFlow"),
63                     )
64                     Text(
65                         "upstreamFlow=${viewModel.stateBackedByFlow}",
66                         Modifier.testTag("upstreamFlow"),
67                     )
68                 }
69             }
70         }
71 
72         composeRule.waitForIdle()
73         composeRule
74             .onNode(hasTestTag("upstreamStateFlow"))
75             .assertTextEquals("upstreamStateFlow=true")
76         composeRule.onNode(hasTestTag("upstreamFlow")).assertTextEquals("upstreamFlow=false")
77 
78         composeRule.runOnUiThread { upstreamStateFlow.value = false }
79         composeRule.waitForIdle()
80         composeRule
81             .onNode(hasTestTag("upstreamStateFlow"))
82             .assertTextEquals("upstreamStateFlow=false")
83         composeRule.onNode(hasTestTag("upstreamFlow")).assertTextEquals("upstreamFlow=true")
84     }
85 }
86