From f770ae065318f747829e4e3fd74e81a3175e5c14 Mon Sep 17 00:00:00 2001 From: ericdecanini Date: Thu, 21 Jul 2022 08:53:08 +0200 Subject: [PATCH] Adds tests for persist space and backstack --- .../im/vector/app/AppStateHandlerImplTest.kt | 116 ++++++++++++++++++ .../java/im/vector/app/AppStateHandlerTest.kt | 71 ----------- .../app/test/fakes/FakeUiStateRepository.kt | 29 +++++ 3 files changed, 145 insertions(+), 71 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/AppStateHandlerImplTest.kt delete mode 100644 vector/src/test/java/im/vector/app/AppStateHandlerTest.kt create mode 100644 vector/src/test/java/im/vector/app/test/fakes/FakeUiStateRepository.kt diff --git a/vector/src/test/java/im/vector/app/AppStateHandlerImplTest.kt b/vector/src/test/java/im/vector/app/AppStateHandlerImplTest.kt new file mode 100644 index 0000000000..f523208c97 --- /dev/null +++ b/vector/src/test/java/im/vector/app/AppStateHandlerImplTest.kt @@ -0,0 +1,116 @@ +/* + * 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 + +import im.vector.app.test.fakes.FakeActiveSessionDataSource +import im.vector.app.test.fakes.FakeActiveSessionHolder +import im.vector.app.test.fakes.FakeAnalyticsTracker +import im.vector.app.test.fakes.FakeSession +import im.vector.app.test.fakes.FakeUiStateRepository +import io.mockk.every +import io.mockk.justRun +import io.mockk.mockk +import org.amshove.kluent.shouldBe +import org.amshove.kluent.shouldBeEqualTo +import org.junit.Before +import org.junit.Test +import org.matrix.android.sdk.api.session.room.model.RoomSummary + +internal class AppStateHandlerImplTest { + + private val spaceId = "spaceId" + private val spaceSummary: RoomSummary = mockk() + private val session = FakeSession.withRoomSummary(spaceSummary) + + private val sessionDataSource = FakeActiveSessionDataSource() + private val uiStateRepository = FakeUiStateRepository() + private val activeSessionHolder = FakeActiveSessionHolder(session) + private val analyticsTracker = FakeAnalyticsTracker() + + private val appStateHandler = AppStateHandlerImpl( + sessionDataSource.instance, + uiStateRepository, + activeSessionHolder.instance, + analyticsTracker, + ) + + @Before + fun setup() { + justRun { uiStateRepository.storeSelectedSpace(any(), any()) } + every { spaceSummary.roomId } returns spaceId + } + + @Test + fun `given selected space doesn't exist, when getCurrentSpace, then return null`() { + val currentSpace = appStateHandler.getCurrentSpace() + + currentSpace shouldBe null + } + + @Test + fun `given selected space exists, when getCurrentSpace, then return selected space`() { + appStateHandler.setCurrentSpace(spaceId, session) + + val currentSpace = appStateHandler.getCurrentSpace() + + currentSpace shouldBe spaceSummary + } + + @Test + fun `given persistNow is true, when setCurrentSpace, then immediately persist to ui state`() { + appStateHandler.setCurrentSpace(spaceId, session, persistNow = true) + + uiStateRepository.verifyStoreSelectedSpace(spaceId, session) + } + + @Test + fun `given persistNow is false, when setCurrentSpace, then don't immediately persist to ui state`() { + appStateHandler.setCurrentSpace(spaceId, session, persistNow = false) + + uiStateRepository.verifyStoreSelectedSpace(spaceId, session, inverse = true) + } + + @Test + fun `given is forward navigation and no current space, when setCurrentSpace, then null added to backstack`() { + appStateHandler.setCurrentSpace(spaceId, session, isForwardNavigation = true) + + val backstack = appStateHandler.getSpaceBackstack() + + backstack.size shouldBe 1 + backstack.first() shouldBe null + } + + @Test + fun `given is forward navigation and is in space, when setCurrentSpace, then previous space added to backstack`() { + appStateHandler.setCurrentSpace(spaceId, session, isForwardNavigation = true) + appStateHandler.setCurrentSpace("secondSpaceId", session, isForwardNavigation = true) + + val backstack = appStateHandler.getSpaceBackstack() + + backstack.size shouldBe 2 + backstack shouldBeEqualTo listOf(null, spaceId) + } + + @Test + fun `given is not forward navigation, when setCurrentSpace, then previous space not added to backstack`() { + appStateHandler.setCurrentSpace(spaceId, session, isForwardNavigation = false) + + val backstack = appStateHandler.getSpaceBackstack() + + backstack.size shouldBe 0 + } +} diff --git a/vector/src/test/java/im/vector/app/AppStateHandlerTest.kt b/vector/src/test/java/im/vector/app/AppStateHandlerTest.kt deleted file mode 100644 index 51e5308a92..0000000000 --- a/vector/src/test/java/im/vector/app/AppStateHandlerTest.kt +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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 - -import im.vector.app.features.analytics.AnalyticsTracker -import im.vector.app.features.ui.UiStateRepository -import im.vector.app.test.fakes.FakeActiveSessionHolder -import im.vector.app.test.fakes.FakeSession -import io.mockk.every -import io.mockk.justRun -import io.mockk.mockk -import org.amshove.kluent.shouldBe -import org.junit.Before -import org.junit.Test -import org.matrix.android.sdk.api.session.room.model.RoomSummary - -internal class AppStateHandlerTest { - - private val spaceId = "spaceId" - private val spaceSummary: RoomSummary = mockk { - every { roomId } returns spaceId - } - private val session = FakeSession.withRoomSummary(spaceSummary) - - private val sessionDataSource: ActiveSessionDataSource = mockk() - private val uiStateRepository: UiStateRepository = mockk() - private val activeSessionHolder = FakeActiveSessionHolder(session).instance - private val analyticsTracker: AnalyticsTracker = mockk() - - private val appStateHandler = AppStateHandlerImpl( - sessionDataSource, - uiStateRepository, - activeSessionHolder, - analyticsTracker, - ) - - @Before - fun setup() { - justRun { uiStateRepository.storeSelectedSpace(any(), any()) } - } - - @Test - fun `given selected space doesn't exist, when getCurrentSpace, then return null`() { - val currentSpace = appStateHandler.getCurrentSpace() - - currentSpace shouldBe null - } - - @Test - fun `given selected space exists, when getCurrentSpace, then return selected space`() { - appStateHandler.setCurrentSpace(spaceId, session) - - val currentSpace = appStateHandler.getCurrentSpace() - - currentSpace shouldBe spaceSummary - } -} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeUiStateRepository.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeUiStateRepository.kt new file mode 100644 index 0000000000..3b38e4512c --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeUiStateRepository.kt @@ -0,0 +1,29 @@ +/* + * 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.ui.UiStateRepository +import io.mockk.mockk +import io.mockk.verify +import org.matrix.android.sdk.api.session.Session + +class FakeUiStateRepository : UiStateRepository by mockk() { + + fun verifyStoreSelectedSpace(roomId: String, session: Session, inverse: Boolean = false) { + verify(inverse = inverse) { storeSelectedSpace(roomId, session.sessionId) } + } +}