Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add a test unit to ensure openDestinationActivity not to be blo…
…cked
  • Loading branch information
jinliu9508 committed May 22, 2025
commit 54c0fd1532f6e3238cc54c23b0d7cb1e5f03c85e
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import kotlinx.coroutines.delay
import kotlinx.coroutines.withTimeout
import org.json.JSONArray
import org.json.JSONObject
import org.robolectric.Robolectric
Expand Down Expand Up @@ -59,7 +61,11 @@ private class Mocks {
every { deviceType } returns IDeviceService.DeviceType.Android
},
mockk<INotificationBackendService>().apply {
coEvery { updateNotificationAsOpened(any(), any(), any(), any()) } returns Unit
coEvery { updateNotificationAsOpened(any(), any(), any(), any()) } coAnswers {
// assume every updateNotificationAsOpened call takes 5 ms
delay(5)
Unit
}
},
mockk<IReceiveReceiptWorkManager>(),
mockk<IAnalyticsTracker>().apply {
Expand All @@ -70,14 +76,13 @@ private class Mocks {

val activity: Activity =
run {
val activityController : ActivityController<Activity>
val activityController: ActivityController<Activity>
Robolectric.buildActivity(Activity::class.java).use { controller ->
controller.setup() // Moves Activity to RESUMED state
activityController = controller
}
activityController.get()
}

}

@RobolectricTest
Expand Down Expand Up @@ -115,4 +120,41 @@ class NotificationLifecycleServiceTests : FunSpec({
)
}
}

test("ensure notificationOpened makes backend updates in a background process") {
// Given
val mocks = Mocks()
val notificationLifecycleService = mocks.notificationLifecycleService
val activity = mocks.activity

// When
val payload = JSONArray()
for (i in 1..1000) {
// adding 1000 different notifications
payload.put(
JSONObject()
.put("alert", "test message")
.put(
"custom",
JSONObject()
.put("i", "UUID$i"),
),
)
}

withTimeout(500) {
// 1000 notifications should be handled within a small amount of time
notificationLifecycleService.notificationOpened(activity, payload)
}

// Then
coVerify(exactly = 1) {
// ensure openDestinationActivity is called within the timeout, prove that the increasing
// number of notifications clicked does not delay the main thread proportionally
notificationLifecycleService.openDestinationActivity(
withArg { Any() },
withArg { Any() },
)
}
}
})