summaryrefslogtreecommitdiff
path: root/tests/shared/src
diff options
context:
space:
mode:
Diffstat (limited to 'tests/shared/src')
-rw-r--r--tests/shared/src/com/android/intentresolver/v2/data/repository/FakeUserRepository.kt61
1 files changed, 61 insertions, 0 deletions
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<User> from List<UserState>
+ 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
+ }
+ }
+ }
+ }
+}