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.settings
18 
19 import android.content.ContentResolver
20 import android.content.Context
21 import android.content.pm.UserInfo
22 import android.os.UserHandle
23 import android.test.mock.MockContentResolver
24 import com.android.systemui.util.mockito.mock
25 import java.util.concurrent.CountDownLatch
26 import java.util.concurrent.Executor
27 
28 /** A fake [UserTracker] to be used in tests. */
29 class FakeUserTracker(
30     private var _userId: Int = 0,
31     private var _userHandle: UserHandle = UserHandle.of(_userId),
32     private var _userInfo: UserInfo = mock(),
33     private var _userProfiles: List<UserInfo> = emptyList(),
34     userContentResolver: ContentResolver = MockContentResolver(),
35     userContext: Context = mock(),
36     private val onCreateCurrentUserContext: (Context) -> Context = { mock() },
37 ) : UserTracker {
38     val callbacks = mutableListOf<UserTracker.Callback>()
39 
40     override val userId: Int
41         get() = _userId
42     override val userHandle: UserHandle
43         get() = _userHandle
44     override val userInfo: UserInfo
45         get() = _userInfo
46     override val userProfiles: List<UserInfo>
47         get() = _userProfiles
48 
49     override val userContentResolver: ContentResolver = userContentResolver
50     override val userContext: Context = userContext
51 
52     override fun addCallback(callback: UserTracker.Callback, executor: Executor) {
53         callbacks.add(callback)
54     }
55 
56     override fun removeCallback(callback: UserTracker.Callback) {
57         callbacks.remove(callback)
58     }
59 
60     override fun createCurrentUserContext(context: Context): Context {
61         return onCreateCurrentUserContext(context)
62     }
63 
64     fun set(userInfos: List<UserInfo>, selectedUserIndex: Int) {
65         _userProfiles = userInfos
66         _userInfo = userInfos[selectedUserIndex]
67         _userId = _userInfo.id
68         _userHandle = UserHandle.of(_userId)
69 
70         onUserChanging()
71         onUserChanged()
72     }
73 
74     fun onUserChanging(userId: Int = _userId) {
75         val copy = callbacks.toList()
76         val latch = CountDownLatch(copy.size)
77         copy.forEach { it.onUserChanging(userId, userContext, latch) }
78     }
79 
80     fun onUserChanged(userId: Int = _userId) {
81         val copy = callbacks.toList()
82         copy.forEach { it.onUserChanged(userId, userContext) }
83     }
84 
85     fun onProfileChanged() {
86         callbacks.forEach { it.onProfilesChanged(_userProfiles) }
87     }
88 }
89