Skip to content

Commit 59ef782

Browse files
authored
Merge pull request #5471 from element-hq/feature/bma/improveUnifiedPushTroubleshotTest
Improve current push provider test: give info about the distributor.
2 parents fab2639 + 7ce4b47 commit 59ef782

File tree

6 files changed

+165
-15
lines changed

6 files changed

+165
-15
lines changed

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/troubleshoot/CurrentPushProviderTest.kt

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
package io.element.android.libraries.push.impl.troubleshoot
99

10-
import dev.zacsweers.metro.AppScope
1110
import dev.zacsweers.metro.ContributesIntoSet
1211
import dev.zacsweers.metro.Inject
13-
import io.element.android.libraries.push.api.GetCurrentPushProvider
12+
import io.element.android.libraries.di.SessionScope
13+
import io.element.android.libraries.matrix.api.core.SessionId
14+
import io.element.android.libraries.push.api.PushService
1415
import io.element.android.libraries.push.impl.R
1516
import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTest
1617
import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestDelegate
@@ -19,10 +20,11 @@ import io.element.android.services.toolbox.api.strings.StringProvider
1920
import kotlinx.coroutines.CoroutineScope
2021
import kotlinx.coroutines.flow.StateFlow
2122

22-
@ContributesIntoSet(AppScope::class)
23+
@ContributesIntoSet(SessionScope::class)
2324
@Inject
2425
class CurrentPushProviderTest(
25-
private val getCurrentPushProvider: GetCurrentPushProvider,
26+
private val pushService: PushService,
27+
private val sessionId: SessionId,
2628
private val stringProvider: StringProvider,
2729
) : NotificationTroubleshootTest {
2830
override val order = 110
@@ -35,17 +37,55 @@ class CurrentPushProviderTest(
3537

3638
override suspend fun run(coroutineScope: CoroutineScope) {
3739
delegate.start()
38-
val provider = getCurrentPushProvider.getCurrentPushProvider()
39-
if (provider != null) {
40-
delegate.updateState(
41-
description = stringProvider.getString(R.string.troubleshoot_notifications_test_current_push_provider_success, provider),
42-
status = NotificationTroubleshootTestState.Status.Success
43-
)
44-
} else {
40+
val pushProvider = pushService.getCurrentPushProvider()
41+
if (pushProvider == null) {
4542
delegate.updateState(
4643
description = stringProvider.getString(R.string.troubleshoot_notifications_test_current_push_provider_failure),
4744
status = NotificationTroubleshootTestState.Status.Failure()
4845
)
46+
} else if (pushProvider.supportMultipleDistributors.not()) {
47+
delegate.updateState(
48+
description = stringProvider.getString(
49+
R.string.troubleshoot_notifications_test_current_push_provider_success,
50+
pushProvider.name
51+
),
52+
status = NotificationTroubleshootTestState.Status.Success
53+
)
54+
} else {
55+
val distributorValue = pushProvider.getCurrentDistributorValue(sessionId)
56+
if (distributorValue == null) {
57+
// No distributors configured
58+
delegate.updateState(
59+
description = stringProvider.getString(
60+
R.string.troubleshoot_notifications_test_current_push_provider_failure_no_distributor,
61+
pushProvider.name
62+
),
63+
status = NotificationTroubleshootTestState.Status.Failure(false)
64+
)
65+
} else {
66+
val distributor = pushProvider.getDistributors().find { it.value == distributorValue }
67+
if (distributor == null) {
68+
// Distributor has been uninstalled?
69+
delegate.updateState(
70+
description = stringProvider.getString(
71+
R.string.troubleshoot_notifications_test_current_push_provider_failure_distributor_not_found,
72+
pushProvider.name,
73+
distributorValue,
74+
distributorValue,
75+
),
76+
status = NotificationTroubleshootTestState.Status.Failure(false)
77+
)
78+
} else {
79+
delegate.updateState(
80+
description = stringProvider.getString(
81+
R.string.troubleshoot_notifications_test_current_push_provider_success_with_distributor,
82+
pushProvider.name,
83+
distributorValue,
84+
),
85+
status = NotificationTroubleshootTestState.Status.Success
86+
)
87+
}
88+
}
4989
}
5090
}
5191

libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/CurrentPushProviderTestTest.kt

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
package io.element.android.libraries.push.impl.troubleshoot
99

1010
import com.google.common.truth.Truth.assertThat
11-
import io.element.android.libraries.push.test.FakeGetCurrentPushProvider
11+
import io.element.android.libraries.matrix.test.A_SESSION_ID
12+
import io.element.android.libraries.push.test.FakePushService
13+
import io.element.android.libraries.pushproviders.api.Distributor
14+
import io.element.android.libraries.pushproviders.test.FakePushProvider
1215
import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState
1316
import io.element.android.libraries.troubleshoot.test.runAndTestState
1417
import io.element.android.services.toolbox.test.strings.FakeStringProvider
@@ -17,10 +20,18 @@ import org.junit.Test
1720

1821
class CurrentPushProviderTestTest {
1922
@Test
20-
fun `test CurrentPushProviderTest with a push provider`() = runTest {
23+
fun `test CurrentPushProviderTest with a push provider and a distributor`() = runTest {
2124
val sut = CurrentPushProviderTest(
22-
getCurrentPushProvider = FakeGetCurrentPushProvider("foo"),
25+
pushService = FakePushService(
26+
currentPushProvider = {
27+
FakePushProvider(
28+
name = "foo",
29+
currentDistributorValue = { "aDistributor" },
30+
)
31+
}
32+
),
2333
stringProvider = FakeStringProvider(),
34+
sessionId = A_SESSION_ID,
2435
)
2536
sut.runAndTestState {
2637
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true))
@@ -31,11 +42,86 @@ class CurrentPushProviderTestTest {
3142
}
3243
}
3344

45+
@Test
46+
fun `test CurrentPushProviderTest with a push provider supporting multiple distributors, distributor found`() = runTest {
47+
val sut = CurrentPushProviderTest(
48+
pushService = FakePushService(
49+
currentPushProvider = {
50+
FakePushProvider(
51+
name = "foo",
52+
currentDistributorValue = { "aDistributor" },
53+
supportMultipleDistributors = true,
54+
distributors = listOf(Distributor("aDistributor", "aDistributor"))
55+
)
56+
},
57+
),
58+
stringProvider = FakeStringProvider(),
59+
sessionId = A_SESSION_ID,
60+
)
61+
sut.runAndTestState {
62+
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true))
63+
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress)
64+
val lastItem = awaitItem()
65+
assertThat(lastItem.status).isEqualTo(NotificationTroubleshootTestState.Status.Success)
66+
assertThat(lastItem.description).contains("foo")
67+
}
68+
}
69+
70+
@Test
71+
fun `test CurrentPushProviderTest with a push provider supporting multiple distributors, no distributor`() = runTest {
72+
val sut = CurrentPushProviderTest(
73+
pushService = FakePushService(
74+
currentPushProvider = {
75+
FakePushProvider(
76+
name = "foo",
77+
currentDistributorValue = { null },
78+
supportMultipleDistributors = true,
79+
)
80+
},
81+
),
82+
stringProvider = FakeStringProvider(),
83+
sessionId = A_SESSION_ID,
84+
)
85+
sut.runAndTestState {
86+
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true))
87+
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress)
88+
val lastItem = awaitItem()
89+
assertThat(lastItem.status).isEqualTo(NotificationTroubleshootTestState.Status.Failure())
90+
}
91+
}
92+
93+
@Test
94+
fun `test CurrentPushProviderTest with a push provider supporting multiple distributors, distributor not found`() = runTest {
95+
val sut = CurrentPushProviderTest(
96+
pushService = FakePushService(
97+
currentPushProvider = {
98+
FakePushProvider(
99+
name = "foo",
100+
currentDistributorValue = { "aDistributor" },
101+
supportMultipleDistributors = true,
102+
distributors = emptyList()
103+
)
104+
},
105+
),
106+
stringProvider = FakeStringProvider(),
107+
sessionId = A_SESSION_ID,
108+
)
109+
sut.runAndTestState {
110+
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true))
111+
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress)
112+
val lastItem = awaitItem()
113+
assertThat(lastItem.status).isEqualTo(NotificationTroubleshootTestState.Status.Failure())
114+
}
115+
}
116+
34117
@Test
35118
fun `test CurrentPushProviderTest without push provider`() = runTest {
36119
val sut = CurrentPushProviderTest(
37-
getCurrentPushProvider = FakeGetCurrentPushProvider(null),
120+
pushService = FakePushService(
121+
currentPushProvider = { null },
122+
),
38123
stringProvider = FakeStringProvider(),
124+
sessionId = A_SESSION_ID,
39125
)
40126
sut.runAndTestState {
41127
assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true))

