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 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. * 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?) { 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) { if (spaceToSet != null) {
val currentPersistedBackstack = vectorPreferences.getPersistedSpaceBackstack().toMutableList() val currentPersistedBackstack = vectorPreferences.getSpaceBackstack().toMutableList()
currentPersistedBackstack.add(spaceToLeave?.roomId) currentPersistedBackstack.add(spaceToLeave?.roomId)
vectorPreferences.setPersistedSpaceBackstack(currentPersistedBackstack) vectorPreferences.setSpaceBackstack(currentPersistedBackstack)
} else { } else {
vectorPreferences.setPersistedSpaceBackstack(emptyList()) vectorPreferences.setSpaceBackstack(emptyList())
} }
} }
@ -143,14 +143,14 @@ class SpaceStateHandlerImpl @Inject constructor(
} }
override fun popSpaceBackstack(): String? { override fun popSpaceBackstack(): String? {
vectorPreferences.getPersistedSpaceBackstack().toMutableList().apply { vectorPreferences.getSpaceBackstack().toMutableList().apply {
val poppedSpaceId = removeLast() val poppedSpaceId = removeLast()
vectorPreferences.setPersistedSpaceBackstack(this) vectorPreferences.setSpaceBackstack(this)
return poppedSpaceId return poppedSpaceId
} }
} }
override fun getPersistedSpaceBackstack() = vectorPreferences.getPersistedSpaceBackstack() override fun getSpaceBackstack() = vectorPreferences.getSpaceBackstack()
override fun getSelectedSpaceFlow() = selectedSpaceFlow override fun getSelectedSpaceFlow() = selectedSpaceFlow

View File

@ -1120,7 +1120,7 @@ class VectorPreferences @Inject constructor(
* *
* Only the IDs of the spaces are stored * 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(",") val spaceIdsJoined = spaceBackstack.takeIf { it.isNotEmpty() }?.joinToString(",")
defaultPrefs.edit().putString(SETTINGS_PERSISTED_SPACE_BACKSTACK, spaceIdsJoined).apply() 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 * 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) val spaceIdsJoined = defaultPrefs.getString(SETTINGS_PERSISTED_SPACE_BACKSTACK, null)
return spaceIdsJoined?.takeIf { it.isNotEmpty() }?.split(",").orEmpty() return spaceIdsJoined?.takeIf { it.isNotEmpty() }?.split(",").orEmpty()
} }

View File

@ -85,7 +85,7 @@ class SpaceListViewModel @AssistedInject constructor(
} }
observeSpaceSummaries() observeSpaceSummaries()
val spaceHistory = spaceStateHandler.getPersistedSpaceBackstack() val spaceHistory = spaceStateHandler.getSpaceBackstack()
.map { it to it?.let { session.roomService().getRoomSummary(it)?.displayName }.orEmpty() } .map { it to it?.let { session.roomService().getRoomSummary(it)?.displayName }.orEmpty() }
spaceStateHandler.getSelectedSpaceFlow() spaceStateHandler.getSelectedSpaceFlow()
.distinctUntilChanged() .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.FakeAnalyticsTracker
import im.vector.app.test.fakes.FakeSession import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.fakes.FakeUiStateRepository import im.vector.app.test.fakes.FakeUiStateRepository
import im.vector.app.test.fakes.FakeVectorPreferences
import im.vector.app.test.fixtures.RoomSummaryFixture.aRoomSummary import im.vector.app.test.fixtures.RoomSummaryFixture.aRoomSummary
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldBeEqualTo import org.amshove.kluent.shouldBeEqualTo
import org.junit.Before
import org.junit.Test import org.junit.Test
internal class SpaceStateHandlerImplTest { internal class SpaceStateHandlerImplTest {
@ -38,14 +40,21 @@ internal class SpaceStateHandlerImplTest {
private val uiStateRepository = FakeUiStateRepository() private val uiStateRepository = FakeUiStateRepository()
private val activeSessionHolder = FakeActiveSessionHolder(session) private val activeSessionHolder = FakeActiveSessionHolder(session)
private val analyticsTracker = FakeAnalyticsTracker() private val analyticsTracker = FakeAnalyticsTracker()
private val vectorPreferences = FakeVectorPreferences()
private val spaceStateHandler = SpaceStateHandlerImpl( private val spaceStateHandler = SpaceStateHandlerImpl(
sessionDataSource.instance, sessionDataSource.instance,
uiStateRepository, uiStateRepository,
activeSessionHolder.instance, activeSessionHolder.instance,
analyticsTracker, analyticsTracker,
vectorPreferences.instance,
) )
@Before
fun setup() {
vectorPreferences.givenSpaceBackstack(emptyList())
}
@Test @Test
fun `given selected space doesn't exist, when getCurrentSpace, then return null`() { fun `given selected space doesn't exist, when getCurrentSpace, then return null`() {
val currentSpace = spaceStateHandler.getCurrentSpace() val currentSpace = spaceStateHandler.getCurrentSpace()
@ -77,33 +86,33 @@ internal class SpaceStateHandlerImplTest {
} }
@Test @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) spaceStateHandler.setCurrentSpace(spaceId, session, isForwardNavigation = true)
val backstack = spaceStateHandler.getSpaceBackstack() vectorPreferences.verifySetSpaceBackstack(listOf(null))
backstack.size shouldBe 1
backstack.first() shouldBe null
} }
@Test @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(spaceId, session, isForwardNavigation = true)
spaceStateHandler.setCurrentSpace("secondSpaceId", session, isForwardNavigation = true) spaceStateHandler.setCurrentSpace("secondSpaceId", session, isForwardNavigation = true)
val backstack = spaceStateHandler.getSpaceBackstack() vectorPreferences.verifySetSpaceBackstack(listOf(spaceId))
backstack.size shouldBe 2
backstack shouldBeEqualTo listOf(null, spaceId)
} }
@Test @Test
fun `given is not forward navigation, when setCurrentSpace, then previous space not added to backstack`() { fun `given is not forward navigation, when setCurrentSpace, then previous space not added to backstack`() {
spaceStateHandler.setCurrentSpace(spaceId, session, isForwardNavigation = false) 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 @Test

View File

@ -19,12 +19,21 @@ package im.vector.app.test.fakes
import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.settings.VectorPreferences
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify
class FakeVectorPreferences { class FakeVectorPreferences {
val instance = mockk<VectorPreferences>() val instance = mockk<VectorPreferences>(relaxUnitFun = true)
fun givenUseCompleteNotificationFormat(value: Boolean) { fun givenUseCompleteNotificationFormat(value: Boolean) {
every { instance.useCompleteNotificationFormat() } returns value 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) }
}
} }