Skip to content

Commit 9b7b91d

Browse files
committed
Add tests for calling preventDefault twice
* The tests ensure calling preventDefault twice work when called in different threads * And a followup call to display does not display the notification
1 parent cf45cfe commit 9b7b91d

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

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

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import io.mockk.every
2121
import io.mockk.just
2222
import io.mockk.mockk
2323
import io.mockk.runs
24+
import kotlinx.coroutines.GlobalScope
2425
import kotlinx.coroutines.delay
26+
import kotlinx.coroutines.launch
2527
import kotlinx.coroutines.withTimeout
2628
import org.json.JSONObject
2729
import org.robolectric.annotation.Config
@@ -66,15 +68,16 @@ private class Mocks {
6668
mockNotificationRepository
6769
}
6870

69-
val notificationGenerationProcessor = NotificationGenerationProcessor(
70-
applicationService,
71-
notificationDisplayer,
72-
MockHelper.configModelStore(),
73-
notificationRepository,
74-
mockk(),
75-
notificationLifecycleService,
76-
MockHelper.time(1111),
77-
)
71+
val notificationGenerationProcessor =
72+
NotificationGenerationProcessor(
73+
applicationService,
74+
notificationDisplayer,
75+
MockHelper.configModelStore(),
76+
notificationRepository,
77+
mockk(),
78+
notificationLifecycleService,
79+
MockHelper.time(1111),
80+
)
7881

7982
val notificationPayload: JSONObject =
8083
JSONObject()
@@ -269,4 +272,55 @@ class NotificationGenerationProcessorTests : FunSpec({
269272
mocks.notificationGenerationProcessor.processNotificationData(context, 1, mocks.notificationPayload, false, 1111)
270273
}
271274
}
275+
276+
test("processNotificationData allows the will display callback to prevent default behavior twice") {
277+
// Given
278+
val context = ApplicationProvider.getApplicationContext<Context>()
279+
val mocks = Mocks()
280+
coEvery { mocks.notificationDisplayer.displayNotification(any()) } returns true
281+
coEvery { mocks.notificationLifecycleService.externalRemoteNotificationReceived(any()) } just runs
282+
coEvery { mocks.notificationLifecycleService.externalNotificationWillShowInForeground(any()) } coAnswers {
283+
val willDisplayEvent = firstArg<INotificationWillDisplayEvent>()
284+
willDisplayEvent.preventDefault(false)
285+
GlobalScope.launch {
286+
delay(100)
287+
willDisplayEvent.preventDefault(true)
288+
delay(100)
289+
willDisplayEvent.notification.display()
290+
}
291+
}
292+
293+
// When
294+
mocks.notificationGenerationProcessor.processNotificationData(context, 1, mocks.notificationPayload, false, 1111)
295+
296+
// Then
297+
coVerify(exactly = 0) {
298+
mocks.notificationDisplayer.displayNotification(any())
299+
}
300+
}
301+
302+
test("processNotificationData allows the received event callback to prevent default behavior twice") {
303+
// Given
304+
val context = ApplicationProvider.getApplicationContext<Context>()
305+
val mocks = Mocks()
306+
coEvery { mocks.notificationDisplayer.displayNotification(any()) } returns true
307+
coEvery { mocks.notificationLifecycleService.externalRemoteNotificationReceived(any()) } coAnswers {
308+
val receivedEvent = firstArg<INotificationReceivedEvent>()
309+
receivedEvent.preventDefault(false)
310+
GlobalScope.launch {
311+
delay(100)
312+
receivedEvent.preventDefault(true)
313+
delay(100)
314+
receivedEvent.notification.display()
315+
}
316+
}
317+
318+
// When
319+
mocks.notificationGenerationProcessor.processNotificationData(context, 1, mocks.notificationPayload, true, 1111)
320+
321+
// Then
322+
coVerify(exactly = 0) {
323+
mocks.notificationDisplayer.displayNotification(any())
324+
}
325+
}
272326
})

0 commit comments

Comments
 (0)