libraries/pushproviders/api/src/main/kotlin/io/element/android/libraries/pushproviders/api/PushProvider.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ interface PushProvider {
2424
*/
2525
val name: String
2626

27+
/**
28+
* true if the Push provider supports multiple distributors.
29+
*/
30+
val supportMultipleDistributors: Boolean
31+
2732
/**
2833
* Return the list of available distributors.
2934
*/
@@ -34,6 +39,11 @@ interface PushProvider {
3439
*/
3540
suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor): Result<Unit>
3641

42+
/**
43+
* Return the current distributor, or null if none.
44+
*/
45+
suspend fun getCurrentDistributorValue(sessionId: SessionId): String?
46+
3747
/**
3848
* Return the current distributor, or null if none.
3949
*/

libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebasePushProvider.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class FirebasePushProvider(
3232
) : PushProvider {
3333
override val index = FirebaseConfig.INDEX
3434
override val name = FirebaseConfig.NAME
35+
override val supportMultipleDistributors = false
3536

3637
override fun getDistributors(): List<Distributor> {
3738
return listOfNotNull(
@@ -54,6 +55,8 @@ class FirebasePushProvider(
5455
)
5556
}
5657

58+
override suspend fun getCurrentDistributorValue(sessionId: SessionId): String = firebaseDistributor.value
59+
5760
override suspend fun getCurrentDistributor(sessionId: SessionId) = firebaseDistributor
5861

5962
override suspend fun unregister(matrixClient: MatrixClient): Result<Unit> {

libraries/pushproviders/test/src/main/kotlin/io/element/android/libraries/pushproviders/test/FakePushProvider.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import io.element.android.tests.testutils.lambda.lambdaError
1717
class FakePushProvider(
1818
override val index: Int = 0,
1919
override val name: String = "aFakePushProvider",
20+
override val supportMultipleDistributors: Boolean = false,
2021
private val distributors: List<Distributor> = listOf(Distributor("aDistributorValue", "aDistributorName")),
22+
private val currentDistributorValue: () -> String? = { lambdaError() },
2123
private val currentDistributor: () -> Distributor? = { distributors.firstOrNull() },
2224
private val currentUserPushConfig: CurrentUserPushConfig? = null,
2325
private val registerWithResult: (MatrixClient, Distributor) -> Result<Unit> = { _, _ -> lambdaError() },
@@ -32,6 +34,10 @@ class FakePushProvider(
3234
return registerWithResult(matrixClient, distributor)
3335
}
3436

37+
override suspend fun getCurrentDistributorValue(sessionId: SessionId): String? {
38+
return currentDistributorValue()
39+
}
40+
3541
override suspend fun getCurrentDistributor(sessionId: SessionId): Distributor? {
3642
return currentDistributor()
3743
}

libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class UnifiedPushProvider(
2929
) : PushProvider {
3030
override val index = UnifiedPushConfig.INDEX
3131
override val name = UnifiedPushConfig.NAME
32+
override val supportMultipleDistributors = true
3233

3334
override fun getDistributors(): List<Distributor> {
3435
return unifiedPushDistributorProvider.getDistributors()
@@ -42,6 +43,10 @@ class UnifiedPushProvider(
4243
}
4344
}
4445

46+
override suspend fun getCurrentDistributorValue(sessionId: SessionId): String? {
47+
return unifiedPushStore.getDistributorValue(sessionId)
48+
}
49+
4550
override suspend fun getCurrentDistributor(sessionId: SessionId): Distributor? {
4651
val distributorValue = unifiedPushStore.getDistributorValue(sessionId)
4752
return getDistributors().find { it.value == distributorValue }

0 commit comments

Comments
 (0)