1 /*
2  * Copyright (C) 2021 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.os.Handler
20 import android.provider.Settings
21 import android.testing.AndroidTestingRunner
22 import android.testing.TestableLooper
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.dump.DumpManager
26 import com.android.systemui.settings.UserTracker
27 import com.android.systemui.util.concurrency.FakeExecutor
28 import com.android.systemui.util.mockito.any
29 import com.android.systemui.util.mockito.capture
30 import com.android.systemui.util.mockito.whenever
31 import com.android.systemui.util.settings.FakeSettings
32 import com.android.systemui.util.time.FakeSystemClock
33 import com.android.systemui.util.wrapper.BuildInfo
34 import com.google.common.truth.Truth.assertThat
35 import org.junit.Before
36 import org.junit.Test
37 import org.junit.runner.RunWith
38 import org.mockito.ArgumentCaptor
39 import org.mockito.Captor
40 import org.mockito.Mock
41 import org.mockito.Mockito.never
42 import org.mockito.Mockito.verify
43 import org.mockito.Mockito.`when`
44 import org.mockito.MockitoAnnotations
45 
46 @SmallTest
47 @RunWith(AndroidTestingRunner::class)
48 @TestableLooper.RunWithLooper
49 class DeviceProvisionedControllerImplTest : SysuiTestCase() {
50 
51     companion object {
52         private const val START_USER = 0
53     }
54 
55     private lateinit var controller: DeviceProvisionedControllerImpl
56 
57     @Mock
58     private lateinit var userTracker: UserTracker
59     @Mock
60     private lateinit var dumpManager: DumpManager
61     @Mock
62     private lateinit var listener: DeviceProvisionedController.DeviceProvisionedListener
63     @Mock
64     private lateinit var buildInfo: BuildInfo
65     @Captor
66     private lateinit var userTrackerCallbackCaptor: ArgumentCaptor<UserTracker.Callback>
67 
68     private lateinit var mainExecutor: FakeExecutor
69     private lateinit var testableLooper: TestableLooper
70     private lateinit var settings: FakeSettings
71 
72     @Before
73     fun setUp() {
74         MockitoAnnotations.initMocks(this)
75         testableLooper = TestableLooper.get(this)
76         mainExecutor = FakeExecutor(FakeSystemClock())
77         settings = FakeSettings()
78         `when`(userTracker.userId).thenReturn(START_USER)
79         whenever(buildInfo.isDebuggable).thenReturn(false)
80         controller = DeviceProvisionedControllerImpl(
81                 settings,
82                 settings,
83                 userTracker,
84                 dumpManager,
85                 buildInfo,
86                 Handler(testableLooper.looper),
87                 mainExecutor
88         )
89     }
90 
91     @Test
92     fun testNotProvisionedByDefault() {
93         init()
94         assertThat(controller.isDeviceProvisioned).isFalse()
95     }
96 
97     @Test
98     fun testFrpNotActiveByDefault() {
99         init()
100         assertThat(controller.isFrpActive).isFalse()
101     }
102 
103     @Test
104     fun testNotUserSetupByDefault() {
105         init()
106         assertThat(controller.isUserSetup(START_USER)).isFalse()
107     }
108 
109     @Test
110     fun testProvisionedWhenCreated() {
111         settings.putInt(Settings.Global.DEVICE_PROVISIONED, 1)
112         init()
113 
114         assertThat(controller.isDeviceProvisioned).isTrue()
115     }
116 
117     @Test
118     fun testFrpActiveWhenCreated() {
119         settings.putInt(Settings.Secure.SECURE_FRP_MODE, 1)
120         init()
121 
122         assertThat(controller.isFrpActive).isTrue()
123     }
124 
125     @Test
126     fun testUserSetupWhenCreated() {
127         settings.putIntForUser(Settings.Secure.USER_SETUP_COMPLETE, 1, START_USER)
128         init()
129 
130         assertThat(controller.isUserSetup(START_USER))
131     }
132 
133     @Test
134     fun testDeviceProvisionedChange() {
135         init()
136 
137         settings.putInt(Settings.Global.DEVICE_PROVISIONED, 1)
138         testableLooper.processAllMessages() // background observer
139 
140         assertThat(controller.isDeviceProvisioned).isTrue()
141     }
142 
143     @Test
144     fun testFrpActiveChange() {
145         init()
146 
147         settings.putInt(Settings.Secure.SECURE_FRP_MODE, 1)
148         testableLooper.processAllMessages() // background observer
149 
150         assertThat(controller.isFrpActive).isTrue()
151     }
152 
153     @Test
154     fun testUserSetupChange() {
155         init()
156 
157         settings.putIntForUser(Settings.Secure.USER_SETUP_COMPLETE, 1, START_USER)
158         testableLooper.processAllMessages() // background observer
159 
160         assertThat(controller.isUserSetup(START_USER)).isTrue()
161     }
162 
163     @Test
164     fun testUserSetupChange_otherUser() {
165         init()
166         val otherUser = 10
167 
168         settings.putIntForUser(Settings.Secure.USER_SETUP_COMPLETE, 1, otherUser)
169         testableLooper.processAllMessages() // background observer
170 
171         assertThat(controller.isUserSetup(START_USER)).isFalse()
172         assertThat(controller.isUserSetup(otherUser)).isTrue()
173     }
174 
175     @Test
176     fun testCurrentUserSetup() {
177         val otherUser = 10
178         settings.putIntForUser(Settings.Secure.USER_SETUP_COMPLETE, 1, otherUser)
179         init()
180 
181         assertThat(controller.isCurrentUserSetup).isFalse()
182         switchUser(otherUser)
183         testableLooper.processAllMessages()
184 
185         assertThat(controller.isCurrentUserSetup).isTrue()
186     }
187 
188     @Test
189     fun testListenerNotCalledOnAdd() {
190         init()
191         controller.addCallback(listener)
192 
193         mainExecutor.runAllReady()
194 
195         verify(listener, never()).onDeviceProvisionedChanged()
196         verify(listener, never()).onFrpActiveChanged()
197         verify(listener, never()).onUserSetupChanged()
198         verify(listener, never()).onUserSwitched()
199     }
200 
201     @Test
202     fun testListenerCalledOnUserSwitched() {
203         init()
204         controller.addCallback(listener)
205 
206         switchUser(10)
207 
208         testableLooper.processAllMessages()
209         mainExecutor.runAllReady()
210 
211         verify(listener).onUserSwitched()
212         verify(listener, never()).onUserSetupChanged()
213         verify(listener, never()).onDeviceProvisionedChanged()
214         verify(listener, never()).onFrpActiveChanged()
215     }
216 
217     @Test
218     fun testListenerCalledOnUserSetupChanged() {
219         init()
220         controller.addCallback(listener)
221 
222         settings.putIntForUser(Settings.Secure.USER_SETUP_COMPLETE, 1, START_USER)
223         testableLooper.processAllMessages()
224         mainExecutor.runAllReady()
225 
226         verify(listener, never()).onUserSwitched()
227         verify(listener).onUserSetupChanged()
228         verify(listener, never()).onDeviceProvisionedChanged()
229         verify(listener, never()).onFrpActiveChanged()
230     }
231 
232     @Test
233     fun testListenerCalledOnDeviceProvisionedChanged() {
234         init()
235         controller.addCallback(listener)
236 
237         settings.putInt(Settings.Global.DEVICE_PROVISIONED, 1)
238         testableLooper.processAllMessages()
239         mainExecutor.runAllReady()
240 
241         verify(listener, never()).onUserSwitched()
242         verify(listener, never()).onUserSetupChanged()
243         verify(listener, never()).onFrpActiveChanged()
244         verify(listener).onDeviceProvisionedChanged()
245     }
246 
247     @Test
248     fun testListenerCalledOnFrpActiveChanged() {
249         init()
250         controller.addCallback(listener)
251 
252         settings.putInt(Settings.Secure.SECURE_FRP_MODE, 1)
253         testableLooper.processAllMessages()
254         mainExecutor.runAllReady()
255 
256         verify(listener, never()).onUserSwitched()
257         verify(listener, never()).onUserSetupChanged()
258         verify(listener, never()).onDeviceProvisionedChanged()
259         verify(listener).onFrpActiveChanged()
260     }
261 
262     @Test
263     fun testRemoveListener() {
264         init()
265         controller.addCallback(listener)
266         controller.removeCallback(listener)
267 
268         switchUser(10)
269         settings.putIntForUser(Settings.Secure.USER_SETUP_COMPLETE, 1, START_USER)
270         settings.putInt(Settings.Global.DEVICE_PROVISIONED, 1)
271         settings.putInt(Settings.Secure.SECURE_FRP_MODE, 1)
272 
273         testableLooper.processAllMessages()
274         mainExecutor.runAllReady()
275 
276         verify(listener, never()).onDeviceProvisionedChanged()
277         verify(listener, never()).onFrpActiveChanged()
278         verify(listener, never()).onUserSetupChanged()
279         verify(listener, never()).onUserSwitched()
280     }
281 
282     private fun init() {
283         controller.init()
284         verify(userTracker).addCallback(capture(userTrackerCallbackCaptor), any())
285     }
286 
287     private fun switchUser(toUser: Int) {
288         `when`(userTracker.userId).thenReturn(toUser)
289         userTrackerCallbackCaptor.value.onUserChanged(toUser, mContext)
290     }
291 }