1 /*
2  * Copyright (C) 2022 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.testing.AndroidTestingRunner
20 import android.testing.TestableLooper
21 import android.testing.ViewUtils
22 import android.view.LayoutInflater
23 import android.view.View
24 import android.widget.FrameLayout
25 import androidx.test.filters.SmallTest
26 import com.android.internal.logging.UiEventLogger
27 import com.android.systemui.R
28 import com.android.systemui.SysuiTestCase
29 import com.android.systemui.plugins.FalsingManager
30 import com.android.systemui.qs.user.UserSwitchDialogController
31 import com.android.systemui.statusbar.SysuiStatusBarStateController
32 import com.android.systemui.statusbar.phone.DozeParameters
33 import com.android.systemui.statusbar.phone.LockscreenGestureLogger
34 import com.android.systemui.statusbar.phone.ScreenOffAnimationController
35 import com.google.common.truth.Truth.assertThat
36 import org.junit.After
37 import org.junit.Before
38 import org.junit.Test
39 import org.junit.runner.RunWith
40 import org.mockito.Mock
41 import org.mockito.Mockito.times
42 import org.mockito.Mockito.verify
43 import org.mockito.Mockito.`when`
44 import org.mockito.MockitoAnnotations
45 
46 @SmallTest
47 @TestableLooper.RunWithLooper
48 @RunWith(AndroidTestingRunner::class)
49 class KeyguardQsUserSwitchControllerTest : SysuiTestCase() {
50     @Mock
51     private lateinit var userSwitcherController: UserSwitcherController
52 
53     @Mock
54     private lateinit var keyguardStateController: KeyguardStateController
55 
56     @Mock
57     private lateinit var falsingManager: FalsingManager
58 
59     @Mock
60     private lateinit var configurationController: ConfigurationController
61 
62     @Mock
63     private lateinit var statusBarStateController: SysuiStatusBarStateController
64 
65     @Mock
66     private lateinit var dozeParameters: DozeParameters
67 
68     @Mock
69     private lateinit var screenOffAnimationController: ScreenOffAnimationController
70 
71     @Mock
72     private lateinit var userSwitchDialogController: UserSwitchDialogController
73 
74     @Mock
75     private lateinit var uiEventLogger: UiEventLogger
76 
77     private lateinit var view: FrameLayout
78     private lateinit var testableLooper: TestableLooper
79     private lateinit var keyguardQsUserSwitchController: KeyguardQsUserSwitchController
80 
81     @Before
82     fun setUp() {
83         MockitoAnnotations.initMocks(this)
84         testableLooper = TestableLooper.get(this)
85 
86         view = LayoutInflater.from(context)
87                 .inflate(R.layout.keyguard_qs_user_switch, null) as FrameLayout
88 
89         keyguardQsUserSwitchController = KeyguardQsUserSwitchController(
90                 view,
91                 context,
92                 context.resources,
93                 userSwitcherController,
94                 keyguardStateController,
95                 falsingManager,
96                 configurationController,
97                 statusBarStateController,
98                 dozeParameters,
99                 screenOffAnimationController,
100                 userSwitchDialogController,
101                 uiEventLogger)
102 
103         ViewUtils.attachView(view)
104         testableLooper.processAllMessages()
105         `when`(userSwitcherController.isKeyguardShowing).thenReturn(true)
106         `when`(keyguardStateController.isShowing).thenReturn(true)
107         `when`(keyguardStateController.isKeyguardGoingAway).thenReturn(false)
108         keyguardQsUserSwitchController.init()
109     }
110 
111     @After
112     fun tearDown() {
113         ViewUtils.detachView(view)
114     }
115 
116     @Test
117     fun testUiEventLogged() {
118         view.findViewById<View>(R.id.kg_multi_user_avatar)?.performClick()
119         verify(uiEventLogger, times(1))
120                 .log(LockscreenGestureLogger.LockscreenUiEvent.LOCKSCREEN_SWITCH_USER_TAP)
121     }
122 
123     @Test
124     fun testAvatarExistsWhenKeyguardGoingAway() {
125         `when`(keyguardStateController.isShowing).thenReturn(false)
126         `when`(keyguardStateController.isKeyguardGoingAway).thenReturn(true)
127         keyguardQsUserSwitchController.updateKeyguardShowing(true /* forceViewUpdate */)
128         assertThat(keyguardQsUserSwitchController.mUserAvatarView.isEmpty).isFalse()
129     }
130 
131     @Test
132     fun testAvatarExistsWhenKeyguardShown() {
133         `when`(keyguardStateController.isShowing).thenReturn(true)
134         `when`(keyguardStateController.isKeyguardGoingAway).thenReturn(false)
135         keyguardQsUserSwitchController.updateKeyguardShowing(true /* forceViewUpdate */)
136         assertThat(keyguardQsUserSwitchController.mUserAvatarView.isEmpty).isFalse()
137     }
138 
139     @Test
140     fun testAvatarGoneWhenKeyguardGone() {
141         `when`(keyguardStateController.isShowing).thenReturn(false)
142         `when`(keyguardStateController.isKeyguardGoingAway).thenReturn(false)
143         keyguardQsUserSwitchController.updateKeyguardShowing(true /* forceViewUpdate */)
144         assertThat(keyguardQsUserSwitchController.mUserAvatarView.isEmpty).isTrue()
145     }
146 }
147