-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create test for
DateTimeObserver
usage in RoomListDataSource
- Loading branch information
1 parent
beb074a
commit b3be170
Showing
4 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...est/kotlin/io/element/android/features/roomlist/impl/datasource/RoomListDataSourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2024 New Vector Ltd. | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
package io.element.android.features.roomlist.impl.datasource | ||
|
||
import app.cash.turbine.test | ||
import com.google.common.truth.Truth.assertThat | ||
import io.element.android.features.roomlist.impl.FakeDateTimeObserver | ||
import io.element.android.libraries.androidutils.system.DateTimeObserver | ||
import io.element.android.libraries.core.coroutine.CoroutineDispatchers | ||
import io.element.android.libraries.dateformatter.api.LastMessageTimestampFormatter | ||
import io.element.android.libraries.eventformatter.api.RoomLastMessageFormatter | ||
import io.element.android.libraries.matrix.api.roomlist.RoomListService | ||
import io.element.android.libraries.matrix.test.notificationsettings.FakeNotificationSettingsService | ||
import io.element.android.libraries.matrix.test.room.aRoomSummary | ||
import io.element.android.libraries.matrix.test.roomlist.FakeRoomListService | ||
import io.element.android.tests.testutils.testCoroutineDispatchers | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
import java.time.Instant | ||
|
||
class RoomListDataSourceTest { | ||
@Test | ||
fun `when DateTimeObserver gets a date change, the room summaries are refreshed`() = runTest { | ||
val roomListService = FakeRoomListService().apply { | ||
postState(RoomListService.State.Running) | ||
postAllRooms(listOf(aRoomSummary())) | ||
} | ||
val dateTimeObserver = FakeDateTimeObserver() | ||
val roomListDataSource = createRoomListDataSource(roomListService = roomListService, dateTimeObserver = dateTimeObserver) | ||
|
||
roomListDataSource.allRooms.test { | ||
// Observe room list items changes | ||
roomListDataSource.launchIn(backgroundScope) | ||
|
||
// Get the initial room list | ||
val initialRoomList = awaitItem() | ||
assertThat(initialRoomList).isNotEmpty() | ||
|
||
// Trigger a date change | ||
dateTimeObserver.given(DateTimeObserver.Event.DateChanged(Instant.MIN, Instant.now())) | ||
|
||
// Check there is a new list and it's not the same as the previous one (although it has the same content) | ||
val newRoomList = awaitItem() | ||
assertThat(newRoomList).isNotSameInstanceAs(initialRoomList) | ||
|
||
cancelAndIgnoreRemainingEvents() | ||
} | ||
} | ||
|
||
@Test | ||
fun `when DateTimeObserver gets a time zone change, the room summaries are refreshed`() = runTest { | ||
val roomListService = FakeRoomListService().apply { | ||
postState(RoomListService.State.Running) | ||
postAllRooms(listOf(aRoomSummary())) | ||
} | ||
val dateTimeObserver = FakeDateTimeObserver() | ||
val roomListDataSource = createRoomListDataSource(roomListService = roomListService, dateTimeObserver = dateTimeObserver) | ||
|
||
roomListDataSource.allRooms.test { | ||
// Observe room list items changes | ||
roomListDataSource.launchIn(backgroundScope) | ||
|
||
// Get the initial room list | ||
val initialRoomList = awaitItem() | ||
assertThat(initialRoomList).isNotEmpty() | ||
|
||
// Trigger a timezone change | ||
dateTimeObserver.given(DateTimeObserver.Event.TimeZoneChanged) | ||
|
||
// Check there is a new list and it's not the same as the previous one (although it has the same content) | ||
val newRoomList = awaitItem() | ||
assertThat(newRoomList).isNotSameInstanceAs(initialRoomList) | ||
|
||
cancelAndIgnoreRemainingEvents() | ||
} | ||
} | ||
|
||
private fun TestScope.createRoomListDataSource( | ||
roomListService: FakeRoomListService = FakeRoomListService(), | ||
roomListRoomSummaryFactory: RoomListRoomSummaryFactory = | ||
RoomListRoomSummaryFactory( | ||
lastMessageTimestampFormatter = LastMessageTimestampFormatter { _ -> "Yesterday" }, | ||
roomLastMessageFormatter = RoomLastMessageFormatter { _, _ -> "Hey" } | ||
), | ||
dispatchers: CoroutineDispatchers = testCoroutineDispatchers(), | ||
notificationSettingsService: FakeNotificationSettingsService = FakeNotificationSettingsService(), | ||
appScope: CoroutineScope = backgroundScope, | ||
dateTimeObserver: FakeDateTimeObserver = FakeDateTimeObserver(), | ||
) = RoomListDataSource( | ||
roomListService = roomListService, | ||
roomListRoomSummaryFactory = roomListRoomSummaryFactory, | ||
coroutineDispatchers = dispatchers, | ||
notificationSettingsService = notificationSettingsService, | ||
appScope = appScope, | ||
dateTimeObserver = dateTimeObserver, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters