Create tests
This commit is contained in:
parent
41e614310f
commit
19caf2e7a7
5 changed files with 546 additions and 6 deletions
|
|
@ -66,8 +66,11 @@ dependencies {
|
||||||
testImplementation(libs.molecule.runtime)
|
testImplementation(libs.molecule.runtime)
|
||||||
testImplementation(libs.test.truth)
|
testImplementation(libs.test.truth)
|
||||||
testImplementation(libs.test.turbine)
|
testImplementation(libs.test.turbine)
|
||||||
|
testImplementation(libs.test.mockk)
|
||||||
testImplementation(projects.libraries.matrix.test)
|
testImplementation(projects.libraries.matrix.test)
|
||||||
testImplementation(projects.libraries.featureflag.test)
|
testImplementation(projects.libraries.featureflag.test)
|
||||||
|
testImplementation(projects.libraries.mediapickers.test)
|
||||||
|
testImplementation(projects.libraries.mediaupload.test)
|
||||||
testImplementation(projects.libraries.preferences.test)
|
testImplementation(projects.libraries.preferences.test)
|
||||||
testImplementation(projects.libraries.pushstore.test)
|
testImplementation(projects.libraries.pushstore.test)
|
||||||
testImplementation(projects.features.rageshake.test)
|
testImplementation(projects.features.rageshake.test)
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,8 @@ import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
import dagger.assisted.Assisted
|
import dagger.assisted.Assisted
|
||||||
import dagger.assisted.AssistedFactory
|
import dagger.assisted.AssistedFactory
|
||||||
import dagger.assisted.AssistedInject
|
import dagger.assisted.AssistedInject
|
||||||
|
|
@ -33,7 +35,6 @@ import io.element.android.libraries.architecture.runCatchingUpdatingState
|
||||||
import io.element.android.libraries.core.mimetype.MimeTypes
|
import io.element.android.libraries.core.mimetype.MimeTypes
|
||||||
import io.element.android.libraries.matrix.api.MatrixClient
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
import io.element.android.libraries.matrix.api.user.MatrixUser
|
import io.element.android.libraries.matrix.api.user.MatrixUser
|
||||||
import io.element.android.libraries.matrix.api.user.getCurrentUser
|
|
||||||
import io.element.android.libraries.matrix.ui.media.AvatarAction
|
import io.element.android.libraries.matrix.ui.media.AvatarAction
|
||||||
import io.element.android.libraries.mediapickers.api.PickerProvider
|
import io.element.android.libraries.mediapickers.api.PickerProvider
|
||||||
import io.element.android.libraries.mediaupload.api.MediaPreProcessor
|
import io.element.android.libraries.mediaupload.api.MediaPreProcessor
|
||||||
|
|
@ -56,8 +57,8 @@ class EditUserProfilePresenter @AssistedInject constructor(
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
override fun present(): EditUserProfileState {
|
override fun present(): EditUserProfileState {
|
||||||
var userAvatarUri = remember { matrixUser.avatarUrl?.let { Uri.parse(it) } }
|
var userAvatarUri by rememberSaveable { mutableStateOf(matrixUser.avatarUrl?.let { Uri.parse(it) }) }
|
||||||
var userDisplayName = remember { matrixUser.displayName }
|
var userDisplayName by rememberSaveable { mutableStateOf(matrixUser.displayName) }
|
||||||
val cameraPhotoPicker = mediaPickerProvider.registerCameraPhotoPicker(
|
val cameraPhotoPicker = mediaPickerProvider.registerCameraPhotoPicker(
|
||||||
onResult = { uri -> if (uri != null) userAvatarUri = uri }
|
onResult = { uri -> if (uri != null) userAvatarUri = uri }
|
||||||
)
|
)
|
||||||
|
|
@ -106,7 +107,7 @@ class EditUserProfilePresenter @AssistedInject constructor(
|
||||||
avatarActions = avatarActions,
|
avatarActions = avatarActions,
|
||||||
saveButtonEnabled = canSave && saveAction.value !is Async.Loading,
|
saveButtonEnabled = canSave && saveAction.value !is Async.Loading,
|
||||||
saveAction = saveAction.value,
|
saveAction = saveAction.value,
|
||||||
eventSink = ::handleEvents
|
eventSink = { handleEvents(it) },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,6 +139,6 @@ class EditUserProfilePresenter @AssistedInject constructor(
|
||||||
} else {
|
} else {
|
||||||
matrixClient.removeAvatar().getOrThrow()
|
matrixClient.removeAvatar().getOrThrow()
|
||||||
}
|
}
|
||||||
}
|
}.onFailure { it.printStackTrace() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,494 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023 New Vector Ltd
|
||||||
|
*
|
||||||
|
* 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 io.element.android.features.preferences.impl.user.editprofile
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import app.cash.molecule.RecompositionMode
|
||||||
|
import app.cash.molecule.moleculeFlow
|
||||||
|
import app.cash.turbine.test
|
||||||
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
import io.element.android.libraries.architecture.Async
|
||||||
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
|
import io.element.android.libraries.matrix.api.user.MatrixUser
|
||||||
|
import io.element.android.libraries.matrix.test.AN_AVATAR_URL
|
||||||
|
import io.element.android.libraries.matrix.test.A_USER_ID
|
||||||
|
import io.element.android.libraries.matrix.test.FakeMatrixClient
|
||||||
|
import io.element.android.libraries.matrix.ui.components.aMatrixUser
|
||||||
|
import io.element.android.libraries.matrix.ui.media.AvatarAction
|
||||||
|
import io.element.android.libraries.mediapickers.test.FakePickerProvider
|
||||||
|
import io.element.android.libraries.mediaupload.api.MediaUploadInfo
|
||||||
|
import io.element.android.libraries.mediaupload.test.FakeMediaPreProcessor
|
||||||
|
import io.element.android.tests.testutils.WarmUpRule
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.mockkStatic
|
||||||
|
import io.mockk.unmockkAll
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.After
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
@ExperimentalCoroutinesApi
|
||||||
|
class EditUserProfilePresenterTest {
|
||||||
|
|
||||||
|
@get:Rule
|
||||||
|
val warmUpRule = WarmUpRule()
|
||||||
|
|
||||||
|
private lateinit var fakePickerProvider: FakePickerProvider
|
||||||
|
private lateinit var fakeMediaPreProcessor: FakeMediaPreProcessor
|
||||||
|
|
||||||
|
private val userAvatarUri: Uri = mockk()
|
||||||
|
private val anotherAvatarUri: Uri = mockk()
|
||||||
|
|
||||||
|
private val fakeFileContents = ByteArray(2)
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
fakePickerProvider = FakePickerProvider()
|
||||||
|
fakeMediaPreProcessor = FakeMediaPreProcessor()
|
||||||
|
mockkStatic(Uri::class)
|
||||||
|
|
||||||
|
every { Uri.parse(AN_AVATAR_URL) } returns userAvatarUri
|
||||||
|
every { Uri.parse(ANOTHER_AVATAR_URL) } returns anotherAvatarUri
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
fun tearDown() {
|
||||||
|
unmockkAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun anEditUserProfilePresenter(
|
||||||
|
matrixClient: MatrixClient = FakeMatrixClient(),
|
||||||
|
matrixUser: MatrixUser = aMatrixUser(),
|
||||||
|
): EditUserProfilePresenter {
|
||||||
|
return EditUserProfilePresenter(
|
||||||
|
matrixClient = matrixClient,
|
||||||
|
matrixUser = matrixUser,
|
||||||
|
mediaPickerProvider = fakePickerProvider,
|
||||||
|
mediaPreProcessor = fakeMediaPreProcessor,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - initial state is created from room info`() = runTest {
|
||||||
|
val user = aMatrixUser(avatarUrl = AN_AVATAR_URL)
|
||||||
|
val presenter = anEditUserProfilePresenter(matrixUser = user)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
assertThat(initialState.userId).isEqualTo(user.userId.value)
|
||||||
|
assertThat(initialState.displayName).isEqualTo(user.displayName)
|
||||||
|
assertThat(initialState.userAvatarUrl).isEqualTo(userAvatarUri)
|
||||||
|
assertThat(initialState.avatarActions).containsExactly(
|
||||||
|
AvatarAction.ChoosePhoto,
|
||||||
|
AvatarAction.TakePhoto,
|
||||||
|
AvatarAction.Remove
|
||||||
|
)
|
||||||
|
assertThat(initialState.saveButtonEnabled).isEqualTo(false)
|
||||||
|
assertThat(initialState.saveAction).isInstanceOf(Async.Uninitialized::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - updates state in response to changes`() = runTest {
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
val presenter = anEditUserProfilePresenter(matrixUser = user)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
assertThat(initialState.displayName).isEqualTo("Name")
|
||||||
|
assertThat(initialState.userAvatarUrl).isEqualTo(userAvatarUri)
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name II"))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(displayName).isEqualTo("Name II")
|
||||||
|
assertThat(userAvatarUrl).isEqualTo(userAvatarUri)
|
||||||
|
}
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name III"))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(displayName).isEqualTo("Name III")
|
||||||
|
assertThat(userAvatarUrl).isEqualTo(userAvatarUri)
|
||||||
|
}
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(displayName).isEqualTo("Name III")
|
||||||
|
assertThat(userAvatarUrl).isNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - obtains avatar uris from gallery`() = runTest {
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
|
||||||
|
fakePickerProvider.givenResult(anotherAvatarUri)
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(matrixUser = user)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
assertThat(initialState.userAvatarUrl).isEqualTo(userAvatarUri)
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(userAvatarUrl).isEqualTo(anotherAvatarUri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - obtains avatar uris from camera`() = runTest {
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
|
||||||
|
fakePickerProvider.givenResult(anotherAvatarUri)
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(matrixUser = user)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
assertThat(initialState.userAvatarUrl).isEqualTo(userAvatarUri)
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.TakePhoto))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(userAvatarUrl).isEqualTo(anotherAvatarUri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - updates save button state`() = runTest {
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
|
||||||
|
fakePickerProvider.givenResult(userAvatarUri)
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(matrixUser = user)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
assertThat(initialState.saveButtonEnabled).isEqualTo(false)
|
||||||
|
|
||||||
|
// Once a change is made, the save button is enabled
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name II"))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(saveButtonEnabled).isEqualTo(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's reverted then the save disables again
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name"))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(saveButtonEnabled).isEqualTo(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make a change...
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(saveButtonEnabled).isEqualTo(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Revert it...
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(saveButtonEnabled).isEqualTo(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - updates save button state when initial values are null`() = runTest {
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = null)
|
||||||
|
|
||||||
|
fakePickerProvider.givenResult(userAvatarUri)
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(matrixUser = user)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
assertThat(initialState.saveButtonEnabled).isEqualTo(false)
|
||||||
|
|
||||||
|
// Once a change is made, the save button is enabled
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("Name II"))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(saveButtonEnabled).isEqualTo(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's reverted then the save disables again
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("fallback"))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(saveButtonEnabled).isEqualTo(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make a change...
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(saveButtonEnabled).isEqualTo(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Revert it...
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
||||||
|
awaitItem().apply {
|
||||||
|
assertThat(saveButtonEnabled).isEqualTo(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - save changes room details if different`() = runTest {
|
||||||
|
val matrixClient = FakeMatrixClient()
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(
|
||||||
|
matrixClient = matrixClient,
|
||||||
|
matrixUser = user
|
||||||
|
)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("New name"))
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
||||||
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
|
skipItems(5)
|
||||||
|
|
||||||
|
assertThat(matrixClient.setDisplayNameCalled).isTrue()
|
||||||
|
assertThat(matrixClient.removeAvatarCalled).isTrue()
|
||||||
|
assertThat(matrixClient.uploadAvatarCalled).isFalse()
|
||||||
|
|
||||||
|
cancelAndIgnoreRemainingEvents()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - save doesn't change room details if they're the same trimmed`() = runTest {
|
||||||
|
val matrixClient = FakeMatrixClient()
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(
|
||||||
|
matrixClient = matrixClient,
|
||||||
|
matrixUser = user
|
||||||
|
)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName(" Name "))
|
||||||
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
|
|
||||||
|
assertThat(matrixClient.setDisplayNameCalled).isTrue()
|
||||||
|
assertThat(matrixClient.uploadAvatarCalled).isFalse()
|
||||||
|
assertThat(matrixClient.removeAvatarCalled).isFalse()
|
||||||
|
|
||||||
|
cancelAndIgnoreRemainingEvents()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - save doesn't change name if it's now empty`() = runTest {
|
||||||
|
val matrixClient = FakeMatrixClient()
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(
|
||||||
|
matrixClient = matrixClient,
|
||||||
|
matrixUser = user
|
||||||
|
)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName(""))
|
||||||
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
|
|
||||||
|
assertThat(matrixClient.setDisplayNameCalled).isFalse()
|
||||||
|
assertThat(matrixClient.uploadAvatarCalled).isFalse()
|
||||||
|
assertThat(matrixClient.removeAvatarCalled).isFalse()
|
||||||
|
|
||||||
|
cancelAndIgnoreRemainingEvents()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - save processes and sets avatar when processor returns successfully`() = runTest {
|
||||||
|
val matrixClient = FakeMatrixClient()
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
|
||||||
|
givenPickerReturnsFile()
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(
|
||||||
|
matrixClient = matrixClient,
|
||||||
|
matrixUser = user
|
||||||
|
)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
||||||
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
|
skipItems(2)
|
||||||
|
|
||||||
|
assertThat(matrixClient.uploadAvatarCalled).isTrue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - save does not set avatar data if processor fails`() = runTest {
|
||||||
|
val matrixClient = FakeMatrixClient()
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(
|
||||||
|
matrixClient = matrixClient,
|
||||||
|
matrixUser = user
|
||||||
|
)
|
||||||
|
|
||||||
|
fakePickerProvider.givenResult(anotherAvatarUri)
|
||||||
|
fakeMediaPreProcessor.givenResult(Result.failure(Throwable("Oh no")))
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
||||||
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
|
skipItems(2)
|
||||||
|
|
||||||
|
assertThat(matrixClient.uploadAvatarCalled).isFalse()
|
||||||
|
|
||||||
|
assertThat(awaitItem().saveAction).isInstanceOf(Async.Failure::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - sets save action to failure if name update fails`() = runTest {
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
val matrixClient = FakeMatrixClient().apply {
|
||||||
|
givenSetDisplayNameResult(Result.failure(Throwable("!")))
|
||||||
|
}
|
||||||
|
|
||||||
|
saveAndAssertFailure(user, matrixClient, EditUserProfileEvents.UpdateDisplayName("New name"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - sets save action to failure if removing avatar fails`() = runTest {
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
val matrixClient = FakeMatrixClient().apply {
|
||||||
|
givenRemoveAvatarResult(Result.failure(Throwable("!")))
|
||||||
|
}
|
||||||
|
|
||||||
|
saveAndAssertFailure(user, matrixClient, EditUserProfileEvents.HandleAvatarAction(AvatarAction.Remove))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - sets save action to failure if setting avatar fails`() = runTest {
|
||||||
|
givenPickerReturnsFile()
|
||||||
|
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
val matrixClient = FakeMatrixClient().apply {
|
||||||
|
givenUploadAvatarResult(Result.failure(Throwable("!")))
|
||||||
|
}
|
||||||
|
|
||||||
|
saveAndAssertFailure(user, matrixClient, EditUserProfileEvents.HandleAvatarAction(AvatarAction.ChoosePhoto))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - CancelSaveChanges resets save action state`() = runTest {
|
||||||
|
givenPickerReturnsFile()
|
||||||
|
|
||||||
|
val user = aMatrixUser(id = A_USER_ID.value, displayName = "Name", avatarUrl = AN_AVATAR_URL)
|
||||||
|
val matrixClient = FakeMatrixClient().apply {
|
||||||
|
givenSetDisplayNameResult(Result.failure(Throwable("!")))
|
||||||
|
}
|
||||||
|
|
||||||
|
val presenter = anEditUserProfilePresenter(matrixUser = user)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.UpdateDisplayName("foo"))
|
||||||
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
|
skipItems(2)
|
||||||
|
|
||||||
|
assertThat(awaitItem().saveAction).isInstanceOf(Async.Failure::class.java)
|
||||||
|
|
||||||
|
initialState.eventSink(EditUserProfileEvents.CancelSaveChanges)
|
||||||
|
assertThat(awaitItem().saveAction).isInstanceOf(Async.Uninitialized::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun saveAndAssertFailure(matrixUser: MatrixUser, matrixClient: MatrixClient, event: EditUserProfileEvents) {
|
||||||
|
val presenter = anEditUserProfilePresenter(matrixUser = matrixUser, matrixClient = matrixClient)
|
||||||
|
|
||||||
|
moleculeFlow(RecompositionMode.Immediate) {
|
||||||
|
presenter.present()
|
||||||
|
}.test {
|
||||||
|
val initialState = awaitItem()
|
||||||
|
|
||||||
|
initialState.eventSink(event)
|
||||||
|
initialState.eventSink(EditUserProfileEvents.Save)
|
||||||
|
skipItems(1)
|
||||||
|
|
||||||
|
assertThat(awaitItem().saveAction).isInstanceOf(Async.Loading::class.java)
|
||||||
|
assertThat(awaitItem().saveAction).isInstanceOf(Async.Failure::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun givenPickerReturnsFile() {
|
||||||
|
mockkStatic(File::readBytes)
|
||||||
|
val processedFile: File = mockk {
|
||||||
|
every { readBytes() } returns fakeFileContents
|
||||||
|
}
|
||||||
|
|
||||||
|
fakePickerProvider.givenResult(anotherAvatarUri)
|
||||||
|
fakeMediaPreProcessor.givenResult(
|
||||||
|
Result.success(
|
||||||
|
MediaUploadInfo.AnyFile(
|
||||||
|
file = processedFile,
|
||||||
|
fileInfo = mockk(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val ANOTHER_AVATAR_URL = "example://camera/foo.jpg"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -58,6 +58,13 @@ class FakeMatrixClient(
|
||||||
private val accountManagementUrlString: Result<String?> = Result.success(null),
|
private val accountManagementUrlString: Result<String?> = Result.success(null),
|
||||||
) : MatrixClient {
|
) : MatrixClient {
|
||||||
|
|
||||||
|
var setDisplayNameCalled: Boolean = false
|
||||||
|
private set
|
||||||
|
var uploadAvatarCalled: Boolean = false
|
||||||
|
private set
|
||||||
|
var removeAvatarCalled: Boolean = false
|
||||||
|
private set
|
||||||
|
|
||||||
private var ignoreUserResult: Result<Unit> = Result.success(Unit)
|
private var ignoreUserResult: Result<Unit> = Result.success(Unit)
|
||||||
private var unignoreUserResult: Result<Unit> = Result.success(Unit)
|
private var unignoreUserResult: Result<Unit> = Result.success(Unit)
|
||||||
private var createRoomResult: Result<RoomId> = Result.success(A_ROOM_ID)
|
private var createRoomResult: Result<RoomId> = Result.success(A_ROOM_ID)
|
||||||
|
|
@ -69,6 +76,9 @@ class FakeMatrixClient(
|
||||||
private val searchUserResults = mutableMapOf<String, Result<MatrixSearchUserResults>>()
|
private val searchUserResults = mutableMapOf<String, Result<MatrixSearchUserResults>>()
|
||||||
private val getProfileResults = mutableMapOf<UserId, Result<MatrixUser>>()
|
private val getProfileResults = mutableMapOf<UserId, Result<MatrixUser>>()
|
||||||
private var uploadMediaResult: Result<String> = Result.success(AN_AVATAR_URL)
|
private var uploadMediaResult: Result<String> = Result.success(AN_AVATAR_URL)
|
||||||
|
private var setDisplayNameResult: Result<Unit> = Result.success(Unit)
|
||||||
|
private var uploadAvatarResult: Result<Unit> = Result.success(Unit)
|
||||||
|
private var removeAvatarResult: Result<Unit> = Result.success(Unit)
|
||||||
|
|
||||||
override suspend fun getRoom(roomId: RoomId): MatrixRoom? {
|
override suspend fun getRoom(roomId: RoomId): MatrixRoom? {
|
||||||
return getRoomResults[roomId]
|
return getRoomResults[roomId]
|
||||||
|
|
@ -141,6 +151,21 @@ class FakeMatrixClient(
|
||||||
return uploadMediaResult
|
return uploadMediaResult
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun setDisplayName(displayName: String): Result<Unit> {
|
||||||
|
setDisplayNameCalled = true
|
||||||
|
return setDisplayNameResult
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun uploadAvatar(mimeType: String, data: ByteArray): Result<Unit> {
|
||||||
|
uploadAvatarCalled = true
|
||||||
|
return uploadAvatarResult
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun removeAvatar(): Result<Unit> {
|
||||||
|
removeAvatarCalled = true
|
||||||
|
return removeAvatarResult
|
||||||
|
}
|
||||||
|
|
||||||
override fun sessionVerificationService(): SessionVerificationService = sessionVerificationService
|
override fun sessionVerificationService(): SessionVerificationService = sessionVerificationService
|
||||||
|
|
||||||
override fun pushersService(): PushersService = pushersService
|
override fun pushersService(): PushersService = pushersService
|
||||||
|
|
@ -197,4 +222,16 @@ class FakeMatrixClient(
|
||||||
fun givenUploadMediaResult(result: Result<String>) {
|
fun givenUploadMediaResult(result: Result<String>) {
|
||||||
uploadMediaResult = result
|
uploadMediaResult = result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun givenSetDisplayNameResult(result: Result<Unit>) {
|
||||||
|
setDisplayNameResult = result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun givenUploadAvatarResult(result: Result<Unit>) {
|
||||||
|
uploadAvatarResult = result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun givenRemoveAvatarResult(result: Result<Unit>) {
|
||||||
|
removeAvatarResult = result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,14 @@ open class MatrixUserProvider : PreviewParameterProvider<MatrixUser> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun aMatrixUser(id: String = "@id_of_alice:server.org", displayName: String = "Alice") = MatrixUser(
|
fun aMatrixUser(
|
||||||
|
id: String = "@id_of_alice:server.org",
|
||||||
|
displayName: String = "Alice",
|
||||||
|
avatarUrl: String? = null,
|
||||||
|
) = MatrixUser(
|
||||||
userId = UserId(id),
|
userId = UserId(id),
|
||||||
displayName = displayName,
|
displayName = displayName,
|
||||||
|
avatarUrl = avatarUrl,
|
||||||
)
|
)
|
||||||
|
|
||||||
fun aMatrixUserList() = listOf(
|
fun aMatrixUserList() = listOf(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue