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 private var _isUserSwitching: Boolean = false, <lambda>null37 userContentResolverProvider: () -> ContentResolver = { MockContentResolver() }, 38 userContext: Context = mock(), <lambda>null39 private val onCreateCurrentUserContext: (Context) -> Context = { mock() }, 40 ) : UserTracker { 41 val callbacks = mutableListOf<UserTracker.Callback>() 42 43 override val userId: Int 44 get() = _userId 45 46 override val userHandle: UserHandle 47 get() = _userHandle 48 49 override val userInfo: UserInfo 50 get() = _userInfo 51 52 override val userProfiles: List<UserInfo> 53 get() = _userProfiles 54 55 override val isUserSwitching: Boolean 56 get() = _isUserSwitching 57 58 // userContentResolver is lazy because Ravenwood doesn't support MockContentResolver() 59 // and we still want to allow people use this class for tests that don't use it. <lambda>null60 override val userContentResolver: ContentResolver by lazy { userContentResolverProvider() } 61 override val userContext: Context = userContext 62 addCallbacknull63 override fun addCallback(callback: UserTracker.Callback, executor: Executor) { 64 callbacks.add(callback) 65 } 66 removeCallbacknull67 override fun removeCallback(callback: UserTracker.Callback) { 68 callbacks.remove(callback) 69 } 70 createCurrentUserContextnull71 override fun createCurrentUserContext(context: Context): Context { 72 return onCreateCurrentUserContext(context) 73 } 74 setnull75 fun set(userInfos: List<UserInfo>, selectedUserIndex: Int) { 76 _userProfiles = userInfos 77 _userInfo = userInfos[selectedUserIndex] 78 _userId = _userInfo.id 79 _userHandle = UserHandle.of(_userId) 80 81 onBeforeUserSwitching() 82 onUserChanging() 83 onUserChanged() 84 onProfileChanged() 85 } 86 onBeforeUserSwitchingnull87 fun onBeforeUserSwitching(userId: Int = _userId) { 88 val copy = callbacks.toList() 89 copy.forEach { it.onBeforeUserSwitching(userId) } 90 } 91 onUserChangingnull92 fun onUserChanging(userId: Int = _userId) { 93 _isUserSwitching = true 94 val copy = callbacks.toList() 95 copy.forEach { it.onUserChanging(userId, userContext) {} } 96 } 97 onUserChangednull98 fun onUserChanged(userId: Int = _userId) { 99 _isUserSwitching = false 100 val copy = callbacks.toList() 101 copy.forEach { it.onUserChanged(userId, userContext) } 102 } 103 onProfileChangednull104 fun onProfileChanged() { 105 callbacks.forEach { it.onProfilesChanged(_userProfiles) } 106 } 107 } 108 109 @Module(includes = [FakeUserTrackerModule.Bindings::class]) 110 class FakeUserTrackerModule( 111 @get:Provides val fakeUserTracker: FakeUserTracker = FakeUserTracker() 112 ) { 113 @Module 114 interface Bindings { bindFakenull115 @Binds fun bindFake(fake: FakeUserTracker): UserTracker 116 } 117 } 118