Fix test on VectorFirebaseMessagingService

This commit is contained in:
Benoit Marty 2024-05-22 18:44:25 +02:00
parent 9762962586
commit e07a9230d4
2 changed files with 10 additions and 7 deletions

View file

@ -22,7 +22,6 @@ import io.element.android.libraries.architecture.bindings
import io.element.android.libraries.core.log.logger.LoggerTag import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.pushproviders.api.PushHandler import io.element.android.libraries.pushproviders.api.PushHandler
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@ -33,8 +32,7 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
@Inject lateinit var firebaseNewTokenHandler: FirebaseNewTokenHandler @Inject lateinit var firebaseNewTokenHandler: FirebaseNewTokenHandler
@Inject lateinit var pushParser: FirebasePushParser @Inject lateinit var pushParser: FirebasePushParser
@Inject lateinit var pushHandler: PushHandler @Inject lateinit var pushHandler: PushHandler
@Inject lateinit var coroutineScope: CoroutineScope
private val coroutineScope = CoroutineScope(SupervisorJob())
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()

View file

@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
@file:OptIn(ExperimentalCoroutinesApi::class)
package io.element.android.libraries.pushproviders.firebase package io.element.android.libraries.pushproviders.firebase
import android.os.Bundle import android.os.Bundle
@ -26,8 +28,10 @@ import io.element.android.libraries.pushproviders.api.PushData
import io.element.android.libraries.pushproviders.api.PushHandler import io.element.android.libraries.pushproviders.api.PushHandler
import io.element.android.tests.testutils.lambda.lambdaRecorder import io.element.android.tests.testutils.lambda.lambdaRecorder
import io.element.android.tests.testutils.lambda.value import io.element.android.tests.testutils.lambda.value
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.Ignore
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner import org.robolectric.RobolectricTestRunner
@ -43,7 +47,6 @@ class VectorFirebaseMessagingServiceTest {
vectorFirebaseMessagingService.onMessageReceived(RemoteMessage(Bundle())) vectorFirebaseMessagingService.onMessageReceived(RemoteMessage(Bundle()))
} }
@Ignore("The test does not wait for the end of the coroutine.")
@Test @Test
fun `test receiving valid data`() = runTest { fun `test receiving valid data`() = runTest {
val lambda = lambdaRecorder<PushData, Unit> { } val lambda = lambdaRecorder<PushData, Unit> { }
@ -59,12 +62,12 @@ class VectorFirebaseMessagingServiceTest {
}, },
) )
) )
advanceUntilIdle()
lambda.assertions() lambda.assertions()
.isCalledOnce() .isCalledOnce()
.with(value(PushData(AN_EVENT_ID, A_ROOM_ID, null, A_SECRET))) .with(value(PushData(AN_EVENT_ID, A_ROOM_ID, null, A_SECRET)))
} }
@Ignore("The test does not wait for the end of the coroutine.")
@Test @Test
fun `test new token is forwarded to the handler`() = runTest { fun `test new token is forwarded to the handler`() = runTest {
val lambda = lambdaRecorder<String, Unit> { } val lambda = lambdaRecorder<String, Unit> { }
@ -72,12 +75,13 @@ class VectorFirebaseMessagingServiceTest {
firebaseNewTokenHandler = FakeFirebaseNewTokenHandler(handleResult = lambda) firebaseNewTokenHandler = FakeFirebaseNewTokenHandler(handleResult = lambda)
) )
vectorFirebaseMessagingService.onNewToken("aToken") vectorFirebaseMessagingService.onNewToken("aToken")
advanceUntilIdle()
lambda.assertions() lambda.assertions()
.isCalledOnce() .isCalledOnce()
.with(value("aToken")) .with(value("aToken"))
} }
private fun createVectorFirebaseMessagingService( private fun TestScope.createVectorFirebaseMessagingService(
firebaseNewTokenHandler: FirebaseNewTokenHandler = FakeFirebaseNewTokenHandler(), firebaseNewTokenHandler: FirebaseNewTokenHandler = FakeFirebaseNewTokenHandler(),
pushHandler: PushHandler = FakePushHandler(), pushHandler: PushHandler = FakePushHandler(),
): VectorFirebaseMessagingService { ): VectorFirebaseMessagingService {
@ -85,6 +89,7 @@ class VectorFirebaseMessagingServiceTest {
this.firebaseNewTokenHandler = firebaseNewTokenHandler this.firebaseNewTokenHandler = firebaseNewTokenHandler
this.pushParser = FirebasePushParser() this.pushParser = FirebasePushParser()
this.pushHandler = pushHandler this.pushHandler = pushHandler
this.coroutineScope = this@createVectorFirebaseMessagingService
} }
} }
} }