xref: /aosp_15_r20/frameworks/base/packages/SystemUI/tests/src/com/android/systemui/lifecycle/ActivatableTest.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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.runtime.getValue
20 import androidx.compose.runtime.mutableStateOf
21 import androidx.compose.ui.test.junit4.createComposeRule
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.SysuiTestCase
25 import com.google.common.truth.Truth.assertThat
26 import org.junit.Rule
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 
30 @SmallTest
31 @RunWith(AndroidJUnit4::class)
32 class ActivatableTest : SysuiTestCase() {
33 
34     @get:Rule val composeRule = createComposeRule()
35 
36     @Test
rememberActivatednull37     fun rememberActivated() {
38         val keepAliveMutable = mutableStateOf(true)
39         var isActive = false
40         composeRule.setContent {
41             val keepAlive by keepAliveMutable
42             if (keepAlive) {
43                 rememberActivated("test") {
44                     FakeActivatable(
45                         onActivation = { isActive = true },
46                         onDeactivation = { isActive = false },
47                     )
48                 }
49             }
50         }
51         assertThat(isActive).isTrue()
52     }
53 
54     @Test
rememberActivated_leavingTheCompositionnull55     fun rememberActivated_leavingTheComposition() {
56         val keepAliveMutable = mutableStateOf(true)
57         var isActive = false
58         composeRule.setContent {
59             val keepAlive by keepAliveMutable
60             if (keepAlive) {
61                 rememberActivated("name") {
62                     FakeActivatable(
63                         onActivation = { isActive = true },
64                         onDeactivation = { isActive = false },
65                     )
66                 }
67             }
68         }
69 
70         // Tear down the composable.
71         composeRule.runOnUiThread { keepAliveMutable.value = false }
72         composeRule.waitForIdle()
73 
74         assertThat(isActive).isFalse()
75     }
76 }
77