mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 05:31:21 +03:00
Adds tests for persist space and backstack
This commit is contained in:
parent
49992f682e
commit
f770ae0653
3 changed files with 145 additions and 71 deletions
116
vector/src/test/java/im/vector/app/AppStateHandlerImplTest.kt
Normal file
116
vector/src/test/java/im/vector/app/AppStateHandlerImplTest.kt
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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) }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue