health: pin timezone and locale in HealthFormatTest (558 tests)
- Commit
1ca767e7279b79e4e7e8a0206717d9837b10ed34- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-15 19:17 UTC (20 days ago)
- Parents
971a4ae
'medical datetimes carry the time of day' expected the rendered time of 2026-03-04T09:30:00Z to contain '1:30', which is only true in America/Los_Angeles (the host zone). The formatters render in the device zone by design, so in UTC the string is '3/4/26 09:30' and in New York '3/4/26 04:30'; the test failed in both, i.e. on every host except the author's. Pin the default timezone to UTC and the locale to US in setup (and restore them afterwards), and assert the UTC wall time '09:30'. The date assertions are unchanged and the formatter keeps honouring the device zone in production. Reproduced under TZ=UTC and TZ=America/New_York before the fix; verified green 3x under both zones after it.
Diff
health/src/test/java/com/vayunmathur/health/ui/HealthFormatTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/ui/HealthFormatTest.kt b/health/src/test/java/com/vayunmathur/health/ui/HealthFormatTest.ktindex 9cf5087e97..c3fba2c732 100644--- a/health/src/test/java/com/vayunmathur/health/ui/HealthFormatTest.kt+++ b/health/src/test/java/com/vayunmathur/health/ui/HealthFormatTest.kt@@ -21,6 +21,28 @@ class HealthFormatTest { private val context = ApplicationProvider.getApplicationContext<android.content.Context>() + // The formatters render in the device zone/locale by design, so the+ // expected wall-clock strings below are only meaningful under a pinned+ // zone and locale: 2026-03-04T09:30:00Z is 09:30 in UTC, 04:30 in+ // New York and 01:30 in Los Angeles, and the suite must not depend on+ // the host it happens to run on.+ private lateinit var defaultTimeZone: java.util.TimeZone+ private lateinit var defaultLocale: java.util.Locale++ @kotlin.test.BeforeTest+ fun pinZoneAndLocale() {+ defaultTimeZone = java.util.TimeZone.getDefault()+ defaultLocale = java.util.Locale.getDefault()+ java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("UTC"))+ java.util.Locale.setDefault(java.util.Locale.US)+ }++ @kotlin.test.AfterTest+ fun restoreZoneAndLocale() {+ java.util.TimeZone.setDefault(defaultTimeZone)+ java.util.Locale.setDefault(defaultLocale)+ }+ @Test fun `durations render as hours and minutes`() { assertEquals("1h 30m", hoursMinutesString(context, 90))@@ -40,8 +62,10 @@ class HealthFormatTest { fun `medical datetimes carry the time of day`() { val text = medicalDateTimeString(java.time.Instant.parse("2026-03-04T09:30:00Z"), true) // Short date plus the time: this is the compact form for dose timestamps.+ // Zone and locale are pinned to UTC / US in setup, so the instant+ // renders as its UTC wall time. assertTrue("3/4/26" in text || "2026" in text, "date missing from $text")- assertTrue("1:30" in text || "01:30" in text, "time missing from $text")+ assertTrue("09:30" in text, "time missing from $text") } @Test