mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-03 16:10:00 +00:00
Add test on wasLastSession value.
This commit is contained in:
committed by
Benoit Marty
parent
e23f9c31c5
commit
11a808a2e0
+4
-2
@@ -10,8 +10,10 @@ package io.element.android.libraries.sessionstorage.impl
|
|||||||
import io.element.android.libraries.matrix.session.SessionData
|
import io.element.android.libraries.matrix.session.SessionData
|
||||||
import io.element.android.libraries.sessionstorage.api.LoginType
|
import io.element.android.libraries.sessionstorage.api.LoginType
|
||||||
|
|
||||||
internal fun aDbSessionData() = SessionData(
|
internal fun aDbSessionData(
|
||||||
userId = "userId",
|
userId: String = "userId",
|
||||||
|
) = SessionData(
|
||||||
|
userId = userId,
|
||||||
deviceId = "deviceId",
|
deviceId = "deviceId",
|
||||||
accessToken = "accessToken",
|
accessToken = "accessToken",
|
||||||
refreshToken = "refreshToken",
|
refreshToken = "refreshToken",
|
||||||
|
|||||||
+24
-2
@@ -23,7 +23,8 @@ import kotlinx.coroutines.test.runTest
|
|||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class) class DefaultSessionObserverTest {
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
class DefaultSessionObserverTest {
|
||||||
private lateinit var database: SessionDatabase
|
private lateinit var database: SessionDatabase
|
||||||
private lateinit var databaseSessionStore: DatabaseSessionStore
|
private lateinit var databaseSessionStore: DatabaseSessionStore
|
||||||
|
|
||||||
@@ -69,7 +70,28 @@ import org.junit.Test
|
|||||||
databaseSessionStore.removeSession(sessionData.userId)
|
databaseSessionStore.removeSession(sessionData.userId)
|
||||||
listener.assertEvents(
|
listener.assertEvents(
|
||||||
TestSessionListener.Event.Created(sessionData.userId),
|
TestSessionListener.Event.Created(sessionData.userId),
|
||||||
TestSessionListener.Event.Deleted(sessionData.userId),
|
TestSessionListener.Event.Deleted(sessionData.userId, true),
|
||||||
|
)
|
||||||
|
coroutineContext.cancelChildren()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `adding and deleting data twice invokes onSessionCreated and onSessionDeleted`() = runTest {
|
||||||
|
val sessionData1 = aDbSessionData(userId = "user1")
|
||||||
|
val sessionData2 = aDbSessionData(userId = "user2")
|
||||||
|
val sut = createDefaultSessionObserver()
|
||||||
|
runCurrent()
|
||||||
|
val listener = TestSessionListener()
|
||||||
|
sut.addListener(listener)
|
||||||
|
databaseSessionStore.addSession(sessionData1.toApiModel())
|
||||||
|
databaseSessionStore.addSession(sessionData2.toApiModel())
|
||||||
|
databaseSessionStore.removeSession(sessionData2.userId)
|
||||||
|
databaseSessionStore.removeSession(sessionData1.userId)
|
||||||
|
listener.assertEvents(
|
||||||
|
TestSessionListener.Event.Created(sessionData1.userId),
|
||||||
|
TestSessionListener.Event.Created(sessionData2.userId),
|
||||||
|
TestSessionListener.Event.Deleted(sessionData2.userId, wasLastSession = false),
|
||||||
|
TestSessionListener.Event.Deleted(sessionData1.userId, wasLastSession = true),
|
||||||
)
|
)
|
||||||
coroutineContext.cancelChildren()
|
coroutineContext.cancelChildren()
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -13,7 +13,7 @@ import io.element.android.libraries.sessionstorage.api.observer.SessionListener
|
|||||||
class TestSessionListener : SessionListener {
|
class TestSessionListener : SessionListener {
|
||||||
sealed interface Event {
|
sealed interface Event {
|
||||||
data class Created(val userId: String) : Event
|
data class Created(val userId: String) : Event
|
||||||
data class Deleted(val userId: String) : Event
|
data class Deleted(val userId: String, val wasLastSession: Boolean) : Event
|
||||||
}
|
}
|
||||||
|
|
||||||
private val trackRecord: MutableList<Event> = mutableListOf()
|
private val trackRecord: MutableList<Event> = mutableListOf()
|
||||||
@@ -23,7 +23,7 @@ class TestSessionListener : SessionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun onSessionDeleted(userId: String, wasLastSession: Boolean) {
|
override suspend fun onSessionDeleted(userId: String, wasLastSession: Boolean) {
|
||||||
trackRecord.add(Event.Deleted(userId))
|
trackRecord.add(Event.Deleted(userId, wasLastSession))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun assertEvents(vararg events: Event) {
|
fun assertEvents(vararg events: Event) {
|
||||||
|
|||||||
Reference in New Issue
Block a user