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 dagger.Binds 26 import dagger.Module 27 import dagger.Provides 28 import java.util.concurrent.Executor 29 30 /** A fake [UserTracker] to be used in tests. */ 31 class FakeUserTracker( 32 private var _userId: Int = 0, 33 private var _userHandle: UserHandle = UserHandle.of(_userId), 34 private var _userInfo: UserInfo = mock(), 35 private var _userProfiles: List<UserInfo> = emptyList(), 36 userContentResolver: ContentResolver = MockContentResolver(), 37 userContext: Context = mock(), <lambda>null38 private val onCreateCurrentUserContext: (Context) -> Context = { mock() }, 39 ) : UserTracker { 40 val callbacks = mutableListOf<UserTracker.Callback>() 41 42 override val userId: Int 43 get() = _userId 44 override val userHandle: UserHandle 45 get() = _userHandle 46 override val userInfo: UserInfo 47 get() = _userInfo 48 override val userProfiles: List<UserInfo> 49 get() = _userProfiles 50 51 override val userContentResolver: ContentResolver = userContentResolver 52 override val userContext: Context = userContext 53 addCallbacknull54 override fun addCallback(callback: UserTracker.Callback, executor: Executor) { 55 callbacks.add(callback) 56 } 57 removeCallbacknull58 override fun removeCallback(callback: UserTracker.Callback) { 59 callbacks.remove(callback) 60 } 61 createCurrentUserContextnull62 override fun createCurrentUserContext(context: Context): Context { 63 return onCreateCurrentUserContext(context) 64 } 65 setnull66 fun set(userInfos: List<UserInfo>, selectedUserIndex: Int) { 67 _userProfiles = userInfos 68 _userInfo = userInfos[selectedUserIndex] 69 _userId = _userInfo.id 70 _userHandle = UserHandle.of(_userId) 71 72 onBeforeUserSwitching() 73 onUserChanging() 74 onUserChanged() 75 onProfileChanged() 76 } 77 onBeforeUserSwitchingnull78 fun onBeforeUserSwitching(userId: Int = _userId) { 79 val copy = callbacks.toList() 80 copy.forEach { it.onBeforeUserSwitching(userId) } 81 } 82 onUserChangingnull83 fun onUserChanging(userId: Int = _userId) { 84 val copy = callbacks.toList() 85 copy.forEach { it.onUserChanging(userId, userContext) {} } 86 } 87 onUserChangednull88 fun onUserChanged(userId: Int = _userId) { 89 val copy = callbacks.toList() 90 copy.forEach { it.onUserChanged(userId, userContext) } 91 } 92 onProfileChangednull93 fun onProfileChanged() { 94 callbacks.forEach { it.onProfilesChanged(_userProfiles) } 95 } 96 } 97 98 @Module(includes = [FakeUserTrackerModule.Bindings::class]) 99 class FakeUserTrackerModule( 100 @get:Provides val fakeUserTracker: FakeUserTracker = FakeUserTracker() 101 ) { 102 @Module 103 interface Bindings { bindFakenull104 @Binds fun bindFake(fake: FakeUserTracker): UserTracker 105 } 106 } 107