Fix test: also inject timezone to avoid relying on the system timezone.
This commit is contained in:
parent
34bcba1712
commit
190d20acbe
3 changed files with 14 additions and 8 deletions
|
|
@ -21,6 +21,7 @@ import dagger.Module
|
|||
import dagger.Provides
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.TimeZone
|
||||
import java.util.*
|
||||
|
||||
@Module
|
||||
|
|
@ -31,4 +32,7 @@ object DateFormatterModule {
|
|||
|
||||
@Provides
|
||||
fun providesLocale(): Locale = Locale.getDefault()
|
||||
|
||||
@Provides
|
||||
fun providesTimezone(): TimeZone = TimeZone.currentSystemDefault()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import kotlin.math.absoluteValue
|
|||
class DefaultLastMessageFormatter @Inject constructor(
|
||||
private val clock: Clock,
|
||||
private val locale: Locale,
|
||||
private val timezone: TimeZone,
|
||||
) : LastMessageFormatter {
|
||||
private val onlyTimeFormatter: DateTimeFormatter by lazy {
|
||||
val pattern = DateFormat.getBestDateTimePattern(locale, "HH:mm") ?: "HH:mm"
|
||||
|
|
@ -59,8 +60,8 @@ class DefaultLastMessageFormatter @Inject constructor(
|
|||
if (timestamp == null) return ""
|
||||
val now: Instant = clock.now()
|
||||
val tsInstant = Instant.fromEpochMilliseconds(timestamp)
|
||||
val nowDateTime = now.toLocalDateTime(TimeZone.currentSystemDefault())
|
||||
val tsDateTime = tsInstant.toLocalDateTime(TimeZone.currentSystemDefault())
|
||||
val nowDateTime = now.toLocalDateTime(timezone)
|
||||
val tsDateTime = tsInstant.toLocalDateTime(timezone)
|
||||
val isSameDay = nowDateTime.date == tsDateTime.date
|
||||
return when {
|
||||
isSameDay -> {
|
||||
|
|
@ -80,7 +81,7 @@ class DefaultLastMessageFormatter @Inject constructor(
|
|||
return if (period.years.absoluteValue >= 1) {
|
||||
formatDateWithYear(date)
|
||||
} else if (period.days.absoluteValue < 2 && period.months.absoluteValue < 1) {
|
||||
getRelativeDay(date.toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds())
|
||||
getRelativeDay(date.toInstant(timezone).toEpochMilliseconds())
|
||||
} else {
|
||||
formatDateWithMonth(date)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package io.element.android.libraries.dateformatter.impl
|
|||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.dateformatter.LastMessageFormatter
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.datetime.TimeZone
|
||||
import org.junit.Test
|
||||
import java.util.Locale
|
||||
|
||||
|
|
@ -43,7 +44,7 @@ class DefaultLastMessageFormatterTest {
|
|||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("20:35")
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -51,7 +52,7 @@ class DefaultLastMessageFormatterTest {
|
|||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:23.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("20:35")
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -59,7 +60,7 @@ class DefaultLastMessageFormatterTest {
|
|||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:34:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("20:34")
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("18:34")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -67,7 +68,7 @@ class DefaultLastMessageFormatterTest {
|
|||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T17:35:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("19:35")
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("17:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -100,6 +101,6 @@ class DefaultLastMessageFormatterTest {
|
|||
*/
|
||||
private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): LastMessageFormatter {
|
||||
val clock = FakeClock().also { it.givenInstant(Instant.parse(currentDate)) }
|
||||
return DefaultLastMessageFormatter(clock, Locale.US)
|
||||
return DefaultLastMessageFormatter(clock, Locale.US, TimeZone.UTC)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue