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.graphics.drawable.Drawable
20 import com.android.systemui.util.mockito.mock
21 
22 /** A fake [UserInfoController] to be used in tests. */
23 class FakeUserInfoController(
24     private val fakeInfo: FakeInfo = FakeInfo(),
25 ) : UserInfoController {
26     private val listeners = LinkedHashSet<UserInfoController.OnUserInfoChangedListener>()
27 
28     /** Update [fakeInfo], then notify the listeners. */
29     fun updateInfo(f: FakeInfo.() -> Unit) {
30         fakeInfo.f()
31         notifyListeners()
32     }
33 
34     private fun notifyListeners() {
35         listeners.forEach { listener ->
36             listener.onUserInfoChanged(fakeInfo.name, fakeInfo.picture, fakeInfo.userAccount)
37         }
38     }
39 
40     override fun addCallback(listener: UserInfoController.OnUserInfoChangedListener) {
41         listeners.add(listener)
42 
43         // The actual implementation notifies the listener when adding it.
44         listener.onUserInfoChanged(fakeInfo.name, fakeInfo.picture, fakeInfo.userAccount)
45     }
46 
47     override fun removeCallback(listener: UserInfoController.OnUserInfoChangedListener) {
48         listeners.remove(listener)
49     }
50 
51     override fun reloadUserInfo() {}
52 
53     class FakeInfo(
54         var name: String = "",
55         var picture: Drawable = mock(),
56         var userAccount: String = "",
57     )
58 }
59