From f30cb97a784ba508a82863ef74ea0135355aad0c Mon Sep 17 00:00:00 2001 From: Mark Renouf Date: Tue, 23 Jan 2024 12:21:43 -0500 Subject: UserInteractor and Profile A domain component which applies business logic to User data. This component maps Users to Profiles, the set of which is defined by Profile.Type Bug: 309960444 Test: atest IntentResolver-tests-unit:UserInteractorTest \ FakeUserRepositoryTest Change-Id: I9832836ae019ba1b0ae45366f9fc0e26bc9b23ce --- .../v2/data/repository/FakeUserRepository.kt | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/shared/src/com/android/intentresolver/v2/data/repository/FakeUserRepository.kt (limited to 'tests/shared') diff --git a/tests/shared/src/com/android/intentresolver/v2/data/repository/FakeUserRepository.kt b/tests/shared/src/com/android/intentresolver/v2/data/repository/FakeUserRepository.kt new file mode 100644 index 00000000..5ed6f506 --- /dev/null +++ b/tests/shared/src/com/android/intentresolver/v2/data/repository/FakeUserRepository.kt @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.v2.data.repository + +import com.android.intentresolver.v2.shared.model.User +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.update + +/** A simple repository which can be initialized from a list and updated. */ +class FakeUserRepository(vararg userList: User) : UserRepository { + internal data class UserState(val user: User, val available: Boolean) + + private val userState = MutableStateFlow(userList.map { UserState(it, available = true) }) + + // Expose a List from List + override val users = userState.map { userList -> userList.map { it.user } } + + fun addUser(user: User, available: Boolean) { + require(userState.value.none { it.user.id == user.id }) { + "A User with ${user.id} already exists!" + } + userState.update { it + UserState(user, available) } + } + + fun removeUser(user: User) { + require(userState.value.any { it.user.id == user.id }) { + "A User with ${user.id} does not exist!" + } + userState.update { it.filterNot { state -> state.user.id == user.id } } + } + + override val availability = + userState.map { userStateList -> userStateList.associate { it.user to it.available } } + + override suspend fun requestState(user: User, available: Boolean) { + userState.update { userStateList -> + userStateList.map { userState -> + if (userState.user.id == user.id) { + UserState(user, available) + } else { + userState + } + } + } + } +} -- cgit v1.2.3-59-g8ed1b