Uses Fake instead of incomplete Mock for WearUtil

Unblocks #2732 and #2733
This commit is contained in:
Ryan Haining 2023-09-10 13:36:30 -07:00
parent 307b114947
commit 9e24cc1607
5 changed files with 98 additions and 132 deletions

View file

@ -2,10 +2,11 @@ package info.nightscout.androidaps
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import info.nightscout.androidaps.interaction.utils.Constants
import info.nightscout.androidaps.interaction.utils.Persistence import info.nightscout.androidaps.interaction.utils.Persistence
import info.nightscout.androidaps.interaction.utils.WearUtil import info.nightscout.androidaps.interaction.utils.WearUtil
import info.nightscout.androidaps.testing.mockers.WearUtilMocker
import info.nightscout.androidaps.testing.mocks.SharedPreferencesMock import info.nightscout.androidaps.testing.mocks.SharedPreferencesMock
import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import info.nightscout.sharedtests.TestBase import info.nightscout.sharedtests.TestBase
@ -13,24 +14,36 @@ import org.junit.jupiter.api.BeforeEach
import org.mockito.ArgumentMatchers import org.mockito.ArgumentMatchers
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.Mockito.`when`
class FakeWearUtil(context: Context, aapsLogger: AAPSLogger) : WearUtil(context, aapsLogger) {
private var clockMsDiff = 0L
override fun timestamp(): Long = REF_NOW + clockMsDiff
fun progressClock(byMilliseconds: Long) {
clockMsDiff += byMilliseconds
}
companion object {
const val REF_NOW = 1572610530000L
}
}
open class WearTestBase : TestBase() { open class WearTestBase : TestBase() {
@Mock lateinit var context: Context @Mock lateinit var context: Context
@Mock lateinit var sp: SP @Mock lateinit var sp: SP
@Mock lateinit var dateUtil: DateUtil @Mock lateinit var dateUtil: DateUtil
@Mock lateinit var wearUtil: WearUtil lateinit var fakeWearUtil: FakeWearUtil
//val wearUtil: WearUtil = Mockito.mock(WearUtil::class.java)
lateinit var wearUtilMocker: WearUtilMocker
lateinit var persistence: Persistence lateinit var persistence: Persistence
private val mockedSharedPrefs: HashMap<String, SharedPreferences> = HashMap() private val mockedSharedPrefs: HashMap<String, SharedPreferences> = HashMap()
@BeforeEach @BeforeEach
fun setup() { fun setup() {
wearUtilMocker = WearUtilMocker(wearUtil) fakeWearUtil = FakeWearUtil(context, aapsLogger)
Mockito.doAnswer { invocation -> Mockito.doAnswer { invocation ->
val key = invocation.getArgument<String>(0) val key = invocation.getArgument<String>(0)
if (mockedSharedPrefs.containsKey(key)) { if (mockedSharedPrefs.containsKey(key)) {
@ -42,12 +55,11 @@ open class WearTestBase : TestBase() {
} }
}.`when`(context).getSharedPreferences(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt()) }.`when`(context).getSharedPreferences(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt())
wearUtilMocker.prepareMockNoReal()
`when`(wearUtil.aapsLogger).thenReturn(aapsLogger)
`when`(wearUtil.context).thenReturn(context)
val rateLimits: MutableMap<String, Long> = HashMap()
`when`(wearUtil.rateLimits).thenReturn(rateLimits)
persistence = Mockito.spy(Persistence(aapsLogger, dateUtil, sp)) persistence = Mockito.spy(Persistence(aapsLogger, dateUtil, sp))
}
companion object {
fun backInTime(d: Int, h: Int, m: Int, s: Int): Long =
FakeWearUtil.REF_NOW - (Constants.DAY_IN_MS * d + Constants.HOUR_IN_MS * h + Constants.MINUTE_IN_MS * m + Constants.SECOND_IN_MS * s)
} }
} }

View file

@ -20,10 +20,9 @@ class DisplayFormatTest : WearTestBase() {
@BeforeEach @BeforeEach
fun mock() { fun mock() {
rawDataMocker = RawDataMocker(wearUtil) rawDataMocker = RawDataMocker()
wearUtilMocker.prepareMock()
displayFormat = DisplayFormat() displayFormat = DisplayFormat()
displayFormat.wearUtil = wearUtil displayFormat.wearUtil = fakeWearUtil
displayFormat.sp = sp displayFormat.sp = sp
displayFormat.context = context displayFormat.context = context
Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(true) Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(true)
@ -33,58 +32,58 @@ class DisplayFormatTest : WearTestBase() {
} }
@Test fun shortTimeSinceTest() { @Test fun shortTimeSinceTest() {
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 0, 0)), "0'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 0, 0)), "0'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 0, 5)), "0'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 0, 5)), "0'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 0, 55)), "0'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 0, 55)), "0'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 1, 0)), "1'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 1, 0)), "1'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 1, 59)), "1'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 1, 59)), "1'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 2, 0)), "2'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 2, 0)), "2'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 3, 0)), "3'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 3, 0)), "3'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 4, 0)), "4'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 4, 0)), "4'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 10, 0)), "10'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 10, 0)), "10'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 30, 0)), "30'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 30, 0)), "30'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 59, 0)), "59'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 59, 0)), "59'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 0, 59, 59)), "59'") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 0, 59, 59)), "59'")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 1, 0, 0)), "1h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 1, 0, 0)), "1h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 1, 30, 0)), "1h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 1, 30, 0)), "1h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 1, 59, 59)), "1h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 1, 59, 59)), "1h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 2, 0, 0)), "2h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 2, 0, 0)), "2h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 3, 0, 0)), "3h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 3, 0, 0)), "3h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 4, 0, 0)), "4h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 4, 0, 0)), "4h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 5, 0, 0)), "5h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 5, 0, 0)), "5h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 12, 0, 0)), "12h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 12, 0, 0)), "12h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 18, 0, 0)), "18h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 18, 0, 0)), "18h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(0, 23, 59, 59)), "23h") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(0, 23, 59, 59)), "23h")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(1, 0, 0, 0)), "1d") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(1, 0, 0, 0)), "1d")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(1, 12, 0, 0)), "1d") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(1, 12, 0, 0)), "1d")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(1, 23, 59, 59)), "1d") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(1, 23, 59, 59)), "1d")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(2, 0, 0, 0)), "2d") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(2, 0, 0, 0)), "2d")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(3, 0, 0, 0)), "3d") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(3, 0, 0, 0)), "3d")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(4, 0, 0, 0)), "4d") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(4, 0, 0, 0)), "4d")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(5, 0, 0, 0)), "5d") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(5, 0, 0, 0)), "5d")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(6, 0, 0, 0)), "6d") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(6, 0, 0, 0)), "6d")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(6, 23, 59, 59)), "6d") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(6, 23, 59, 59)), "6d")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(7, 0, 0, 0)), "1w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(7, 0, 0, 0)), "1w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(8, 0, 0, 0)), "1w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(8, 0, 0, 0)), "1w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(9, 0, 0, 0)), "1w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(9, 0, 0, 0)), "1w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(13, 23, 59, 59)), "1w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(13, 23, 59, 59)), "1w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(14, 0, 0, 0)), "2w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(14, 0, 0, 0)), "2w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(21, 0, 0, 0)), "3w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(21, 0, 0, 0)), "3w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(28, 0, 0, 0)), "4w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(28, 0, 0, 0)), "4w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(31, 0, 0, 0)), "4w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(31, 0, 0, 0)), "4w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(32, 0, 0, 0)), "4w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(32, 0, 0, 0)), "4w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(35, 0, 0, 0)), "5w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(35, 0, 0, 0)), "5w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(100, 0, 0, 0)), "14w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(100, 0, 0, 0)), "14w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(200, 0, 0, 0)), "28w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(200, 0, 0, 0)), "28w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(365, 0, 0, 0)), "52w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(365, 0, 0, 0)), "52w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(366, 0, 0, 0)), "52w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(366, 0, 0, 0)), "52w")
Assertions.assertEquals(displayFormat.shortTimeSince(wearUtilMocker.backInTime(367, 0, 0, 0)), "52w") Assertions.assertEquals(displayFormat.shortTimeSince(backInTime(367, 0, 0, 0)), "52w")
} }
@Test fun shortTrendTest() { @Test fun shortTrendTest() {
val raw = RawDisplayData() val raw = RawDisplayData()
Assertions.assertEquals(displayFormat.shortTrend(raw), "-- Δ--") Assertions.assertEquals(displayFormat.shortTrend(raw), "-- Δ--")
raw.singleBg.timeStamp = wearUtilMocker.backInTime(0, 0, 2, 0) raw.singleBg.timeStamp = backInTime(0, 0, 2, 0)
Assertions.assertEquals(displayFormat.shortTrend(raw), "2' Δ--") Assertions.assertEquals(displayFormat.shortTrend(raw), "2' Δ--")
Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(true) Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(true)

View file

@ -1,8 +1,8 @@
package info.nightscout.androidaps.interaction.utils package info.nightscout.androidaps.interaction.utils
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import info.nightscout.androidaps.FakeWearUtil
import info.nightscout.androidaps.WearTestBase import info.nightscout.androidaps.WearTestBase
import info.nightscout.androidaps.testing.mockers.WearUtilMocker
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
/** /**
@ -14,13 +14,13 @@ class WearUtilTest : WearTestBase() {
@Test fun timestampAndTimeDiffsTest() { @Test fun timestampAndTimeDiffsTest() {
// smoke for mocks - since we freeze "now" to get stable tests // smoke for mocks - since we freeze "now" to get stable tests
assertThat(wearUtil.timestamp()).isEqualTo(WearUtilMocker.REF_NOW) assertThat(fakeWearUtil.timestamp()).isEqualTo(FakeWearUtil.REF_NOW)
assertThat(wearUtil.msTill(WearUtilMocker.REF_NOW)).isEqualTo(0L) assertThat(fakeWearUtil.msTill(FakeWearUtil.REF_NOW)).isEqualTo(0L)
assertThat(wearUtil.msTill(WearUtilMocker.REF_NOW + 3456L)).isEqualTo(3456L) assertThat(fakeWearUtil.msTill(FakeWearUtil.REF_NOW + 3456L)).isEqualTo(3456L)
assertThat(wearUtil.msTill(WearUtilMocker.REF_NOW - 6294L)).isEqualTo(-6294L) assertThat(fakeWearUtil.msTill(FakeWearUtil.REF_NOW - 6294L)).isEqualTo(-6294L)
assertThat(wearUtil.msTill(WearUtilMocker.REF_NOW)).isEqualTo(0L) assertThat(fakeWearUtil.msTill(FakeWearUtil.REF_NOW)).isEqualTo(0L)
assertThat(wearUtil.msSince(WearUtilMocker.REF_NOW + 3456L)).isEqualTo(-3456L) assertThat(fakeWearUtil.msSince(FakeWearUtil.REF_NOW + 3456L)).isEqualTo(-3456L)
assertThat(wearUtil.msSince(WearUtilMocker.REF_NOW - 6294L)).isEqualTo(6294L) assertThat(fakeWearUtil.msSince(FakeWearUtil.REF_NOW - 6294L)).isEqualTo(6294L)
} }
@Test fun joinSetTest() { @Test fun joinSetTest() {
@ -33,7 +33,7 @@ class WearUtilTest : WearTestBase() {
// THEN // THEN
// we cannot guarantee order of items in joined string // we cannot guarantee order of items in joined string
// but all items have to be there // but all items have to be there
assertThat(joined).hasLength(refSet.sumOf { it.length } + (refSet.size-1)) assertThat(joined).hasLength(refSet.sumOf { it.length } + (refSet.size - 1))
assertThat("|$joined|").contains("|element1|") assertThat("|$joined|").contains("|element1|")
assertThat("|$joined|").contains("|second-elem|") assertThat("|$joined|").contains("|second-elem|")
assertThat("|$joined|").contains("|3rd|") assertThat("|$joined|").contains("|3rd|")
@ -47,7 +47,7 @@ class WearUtilTest : WearTestBase() {
val set = persistence.explodeSet(serializedSet, ":") val set = persistence.explodeSet(serializedSet, ":")
// THEN // THEN
assertThat(set).containsExactly( "element1", "second-elem", "3rd") assertThat(set).containsExactly("element1", "second-elem", "3rd")
} }
@Test fun explodeSetEmptyElemsTest() { @Test fun explodeSetEmptyElemsTest() {
@ -69,7 +69,8 @@ class WearUtilTest : WearTestBase() {
"3rd", "3rd",
"czwarty", "czwarty",
"V", "V",
"6") "6"
)
// WHEN // WHEN
val joinedSet = persistence.joinSet(refSet, "#") val joinedSet = persistence.joinSet(refSet, "#")
@ -97,14 +98,13 @@ class WearUtilTest : WearTestBase() {
} }
*/ */
@Test fun rateLimitTest() { @Test fun rateLimitTest() {
wearUtilMocker.prepareMockNoReal()
// WHEN // WHEN
val firstCall = wearUtil.isBelowRateLimit("test-limit", 3) val firstCall = fakeWearUtil.isBelowRateLimit("test-limit", 3)
val callAfterward = wearUtil.isBelowRateLimit("test-limit", 3) val callAfterward = fakeWearUtil.isBelowRateLimit("test-limit", 3)
wearUtilMocker.progressClock(500L) fakeWearUtil.progressClock(500L)
val callTooSoon = wearUtil.isBelowRateLimit("test-limit", 3) val callTooSoon = fakeWearUtil.isBelowRateLimit("test-limit", 3)
wearUtilMocker.progressClock(3100L) fakeWearUtil.progressClock(3100L)
val callAfterRateLimit = wearUtil.isBelowRateLimit("test-limit", 3) val callAfterRateLimit = fakeWearUtil.isBelowRateLimit("test-limit", 3)
// THEN // THEN
assertThat(firstCall).isTrue() assertThat(firstCall).isTrue()

View file

@ -1,14 +1,12 @@
package info.nightscout.androidaps.testing.mockers package info.nightscout.androidaps.testing.mockers
import info.nightscout.androidaps.data.RawDisplayData import info.nightscout.androidaps.data.RawDisplayData
import info.nightscout.androidaps.interaction.utils.WearUtil import info.nightscout.androidaps.WearTestBase
import info.nightscout.shared.SafeParse.stringToDouble import info.nightscout.shared.SafeParse.stringToDouble
import info.nightscout.rx.weardata.EventData import info.nightscout.rx.weardata.EventData
import info.nightscout.rx.weardata.EventData.SingleBg import info.nightscout.rx.weardata.EventData.SingleBg
class RawDataMocker(wearUtil: WearUtil) { class RawDataMocker() {
private val wearUtilMocker: WearUtilMocker = WearUtilMocker(wearUtil)
fun rawSgv(sgv: String?, m: Int, deltaString: String): RawDisplayData { fun rawSgv(sgv: String?, m: Int, deltaString: String): RawDisplayData {
val raw = RawDisplayData() val raw = RawDisplayData()
@ -25,7 +23,7 @@ class RawDataMocker(wearUtil: WearUtil) {
} }
raw.singleBg = SingleBg( raw.singleBg = SingleBg(
wearUtilMocker.backInTime(0, 0, m, 0), WearTestBase.backInTime(0, 0, m, 0),
sgv!!, sgv!!,
"", "",
d, d,
@ -45,7 +43,7 @@ class RawDataMocker(wearUtil: WearUtil) {
fun rawDelta(m: Int, delta: String): RawDisplayData { fun rawDelta(m: Int, delta: String): RawDisplayData {
val raw = RawDisplayData() val raw = RawDisplayData()
raw.singleBg = SingleBg( raw.singleBg = SingleBg(
wearUtilMocker.backInTime(0, 0, m, 0), WearTestBase.backInTime(0, 0, m, 0),
"", "",
"", "",
"", "",

View file

@ -1,43 +0,0 @@
package info.nightscout.androidaps.testing.mockers
import info.nightscout.androidaps.interaction.utils.Constants
import info.nightscout.androidaps.interaction.utils.WearUtil
import info.nightscout.annotations.OpenForTesting
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
@OpenForTesting
class WearUtilMocker(private val wearUtil: WearUtil) {
private var clockMsDiff = 0L
fun prepareMock() {
resetClock()
// because we cleverly used timestamp() by implementation, we can mock it
// and control the time in tests
Mockito.`when`(wearUtil.timestamp()).thenReturn(REF_NOW + clockMsDiff)
}
fun prepareMockNoReal() {
resetClock()
Mockito.doAnswer { REF_NOW + clockMsDiff }.`when`(wearUtil).timestamp()
Mockito.doReturn(null).`when`(wearUtil).getWakeLock(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt())
}
private fun resetClock() {
clockMsDiff = 0L
}
fun progressClock(byMilliseconds: Long) {
clockMsDiff += byMilliseconds
}
fun backInTime(d: Int, h: Int, m: Int, s: Int): Long {
return REF_NOW - (Constants.DAY_IN_MS * d + Constants.HOUR_IN_MS * h + Constants.MINUTE_IN_MS * m + Constants.SECOND_IN_MS * s)
}
companion object {
const val REF_NOW = 1572610530000L
}
}