@@ -25,7 +25,8 @@ import org.junit.runner.RunWith
2525import org.junit.runners.Parameterized
2626import org.junit.runners.Parameterized.Parameters
2727import java.util.*
28- import java.util.concurrent.TimeUnit
28+ import java.util.concurrent.TimeUnit.MINUTES
29+ import java.util.concurrent.TimeUnit.SECONDS
2930
3031@RunWith(Parameterized ::class )
3132class DataClearerTimeKeeperTest (private val testCase : TestCase ) {
@@ -38,44 +39,49 @@ class DataClearerTimeKeeperTest(private val testCase: TestCase) {
3839 @Parameters(name = " Test case: {index} - {0}" )
3940 fun testData (): Array <TestCase > {
4041
41- val timeNow = SystemClock .elapsedRealtime()
42+ fun timeNow (): () -> Long = { SystemClock .elapsedRealtime() }
4243
4344 return arrayOf(
4445 // APP_EXIT_ONLY shouldn't be passed to this method - always expected to return false regardless of other configuration/inputs
45- TestCase (false , APP_EXIT_ONLY , TimeUnit .MINUTES .toMillis(5 ), timeNow),
46- TestCase (false , APP_EXIT_ONLY , TimeUnit .MINUTES .toMillis(0 ), timeNow),
47- TestCase (false , APP_EXIT_ONLY , TimeUnit .MINUTES .toMillis(- 5 ), timeNow),
46+ TestCase (false , APP_EXIT_ONLY , MINUTES .toMillis(5 ), timeNow()),
47+ TestCase (false , APP_EXIT_ONLY , MINUTES .toMillis(0 ), timeNow()),
48+ TestCase (false , APP_EXIT_ONLY , MINUTES .toMillis(- 5 ), timeNow()),
49+
50+ // will return true when duration is >= 5 secs
51+ TestCase (false , APP_EXIT_OR_5_SECONDS , SECONDS .toMillis(4 ), timeNow()),
52+ TestCase (true , APP_EXIT_OR_5_SECONDS , SECONDS .toMillis(5 ), timeNow()),
53+ TestCase (true , APP_EXIT_OR_5_SECONDS , SECONDS .toMillis(6 ), timeNow()),
4854
4955 // will return true when duration is >= 5 mins
50- TestCase (false , APP_EXIT_OR_5_MINS , TimeUnit . MINUTES .toMillis(4 ), timeNow),
51- TestCase (true , APP_EXIT_OR_5_MINS , TimeUnit . MINUTES .toMillis(5 ), timeNow),
52- TestCase (true , APP_EXIT_OR_5_MINS , TimeUnit . MINUTES .toMillis(6 ), timeNow),
56+ TestCase (false , APP_EXIT_OR_5_MINS , MINUTES .toMillis(4 ), timeNow() ),
57+ TestCase (true , APP_EXIT_OR_5_MINS , MINUTES .toMillis(5 ), timeNow() ),
58+ TestCase (true , APP_EXIT_OR_5_MINS , MINUTES .toMillis(6 ), timeNow() ),
5359
5460 // will return true when duration is >= 15 mins
55- TestCase (false , APP_EXIT_OR_15_MINS , TimeUnit . MINUTES .toMillis(14 ), timeNow),
56- TestCase (true , APP_EXIT_OR_15_MINS , TimeUnit . MINUTES .toMillis(15 ), timeNow),
57- TestCase (true , APP_EXIT_OR_15_MINS , TimeUnit . MINUTES .toMillis(16 ), timeNow),
61+ TestCase (false , APP_EXIT_OR_15_MINS , MINUTES .toMillis(14 ), timeNow() ),
62+ TestCase (true , APP_EXIT_OR_15_MINS , MINUTES .toMillis(15 ), timeNow() ),
63+ TestCase (true , APP_EXIT_OR_15_MINS , MINUTES .toMillis(16 ), timeNow() ),
5864
5965 // will return true when duration is >= 30 mins
60- TestCase (false , APP_EXIT_OR_30_MINS , TimeUnit . MINUTES .toMillis(29 ), timeNow),
61- TestCase (true , APP_EXIT_OR_30_MINS , TimeUnit . MINUTES .toMillis(30 ), timeNow),
62- TestCase (true , APP_EXIT_OR_30_MINS , TimeUnit . MINUTES .toMillis(31 ), timeNow),
66+ TestCase (false , APP_EXIT_OR_30_MINS , MINUTES .toMillis(29 ), timeNow() ),
67+ TestCase (true , APP_EXIT_OR_30_MINS , MINUTES .toMillis(30 ), timeNow() ),
68+ TestCase (true , APP_EXIT_OR_30_MINS , MINUTES .toMillis(31 ), timeNow() ),
6369
6470 // will return true when duration is >= 60 mins
65- TestCase (false , APP_EXIT_OR_60_MINS , TimeUnit . MINUTES .toMillis(59 ), timeNow),
66- TestCase (true , APP_EXIT_OR_60_MINS , TimeUnit . MINUTES .toMillis(60 ), timeNow),
67- TestCase (true , APP_EXIT_OR_60_MINS , TimeUnit . MINUTES .toMillis(61 ), timeNow)
71+ TestCase (false , APP_EXIT_OR_60_MINS , MINUTES .toMillis(59 ), timeNow() ),
72+ TestCase (true , APP_EXIT_OR_60_MINS , MINUTES .toMillis(60 ), timeNow() ),
73+ TestCase (true , APP_EXIT_OR_60_MINS , MINUTES .toMillis(61 ), timeNow() )
6874 )
6975 }
7076 }
7177
7278 @Test
7379 fun enoughTimePassed () {
74- val timestamp = getPastTimestamp(testCase.durationBackgrounded, testCase.timeNow)
80+ val timestamp = getPastTimestamp(testCase.durationBackgrounded, testCase.timeNow.invoke() )
7581 assertEquals(testCase.expected, testee.hasEnoughTimeElapsed(backgroundedTimestamp = timestamp, clearWhenOption = testCase.clearWhenOption))
7682 }
7783
78- private fun getPastTimestamp (millisPreviously : Long , timeNow : Long = SystemClock .elapsedRealtime() ): Long {
84+ private fun getPastTimestamp (millisPreviously : Long , timeNow : Long ): Long {
7985 return Calendar .getInstance().also {
8086 it.timeInMillis = timeNow
8187 it.add(Calendar .MILLISECOND , (- millisPreviously).toInt())
@@ -86,6 +92,6 @@ class DataClearerTimeKeeperTest(private val testCase: TestCase) {
8692 val expected : Boolean ,
8793 val clearWhenOption : ClearWhenOption ,
8894 val durationBackgrounded : Long ,
89- val timeNow : Long
95+ val timeNow : () -> Long
9096 )
9197}
0 commit comments