HomeFormatTest.kt
| 1 | package net.lexcom.opentracker.ui |
| 2 | |
| 3 | import kotlin.test.Test |
| 4 | import kotlin.test.assertEquals |
| 5 | |
| 6 | /** |
| 7 | * Only the age formatter. It is the one part of the home screen with a branch |
| 8 | * that can be wrong, and the only one that can be wrong *quietly*: a bad unit |
| 9 | * still renders a plausible looking string. |
| 10 | */ |
| 11 | class HomeFormatTest { |
| 12 | |
| 13 | /** Fixed so the test never depends on the machine's clock. */ |
| 14 | private val nowMs = 1_785_000_000_000L |
| 15 | private val nowS = nowMs / 1000 |
| 16 | |
| 17 | private fun ageAt(secondsAgo: Long) = formatAge(nowS - secondsAgo, nowMs) |
| 18 | |
| 19 | @Test |
| 20 | fun `an age below a minute is shown in seconds`() { |
| 21 | assertEquals("0s ago", ageAt(0)) |
| 22 | assertEquals("59s ago", ageAt(59)) |
| 23 | } |
| 24 | |
| 25 | @Test |
| 26 | fun `an age of a minute or more is shown in minutes`() { |
| 27 | assertEquals("1m ago", ageAt(60)) |
| 28 | assertEquals("59m ago", ageAt(3599)) |
| 29 | } |
| 30 | |
| 31 | @Test |
| 32 | fun `an age of an hour or more is shown in hours`() { |
| 33 | assertEquals("1h ago", ageAt(3600)) |
| 34 | assertEquals("25h ago", ageAt(25 * 3600)) |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | fun `a timestamp from the future reads as zero, never as a negative age`() { |
| 39 | assertEquals("0s ago", ageAt(-90)) |
| 40 | } |
| 41 | |
| 42 | @Test |
| 43 | fun `accuracy is rounded from decimetres to metres`() { |
| 44 | assertEquals("±1 m", formatAccuracy(12)) |
| 45 | assertEquals("±2 m", formatAccuracy(15)) |
| 46 | assertEquals(null, formatAccuracy(null)) |
| 47 | } |
| 48 | } |
| 49 |