Browse Source

Fix test: also inject timezone to avoid relying on the system timezone.

kittykat-patch-1
Benoit Marty 2 years ago committed by Benoit Marty
parent
commit
190d20acbe
  1. 4
      libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/di/DateFormatterModule.kt
  2. 7
      libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt
  3. 11
      libraries/dateformatter/src/test/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatterTest.kt

4
libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/di/DateFormatterModule.kt

@ -21,6 +21,7 @@ import dagger.Module
import dagger.Provides import dagger.Provides
import io.element.android.libraries.di.AppScope import io.element.android.libraries.di.AppScope
import kotlinx.datetime.Clock import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import java.util.* import java.util.*
@Module @Module
@ -31,4 +32,7 @@ object DateFormatterModule {
@Provides @Provides
fun providesLocale(): Locale = Locale.getDefault() fun providesLocale(): Locale = Locale.getDefault()
@Provides
fun providesTimezone(): TimeZone = TimeZone.currentSystemDefault()
} }

7
libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt

@ -39,6 +39,7 @@ import kotlin.math.absoluteValue
class DefaultLastMessageFormatter @Inject constructor( class DefaultLastMessageFormatter @Inject constructor(
private val clock: Clock, private val clock: Clock,
private val locale: Locale, private val locale: Locale,
private val timezone: TimeZone,
) : LastMessageFormatter { ) : LastMessageFormatter {
private val onlyTimeFormatter: DateTimeFormatter by lazy { private val onlyTimeFormatter: DateTimeFormatter by lazy {
val pattern = DateFormat.getBestDateTimePattern(locale, "HH:mm") ?: "HH:mm" val pattern = DateFormat.getBestDateTimePattern(locale, "HH:mm") ?: "HH:mm"
@ -59,8 +60,8 @@ class DefaultLastMessageFormatter @Inject constructor(
if (timestamp == null) return "" if (timestamp == null) return ""
val now: Instant = clock.now() val now: Instant = clock.now()
val tsInstant = Instant.fromEpochMilliseconds(timestamp) val tsInstant = Instant.fromEpochMilliseconds(timestamp)
val nowDateTime = now.toLocalDateTime(TimeZone.currentSystemDefault()) val nowDateTime = now.toLocalDateTime(timezone)
val tsDateTime = tsInstant.toLocalDateTime(TimeZone.currentSystemDefault()) val tsDateTime = tsInstant.toLocalDateTime(timezone)
val isSameDay = nowDateTime.date == tsDateTime.date val isSameDay = nowDateTime.date == tsDateTime.date
return when { return when {
isSameDay -> { isSameDay -> {
@ -80,7 +81,7 @@ class DefaultLastMessageFormatter @Inject constructor(
return if (period.years.absoluteValue >= 1) { return if (period.years.absoluteValue >= 1) {
formatDateWithYear(date) formatDateWithYear(date)
} else if (period.days.absoluteValue < 2 && period.months.absoluteValue < 1) { } else if (period.days.absoluteValue < 2 && period.months.absoluteValue < 1) {
getRelativeDay(date.toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()) getRelativeDay(date.toInstant(timezone).toEpochMilliseconds())
} else { } else {
formatDateWithMonth(date) formatDateWithMonth(date)
} }

11
libraries/dateformatter/src/test/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatterTest.kt

@ -19,6 +19,7 @@ package io.element.android.libraries.dateformatter.impl
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.dateformatter.LastMessageFormatter import io.element.android.libraries.dateformatter.LastMessageFormatter
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import org.junit.Test import org.junit.Test
import java.util.Locale import java.util.Locale
@ -43,7 +44,7 @@ class DefaultLastMessageFormatterTest {
val now = "1980-04-06T18:35:24.00Z" val now = "1980-04-06T18:35:24.00Z"
val dat = "1980-04-06T18:35:24.00Z" val dat = "1980-04-06T18:35:24.00Z"
val formatter = createFormatter(now) 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 @Test
@ -51,7 +52,7 @@ class DefaultLastMessageFormatterTest {
val now = "1980-04-06T18:35:24.00Z" val now = "1980-04-06T18:35:24.00Z"
val dat = "1980-04-06T18:35:23.00Z" val dat = "1980-04-06T18:35:23.00Z"
val formatter = createFormatter(now) 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 @Test
@ -59,7 +60,7 @@ class DefaultLastMessageFormatterTest {
val now = "1980-04-06T18:35:24.00Z" val now = "1980-04-06T18:35:24.00Z"
val dat = "1980-04-06T18:34:24.00Z" val dat = "1980-04-06T18:34:24.00Z"
val formatter = createFormatter(now) 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 @Test
@ -67,7 +68,7 @@ class DefaultLastMessageFormatterTest {
val now = "1980-04-06T18:35:24.00Z" val now = "1980-04-06T18:35:24.00Z"
val dat = "1980-04-06T17:35:24.00Z" val dat = "1980-04-06T17:35:24.00Z"
val formatter = createFormatter(now) 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 @Test
@ -100,6 +101,6 @@ class DefaultLastMessageFormatterTest {
*/ */
private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): LastMessageFormatter { private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): LastMessageFormatter {
val clock = FakeClock().also { it.givenInstant(Instant.parse(currentDate)) } val clock = FakeClock().also { it.givenInstant(Instant.parse(currentDate)) }
return DefaultLastMessageFormatter(clock, Locale.US) return DefaultLastMessageFormatter(clock, Locale.US, TimeZone.UTC)
} }
} }

Loading…
Cancel
Save