From dc489d8a927b1e9062b521de3ef1609852e0b565 Mon Sep 17 00:00:00 2001 From: ialokim Date: Tue, 29 Sep 2020 23:55:18 +0200 Subject: [PATCH] round delay/relativeTime to nearest minute instead of flooring, fix formatDelay tests --- .../de/grobox/transportr/utils/DateUtils.kt | 24 +++-- .../grobox/transportr/utils/DateUtilsTest.kt | 93 ++++++++++++++----- 2 files changed, 89 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/de/grobox/transportr/utils/DateUtils.kt b/app/src/main/java/de/grobox/transportr/utils/DateUtils.kt index 97f4f1059..1c531fb96 100644 --- a/app/src/main/java/de/grobox/transportr/utils/DateUtils.kt +++ b/app/src/main/java/de/grobox/transportr/utils/DateUtils.kt @@ -28,6 +28,15 @@ import java.util.* import kotlin.math.abs object DateUtils { + fun millisToMinutes(millis: Long): Long { + val seconds = millis / 1000 + return seconds / 60 + when { + seconds % 60 >= 30 -> 1 + seconds % 60 <= -30 -> -1 + else -> 0 + } + } + fun formatDate(context: Context, date: Date): String { val df = DateFormat.getDateFormat(context) return df.format(date) @@ -48,7 +57,7 @@ object DateUtils { fun formatDuration(duration: Long): String { // get duration in minutes - val durationMinutes = duration / 1000 / 60 + val durationMinutes = millisToMinutes(duration) val m = durationMinutes % 60 val h = durationMinutes / 60 return "$h:${m.toString().padStart(2, '0')}" @@ -58,10 +67,13 @@ object DateUtils { return formatDuration(end.time - start.time) } - fun formatDelay(context: Context, delay: Long) = Delay( - delay = "${if (delay >= 0) '+' else ""}${delay / 1000 / 60}", - color = ContextCompat.getColor(context, if (delay <= 0) R.color.md_green_500 else R.color.md_red_500) - ) + fun formatDelay(context: Context, delay: Long): Delay { + val delayMinutes = millisToMinutes(delay) + return Delay( + delay = "${if (delayMinutes >= 0) '+' else ""}$delayMinutes", + color = ContextCompat.getColor(context, if (delayMinutes > 0) R.color.md_red_500 else R.color.md_green_500) + ) + } data class Delay( val delay: String, @@ -101,7 +113,7 @@ object DateUtils { * Returns difference in minutes */ private fun getDifferenceInMinutes(d1: Date, d2: Date): Long { - return (d2.time - d1.time) / 1000 / 60 + return millisToMinutes(d2.time - d1.time) } private fun getDifferenceInMinutes(date: Date): Long { diff --git a/app/src/test/java/de/grobox/transportr/utils/DateUtilsTest.kt b/app/src/test/java/de/grobox/transportr/utils/DateUtilsTest.kt index aedd04843..c109f102d 100644 --- a/app/src/test/java/de/grobox/transportr/utils/DateUtilsTest.kt +++ b/app/src/test/java/de/grobox/transportr/utils/DateUtilsTest.kt @@ -20,6 +20,7 @@ package de.grobox.transportr.utils import android.content.Context +import android.content.res.Resources import android.view.View import de.grobox.transportr.R import org.junit.Assert @@ -35,16 +36,27 @@ class DateUtilsTest { @Mock lateinit var context: Context + @Mock + lateinit var resources: Resources private fun minuteToMillis(minutes: Float): Long { return (minutes * android.text.format.DateUtils.MINUTE_IN_MILLIS).toLong() } + @Test + fun millisToMinutes() { + Assert.assertEquals(0, DateUtils.millisToMinutes(minuteToMillis(0f))) + Assert.assertEquals(0, DateUtils.millisToMinutes(minuteToMillis(0.3f))) + Assert.assertEquals(0, DateUtils.millisToMinutes(minuteToMillis(-0.3f))) + Assert.assertEquals(1, DateUtils.millisToMinutes(minuteToMillis(0.8f))) + Assert.assertEquals(-1, DateUtils.millisToMinutes(minuteToMillis(-0.8f))) + } + @Test fun formatDuration() { Assert.assertEquals("0:05", DateUtils.formatDuration(minuteToMillis(5f))) Assert.assertEquals("0:05", DateUtils.formatDuration(minuteToMillis(5.3f))) - //Assert.assertEquals("0:06", DateUtils.formatDuration(minuteToMillis(5.8f))) //todo: round to nearest minute + Assert.assertEquals("0:06", DateUtils.formatDuration(minuteToMillis(5.8f))) Assert.assertEquals("0:15", DateUtils.formatDuration(minuteToMillis(15f))) Assert.assertEquals("1:05", DateUtils.formatDuration(minuteToMillis(65f))) Assert.assertEquals("1:05", DateUtils.formatDuration(minuteToMillis(65.3f))) @@ -53,21 +65,8 @@ class DateUtilsTest { Assert.assertEquals("1:05", DateUtils.formatDuration(Date(), Date().apply { time += minuteToMillis(65f) })) } - @Test - fun formatDelay() { - Assert.assertEquals("+0", DateUtils.formatDelay(minuteToMillis(0f))) - Assert.assertEquals("+0", DateUtils.formatDelay(minuteToMillis(0.3f))) - //Assert.assertEquals("+0", DateUtils.formatDelay(minuteToMillis(-0.3f))) //todo: round to nearest minute - //Assert.assertEquals("+1", DateUtils.formatDelay(minuteToMillis(0.8f))) //todo: round to nearest minute - //Assert.assertEquals("-1", DateUtils.formatDelay(minuteToMillis(-0.8f))) //todo: round to nearest minute - Assert.assertEquals("+1", DateUtils.formatDelay(minuteToMillis(1f))) - Assert.assertEquals("-1", DateUtils.formatDelay(minuteToMillis(-1f))) - //Assert.assertEquals("+10", DateUtils.formatDelay(minuteToMillis(9.8f))) //todo: round to nearest minute - //Assert.assertEquals("-10", DateUtils.formatDelay(minuteToMillis(-9.8f))) //todo: round to nearest minute - Assert.assertEquals("+100", DateUtils.formatDelay(minuteToMillis(100f))) - Assert.assertEquals("-100", DateUtils.formatDelay(minuteToMillis(-100f))) - } - + private val GREEN = 0 + private val RED = 1 private fun getNow() = "now" private fun getIn(difference: Any) = "in $difference" private fun getAgo(difference: Any) = "$difference ago" @@ -75,11 +74,63 @@ class DateUtilsTest { @Before fun initMocks() { MockitoAnnotations.initMocks(this) + `when`(context.resources).thenReturn(resources) + `when`(resources.getColor(R.color.md_green_500)).thenReturn(GREEN) + `when`(resources.getColor(R.color.md_red_500)).thenReturn(RED) + `when`(context.getString(R.string.now_small)).thenReturn(getNow()) `when`(context.getString(eq(R.string.in_x_minutes), anyLong())).thenAnswer { i -> getIn(i.arguments[1]) } `when`(context.getString(eq(R.string.x_minutes_ago), anyLong())).thenAnswer { i -> getAgo(i.arguments[1]) } } + @Test + fun formatDelay() { + Assert.assertEquals( + DateUtils.Delay("+0", GREEN), + DateUtils.formatDelay(context, minuteToMillis(0f)) + ) + Assert.assertEquals( + DateUtils.Delay("+0", GREEN), + DateUtils.formatDelay(context, minuteToMillis(0.3f)) + ) + Assert.assertEquals( + DateUtils.Delay("+0", GREEN), + DateUtils.formatDelay(context, minuteToMillis(-0.3f)) + ) + Assert.assertEquals( + DateUtils.Delay("+1", RED), + DateUtils.formatDelay(context, minuteToMillis(0.8f)) + ) + Assert.assertEquals( + DateUtils.Delay("-1", GREEN), + DateUtils.formatDelay(context, minuteToMillis(-0.8f)) + ) + Assert.assertEquals( + DateUtils.Delay("+1", RED), + DateUtils.formatDelay(context, minuteToMillis(1f)) + ) + Assert.assertEquals( + DateUtils.Delay("-1", GREEN), + DateUtils.formatDelay(context, minuteToMillis(-1f)) + ) + Assert.assertEquals( + DateUtils.Delay("+10", RED), + DateUtils.formatDelay(context, minuteToMillis(9.8f)) + ) + Assert.assertEquals( + DateUtils.Delay("-10", GREEN), + DateUtils.formatDelay(context, minuteToMillis(-9.8f)) + ) + Assert.assertEquals( + DateUtils.Delay("+100", RED), + DateUtils.formatDelay(context, minuteToMillis(100f)) + ) + Assert.assertEquals( + DateUtils.Delay("-100", GREEN), + DateUtils.formatDelay(context, minuteToMillis(-100f)) + ) + } + @Test fun formatRelativeTime() { Assert.assertEquals( @@ -94,16 +145,14 @@ class DateUtilsTest { DateUtils.RelativeTime(getNow(), View.VISIBLE), DateUtils.formatRelativeTime(context, Date().apply { time -= minuteToMillis(0.4f) }) ) - //todo: round to nearest minute? - /*Assert.assertEquals( + Assert.assertEquals( DateUtils.RelativeTime(getIn(1), View.VISIBLE), DateUtils.formatRelativeTime(context, Date().apply { time += minuteToMillis(0.8f) }) - )*/ - //todo: round to nearest minute? - /*Assert.assertEquals( + ) + Assert.assertEquals( DateUtils.RelativeTime(getAgo(1), View.VISIBLE), DateUtils.formatRelativeTime(context, Date().apply { time -= minuteToMillis(0.8f) }) - )*/ + ) Assert.assertEquals( DateUtils.RelativeTime(getIn(5), View.VISIBLE), DateUtils.formatRelativeTime(context, Date().apply { time += minuteToMillis(5f) })