1 /*
2  * Copyright (C) 2020 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.statusbar.policy
18 
19 import android.app.NotificationManager
20 import android.testing.AndroidTestingRunner
21 import android.testing.TestableLooper.RunWithLooper
22 
23 import androidx.test.filters.SmallTest
24 
25 import com.android.systemui.SysuiTestCase
26 import com.android.systemui.util.concurrency.FakeExecutor
27 import com.android.systemui.util.time.FakeSystemClock
28 
29 import org.junit.Before
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 import org.mockito.ArgumentMatchers.anyInt
33 import org.mockito.ArgumentMatchers.anyString
34 import org.mockito.Mock
35 import org.mockito.Mockito
36 import org.mockito.Mockito.verify
37 import org.mockito.MockitoAnnotations
38 
anyObjectnull39 private fun <T> anyObject(): T {
40     return Mockito.anyObject<T>()
41 }
42 
43 @RunWith(AndroidTestingRunner::class)
44 @RunWithLooper()
45 @SmallTest
46 class BatteryStateNotifierTest : SysuiTestCase() {
47     @Mock private lateinit var batteryController: BatteryController
48     @Mock private lateinit var noMan: NotificationManager
49 
50     private val clock = FakeSystemClock()
51     private val executor = FakeExecutor(clock)
52 
53     private lateinit var notifier: BatteryStateNotifier
54 
55     @Before
setupnull56     fun setup() {
57         MockitoAnnotations.initMocks(this)
58         notifier = BatteryStateNotifier(batteryController, noMan, executor, context)
59         notifier.startListening()
60 
61         context.ensureTestableResources()
62     }
63 
64     @Test
testNotifyWhenStateUnknownnull65     fun testNotifyWhenStateUnknown() {
66         notifier.onBatteryUnknownStateChanged(true)
67         verify(noMan).notify(anyString(), anyInt(), anyObject())
68     }
69 
70     @Test
testCancelAfterDelaynull71     fun testCancelAfterDelay() {
72         notifier.onBatteryUnknownStateChanged(true)
73         notifier.onBatteryUnknownStateChanged(false)
74 
75         clock.advanceTime(DELAY_MILLIS + 1)
76         verify(noMan).cancel(anyInt())
77     }
78 }
79 
80 // From BatteryStateNotifier.kt
81 private const val DELAY_MILLIS: Long = 40 * 60 * 60 * 1000
82