diff --git a/build.gradle.kts b/build.gradle.kts index b4ebcf6268..7171d5b079 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -93,6 +93,8 @@ allprojects { // Fix compilation warning for annotations // See https://youtrack.jetbrains.com/issue/KT-73255/Change-defaulting-rule-for-annotations for more details freeCompilerArgs.add("-Xannotation-default-target=first-only") + // Opt-in to context receivers + freeCompilerArgs.add("-Xcontext-parameters") } } } diff --git a/libraries/permissions/impl/src/test/kotlin/io/element/android/libraries/permissions/impl/troubleshoot/NotificationTroubleshootCheckPermissionTestTest.kt b/libraries/permissions/impl/src/test/kotlin/io/element/android/libraries/permissions/impl/troubleshoot/NotificationTroubleshootCheckPermissionTestTest.kt index cbd7c2b8c3..41a7260ca2 100644 --- a/libraries/permissions/impl/src/test/kotlin/io/element/android/libraries/permissions/impl/troubleshoot/NotificationTroubleshootCheckPermissionTestTest.kt +++ b/libraries/permissions/impl/src/test/kotlin/io/element/android/libraries/permissions/impl/troubleshoot/NotificationTroubleshootCheckPermissionTestTest.kt @@ -8,12 +8,12 @@ package io.element.android.libraries.permissions.impl.troubleshoot import android.os.Build -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.permissions.impl.action.FakePermissionActions import io.element.android.libraries.permissions.test.FakePermissionStateProvider import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState import io.element.android.libraries.troubleshoot.test.FakeNotificationTroubleshootNavigator +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.services.toolbox.test.sdk.FakeBuildVersionSdkIntProvider import io.element.android.services.toolbox.test.strings.FakeStringProvider import kotlinx.coroutines.launch @@ -29,10 +29,7 @@ class NotificationTroubleshootCheckPermissionTestTest { permissionActions = FakePermissionActions(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -48,10 +45,7 @@ class NotificationTroubleshootCheckPermissionTestTest { permissionActions = FakePermissionActions(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -75,16 +69,13 @@ class NotificationTroubleshootCheckPermissionTestTest { permissionActions = actions, stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() assertThat(lastItem.status).isEqualTo(NotificationTroubleshootTestState.Status.Failure(hasQuickFix = true)) // Quick fix - launch { + backgroundScope.launch { sut.quickFix(this, FakeNotificationTroubleshootNavigator()) // Run the test again (IRL it will be done thanks to the resuming of the application) sut.run(this) @@ -110,10 +101,7 @@ class NotificationTroubleshootCheckPermissionTestTest { permissionActions = actions, stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Failure(hasQuickFix = true)) diff --git a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/CurrentPushProviderTestTest.kt b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/CurrentPushProviderTestTest.kt index f45a89ff72..95bfced7de 100644 --- a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/CurrentPushProviderTestTest.kt +++ b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/CurrentPushProviderTestTest.kt @@ -7,12 +7,11 @@ package io.element.android.libraries.push.impl.troubleshoot -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.push.test.FakeGetCurrentPushProvider import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.services.toolbox.test.strings.FakeStringProvider -import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest import org.junit.Test @@ -23,10 +22,7 @@ class CurrentPushProviderTestTest { getCurrentPushProvider = FakeGetCurrentPushProvider("foo"), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -41,10 +37,7 @@ class CurrentPushProviderTestTest { getCurrentPushProvider = FakeGetCurrentPushProvider(null), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() diff --git a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/IgnoredUsersTestTest.kt b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/IgnoredUsersTestTest.kt index ed1cf93fce..e38baa7321 100644 --- a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/IgnoredUsersTestTest.kt +++ b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/IgnoredUsersTestTest.kt @@ -7,18 +7,17 @@ package io.element.android.libraries.push.impl.troubleshoot -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.matrix.test.A_USER_ID import io.element.android.libraries.matrix.test.A_USER_ID_2 import io.element.android.libraries.matrix.test.FakeMatrixClient import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootNavigator import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.services.toolbox.test.strings.FakeStringProvider import io.element.android.tests.testutils.lambda.lambdaRecorder import kotlinx.collections.immutable.persistentListOf import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest import org.junit.Test @@ -57,10 +56,7 @@ class IgnoredUsersTestTest { ), stringProvider = FakeStringProvider(), ) - backgroundScope.launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -76,10 +72,7 @@ class IgnoredUsersTestTest { ), stringProvider = FakeStringProvider(), ) - backgroundScope.launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() diff --git a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/NotificationTestTest.kt b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/NotificationTestTest.kt index 2c0dbeff10..eecb3801fd 100644 --- a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/NotificationTestTest.kt +++ b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/NotificationTestTest.kt @@ -7,14 +7,13 @@ package io.element.android.libraries.push.impl.troubleshoot -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.push.impl.notifications.fake.FakeNotificationCreator import io.element.android.libraries.push.impl.notifications.fake.FakeNotificationDisplayer import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.services.toolbox.test.strings.FakeStringProvider import io.element.android.tests.testutils.lambda.lambdaRecorder -import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest import org.junit.Test @@ -31,10 +30,7 @@ class NotificationTestTest { fun `test NotificationTest notification cannot be displayed`() = runTest { fakeNotificationDisplayer.displayDiagnosticNotificationResult = lambdaRecorder { _ -> false } val sut = createNotificationTest() - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) assertThat(awaitItem().status).isInstanceOf(NotificationTroubleshootTestState.Status.Failure::class.java) @@ -44,10 +40,7 @@ class NotificationTestTest { @Test fun `test NotificationTest user does not click on notification`() = runTest { val sut = createNotificationTest() - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.WaitingForUser) @@ -60,10 +53,7 @@ class NotificationTestTest { @Test fun `test NotificationTest user clicks on notification`() = runTest { val sut = createNotificationTest() - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.WaitingForUser) diff --git a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/PushLoopbackTestTest.kt b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/PushLoopbackTestTest.kt index e32537e748..f06b6a9e1b 100644 --- a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/PushLoopbackTestTest.kt +++ b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/PushLoopbackTestTest.kt @@ -7,7 +7,6 @@ package io.element.android.libraries.push.impl.troubleshoot -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.matrix.test.AN_EXCEPTION import io.element.android.libraries.matrix.test.A_FAILURE_REASON @@ -16,10 +15,10 @@ import io.element.android.libraries.push.test.FakePushService import io.element.android.libraries.pushproviders.test.FakePushProvider import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState import io.element.android.libraries.troubleshoot.test.FakeNotificationTroubleshootNavigator +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.services.toolbox.test.strings.FakeStringProvider import io.element.android.services.toolbox.test.systemclock.FakeSystemClock import io.element.android.tests.testutils.lambda.lambdaRecorder -import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest import org.junit.Test @@ -33,10 +32,7 @@ class PushLoopbackTestTest { clock = FakeSystemClock(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -57,10 +53,7 @@ class PushLoopbackTestTest { clock = FakeSystemClock(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -90,10 +83,7 @@ class PushLoopbackTestTest { clock = FakeSystemClock(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -116,10 +106,7 @@ class PushLoopbackTestTest { clock = FakeSystemClock(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -140,10 +127,7 @@ class PushLoopbackTestTest { clock = FakeSystemClock(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -164,10 +148,7 @@ class PushLoopbackTestTest { clock = FakeSystemClock(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() diff --git a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/PushProvidersTestTest.kt b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/PushProvidersTestTest.kt index b768e68437..b274f0bdab 100644 --- a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/PushProvidersTestTest.kt +++ b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/troubleshoot/PushProvidersTestTest.kt @@ -7,12 +7,11 @@ package io.element.android.libraries.push.impl.troubleshoot -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.pushproviders.test.FakePushProvider import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.services.toolbox.test.strings.FakeStringProvider -import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest import org.junit.Test @@ -23,10 +22,7 @@ class PushProvidersTestTest { pushProviders = emptySet(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -45,10 +41,7 @@ class PushProvidersTestTest { ), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(true)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() diff --git a/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/troubleshoot/FirebaseAvailabilityTestTest.kt b/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/troubleshoot/FirebaseAvailabilityTestTest.kt index acb917b60f..ce617799f5 100644 --- a/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/troubleshoot/FirebaseAvailabilityTestTest.kt +++ b/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/troubleshoot/FirebaseAvailabilityTestTest.kt @@ -7,14 +7,13 @@ package io.element.android.libraries.pushproviders.firebase.troubleshoot -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.pushproviders.firebase.FakeIsPlayServiceAvailable import io.element.android.libraries.pushproviders.firebase.FirebaseConfig import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState import io.element.android.libraries.troubleshoot.api.test.TestFilterData +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.services.toolbox.test.strings.FakeStringProvider -import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest import org.junit.Test @@ -25,10 +24,7 @@ class FirebaseAvailabilityTestTest { isPlayServiceAvailable = FakeIsPlayServiceAvailable(true), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -42,10 +38,7 @@ class FirebaseAvailabilityTestTest { isPlayServiceAvailable = FakeIsPlayServiceAvailable(false), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() diff --git a/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/troubleshoot/FirebaseTokenTestTest.kt b/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/troubleshoot/FirebaseTokenTestTest.kt index eb3ae403fb..7e6981ef57 100644 --- a/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/troubleshoot/FirebaseTokenTestTest.kt +++ b/libraries/pushproviders/firebase/src/test/kotlin/io/element/android/libraries/pushproviders/firebase/troubleshoot/FirebaseTokenTestTest.kt @@ -7,7 +7,6 @@ package io.element.android.libraries.pushproviders.firebase.troubleshoot -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.pushproviders.firebase.FakeFirebaseTroubleshooter import io.element.android.libraries.pushproviders.firebase.FirebaseConfig @@ -15,8 +14,8 @@ import io.element.android.libraries.pushproviders.firebase.InMemoryFirebaseStore import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState import io.element.android.libraries.troubleshoot.api.test.TestFilterData import io.element.android.libraries.troubleshoot.test.FakeNotificationTroubleshootNavigator +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.services.toolbox.test.strings.FakeStringProvider -import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest import org.junit.Test @@ -28,10 +27,7 @@ class FirebaseTokenTestTest { firebaseTroubleshooter = FakeFirebaseTroubleshooter(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -54,10 +50,7 @@ class FirebaseTokenTestTest { ), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -82,10 +75,7 @@ class FirebaseTokenTestTest { ), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Failure(hasQuickFix = true)) diff --git a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/troubleshoot/UnifiedPushMatrixGatewayTestTest.kt b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/troubleshoot/UnifiedPushMatrixGatewayTestTest.kt index 9729e04eb1..f4a0cf5da4 100644 --- a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/troubleshoot/UnifiedPushMatrixGatewayTestTest.kt +++ b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/troubleshoot/UnifiedPushMatrixGatewayTestTest.kt @@ -7,7 +7,6 @@ package io.element.android.libraries.pushproviders.unifiedpush.troubleshoot -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig import io.element.android.libraries.pushproviders.test.aCurrentUserPushConfig @@ -18,8 +17,8 @@ import io.element.android.libraries.pushproviders.unifiedpush.matrixDiscoveryRes import io.element.android.libraries.pushproviders.unifiedpush.network.DiscoveryResponse import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState import io.element.android.libraries.troubleshoot.api.test.TestFilterData +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.tests.testutils.testCoroutineDispatchers -import kotlinx.coroutines.launch import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest import org.junit.Test @@ -31,10 +30,7 @@ class UnifiedPushMatrixGatewayTestTest { currentUserPushConfig = aCurrentUserPushConfig(), discoveryResponse = matrixDiscoveryResponse, ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -48,10 +44,7 @@ class UnifiedPushMatrixGatewayTestTest { currentUserPushConfig = null, discoveryResponse = matrixDiscoveryResponse, ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -65,10 +58,7 @@ class UnifiedPushMatrixGatewayTestTest { currentUserPushConfig = aCurrentUserPushConfig(), discoveryResponse = invalidDiscoveryResponse, ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -85,10 +75,7 @@ class UnifiedPushMatrixGatewayTestTest { currentUserPushConfig = aCurrentUserPushConfig(), discoveryResponse = { error("Network error") }, ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() diff --git a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/troubleshoot/UnifiedPushTestTest.kt b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/troubleshoot/UnifiedPushTestTest.kt index 257be14f6c..28ab0186da 100644 --- a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/troubleshoot/UnifiedPushTestTest.kt +++ b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/troubleshoot/UnifiedPushTestTest.kt @@ -7,13 +7,13 @@ package io.element.android.libraries.pushproviders.unifiedpush.troubleshoot -import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.pushproviders.api.Distributor import io.element.android.libraries.pushproviders.unifiedpush.UnifiedPushConfig import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState import io.element.android.libraries.troubleshoot.api.test.TestFilterData import io.element.android.libraries.troubleshoot.test.FakeNotificationTroubleshootNavigator +import io.element.android.libraries.troubleshoot.test.runAndTestState import io.element.android.services.toolbox.test.strings.FakeStringProvider import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest @@ -31,10 +31,7 @@ class UnifiedPushTestTest { openDistributorWebPageAction = FakeOpenDistributorWebPageAction(), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() @@ -58,16 +55,13 @@ class UnifiedPushTestTest { ), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() assertThat(lastItem.status).isEqualTo(NotificationTroubleshootTestState.Status.Failure(hasQuickFix = true)) // Quick fix - launch { + backgroundScope.launch { sut.quickFix(this, FakeNotificationTroubleshootNavigator()) sut.run(this) } @@ -92,10 +86,7 @@ class UnifiedPushTestTest { ), stringProvider = FakeStringProvider(), ) - launch { - sut.run(this) - } - sut.state.test { + sut.runAndTestState { assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.Idle(false)) assertThat(awaitItem().status).isEqualTo(NotificationTroubleshootTestState.Status.InProgress) val lastItem = awaitItem() diff --git a/libraries/troubleshoot/test/build.gradle.kts b/libraries/troubleshoot/test/build.gradle.kts index 664c4e4d1d..8321ea5b4f 100644 --- a/libraries/troubleshoot/test/build.gradle.kts +++ b/libraries/troubleshoot/test/build.gradle.kts @@ -15,4 +15,7 @@ android { dependencies { implementation(projects.libraries.troubleshoot.api) implementation(projects.tests.testutils) + implementation(libs.coroutines.test) + implementation(libs.test.core) + implementation(libs.test.turbine) } diff --git a/libraries/troubleshoot/test/src/main/kotlin/io/element/android/libraries/troubleshoot/test/Utils.kt b/libraries/troubleshoot/test/src/main/kotlin/io/element/android/libraries/troubleshoot/test/Utils.kt new file mode 100644 index 0000000000..2a8ce30f47 --- /dev/null +++ b/libraries/troubleshoot/test/src/main/kotlin/io/element/android/libraries/troubleshoot/test/Utils.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2025 New Vector Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial + * Please see LICENSE files in the repository root for full details. + */ + +package io.element.android.libraries.troubleshoot.test + +import app.cash.turbine.TurbineTestContext +import app.cash.turbine.test +import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTest +import io.element.android.libraries.troubleshoot.api.test.NotificationTroubleshootTestState +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.TestScope + +context(testScope: TestScope) +suspend fun NotificationTroubleshootTest.runAndTestState( + validate: suspend TurbineTestContext.() -> Unit, +) { + testScope.backgroundScope.launch { + run(this) + } + state.test(validate = validate) +}