1 /*
2  * Copyright (C) 2023 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 package com.android.systemui.flags
17 
18 import android.test.suitebuilder.annotation.SmallTest
19 import com.android.systemui.SysuiTestCase
20 import com.android.systemui.statusbar.policy.BatteryController
21 import com.google.common.truth.Truth.assertThat
22 import org.junit.Before
23 import org.junit.Test
24 import org.mockito.ArgumentCaptor
25 import org.mockito.Mock
26 import org.mockito.Mockito.verify
27 import org.mockito.Mockito.`when` as whenever
28 import org.mockito.MockitoAnnotations
29 
30 /**
31  * Be careful with the {FeatureFlagsReleaseRestarter} in this test. It has a call to System.exit()!
32  */
33 @SmallTest
34 class PluggedInConditionTest : SysuiTestCase() {
35     private lateinit var condition: PluggedInCondition
36 
37     @Mock private lateinit var batteryController: BatteryController
38 
39     @Before
40     fun setup() {
41         MockitoAnnotations.initMocks(this)
42         condition = PluggedInCondition(batteryController)
43     }
44 
45     @Test
46     fun testCondition_unplugged() {
47         whenever(batteryController.isPluggedIn).thenReturn(false)
48 
49         assertThat(condition.canRestartNow({})).isFalse()
50     }
51 
52     @Test
53     fun testCondition_pluggedIn() {
54         whenever(batteryController.isPluggedIn).thenReturn(true)
55 
56         assertThat(condition.canRestartNow({})).isTrue()
57     }
58 
59     @Test
60     fun testCondition_invokesRetry() {
61         whenever(batteryController.isPluggedIn).thenReturn(false)
62         var retried = false
63         val retryFn = { retried = true }
64 
65         // No restart yet, but we do register a listener now.
66         assertThat(condition.canRestartNow(retryFn)).isFalse()
67         val captor =
68             ArgumentCaptor.forClass(BatteryController.BatteryStateChangeCallback::class.java)
69         verify(batteryController).addCallback(captor.capture())
70 
71         whenever(batteryController.isPluggedIn).thenReturn(true)
72 
73         captor.value.onBatteryLevelChanged(0, true, true)
74         assertThat(retried).isTrue()
75     }
76 }
77