Skip to content

Commit 54c0fd1

Browse files
committed
test: add a test unit to ensure openDestinationActivity not to be blocked
1 parent ef2286a commit 54c0fd1

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

OneSignalSDK/onesignal/notifications/src/test/java/com/onesignal/notifications/internal/lifecycle/NotificationLifecycleServiceTests.kt

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import io.mockk.coVerify
2323
import io.mockk.every
2424
import io.mockk.mockk
2525
import io.mockk.spyk
26+
import kotlinx.coroutines.delay
27+
import kotlinx.coroutines.withTimeout
2628
import org.json.JSONArray
2729
import org.json.JSONObject
2830
import org.robolectric.Robolectric
@@ -59,7 +61,11 @@ private class Mocks {
5961
every { deviceType } returns IDeviceService.DeviceType.Android
6062
},
6163
mockk<INotificationBackendService>().apply {
62-
coEvery { updateNotificationAsOpened(any(), any(), any(), any()) } returns Unit
64+
coEvery { updateNotificationAsOpened(any(), any(), any(), any()) } coAnswers {
65+
// assume every updateNotificationAsOpened call takes 5 ms
66+
delay(5)
67+
Unit
68+
}
6369
},
6470
mockk<IReceiveReceiptWorkManager>(),
6571
mockk<IAnalyticsTracker>().apply {
@@ -70,14 +76,13 @@ private class Mocks {
7076

7177
val activity: Activity =
7278
run {
73-
val activityController : ActivityController<Activity>
79+
val activityController: ActivityController<Activity>
7480
Robolectric.buildActivity(Activity::class.java).use { controller ->
7581
controller.setup() // Moves Activity to RESUMED state
7682
activityController = controller
7783
}
7884
activityController.get()
7985
}
80-
8186
}
8287

8388
@RobolectricTest
@@ -115,4 +120,41 @@ class NotificationLifecycleServiceTests : FunSpec({
115120
)
116121
}
117122
}
123+
124+
test("ensure notificationOpened makes backend updates in a background process") {
125+
// Given
126+
val mocks = Mocks()
127+
val notificationLifecycleService = mocks.notificationLifecycleService
128+
val activity = mocks.activity
129+
130+
// When
131+
val payload = JSONArray()
132+
for (i in 1..1000) {
133+
// adding 1000 different notifications
134+
payload.put(
135+
JSONObject()
136+
.put("alert", "test message")
137+
.put(
138+
"custom",
139+
JSONObject()
140+
.put("i", "UUID$i"),
141+
),
142+
)
143+
}
144+
145+
withTimeout(500) {
146+
// 1000 notifications should be handled within a small amount of time
147+
notificationLifecycleService.notificationOpened(activity, payload)
148+
}
149+
150+
// Then
151+
coVerify(exactly = 1) {
152+
// ensure openDestinationActivity is called within the timeout, prove that the increasing
153+
// number of notifications clicked does not delay the main thread proportionally
154+
notificationLifecycleService.openDestinationActivity(
155+
withArg { Any() },
156+
withArg { Any() },
157+
)
158+
}
159+
}
118160
})

0 commit comments

Comments
 (0)