From 75cbb727a443e78f972072b485c5b96d7d83bb9b Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 2 Mar 2022 17:45:29 +0000 Subject: [PATCH] cleaning up test names and bodies to be clearer --- .../onboarding/OnboardingViewModelTest.kt | 45 ++++++++++--------- .../fixtures/HomeserverCapabilityFixture.kt | 40 +++++++++++++++++ 2 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/test/fixtures/HomeserverCapabilityFixture.kt diff --git a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt index a36c3cbb7a..b485a3fa44 100644 --- a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt @@ -37,6 +37,7 @@ import im.vector.app.test.fakes.FakeUri import im.vector.app.test.fakes.FakeUriFilenameResolver import im.vector.app.test.fakes.FakeVectorFeatures import im.vector.app.test.fakes.FakeVectorOverrides +import im.vector.app.test.fixtures.aHomeServerCapabilities import im.vector.app.test.test import kotlinx.coroutines.test.runBlockingTest import org.junit.Before @@ -51,6 +52,7 @@ private const val A_DISPLAY_NAME = "a display name" private const val A_PICTURE_FILENAME = "a-picture.png" private val AN_ERROR = RuntimeException("an error!") private val A_LOADABLE_REGISTER_ACTION = RegisterAction.StartRegistration +private val A_HOMESERVER_CAPABILITIES = aHomeServerCapabilities(canChangeDisplayName = true, canChangeAvatar = true) class OnboardingViewModelTest { @@ -129,38 +131,27 @@ class OnboardingViewModelTest { } @Test - fun `given registration has started and has dummy step to do, when handling action, then ignores other steps and executes dummy`() = runBlockingTest { + fun `when registering account, then updates state and emits account created event`() = runBlockingTest { + givenRegistrationResultFor(A_LOADABLE_REGISTER_ACTION, RegistrationResult.Success(fakeSession)) + givenSuccessfullyCreatesAccount(A_HOMESERVER_CAPABILITIES) val test = viewModel.test(this) - val homeServerCapabilities = HomeServerCapabilities(canChangeDisplayName = true, canChangeAvatar = true) - fakeSession.fakeHomeServerCapabilitiesService.givenCapabilities(homeServerCapabilities) - val flowResult = FlowResult(missingStages = listOf(Stage.Dummy(mandatory = true), Stage.Email(true)), completedStages = emptyList()) - givenRegistrationResultsFor(listOf( - A_LOADABLE_REGISTER_ACTION to RegistrationResult.FlowResponse(flowResult), - RegisterAction.RegisterDummy to RegistrationResult.Success(fakeSession) - )) - givenSuccessfullyCreatesAccount() - viewModel.handle(OnboardingAction.PostRegisterAction(A_LOADABLE_REGISTER_ACTION)) test .assertStatesWithPrevious( initialState, { copy(asyncRegistration = Loading()) }, - { copy(asyncLoginAction = Success(Unit), personalizationState = homeServerCapabilities.toPersonalisationState()) }, - { copy(asyncRegistration = Uninitialized) }, - - ) + { copy(asyncLoginAction = Success(Unit), personalizationState = A_HOMESERVER_CAPABILITIES.toPersonalisationState()) }, + { copy(asyncLoginAction = Success(Unit), asyncRegistration = Uninitialized) } + ) .assertEvents(OnboardingViewEvents.OnAccountCreated) .finish() } @Test - fun `given homeserver does not support personalisation, when registering account, then updates state and emits account created event`() = runBlockingTest { - val homeServerCapabilities = HomeServerCapabilities(canChangeDisplayName = false, canChangeAvatar = false) - fakeSession.fakeHomeServerCapabilitiesService.givenCapabilities(homeServerCapabilities) - givenRegistrationResultFor(A_LOADABLE_REGISTER_ACTION, RegistrationResult.Success(fakeSession)) - givenSuccessfullyCreatesAccount() + fun `given registration has started and has dummy step to do, when handling action, then ignores other steps and executes dummy`() = runBlockingTest { + givenSuccessfulRegistrationForStartAndDummySteps(missingStages = listOf(Stage.Dummy(mandatory = true))) val test = viewModel.test(this) viewModel.handle(OnboardingAction.PostRegisterAction(A_LOADABLE_REGISTER_ACTION)) @@ -169,8 +160,8 @@ class OnboardingViewModelTest { .assertStatesWithPrevious( initialState, { copy(asyncRegistration = Loading()) }, - { copy(asyncLoginAction = Success(Unit), personalizationState = homeServerCapabilities.toPersonalisationState()) }, - { copy(asyncLoginAction = Success(Unit), asyncRegistration = Uninitialized) } + { copy(asyncLoginAction = Success(Unit), personalizationState = A_HOMESERVER_CAPABILITIES.toPersonalisationState()) }, + { copy(asyncRegistration = Uninitialized) } ) .assertEvents(OnboardingViewEvents.OnAccountCreated) .finish() @@ -339,7 +330,17 @@ class OnboardingViewModelTest { ) } - private fun givenSuccessfullyCreatesAccount() { + private fun givenSuccessfulRegistrationForStartAndDummySteps(missingStages: List) { + val flowResult = FlowResult(missingStages = missingStages, completedStages = emptyList()) + givenRegistrationResultsFor(listOf( + A_LOADABLE_REGISTER_ACTION to RegistrationResult.FlowResponse(flowResult), + RegisterAction.RegisterDummy to RegistrationResult.Success(fakeSession) + )) + givenSuccessfullyCreatesAccount(A_HOMESERVER_CAPABILITIES) + } + + private fun givenSuccessfullyCreatesAccount(homeServerCapabilities: HomeServerCapabilities) { + fakeSession.fakeHomeServerCapabilitiesService.givenCapabilities(homeServerCapabilities) fakeActiveSessionHolder.expectSetsActiveSession(fakeSession) fakeAuthenticationService.expectReset() fakeSession.expectStartsSyncing() diff --git a/vector/src/test/java/im/vector/app/test/fixtures/HomeserverCapabilityFixture.kt b/vector/src/test/java/im/vector/app/test/fixtures/HomeserverCapabilityFixture.kt new file mode 100644 index 0000000000..a4d9869a89 --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fixtures/HomeserverCapabilityFixture.kt @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 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 im.vector.app.test.fixtures + +import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities +import org.matrix.android.sdk.api.session.homeserver.RoomVersionCapabilities + +fun aHomeServerCapabilities( + canChangePassword: Boolean = true, + canChangeDisplayName: Boolean = true, + canChangeAvatar: Boolean = true, + canChange3pid: Boolean = true, + maxUploadFileSize: Long = 100L, + lastVersionIdentityServerSupported: Boolean = false, + defaultIdentityServerUrl: String? = null, + roomVersions: RoomVersionCapabilities? = null +) = HomeServerCapabilities( + canChangePassword, + canChangeDisplayName, + canChangeAvatar, + canChange3pid, + maxUploadFileSize, + lastVersionIdentityServerSupported, + defaultIdentityServerUrl, + roomVersions +)