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.controls.dagger
18 
19 import android.testing.AndroidTestingRunner
20 import androidx.test.filters.SmallTest
21 import com.android.internal.widget.LockPatternUtils
22 import com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED
23 import com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.controls.controller.ControlsController
26 import com.android.systemui.controls.controller.ControlsTileResourceConfiguration
27 import com.android.systemui.controls.management.ControlsListingController
28 import com.android.systemui.controls.settings.FakeControlsSettingsRepository
29 import com.android.systemui.controls.ui.ControlsUiController
30 import com.android.systemui.settings.UserTracker
31 import com.android.systemui.statusbar.policy.KeyguardStateController
32 import dagger.Lazy
33 import java.util.Optional
34 import org.junit.Assert.assertEquals
35 import org.junit.Assert.assertFalse
36 import org.junit.Assert.assertTrue
37 import org.junit.Before
38 import org.junit.Test
39 import org.junit.runner.RunWith
40 import org.mockito.Answers
41 import org.mockito.Mock
42 import org.mockito.Mockito
43 import org.mockito.Mockito.any
44 import org.mockito.Mockito.anyInt
45 import org.mockito.Mockito.`when`
46 import org.mockito.MockitoAnnotations
47 
48 @SmallTest
49 @RunWith(AndroidTestingRunner::class)
50 class ControlsComponentTest : SysuiTestCase() {
51 
52     @Mock private lateinit var controller: ControlsController
53     @Mock private lateinit var uiController: ControlsUiController
54     @Mock private lateinit var listingController: ControlsListingController
55     @Mock private lateinit var keyguardStateController: KeyguardStateController
56     @Mock(answer = Answers.RETURNS_DEEP_STUBS) private lateinit var userTracker: UserTracker
57     @Mock private lateinit var lockPatternUtils: LockPatternUtils
58     @Mock
59     private lateinit var optionalControlsTileResourceConfiguration:
60         Optional<ControlsTileResourceConfiguration>
61     @Mock private lateinit var controlsTileResourceConfiguration: ControlsTileResourceConfiguration
62 
63     private lateinit var controlsSettingsRepository: FakeControlsSettingsRepository
64 
65     companion object {
66         fun <T> eq(value: T): T = Mockito.eq(value) ?: value
67     }
68 
69     @Before
70     fun setUp() {
71         MockitoAnnotations.initMocks(this)
72 
73         controlsSettingsRepository = FakeControlsSettingsRepository()
74 
75         `when`(userTracker.userHandle.identifier).thenReturn(0)
76         `when`(optionalControlsTileResourceConfiguration.orElse(any()))
77             .thenReturn(controlsTileResourceConfiguration)
78     }
79 
80     @Test
81     fun testFeatureEnabled() {
82         val component = setupComponent(true)
83 
84         assertTrue(component.getControlsController().isPresent)
85         assertEquals(controller, component.getControlsController().get())
86         assertTrue(component.getControlsUiController().isPresent)
87         assertEquals(uiController, component.getControlsUiController().get())
88         assertTrue(component.getControlsListingController().isPresent)
89         assertEquals(listingController, component.getControlsListingController().get())
90     }
91 
92     @Test
93     fun testFeatureDisabled() {
94         val component = setupComponent(false)
95 
96         assertFalse(component.getControlsController().isPresent)
97         assertFalse(component.getControlsUiController().isPresent)
98         assertFalse(component.getControlsListingController().isPresent)
99     }
100 
101     @Test
102     fun testFeatureDisabledVisibility() {
103         val component = setupComponent(false)
104 
105         assertEquals(ControlsComponent.Visibility.UNAVAILABLE, component.getVisibility())
106     }
107 
108     @Test
109     fun testFeatureEnabledAfterBootVisibility() {
110         `when`(lockPatternUtils.getStrongAuthForUser(anyInt()))
111             .thenReturn(STRONG_AUTH_REQUIRED_AFTER_BOOT)
112         val component = setupComponent(true)
113 
114         assertEquals(ControlsComponent.Visibility.AVAILABLE_AFTER_UNLOCK, component.getVisibility())
115     }
116 
117     @Test
118     fun testFeatureEnabledAndCannotShowOnLockScreenVisibility() {
119         `when`(lockPatternUtils.getStrongAuthForUser(anyInt())).thenReturn(STRONG_AUTH_NOT_REQUIRED)
120         `when`(keyguardStateController.isUnlocked()).thenReturn(false)
121         controlsSettingsRepository.setCanShowControlsInLockscreen(false)
122         val component = setupComponent(true)
123 
124         assertEquals(ControlsComponent.Visibility.AVAILABLE_AFTER_UNLOCK, component.getVisibility())
125     }
126 
127     @Test
128     fun testFeatureEnabledAndCanShowOnLockScreenVisibility() {
129         `when`(lockPatternUtils.getStrongAuthForUser(anyInt())).thenReturn(STRONG_AUTH_NOT_REQUIRED)
130         `when`(keyguardStateController.isUnlocked()).thenReturn(false)
131         controlsSettingsRepository.setCanShowControlsInLockscreen(true)
132         val component = setupComponent(true)
133 
134         assertEquals(ControlsComponent.Visibility.AVAILABLE, component.getVisibility())
135     }
136 
137     @Test
138     fun testFeatureEnabledAndCanShowWhileUnlockedVisibility() {
139         controlsSettingsRepository.setCanShowControlsInLockscreen(false)
140         `when`(lockPatternUtils.getStrongAuthForUser(anyInt())).thenReturn(STRONG_AUTH_NOT_REQUIRED)
141         `when`(keyguardStateController.isUnlocked()).thenReturn(true)
142         val component = setupComponent(true)
143 
144         assertEquals(ControlsComponent.Visibility.AVAILABLE, component.getVisibility())
145     }
146 
147     @Test
148     fun testGetTileImageId() {
149         val tileImageId = 0
150 
151         `when`(controlsTileResourceConfiguration.getTileImageId()).thenReturn(tileImageId)
152         val component = setupComponent(true)
153         assertEquals(component.getTileImageId(), tileImageId)
154     }
155 
156     @Test
157     fun testGetTileTitleId() {
158         val tileTitleId = 0
159 
160         `when`(controlsTileResourceConfiguration.getTileTitleId()).thenReturn(tileTitleId)
161         val component = setupComponent(true)
162         assertEquals(component.getTileTitleId(), tileTitleId)
163     }
164 
165     @Test
166     fun getPackageName() {
167         val packageName = "packageName"
168         `when`(controlsTileResourceConfiguration.getPackageName()).thenReturn(packageName)
169         val component = setupComponent(true)
170         assertEquals(component.getPackageName(), packageName)
171     }
172 
173     private fun setupComponent(enabled: Boolean): ControlsComponent {
174         return ControlsComponent(
175             enabled,
176             mContext,
177             Lazy { controller },
178             Lazy { uiController },
179             Lazy { listingController },
180             lockPatternUtils,
181             keyguardStateController,
182             userTracker,
183             controlsSettingsRepository,
184             optionalControlsTileResourceConfiguration
185         )
186     }
187 }
188