Adds tests related to backstack

This commit is contained in:
ericdecanini 2022-08-18 16:51:54 +02:00
parent 2fb794dd59
commit 789dffe4df
6 changed files with 42 additions and 24 deletions

View file

@ -59,7 +59,7 @@ interface SpaceStateHandler : DefaultLifecycleObserver {
*/
fun popSpaceBackstack(): String?
fun getPersistedSpaceBackstack(): List<String?>
fun getSpaceBackstack(): List<String?>
/**
* Gets a flow of the selected space for clients to react immediately to space changes.

View file

@ -109,13 +109,13 @@ class SpaceStateHandlerImpl @Inject constructor(
}
private fun addToBackstack(spaceToLeave: RoomSummary?, spaceToSet: RoomSummary?) {
// Only add to the persisted backstack if the space to set is not All Chats, else reset the persisted stack
// Only add to the backstack if the space to set is not All Chats, else clear the backstack
if (spaceToSet != null) {
val currentPersistedBackstack = vectorPreferences.getPersistedSpaceBackstack().toMutableList()
val currentPersistedBackstack = vectorPreferences.getSpaceBackstack().toMutableList()
currentPersistedBackstack.add(spaceToLeave?.roomId)
vectorPreferences.setPersistedSpaceBackstack(currentPersistedBackstack)
vectorPreferences.setSpaceBackstack(currentPersistedBackstack)
} else {
vectorPreferences.setPersistedSpaceBackstack(emptyList())
vectorPreferences.setSpaceBackstack(emptyList())
}
}
@ -143,14 +143,14 @@ class SpaceStateHandlerImpl @Inject constructor(
}
override fun popSpaceBackstack(): String? {
vectorPreferences.getPersistedSpaceBackstack().toMutableList().apply {
vectorPreferences.getSpaceBackstack().toMutableList().apply {
val poppedSpaceId = removeLast()
vectorPreferences.setPersistedSpaceBackstack(this)
vectorPreferences.setSpaceBackstack(this)
return poppedSpaceId
}
}
override fun getPersistedSpaceBackstack() = vectorPreferences.getPersistedSpaceBackstack()
override fun getSpaceBackstack() = vectorPreferences.getSpaceBackstack()
override fun getSelectedSpaceFlow() = selectedSpaceFlow

View file

@ -1120,7 +1120,7 @@ class VectorPreferences @Inject constructor(
*
* Only the IDs of the spaces are stored
*/
fun setPersistedSpaceBackstack(spaceBackstack: List<String?>) {
fun setSpaceBackstack(spaceBackstack: List<String?>) {
val spaceIdsJoined = spaceBackstack.takeIf { it.isNotEmpty() }?.joinToString(",")
defaultPrefs.edit().putString(SETTINGS_PERSISTED_SPACE_BACKSTACK, spaceIdsJoined).apply()
}
@ -1128,7 +1128,7 @@ class VectorPreferences @Inject constructor(
/**
* Gets the space backstack used for up navigation
*/
fun getPersistedSpaceBackstack(): List<String?> {
fun getSpaceBackstack(): List<String?> {
val spaceIdsJoined = defaultPrefs.getString(SETTINGS_PERSISTED_SPACE_BACKSTACK, null)
return spaceIdsJoined?.takeIf { it.isNotEmpty() }?.split(",").orEmpty()
}

View file

@ -85,7 +85,7 @@ class SpaceListViewModel @AssistedInject constructor(
}
observeSpaceSummaries()
val spaceHistory = spaceStateHandler.getPersistedSpaceBackstack()
val spaceHistory = spaceStateHandler.getSpaceBackstack()
.map { it to it?.let { session.roomService().getRoomSummary(it)?.displayName }.orEmpty() }
spaceStateHandler.getSelectedSpaceFlow()
.distinctUntilChanged()

View file

@ -21,11 +21,13 @@ 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 im.vector.app.test.fakes.FakeVectorPreferences
import im.vector.app.test.fixtures.RoomSummaryFixture.aRoomSummary
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Before
import org.junit.Test
internal class SpaceStateHandlerImplTest {
@ -38,14 +40,21 @@ internal class SpaceStateHandlerImplTest {
private val uiStateRepository = FakeUiStateRepository()
private val activeSessionHolder = FakeActiveSessionHolder(session)
private val analyticsTracker = FakeAnalyticsTracker()
private val vectorPreferences = FakeVectorPreferences()
private val spaceStateHandler = SpaceStateHandlerImpl(
sessionDataSource.instance,
uiStateRepository,
activeSessionHolder.instance,
analyticsTracker,
vectorPreferences.instance,
)
@Before
fun setup() {
vectorPreferences.givenSpaceBackstack(emptyList())
}
@Test
fun `given selected space doesn't exist, when getCurrentSpace, then return null`() {
val currentSpace = spaceStateHandler.getCurrentSpace()
@ -77,33 +86,33 @@ internal class SpaceStateHandlerImplTest {
}
@Test
fun `given is forward navigation and no current space, when setCurrentSpace, then null added to backstack`() {
fun `given not in space and is forward navigation, when setCurrentSpace, then null added to backstack`() {
spaceStateHandler.setCurrentSpace(spaceId, session, isForwardNavigation = true)
val backstack = spaceStateHandler.getSpaceBackstack()
backstack.size shouldBe 1
backstack.first() shouldBe null
vectorPreferences.verifySetSpaceBackstack(listOf(null))
}
@Test
fun `given is forward navigation and is in space, when setCurrentSpace, then previous space added to backstack`() {
fun `given in space and is forward navigation, when setCurrentSpace, then previous space added to backstack`() {
spaceStateHandler.setCurrentSpace(spaceId, session, isForwardNavigation = true)
spaceStateHandler.setCurrentSpace("secondSpaceId", session, isForwardNavigation = true)
val backstack = spaceStateHandler.getSpaceBackstack()
backstack.size shouldBe 2
backstack shouldBeEqualTo listOf(null, spaceId)
vectorPreferences.verifySetSpaceBackstack(listOf(spaceId))
}
@Test
fun `given is not forward navigation, when setCurrentSpace, then previous space not added to backstack`() {
spaceStateHandler.setCurrentSpace(spaceId, session, isForwardNavigation = false)
val backstack = spaceStateHandler.getSpaceBackstack()
vectorPreferences.verifySetSpaceBackstack(listOf(spaceId), inverse = true)
}
backstack.size shouldBe 0
@Test
fun `given navigating to all chats, when setCurrentSpace, then backstack cleared`() {
spaceStateHandler.setCurrentSpace(spaceId, session)
spaceStateHandler.setCurrentSpace(null, session)
vectorPreferences.verifySetSpaceBackstack(emptyList())
}
@Test

View file

@ -19,12 +19,21 @@ package im.vector.app.test.fakes
import im.vector.app.features.settings.VectorPreferences
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
class FakeVectorPreferences {
val instance = mockk<VectorPreferences>()
val instance = mockk<VectorPreferences>(relaxUnitFun = true)
fun givenUseCompleteNotificationFormat(value: Boolean) {
every { instance.useCompleteNotificationFormat() } returns value
}
fun givenSpaceBackstack(value: List<String?>) {
every { instance.getSpaceBackstack() } returns value
}
fun verifySetSpaceBackstack(value: List<String?>, inverse: Boolean = false) {
verify(inverse = inverse) { instance.setSpaceBackstack(value) }
}
}