mirror of
https://github.com/element-hq/element-android
synced 2024-11-23 01:45:36 +03:00
Adding unit tests for GetNotificationCountForSpacesUseCase
This commit is contained in:
parent
a509da54e8
commit
e8c95551c1
5 changed files with 202 additions and 4 deletions
|
@ -32,7 +32,6 @@ import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
|
|||
import org.matrix.android.sdk.api.session.room.summary.RoomAggregateNotificationCount
|
||||
import javax.inject.Inject
|
||||
|
||||
// TODO add unit tests
|
||||
class GetNotificationCountForSpacesUseCase @Inject constructor(
|
||||
private val activeSessionHolder: ActiveSessionHolder,
|
||||
private val autoAcceptInvites: AutoAcceptInvites,
|
||||
|
@ -64,7 +63,7 @@ class GetNotificationCountForSpacesUseCase @Inject constructor(
|
|||
highlightCount = totalCount.highlightCount + inviteCount,
|
||||
)
|
||||
}
|
||||
?.flowOn(Dispatchers.Default)
|
||||
?.flowOn(session.coroutineDispatchers.main)
|
||||
?: emptyFlow()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,174 @@
|
|||
/*
|
||||
* Copyright (c) 2023 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.features.spaces.notification
|
||||
|
||||
import androidx.paging.PagedList
|
||||
import im.vector.app.test.fakes.FakeActiveSessionHolder
|
||||
import im.vector.app.test.fakes.FakeAutoAcceptInvites
|
||||
import im.vector.app.test.fakes.FakeFlowLiveDataConversions
|
||||
import im.vector.app.test.fakes.givenAsFlow
|
||||
import im.vector.app.test.test
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkStatic
|
||||
import io.mockk.unmockkAll
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.flow.sample
|
||||
import kotlinx.coroutines.test.advanceUntilIdle
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.matrix.android.sdk.api.query.SpaceFilter
|
||||
import org.matrix.android.sdk.api.session.room.RoomSortOrder
|
||||
import org.matrix.android.sdk.api.session.room.model.Membership
|
||||
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
||||
import org.matrix.android.sdk.api.session.room.summary.RoomAggregateNotificationCount
|
||||
|
||||
internal class GetNotificationCountForSpacesUseCaseTest {
|
||||
|
||||
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
|
||||
private val fakeAutoAcceptInvites = FakeAutoAcceptInvites()
|
||||
private val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions()
|
||||
|
||||
private val getNotificationCountForSpacesUseCase = GetNotificationCountForSpacesUseCase(
|
||||
activeSessionHolder = fakeActiveSessionHolder.instance,
|
||||
autoAcceptInvites = fakeAutoAcceptInvites,
|
||||
)
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
fakeFlowLiveDataConversions.setup()
|
||||
mockkStatic("kotlinx.coroutines.flow.FlowKt")
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
unmockkAll()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given space filter and hide invites when execute then correct notification count is returned`() = runTest {
|
||||
// given
|
||||
val spaceFilter = SpaceFilter.NoFilter
|
||||
val pagedList = mockk<PagedList<RoomSummary>>()
|
||||
val pagedListFlow = fakeActiveSessionHolder.fakeSession
|
||||
.fakeRoomService
|
||||
.givenGetPagedRoomSummariesLiveReturns(pagedList)
|
||||
.givenAsFlow()
|
||||
every { pagedListFlow.sample(any<Long>()) } returns pagedListFlow
|
||||
val expectedNotificationCount = RoomAggregateNotificationCount(
|
||||
notificationCount = 1,
|
||||
highlightCount = 0,
|
||||
)
|
||||
fakeActiveSessionHolder.fakeSession
|
||||
.fakeRoomService
|
||||
.givenGetNotificationCountForRoomsReturns(expectedNotificationCount)
|
||||
fakeAutoAcceptInvites._hideInvites = true
|
||||
|
||||
// When
|
||||
val testObserver = getNotificationCountForSpacesUseCase.execute(spaceFilter).test(this)
|
||||
advanceUntilIdle()
|
||||
|
||||
// Then
|
||||
testObserver
|
||||
.assertValues(expectedNotificationCount)
|
||||
.finish()
|
||||
verify {
|
||||
fakeActiveSessionHolder.fakeSession.fakeRoomService.getNotificationCountForRooms(
|
||||
queryParams = match { it.memberships == listOf(Membership.JOIN) && it.spaceFilter == spaceFilter }
|
||||
)
|
||||
fakeActiveSessionHolder.fakeSession.fakeRoomService.getPagedRoomSummariesLive(
|
||||
queryParams = match { it.memberships == listOf(Membership.JOIN) && it.spaceFilter == spaceFilter },
|
||||
pagedListConfig = any(),
|
||||
sortOrder = RoomSortOrder.NONE,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given space filter and show invites when execute then correct notification count is returned`() = runTest {
|
||||
// given
|
||||
val spaceFilter = SpaceFilter.NoFilter
|
||||
val pagedList = mockk<PagedList<RoomSummary>>()
|
||||
val pagedListFlow = fakeActiveSessionHolder.fakeSession
|
||||
.fakeRoomService
|
||||
.givenGetPagedRoomSummariesLiveReturns(pagedList)
|
||||
.givenAsFlow()
|
||||
every { pagedListFlow.sample(any<Long>()) } returns pagedListFlow
|
||||
val notificationCount = RoomAggregateNotificationCount(
|
||||
notificationCount = 1,
|
||||
highlightCount = 0,
|
||||
)
|
||||
fakeActiveSessionHolder.fakeSession
|
||||
.fakeRoomService
|
||||
.givenGetNotificationCountForRoomsReturns(notificationCount)
|
||||
val invitedRooms = listOf<RoomSummary>(mockk())
|
||||
fakeActiveSessionHolder.fakeSession
|
||||
.fakeRoomService
|
||||
.givenGetRoomSummaries(invitedRooms)
|
||||
fakeAutoAcceptInvites._hideInvites = false
|
||||
val expectedNotificationCount = RoomAggregateNotificationCount(
|
||||
notificationCount = notificationCount.notificationCount + invitedRooms.size,
|
||||
highlightCount = notificationCount.highlightCount + invitedRooms.size,
|
||||
)
|
||||
|
||||
// When
|
||||
val testObserver = getNotificationCountForSpacesUseCase.execute(spaceFilter).test(this)
|
||||
advanceUntilIdle()
|
||||
|
||||
// Then
|
||||
testObserver
|
||||
.assertValues(expectedNotificationCount)
|
||||
.finish()
|
||||
verify {
|
||||
fakeActiveSessionHolder.fakeSession.fakeRoomService.getRoomSummaries(
|
||||
queryParams = match { it.memberships == listOf(Membership.INVITE) }
|
||||
)
|
||||
fakeActiveSessionHolder.fakeSession.fakeRoomService.getNotificationCountForRooms(
|
||||
queryParams = match { it.memberships == listOf(Membership.JOIN) && it.spaceFilter == spaceFilter }
|
||||
)
|
||||
fakeActiveSessionHolder.fakeSession.fakeRoomService.getPagedRoomSummariesLive(
|
||||
queryParams = match { it.memberships == listOf(Membership.JOIN) && it.spaceFilter == spaceFilter },
|
||||
pagedListConfig = any(),
|
||||
sortOrder = RoomSortOrder.NONE,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given no active session when execute then empty flow is returned`() = runTest {
|
||||
// given
|
||||
val spaceFilter = SpaceFilter.NoFilter
|
||||
fakeActiveSessionHolder.givenGetSafeActiveSessionReturns(null)
|
||||
|
||||
// When
|
||||
val testObserver = getNotificationCountForSpacesUseCase.execute(spaceFilter).test(this)
|
||||
|
||||
// Then
|
||||
testObserver
|
||||
.assertNoValues()
|
||||
.finish()
|
||||
verify(inverse = true) {
|
||||
fakeActiveSessionHolder.fakeSession.fakeRoomService.getPagedRoomSummariesLive(
|
||||
queryParams = any(),
|
||||
pagedListConfig = any(),
|
||||
sortOrder = any(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,7 +21,11 @@ import im.vector.app.features.invite.AutoAcceptInvites
|
|||
class FakeAutoAcceptInvites : AutoAcceptInvites {
|
||||
|
||||
var _isEnabled: Boolean = false
|
||||
var _hideInvites: Boolean = false
|
||||
|
||||
override val isEnabled: Boolean
|
||||
get() = _isEnabled
|
||||
|
||||
override val hideInvites: Boolean
|
||||
get() = _hideInvites
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ import androidx.lifecycle.LiveData
|
|||
import androidx.lifecycle.asFlow
|
||||
import io.mockk.every
|
||||
import io.mockk.mockkStatic
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
|
||||
class FakeFlowLiveDataConversions {
|
||||
|
@ -28,6 +29,8 @@ class FakeFlowLiveDataConversions {
|
|||
}
|
||||
}
|
||||
|
||||
fun <T> LiveData<T>.givenAsFlow() {
|
||||
every { asFlow() } returns flowOf(value!!)
|
||||
fun <T> LiveData<T>.givenAsFlow(): Flow<T> {
|
||||
return flowOf(value!!).also {
|
||||
every { asFlow() } returns it
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,10 +16,14 @@
|
|||
|
||||
package im.vector.app.test.fakes
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.paging.PagedList
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import org.matrix.android.sdk.api.session.room.RoomService
|
||||
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
||||
import org.matrix.android.sdk.api.session.room.summary.RoomAggregateNotificationCount
|
||||
|
||||
class FakeRoomService(
|
||||
private val fakeRoom: FakeRoom = FakeRoom()
|
||||
|
@ -34,4 +38,18 @@ class FakeRoomService(
|
|||
fun set(roomSummary: RoomSummary?) {
|
||||
every { getRoomSummary(any()) } returns roomSummary
|
||||
}
|
||||
|
||||
fun givenGetPagedRoomSummariesLiveReturns(pagedList: PagedList<RoomSummary>): LiveData<PagedList<RoomSummary>> {
|
||||
return MutableLiveData(pagedList).also {
|
||||
every { getPagedRoomSummariesLive(queryParams = any(), pagedListConfig = any(), sortOrder = any()) } returns it
|
||||
}
|
||||
}
|
||||
|
||||
fun givenGetNotificationCountForRoomsReturns(roomAggregateNotificationCount: RoomAggregateNotificationCount) {
|
||||
every { getNotificationCountForRooms(queryParams = any()) } returns roomAggregateNotificationCount
|
||||
}
|
||||
|
||||
fun givenGetRoomSummaries(roomSummaries: List<RoomSummary>) {
|
||||
every { getRoomSummaries(any()) } returns roomSummaries
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue