diff --git a/app/src/main/java/de/bollwerk/app/notification/NotificationHelper.kt b/app/src/main/java/de/bollwerk/app/notification/NotificationHelper.kt index 2098487..9e25cd0 100644 --- a/app/src/main/java/de/bollwerk/app/notification/NotificationHelper.kt +++ b/app/src/main/java/de/bollwerk/app/notification/NotificationHelper.kt @@ -95,8 +95,7 @@ internal class NotificationHelper @Inject constructor( senderId: String, senderUsername: String ): Boolean { - // Suppress only while app is foreground and the related chat/messaging UI is visible. - if (isAppInForeground && (isMessagingAreaVisible || senderId == activeChatPartnerId)) return false + if (shouldSuppressNotification(senderId)) return false val chatIntent = Intent(context, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP @@ -168,6 +167,15 @@ internal class NotificationHelper @Inject constructor( return true } + internal fun shouldSuppressNotification(senderId: String): Boolean { + // Suppress only when the app is foreground and the exact chat is currently visible. + return isAppInForeground && isMessagingAreaVisible && senderId == activeChatPartnerId + } + + internal fun setAppInForegroundForTest(isForeground: Boolean) { + isAppInForeground = isForeground + } + /// Entfernt Benachrichtigungen für einen bestimmten Absender (wenn der Chat geöffnet wird). fun cancelNotificationForSender(senderId: String) { val notificationManager = NotificationManagerCompat.from(context) diff --git a/app/src/test/java/de/bollwerk/app/notification/NotificationHelperTest.kt b/app/src/test/java/de/bollwerk/app/notification/NotificationHelperTest.kt index b453f3a..93f1cc2 100644 --- a/app/src/test/java/de/bollwerk/app/notification/NotificationHelperTest.kt +++ b/app/src/test/java/de/bollwerk/app/notification/NotificationHelperTest.kt @@ -5,7 +5,7 @@ import android.content.Context import io.mockk.every import io.mockk.mockk import org.junit.Assert.assertFalse -import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test @@ -28,36 +28,48 @@ class NotificationHelperTest { @Test fun test_showNewMessageNotification_activeChatMatchesSender_returnsFalse() { // Given + notificationHelper.setAppInForegroundForTest(true) notificationHelper.setActiveChat("user-123") + notificationHelper.setMessagingAreaVisible(true) // When - val result = notificationHelper.showNewMessageNotification( - senderId = "user-123", - senderUsername = "Bob" - ) + val result = notificationHelper.shouldSuppressNotification("user-123") // Then - assertFalse(result) + assertTrue(result) } @Test fun test_setActiveChat_setsAndClears() { // Given + notificationHelper.setAppInForegroundForTest(true) notificationHelper.setActiveChat("user-123") + notificationHelper.setMessagingAreaVisible(true) // When – message from same user is suppressed - val suppressed = notificationHelper.showNewMessageNotification( - senderId = "user-123", - senderUsername = "Bob" - ) + val suppressed = notificationHelper.shouldSuppressNotification("user-123") // Then - assertFalse(suppressed) + assertTrue(suppressed) // When – clear active chat notificationHelper.setActiveChat(null) // Then – message from same user is no longer suppressed at the guard - // (actual notification display needs Android runtime, tested via instrumentation) + assertFalse(notificationHelper.shouldSuppressNotification("user-123")) + } + + @Test + fun test_showNewMessageNotification_activeChatNotVisible_notSuppressedByGuard() { + // Given + notificationHelper.setAppInForegroundForTest(true) + notificationHelper.setActiveChat("user-123") + notificationHelper.setMessagingAreaVisible(false) + + // When + val result = notificationHelper.shouldSuppressNotification("user-123") + + // Then + assertFalse(result) } }