Skip to content

Commit 1999261

Browse files
authored
Delete heavyweight internal testing classes (#303)
Use private fields instead, which are set in tests using reflection.
1 parent d8fc629 commit 1999261

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

datetimeformatters/src/androidTest/java/dev/drewhamilton/androidtime/format/test/TimeSettingTest.kt

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import android.provider.Settings
88
import androidx.core.os.ConfigurationCompat
99
import androidx.core.os.LocaleListCompat
1010
import androidx.test.platform.app.InstrumentationRegistry
11-
import dev.drewhamilton.androidtime.format.TestSystemTimeSetting
12-
import dev.drewhamilton.androidtime.format.testSystemTimeSetting
11+
import dev.drewhamilton.androidtime.format.AndroidDateTimeFormatter
1312
import java.util.Locale
14-
import org.junit.After
13+
import org.junit.Before
1514

1615
/**
1716
* A base test class that facilitates using and changing [testSystemTimeSetting], which mimics the
@@ -31,12 +30,9 @@ abstract class TimeSettingTest {
3130
}
3231

3332
protected var systemTimeSetting: String?
34-
get() = when (val testSystemTimeSetting = testSystemTimeSetting) {
35-
is TestSystemTimeSetting.Set -> testSystemTimeSetting.value
36-
is TestSystemTimeSetting.Unset -> null
37-
}
33+
get() = AndroidDateTimeFormatter.testSystemTimeSetting
3834
set(value) {
39-
testSystemTimeSetting = TestSystemTimeSetting.Set(value)
35+
AndroidDateTimeFormatter.testSystemTimeSetting = value
4036
}
4137

4238
private fun Context.copyWithLocale(locale: Locale): Context {
@@ -55,10 +51,34 @@ abstract class TimeSettingTest {
5551
locale = locales[0]
5652
}
5753

58-
@After fun restoreTimeSetting() {
59-
testSystemTimeSetting = TestSystemTimeSetting.Unset
54+
@Before fun enableTestSystemTimeSetting() {
55+
AndroidDateTimeFormatter.useTestSystemTimeSetting = true
6056
}
6157

58+
private var AndroidDateTimeFormatter.useTestSystemTimeSetting: Boolean
59+
get() = javaClass.declaredFields
60+
.single { it.name == "useTestSystemTimeSetting" }
61+
.apply { isAccessible = true }
62+
.get(this) as Boolean
63+
set(value) {
64+
javaClass.declaredFields
65+
.single { it.name == "useTestSystemTimeSetting" }
66+
.apply { isAccessible = true }
67+
.set(this, value)
68+
}
69+
70+
private var AndroidDateTimeFormatter.testSystemTimeSetting: String?
71+
get() = javaClass.declaredFields
72+
.single { it.name == "testSystemTimeSetting" }
73+
.apply { isAccessible = true }
74+
.get(this) as String?
75+
set(value) {
76+
javaClass.declaredFields
77+
.single { it.name == "testSystemTimeSetting" }
78+
.apply { isAccessible = true }
79+
.set(this, value)
80+
}
81+
6282
protected companion object {
6383
const val TIME_SETTING_12 = "12"
6484
const val TIME_SETTING_24 = "24"

datetimeformatters/src/main/java/dev/drewhamilton/androidtime/format/AndroidDateTimeFormatter.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,11 @@ object AndroidDateTimeFormatter {
349349
}
350350

351351
@JvmStatic private fun Context.timeSetting(): String? {
352-
// TODO: Support testing without a static field?
353-
return when (val testSystemTimeSetting = testSystemTimeSetting) {
352+
return if (useTestSystemTimeSetting) {
353+
testSystemTimeSetting
354+
} else {
354355
// Prod code path:
355-
is TestSystemTimeSetting.Unset -> {
356-
Settings.System.getString(contentResolver, Settings.System.TIME_12_24)
357-
}
358-
359-
// Test code path:
360-
is TestSystemTimeSetting.Set -> {
361-
testSystemTimeSetting.value
362-
}
356+
Settings.System.getString(contentResolver, Settings.System.TIME_12_24)
363357
}
364358
}
365359

@@ -498,4 +492,9 @@ object AndroidDateTimeFormatter {
498492
}
499493
return locale!!
500494
}
495+
496+
//region Test
497+
private var useTestSystemTimeSetting: Boolean = false
498+
private var testSystemTimeSetting: String? = null
499+
//endregion
501500
}

datetimeformatters/src/main/java/dev/drewhamilton/androidtime/format/TestSystemTimeSetting.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)