diff options
| -rw-r--r-- | java/src/com/android/intentresolver/v2/domain/interactor/UserInteractor.kt | 16 | ||||
| -rw-r--r-- | tests/unit/src/com/android/intentresolver/v2/domain/interactor/UserInteractorTest.kt | 28 |
2 files changed, 21 insertions, 23 deletions
diff --git a/java/src/com/android/intentresolver/v2/domain/interactor/UserInteractor.kt b/java/src/com/android/intentresolver/v2/domain/interactor/UserInteractor.kt index c8df9684..72b604c2 100644 --- a/java/src/com/android/intentresolver/v2/domain/interactor/UserInteractor.kt +++ b/java/src/com/android/intentresolver/v2/domain/interactor/UserInteractor.kt @@ -26,9 +26,7 @@ import com.android.intentresolver.v2.shared.model.User.Role import javax.inject.Inject import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.mapNotNull /** The high level User interface. */ class UserInteractor @@ -67,20 +65,16 @@ constructor( it.primary.id == launchedAs.identifier || it.clone?.id == launchedAs.identifier } } - /** - * Provides a flow to report on the availability of the profile. An unavailable profile may be + * Provides a flow to report on the availability of profile. An unavailable profile may be * hidden or appear disabled within the app. */ - fun isAvailable(type: Type): Flow<Boolean> { - val profileFlow = profiles.map { list -> list.firstOrNull { it.type == type } } - return combine(profileFlow, userRepository.availability) { profile, availability -> - when (profile) { - null -> false - else -> availability.getOrDefault(profile.primary, false) + val availability: Flow<Map<Profile, Boolean>> = + combine(profiles, userRepository.availability) { profiles, availability -> + profiles.associateWith { + availability.getOrDefault(it.primary, false) } } - } /** * Request the profile state be updated. In the case of enabling, the operation could take diff --git a/tests/unit/src/com/android/intentresolver/v2/domain/interactor/UserInteractorTest.kt b/tests/unit/src/com/android/intentresolver/v2/domain/interactor/UserInteractorTest.kt index b66fabfd..a81a315b 100644 --- a/tests/unit/src/com/android/intentresolver/v2/domain/interactor/UserInteractorTest.kt +++ b/tests/unit/src/com/android/intentresolver/v2/domain/interactor/UserInteractorTest.kt @@ -41,6 +41,10 @@ class UserInteractorTest { private val workUser = User(id = baseId + 2, role = Role.WORK) private val privateUser = User(id = baseId + 3, role = Role.PRIVATE) + val personalProfile = Profile(PERSONAL, personalUser) + val workProfile = Profile(WORK, workUser) + val privateProfile = Profile(PRIVATE, privateUser) + @Test fun launchedByProfile(): Unit = runTest { val profileInteractor = @@ -146,37 +150,37 @@ class UserInteractorTest { val userRepo = FakeUserRepository(listOf(personalUser)) userRepo.addUser(workUser, false) - val profileInteractor = + val interactor = UserInteractor(userRepository = userRepo, launchedAs = personalUser.handle) - val personalAvailable by collectLastValue(profileInteractor.isAvailable(PERSONAL)) - val workAvailable by collectLastValue(profileInteractor.isAvailable(WORK)) - assertWithMessage("personalAvailable").that(personalAvailable!!).isTrue() + val availability by collectLastValue(interactor.availability) - assertWithMessage("workAvailable").that(workAvailable!!).isFalse() + assertWithMessage("personalAvailable").that(availability?.get(personalProfile)).isTrue() + assertWithMessage("workAvailable").that(availability?.get(workProfile)).isFalse() } @Test fun isAvailable() = runTest { val userRepo = FakeUserRepository(listOf(workUser, personalUser)) - val profileInteractor = + val interactor = UserInteractor(userRepository = userRepo, launchedAs = personalUser.handle) - val workAvailable by collectLastValue(profileInteractor.isAvailable(WORK)) + + val availability by collectLastValue(interactor.availability) // Default state is enabled in FakeUserManager - assertWithMessage("workAvailable").that(workAvailable).isTrue() + assertWithMessage("workAvailable").that(availability?.get(workProfile)).isTrue() // Making user unavailable makes profile unavailable userRepo.requestState(workUser, false) - assertWithMessage("workAvailable").that(workAvailable).isFalse() + assertWithMessage("workAvailable").that(availability?.get(workProfile)).isFalse() // Making user available makes profile available again userRepo.requestState(workUser, true) - assertWithMessage("workAvailable").that(workAvailable).isTrue() + assertWithMessage("workAvailable").that(availability?.get(workProfile)).isTrue() - // When a user is removed availability should update to false + // When a user is removed availability is removed as well. userRepo.removeUser(workUser) - assertWithMessage("workAvailable").that(workAvailable).isFalse() + assertWithMessage("workAvailable").that(availability?.get(workProfile)).isNull() } /** |