diff --git a/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt b/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt index c661a66767..0282556900 100644 --- a/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt +++ b/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt @@ -188,7 +188,15 @@ class DefaultNavigator @Inject constructor( return } spaceStateHandler.setCurrentSpace(spaceId, overriddenSpaceName = overriddenSpaceName) - when (postSwitchSpaceAction) { + handlePostSwitchAction(context, spaceId, postSwitchSpaceAction) + } + + private fun handlePostSwitchAction( + context: Context, + spaceId: String, + action: Navigator.PostSwitchSpaceAction, + ) { + when (action) { Navigator.PostSwitchSpaceAction.None -> { // go back to home if we are showing room details? // This is a bit ugly, but the navigator is supposed to know about the activity stack @@ -204,9 +212,9 @@ class DefaultNavigator @Inject constructor( } is Navigator.PostSwitchSpaceAction.OpenDefaultRoom -> { val args = TimelineArgs( - postSwitchSpaceAction.roomId, + action.roomId, eventId = null, - openShareSpaceForId = spaceId.takeIf { postSwitchSpaceAction.showShareSheet } + openShareSpaceForId = spaceId.takeIf { action.showShareSheet } ) val intent = RoomDetailActivity.newIntent(context, args, false) startActivity(context, intent, false) diff --git a/vector/src/test/java/im/vector/app/features/navigation/DefaultNavigatorTest.kt b/vector/src/test/java/im/vector/app/features/navigation/DefaultNavigatorTest.kt new file mode 100644 index 0000000000..79c514083c --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/navigation/DefaultNavigatorTest.kt @@ -0,0 +1,77 @@ +/* + * 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.features.navigation + +import im.vector.app.test.fakes.FakeActiveSessionHolder +import im.vector.app.test.fakes.FakeAnalyticsTracker +import im.vector.app.test.fakes.FakeContext +import im.vector.app.test.fakes.FakeSpaceStateHandler +import im.vector.app.test.fakes.FakeSupportedVerificationMethodsProvider +import im.vector.app.test.fakes.FakeVectorFeatures +import im.vector.app.test.fakes.FakeVectorPreferences +import im.vector.app.test.fakes.FakeWidgetArgsBuilder +import im.vector.app.test.fixtures.RoomSummaryFixture.aRoomSummary +import org.junit.Test + +internal class DefaultNavigatorTest { + + private val sessionHolder = FakeActiveSessionHolder() + private val vectorPreferences = FakeVectorPreferences() + private val widgetArgsBuilder = FakeWidgetArgsBuilder() + private val spaceStateHandler = FakeSpaceStateHandler() + private val supportedVerificationMethodsProvider = FakeSupportedVerificationMethodsProvider() + private val features = FakeVectorFeatures() + private val analyticsTracker = FakeAnalyticsTracker() + + private val navigator = DefaultNavigator( + sessionHolder.instance, + vectorPreferences.instance, + widgetArgsBuilder.instance, + spaceStateHandler, + supportedVerificationMethodsProvider.instance, + features, + analyticsTracker, + ) + + /** + * The below tests are by no means all that we want to test in [DefaultNavigator] + * Please add relevant tests as you make changes to or related to other functions in the class + */ + + @Test + fun `when switchToSpace, then current space set`() { + val spaceId = "space-id" + val spaceSummary = aRoomSummary(spaceId) + sessionHolder.fakeSession.fakeRoomService.getRoomSummaryReturns(spaceSummary) + + navigator.switchToSpace(FakeContext().instance, spaceId, Navigator.PostSwitchSpaceAction.None) + + spaceStateHandler.verifySetCurrentSpace(spaceId) + } + + @Test + fun `given non-null overriddenSpaceName, when switchToSpace, then current space set`() { + val spaceId = "space-id" + val spaceSummary = aRoomSummary(spaceId) + sessionHolder.fakeSession.fakeRoomService.getRoomSummaryReturns(spaceSummary) + val overriddenSpaceName = "new-space-name" + + navigator.switchToSpace(FakeContext().instance, spaceId, Navigator.PostSwitchSpaceAction.None, overriddenSpaceName) + + spaceStateHandler.verifySetCurrentSpace(spaceId, overriddenSpaceName = overriddenSpaceName) + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeRoomService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeRoomService.kt index b09256f747..506e96ba11 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeRoomService.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeRoomService.kt @@ -16,12 +16,18 @@ package im.vector.app.test.fakes +import io.mockk.every import io.mockk.mockk import org.matrix.android.sdk.api.session.room.RoomService +import org.matrix.android.sdk.api.session.room.model.RoomSummary class FakeRoomService( private val fakeRoom: FakeRoom = FakeRoom() ) : RoomService by mockk() { override fun getRoom(roomId: String) = fakeRoom + + fun getRoomSummaryReturns(roomSummary: RoomSummary?) { + every { getRoomSummary(any()) } returns roomSummary + } } diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt index 18af88ba0f..ee016ecae3 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt @@ -37,7 +37,7 @@ class FakeSession( val fakeProfileService: FakeProfileService = FakeProfileService(), val fakeHomeServerCapabilitiesService: FakeHomeServerCapabilitiesService = FakeHomeServerCapabilitiesService(), val fakeSharedSecretStorageService: FakeSharedSecretStorageService = FakeSharedSecretStorageService(), - private val fakeRoomService: FakeRoomService = FakeRoomService(), + val fakeRoomService: FakeRoomService = FakeRoomService(), private val fakeEventService: FakeEventService = FakeEventService(), ) : Session by mockk(relaxed = true) { diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeSpaceStateHandler.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeSpaceStateHandler.kt new file mode 100644 index 0000000000..04358d6a1f --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeSpaceStateHandler.kt @@ -0,0 +1,28 @@ +/* + * 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.fakes + +import im.vector.app.SpaceStateHandler +import io.mockk.mockk +import io.mockk.verify + +class FakeSpaceStateHandler : SpaceStateHandler by mockk(relaxUnitFun = true) { + + fun verifySetCurrentSpace(spaceId: String, overriddenSpaceName: String? = null) { + verify { setCurrentSpace(spaceId, overriddenSpaceName = overriddenSpaceName) } + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeSupportedVerificationMethodsProvider.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeSupportedVerificationMethodsProvider.kt new file mode 100644 index 0000000000..ecb7c3289b --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeSupportedVerificationMethodsProvider.kt @@ -0,0 +1,25 @@ +/* + * 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.fakes + +import im.vector.app.features.crypto.verification.SupportedVerificationMethodsProvider +import io.mockk.mockk + +class FakeSupportedVerificationMethodsProvider { + + val instance = mockk() +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeWidgetArgsBuilder.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeWidgetArgsBuilder.kt new file mode 100644 index 0000000000..3a810b2c23 --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeWidgetArgsBuilder.kt @@ -0,0 +1,25 @@ +/* + * 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.fakes + +import im.vector.app.features.widgets.WidgetArgsBuilder +import io.mockk.mockk + +class FakeWidgetArgsBuilder { + + val instance = mockk() +}