Skip to content

Commit 79ea847

Browse files
fix: add clockProvider to CalendarProvider for test time injection (#228)
CalendarProviderReminderTest was failing because: - CalendarProvider.getNextEventReminderTime() filters reminders using clock.currentTimeMillis() - With test time changed to December 2023 (past) and real time being January 2026 - All reminders were filtered out as 'in the past', returning 0 Added clockProvider pattern to CalendarProvider (same as ApplicationController) and inject test clock via CalendarProviderTestFixture. Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent bb3acab commit 79ea847

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

android/app/src/androidTest/java/com/github/quarck/calnotify/testutils/CalendarProviderTestFixture.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class CalendarProviderTestFixture {
2929
}
3030

3131
private fun setupInitialState() {
32+
// Inject test clock into CalendarProvider for deterministic time behavior
33+
CalendarProvider.clockProvider = { timeProvider.testClock }
34+
3235
// Clear any existing settings that might affect calendar handling
3336
val settings = Settings(contextProvider.fakeContext)
3437
settings.setBoolean("enable_manual_calendar_rescan", false)
@@ -324,6 +327,9 @@ class CalendarProviderTestFixture {
324327
* Cleans up test resources
325328
*/
326329
fun cleanup() {
330+
// Reset CalendarProvider clock to prevent test pollution
331+
CalendarProvider.resetClockProvider()
332+
327333
baseFixture.cleanup()
328334
}
329335

android/app/src/main/java/com/github/quarck/calnotify/calendar/CalendarProvider.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,22 @@ object CalendarProvider : CalendarProviderInterface {
4141
private const val LOG_TAG = "CalendarProvider"
4242

4343
/**
44-
* Clock implementation for time operations
44+
* Provider for Clock - enables DI for testing
4545
*/
46-
override val clock: CNPlusClockInterface = CNPlusSystemClock()
46+
var clockProvider: (() -> CNPlusClockInterface)? = null
47+
48+
/**
49+
* Clock implementation for time operations - uses clockProvider if set, otherwise real clock
50+
*/
51+
override val clock: CNPlusClockInterface
52+
get() = clockProvider?.invoke() ?: CNPlusSystemClock()
53+
54+
/**
55+
* Reset clock provider - call in @After to prevent test pollution
56+
*/
57+
fun resetClockProvider() {
58+
clockProvider = null
59+
}
4760

4861
private val alertFields =
4962
arrayOf(

0 commit comments

Comments
 (0)