Skip to content

Commit be52447

Browse files
committed
review + tests
1 parent 277602f commit be52447

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed

features/call/impl/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ dependencies {
100100
testImplementation(projects.libraries.push.test)
101101
testImplementation(projects.services.analytics.test)
102102
testImplementation(projects.services.appnavstate.test)
103+
testImplementation(projects.services.toolbox.test)
103104
testImplementation(projects.tests.testutils)
104105
testImplementation(libs.androidx.compose.ui.test.junit)
105106
testReleaseImplementation(libs.androidx.compose.ui.test.manifest)

features/call/impl/src/main/kotlin/io/element/android/features/call/impl/notifications/CallNotificationData.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ data class CallNotificationData(
2626
val notificationChannelId: String,
2727
val timestamp: Long,
2828
val textContent: String?,
29+
// Expiration timestamp in millis since epoch
2930
val expirationTimestamp: Long,
3031
) : Parcelable

features/call/impl/src/main/kotlin/io/element/android/features/call/impl/utils/ActiveCallManager.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import io.element.android.libraries.push.api.notifications.ForegroundServiceType
3434
import io.element.android.libraries.push.api.notifications.NotificationIdProvider
3535
import io.element.android.libraries.push.api.notifications.OnMissedCallNotificationHandler
3636
import io.element.android.services.appnavstate.api.AppForegroundStateService
37+
import io.element.android.services.toolbox.api.systemclock.SystemClock
3738
import kotlinx.coroutines.CoroutineScope
3839
import kotlinx.coroutines.ExperimentalCoroutinesApi
3940
import kotlinx.coroutines.Job
@@ -98,6 +99,7 @@ class DefaultActiveCallManager(
9899
private val defaultCurrentCallService: DefaultCurrentCallService,
99100
private val appForegroundStateService: AppForegroundStateService,
100101
private val imageLoaderHolder: ImageLoaderHolder,
102+
private val systemClock: SystemClock,
101103
) : ActiveCallManager {
102104
private val tag = "DefaultActiveCallManager"
103105
private var timedOutCallJob: Job? = null
@@ -120,7 +122,7 @@ class DefaultActiveCallManager(
120122
mutex.withLock {
121123
val ringDuration =
122124
min(
123-
notificationData.expirationTimestamp - System.currentTimeMillis(),
125+
notificationData.expirationTimestamp - systemClock.epochMillis(),
124126
ElementCallConfig.RINGING_CALL_DURATION_SECONDS * 1000L
125127
)
126128

features/call/impl/src/test/kotlin/io/element/android/features/call/DefaultElementCallEntryPointTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class DefaultElementCallEntryPointTest {
5959
senderName = "senderName",
6060
avatarUrl = "avatarUrl",
6161
timestamp = 0,
62+
expirationTimestamp = 0,
6263
notificationChannelId = "notificationChannelId",
6364
textContent = "textContent",
6465
)

features/call/impl/src/test/kotlin/io/element/android/features/call/notifications/RingingCallNotificationCreatorTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class RingingCallNotificationCreatorTest {
7373
roomAvatarUrl = "https://example.com/avatar.jpg",
7474
notificationChannelId = "channelId",
7575
timestamp = 0L,
76+
expirationTimestamp = 20L,
7677
textContent = "textContent",
7778
)
7879

features/call/impl/src/test/kotlin/io/element/android/features/call/utils/DefaultActiveCallManagerTest.kt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import io.element.android.libraries.push.test.notifications.FakeImageLoaderHolde
3939
import io.element.android.libraries.push.test.notifications.FakeOnMissedCallNotificationHandler
4040
import io.element.android.libraries.push.test.notifications.push.FakeNotificationBitmapLoader
4141
import io.element.android.services.appnavstate.test.FakeAppForegroundStateService
42+
import io.element.android.services.toolbox.test.systemclock.A_FAKE_TIMESTAMP
43+
import io.element.android.services.toolbox.test.systemclock.FakeSystemClock
4244
import io.element.android.tests.testutils.lambda.lambdaRecorder
4345
import io.element.android.tests.testutils.lambda.value
4446
import io.element.android.tests.testutils.plantTestTimber
@@ -368,6 +370,83 @@ class DefaultActiveCallManagerTest {
368370
assertThat(manager.activeCall.value).isNotNull()
369371
}
370372

373+
@OptIn(ExperimentalCoroutinesApi::class)
374+
@Test
375+
fun `IncomingCall - rings no longer than expiration time`() = runTest {
376+
setupShadowPowerManager()
377+
val notificationManagerCompat = mockk<NotificationManagerCompat>(relaxed = true)
378+
val clock = FakeSystemClock()
379+
val manager = createActiveCallManager(notificationManagerCompat = notificationManagerCompat, systemClock = clock)
380+
381+
assertThat(manager.activeWakeLock?.isHeld).isFalse()
382+
assertThat(manager.activeCall.value).isNull()
383+
384+
val eventTimestamp = A_FAKE_TIMESTAMP
385+
// The call should not ring more than 30 seconds after the initial event was sent
386+
val expirationTimestamp = eventTimestamp + 30_000
387+
388+
val callNotificationData = aCallNotificationData(
389+
timestamp = eventTimestamp,
390+
expirationTimestamp = expirationTimestamp,
391+
)
392+
393+
// suppose it took 10s to be notified
394+
clock.epochMillisResult = eventTimestamp + 10_000
395+
manager.registerIncomingCall(callNotificationData)
396+
397+
assertThat(manager.activeCall.value).isEqualTo(
398+
ActiveCall(
399+
callType = CallType.RoomCall(
400+
sessionId = callNotificationData.sessionId,
401+
roomId = callNotificationData.roomId,
402+
),
403+
callState = CallState.Ringing(callNotificationData)
404+
)
405+
)
406+
407+
runCurrent()
408+
409+
assertThat(manager.activeWakeLock?.isHeld).isTrue()
410+
verify { notificationManagerCompat.notify(notificationId, any()) }
411+
412+
// advance by 21s it should have stopped ringing
413+
advanceTimeBy(21_000)
414+
runCurrent()
415+
416+
verify { notificationManagerCompat.cancel(any()) }
417+
}
418+
@OptIn(ExperimentalCoroutinesApi::class)
419+
@Test
420+
fun `IncomingCall - ignore expired ring lifetime`() = runTest {
421+
setupShadowPowerManager()
422+
val notificationManagerCompat = mockk<NotificationManagerCompat>(relaxed = true)
423+
val clock = FakeSystemClock()
424+
val manager = createActiveCallManager(notificationManagerCompat = notificationManagerCompat, systemClock = clock)
425+
426+
assertThat(manager.activeWakeLock?.isHeld).isFalse()
427+
assertThat(manager.activeCall.value).isNull()
428+
429+
val eventTimestamp = A_FAKE_TIMESTAMP
430+
// The call should not ring more than 30 seconds after the initial event was sent
431+
val expirationTimestamp = eventTimestamp + 30_000
432+
433+
val callNotificationData = aCallNotificationData(
434+
timestamp = eventTimestamp,
435+
expirationTimestamp = expirationTimestamp,
436+
)
437+
438+
// suppose it took 35s to be notified
439+
clock.epochMillisResult = eventTimestamp + 35_000
440+
manager.registerIncomingCall(callNotificationData)
441+
442+
assertThat(manager.activeCall.value).isNull()
443+
444+
runCurrent()
445+
446+
assertThat(manager.activeWakeLock?.isHeld).isFalse()
447+
verify(exactly = 0) { notificationManagerCompat.notify(notificationId, any()) }
448+
}
449+
371450
private fun setupShadowPowerManager() {
372451
shadowOf(InstrumentationRegistry.getInstrumentation().targetContext.getSystemService<PowerManager>()).apply {
373452
setIsWakeLockLevelSupported(PowerManager.PARTIAL_WAKE_LOCK, true)
@@ -378,6 +457,7 @@ class DefaultActiveCallManagerTest {
378457
matrixClientProvider: FakeMatrixClientProvider = FakeMatrixClientProvider(),
379458
onMissedCallNotificationHandler: FakeOnMissedCallNotificationHandler = FakeOnMissedCallNotificationHandler(),
380459
notificationManagerCompat: NotificationManagerCompat = mockk(relaxed = true),
460+
systemClock: FakeSystemClock = FakeSystemClock(),
381461
) = DefaultActiveCallManager(
382462
context = InstrumentationRegistry.getInstrumentation().targetContext,
383463
coroutineScope = backgroundScope,
@@ -393,5 +473,6 @@ class DefaultActiveCallManagerTest {
393473
defaultCurrentCallService = DefaultCurrentCallService(),
394474
appForegroundStateService = FakeAppForegroundStateService(),
395475
imageLoaderHolder = FakeImageLoaderHolder(),
476+
systemClock = systemClock,
396477
)
397478
}

0 commit comments

Comments
 (0)