Skip to content

Commit 64b309c

Browse files
committed
Add unit tests to validate we mark the notification as dismissed without waiting
1 parent ad2425d commit 64b309c

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

OneSignalSDK/onesignal/notifications/src/test/java/com/onesignal/notifications/internal/generation/NotificationGenerationProcessorTests.kt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.mockk.just
2323
import io.mockk.mockk
2424
import io.mockk.runs
2525
import kotlinx.coroutines.delay
26+
import kotlinx.coroutines.withTimeout
2627
import org.json.JSONObject
2728
import org.robolectric.annotation.Config
2829

@@ -373,4 +374,99 @@ class NotificationGenerationProcessorTests : FunSpec({
373374
)
374375
}
375376
}
377+
378+
test("processNotificationData should immediately drop the notification when will display callback indicates to") {
379+
// Given
380+
val context = ApplicationProvider.getApplicationContext<Context>()
381+
val mockTime = MockHelper.time(1111)
382+
val mockApplicationService = AndroidMockHelper.applicationService()
383+
every { mockApplicationService.isInForeground } returns true
384+
val mockNotificationDisplayer = mockk<INotificationDisplayer>()
385+
val mockNotificationRepository = mockk<INotificationRepository>()
386+
coEvery { mockNotificationRepository.doesNotificationExist(any()) } returns false
387+
coEvery { mockNotificationRepository.createNotification(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } just runs
388+
val mockNotificationSummaryManager = mockk<INotificationSummaryManager>()
389+
val mockNotificationLifecycleService = mockk<INotificationLifecycleService>()
390+
coEvery { mockNotificationLifecycleService.canReceiveNotification(any()) } returns true
391+
coEvery { mockNotificationLifecycleService.notificationReceived(any()) } just runs
392+
coEvery { mockNotificationLifecycleService.externalRemoteNotificationReceived(any()) } just runs
393+
coEvery { mockNotificationLifecycleService.externalNotificationWillShowInForeground(any()) } answers {
394+
val willDisplayEvent = firstArg<INotificationWillDisplayEvent>()
395+
// Setting discard parameter to true indicating we should immediately discard
396+
willDisplayEvent.preventDefault(true)
397+
}
398+
399+
val notificationGenerationProcessor =
400+
NotificationGenerationProcessor(
401+
mockApplicationService,
402+
mockNotificationDisplayer,
403+
MockHelper.configModelStore(),
404+
mockNotificationRepository,
405+
mockNotificationSummaryManager,
406+
mockNotificationLifecycleService,
407+
mockTime,
408+
)
409+
410+
val payload =
411+
JSONObject()
412+
.put("alert", "test message")
413+
.put("title", "test title")
414+
.put(
415+
"custom",
416+
JSONObject()
417+
.put("i", "UUID1"),
418+
)
419+
420+
// If discard is set to false this should timeout waiting for display()
421+
withTimeout(1_000) {
422+
notificationGenerationProcessor.processNotificationData(context, 1, payload, false, 1111)
423+
}
424+
}
425+
426+
test("processNotificationData should immediately drop the notification when received event callback indicates to") {
427+
// Given
428+
val context = ApplicationProvider.getApplicationContext<Context>()
429+
val mockTime = MockHelper.time(1111)
430+
val mockApplicationService = AndroidMockHelper.applicationService()
431+
every { mockApplicationService.isInForeground } returns true
432+
val mockNotificationDisplayer = mockk<INotificationDisplayer>()
433+
val mockNotificationRepository = mockk<INotificationRepository>()
434+
coEvery { mockNotificationRepository.doesNotificationExist(any()) } returns false
435+
coEvery { mockNotificationRepository.createNotification(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } just runs
436+
val mockNotificationSummaryManager = mockk<INotificationSummaryManager>()
437+
val mockNotificationLifecycleService = mockk<INotificationLifecycleService>()
438+
coEvery { mockNotificationLifecycleService.canReceiveNotification(any()) } returns true
439+
coEvery { mockNotificationLifecycleService.notificationReceived(any()) } just runs
440+
coEvery { mockNotificationLifecycleService.externalRemoteNotificationReceived(any()) } answers {
441+
val receivedEvent = firstArg<INotificationReceivedEvent>()
442+
receivedEvent.preventDefault(true)
443+
}
444+
coEvery { mockNotificationLifecycleService.externalNotificationWillShowInForeground(any()) } just runs
445+
446+
val notificationGenerationProcessor =
447+
NotificationGenerationProcessor(
448+
mockApplicationService,
449+
mockNotificationDisplayer,
450+
MockHelper.configModelStore(),
451+
mockNotificationRepository,
452+
mockNotificationSummaryManager,
453+
mockNotificationLifecycleService,
454+
mockTime,
455+
)
456+
457+
val payload =
458+
JSONObject()
459+
.put("alert", "test message")
460+
.put("title", "test title")
461+
.put(
462+
"custom",
463+
JSONObject()
464+
.put("i", "UUID1"),
465+
)
466+
467+
// If discard is set to false this should timeout waiting for display()
468+
withTimeout(1_000) {
469+
notificationGenerationProcessor.processNotificationData(context, 1, payload, false, 1111)
470+
}
471+
}
376472
})

0 commit comments

Comments
 (0)