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 com.android.systemui.statusbar.policy.UserSwitcherController.UserSwitchCallback
20 import com.android.systemui.util.mockito.any
21 import com.android.systemui.util.mockito.mock
22 import org.mockito.Mockito.`when` as whenever
23 
24 /**
25  * A wrapper around a mocked [UserSwitcherController] to be used in tests.
26  *
27  * Note that this was implemented as a mock wrapper instead of fake implementation of a common
28  * interface given how big the UserSwitcherController grew.
29  */
30 class MockUserSwitcherControllerWrapper(
31     currentUserName: String = "",
32 ) {
33     val controller: UserSwitcherController = mock()
34     private val callbacks = LinkedHashSet<UserSwitchCallback>()
35 
36     var currentUserName = currentUserName
37         set(value) {
38             if (value != field) {
39                 field = value
40                 notifyCallbacks()
41             }
42         }
43 
44     private fun notifyCallbacks() {
45         callbacks.forEach { it.onUserSwitched() }
46     }
47 
48     init {
49         whenever(controller.addUserSwitchCallback(any())).then { invocation ->
50             callbacks.add(invocation.arguments.first() as UserSwitchCallback)
51         }
52 
53         whenever(controller.removeUserSwitchCallback(any())).then { invocation ->
54             callbacks.remove(invocation.arguments.first() as UserSwitchCallback)
55         }
56 
57         whenever(controller.currentUserName).thenAnswer { this.currentUserName }
58     }
59 }
60