Skip to content

Commit

Permalink
Add tests for calling preventDefault twice
Browse files Browse the repository at this point in the history
* The tests ensure calling preventDefault twice work when called in different threads
* And a followup call to display does not display the notification
  • Loading branch information
nan-li committed Jul 9, 2024
1 parent cf45cfe commit 7d4391d
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout
import org.json.JSONObject
import org.robolectric.annotation.Config
Expand Down Expand Up @@ -269,4 +271,55 @@ class NotificationGenerationProcessorTests : FunSpec({
mocks.notificationGenerationProcessor.processNotificationData(context, 1, mocks.notificationPayload, false, 1111)
}
}

test("processNotificationData allows the will display callback to prevent default behavior twice") {
// Given
val context = ApplicationProvider.getApplicationContext<Context>()
val mocks = Mocks()
coEvery { mocks.notificationDisplayer.displayNotification(any()) } returns true
coEvery { mocks.notificationLifecycleService.externalRemoteNotificationReceived(any()) } just runs
coEvery { mocks.notificationLifecycleService.externalNotificationWillShowInForeground(any()) } coAnswers {
val willDisplayEvent = firstArg<INotificationWillDisplayEvent>()
willDisplayEvent.preventDefault(false)
GlobalScope.launch {
delay(100)
willDisplayEvent.preventDefault(true)
delay(100)
willDisplayEvent.notification.display()
}
}

// When
mocks.notificationGenerationProcessor.processNotificationData(context, 1, mocks.notificationPayload, false, 1111)

// Then
coVerify(exactly = 0) {
mocks.notificationDisplayer.displayNotification(any())
}
}

test("processNotificationData allows the received event callback to prevent default behavior twice") {
// Given
val context = ApplicationProvider.getApplicationContext<Context>()
val mocks = Mocks()
coEvery { mocks.notificationDisplayer.displayNotification(any()) } returns true
coEvery { mocks.notificationLifecycleService.externalRemoteNotificationReceived(any()) } coAnswers {
val receivedEvent = firstArg<INotificationReceivedEvent>()
receivedEvent.preventDefault(false)
GlobalScope.launch {
delay(100)
receivedEvent.preventDefault(true)
delay(100)
receivedEvent.notification.display()
}
}

// When
mocks.notificationGenerationProcessor.processNotificationData(context, 1, mocks.notificationPayload, true, 1111)

// Then
coVerify(exactly = 0) {
mocks.notificationDisplayer.displayNotification(any())
}
}
})

0 comments on commit 7d4391d

Please sign in to comment.