Skip to content

Commit 52f1825

Browse files
committed
test: hopefully clean state in between tests right
1 parent 0c7ce21 commit 52f1825

9 files changed

+141
-0
lines changed

android/app/src/androidTest/java/com/github/quarck/calnotify/calendar/CalendarProviderEventTest.kt

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class CalendarProviderEventTest {
2929
DevLog.info(LOG_TAG, "Setting up test environment")
3030
fixture = CalendarProviderTestFixture()
3131

32+
// Clear any existing test state
33+
fixture.clearTestState()
34+
3235
// Create a test calendar for all event tests
3336
testCalendarId = fixture.createCalendarWithSettings(
3437
"Test Calendar",

android/app/src/androidTest/java/com/github/quarck/calnotify/calendar/CalendarProviderReminderTest.kt

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class CalendarProviderReminderTest {
3232
DevLog.info(LOG_TAG, "Setting up test environment")
3333
fixture = CalendarProviderTestFixture()
3434

35+
// Clear any existing test state
36+
fixture.clearTestState()
37+
3538
// Create a test calendar for all reminder tests
3639
testCalendarId = fixture.createCalendarWithSettings(
3740
"Test Calendar",

android/app/src/androidTest/java/com/github/quarck/calnotify/calendarmonitor/CalendarTestFixtureExampleTest.kt

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class CalendarTestFixtureExampleTest {
4040
// Create the base fixture
4141
baseFixture = BaseCalendarTestFixture.Builder().build()
4242

43+
// Clear any existing test state
44+
baseFixture.clearTestState()
45+
4346
// Set up a test calendar
4447
baseFixture.setupTestCalendar("Example Test Calendar")
4548
}

android/app/src/androidTest/java/com/github/quarck/calnotify/calendarmonitor/ComponentIsolationTest.kt

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class ComponentIsolationTest {
5454
fun setup() {
5555
DevLog.info(LOG_TAG, "Setting up base test environment")
5656
MockKAnnotations.init(this)
57+
58+
// Clear any previous mock states
59+
unmockkAll()
5760
}
5861

5962
@After

android/app/src/androidTest/java/com/github/quarck/calnotify/calendarmonitor/FixturedCalendarMonitorServiceTest.kt

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class FixturedCalendarMonitorServiceTest {
3939
fun setup() {
4040
DevLog.info(LOG_TAG, "Setting up fixture for calendar monitor tests")
4141
fixture = CalendarMonitorTestFixture()
42+
43+
// Clear any existing test state
44+
fixture.clearTestState()
4245
}
4346

4447
@After

android/app/src/androidTest/java/com/github/quarck/calnotify/calendarmonitor/SimpleCalendarMonitoringTest.kt

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class SimpleCalendarMonitoringTest {
3636
fun setup() {
3737
DevLog.info(LOG_TAG, "Setting up test with CalendarMonitorTestFixture")
3838
fixture = CalendarMonitorTestFixture()
39+
40+
// Clear any existing test state
41+
fixture.clearTestState()
3942
}
4043

4144
@After

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

+28
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,34 @@ class BaseCalendarTestFixture private constructor(builder: Builder) {
388388
timeProvider.advanceTime(milliseconds)
389389
}
390390

391+
/**
392+
* Clears all test state to ensure test isolation
393+
*/
394+
fun clearTestState() {
395+
DevLog.info(LOG_TAG, "Clearing test state for BaseCalendarTestFixture")
396+
397+
// Clear storage databases
398+
clearStorages()
399+
400+
// Reset test clock to a known state
401+
timeProvider.testClock.setCurrentTime(System.currentTimeMillis())
402+
403+
// Clear any mock overrides
404+
unmockkAll()
405+
406+
// Reinitialize components
407+
timeProvider.setup()
408+
contextProvider.setup()
409+
calendarProvider.setup()
410+
applicationComponents.setup()
411+
412+
// Reset calendar monitor state
413+
val monitorState = CalendarMonitorState(contextProvider.fakeContext)
414+
monitorState.firstScanEver = false
415+
monitorState.prevEventScanTo = timeProvider.testClock.currentTimeMillis()
416+
monitorState.prevEventFireFromScan = timeProvider.testClock.currentTimeMillis()
417+
}
418+
391419
/**
392420
* Cleans up all mocks and resources
393421
*/

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

+29
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,33 @@ class CalendarMonitorTestFixture {
366366
DevLog.info(LOG_TAG, "Cleaning up CalendarMonitorTestFixture")
367367
baseFixture.cleanup()
368368
}
369+
370+
/**
371+
* Clears all test state to ensure test isolation
372+
*/
373+
fun clearTestState() {
374+
DevLog.info(LOG_TAG, "Clearing test state for CalendarMonitorTestFixture")
375+
376+
// Get the context from the base fixture
377+
val context = contextProvider.fakeContext
378+
379+
// Clear monitoring state
380+
MonitorStorage(context).classCustomUse { db ->
381+
db.deleteAlertsMatching { true }
382+
DevLog.info(LOG_TAG, "Cleared all alerts from monitor storage")
383+
}
384+
385+
// Clear events storage if needed
386+
// TODO: do we need this here?
387+
// applicationComponents.clearEventsStorage()
388+
389+
// Reset calendar monitor state
390+
val monitorState = CalendarMonitorState(context)
391+
monitorState.firstScanEver = false
392+
monitorState.prevEventScanTo = timeProvider.testClock.currentTimeMillis()
393+
monitorState.prevEventFireFromScan = timeProvider.testClock.currentTimeMillis()
394+
395+
// Reset the base fixture test state
396+
baseFixture.clearTestState()
397+
}
369398
}

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

+66
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.quarck.calnotify.testutils
22

33
import android.content.ContentValues
4+
import android.content.Context
45
import android.provider.CalendarContract
56
import com.github.quarck.calnotify.Settings
67
import com.github.quarck.calnotify.calendar.*
@@ -325,4 +326,69 @@ class CalendarProviderTestFixture {
325326
fun cleanup() {
326327
baseFixture.cleanup()
327328
}
329+
330+
/**
331+
* Clears all test state to ensure test isolation
332+
*/
333+
fun clearTestState() {
334+
DevLog.info(LOG_TAG, "Clearing test state")
335+
336+
// Clear all calendar data
337+
clearAllEvents()
338+
clearAllReminders()
339+
clearAllAlerts()
340+
341+
// Clear storage databases
342+
clearStorages(contextProvider.fakeContext)
343+
344+
// Reset test clock to a known state
345+
timeProvider.testClock.setCurrentTime(System.currentTimeMillis())
346+
347+
// Clear any mock overrides
348+
unmockkAll()
349+
350+
// Reset calendar provider state
351+
setupCalendarProvider()
352+
}
353+
354+
private fun clearAllEvents() {
355+
val context = contextProvider.fakeContext
356+
val eventsUri = CalendarContract.Events.CONTENT_URI
357+
context.contentResolver.delete(eventsUri, null, null)
358+
}
359+
360+
private fun clearAllReminders() {
361+
val context = contextProvider.fakeContext
362+
val remindersUri = CalendarContract.Reminders.CONTENT_URI
363+
context.contentResolver.delete(remindersUri, null, null)
364+
}
365+
366+
private fun clearAllAlerts() {
367+
val context = contextProvider.fakeContext
368+
val alertsUri = CalendarContract.CalendarAlerts.CONTENT_URI
369+
context.contentResolver.delete(alertsUri, null, null)
370+
}
371+
372+
/**
373+
* Clears all storage databases
374+
*/
375+
private fun clearStorages(context: Context) {
376+
baseFixture.clearStorages()
377+
}
378+
379+
/**
380+
* Clears all MockK overrides
381+
*/
382+
private fun unmockkAll() {
383+
// Use MockK's unmockAll function to clear all mocks
384+
io.mockk.unmockkAll()
385+
}
386+
387+
/**
388+
* Resets the calendar provider to its initial state
389+
*/
390+
private fun setupCalendarProvider() {
391+
// Re-setup the calendar provider from the base fixture
392+
baseFixture.calendarProvider.setup()
393+
}
328394
}

0 commit comments

Comments
 (0)