Skip to content

Commit dfa80ef

Browse files
authored
test: fix local flakiness in notification dismiss (#147)
1 parent 7ad7b9e commit dfa80ef

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ class UITestFixture {
179179
/**
180180
* Sets up lightweight mocking to prevent calendar reloads from clearing test events.
181181
* This mocks ApplicationController.onMainActivityResumed to skip the calendar rescan.
182+
* Also mocks alarm-triggered notification posting to prevent flaky tests from stale
183+
* AlarmManager alarms that persist across test runs on local emulators.
182184
*/
183185
private fun setupCalendarReloadPrevention() {
184186
DevLog.info(LOG_TAG, "Setting up calendar reload prevention")
@@ -196,6 +198,17 @@ class UITestFixture {
196198
ApplicationController.onCalendarChanged(any())
197199
} just Runs
198200

201+
// Mock alarm-triggered methods to prevent notifications from stale AlarmManager alarms.
202+
// Local emulators persist alarms across test runs, causing flaky notification interference.
203+
// (GitHub Actions uses fresh emulators so this is less common there.)
204+
every {
205+
ApplicationController.onEventAlarm(any())
206+
} just Runs
207+
208+
every {
209+
ApplicationController.fireEventReminder(any(), any(), any())
210+
} just Runs
211+
199212
calendarReloadPrevented = true
200213
DevLog.info(LOG_TAG, "Calendar reload prevention active")
201214
}
@@ -551,6 +564,9 @@ class UITestFixture {
551564

552565
/**
553566
* Launches MainActivity with ActivityScenario.
567+
*
568+
* If calendar reload prevention is active, also clears any pre-existing notifications
569+
* to prevent them from interfering with UI tests (e.g., blocking toolbar buttons).
554570
*/
555571
fun launchMainActivity(): ActivityScenario<MainActivity> {
556572
DevLog.info(LOG_TAG, "Launching MainActivity")
@@ -560,6 +576,13 @@ class UITestFixture {
560576
DevLog.info(LOG_TAG, "MainActivity is ready: ${activity.javaClass.simpleName}")
561577
}
562578
dismissStartupDialogs()
579+
580+
// Clear any pre-existing notifications that could interfere with UI tests
581+
// (e.g., heads-up notifications blocking toolbar buttons)
582+
if (calendarReloadPrevented) {
583+
cancelAllNotifications()
584+
}
585+
563586
return scenario
564587
}
565588

android/app/src/androidTest/java/com/github/quarck/calnotify/ui/MainActivityTest.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ class MainActivityTest : BaseUltronTest() {
298298

299299
@Test
300300
fun search_filters_events() {
301-
fixture.cancelAllNotifications()
302301
fixture.createEvent(title = "Alpha Meeting")
303302
fixture.createEvent(title = "Beta Meeting")
304303

@@ -324,7 +323,6 @@ class MainActivityTest : BaseUltronTest() {
324323

325324
@Test
326325
fun first_back_press_hides_keyboard_keeps_filter() {
327-
fixture.cancelAllNotifications()
328326
fixture.createEvent(title = "Alpha Meeting")
329327
fixture.createEvent(title = "Beta Meeting")
330328

@@ -356,7 +354,6 @@ class MainActivityTest : BaseUltronTest() {
356354

357355
@Test
358356
fun second_back_press_clears_filter() {
359-
fixture.cancelAllNotifications()
360357
fixture.createEvent(title = "Alpha Meeting")
361358
fixture.createEvent(title = "Beta Meeting")
362359

0 commit comments

Comments
 (0)