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 @@ -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 { @@ -31,4 +32,7 @@ object DateFormatterModule {
@Provides
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 @@ -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( @@ -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( @@ -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)
}

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 @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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…
Cancel
Save