Using use cases inside component for endpoint testing

This commit is contained in:
Maxime NATUREL 2022-11-29 17:28:48 +01:00
parent 58efe90f7d
commit b29191e892

View file

@ -17,14 +17,17 @@
package im.vector.app.features.settings.troubleshoot package im.vector.app.features.settings.troubleshoot
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer
import androidx.work.WorkInfo import androidx.work.WorkInfo
import androidx.work.WorkManager import androidx.work.WorkManager
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.pushers.PushersManager import im.vector.app.core.pushers.PushersManager
import im.vector.app.core.pushers.RegisterUnifiedPushUseCase
import im.vector.app.core.pushers.UnifiedPushHelper import im.vector.app.core.pushers.UnifiedPushHelper
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
import im.vector.app.features.session.coroutineScope
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.pushers.PusherState import org.matrix.android.sdk.api.session.pushers.PusherState
import javax.inject.Inject import javax.inject.Inject
@ -34,6 +37,8 @@ class TestEndpointAsTokenRegistration @Inject constructor(
private val pushersManager: PushersManager, private val pushersManager: PushersManager,
private val activeSessionHolder: ActiveSessionHolder, private val activeSessionHolder: ActiveSessionHolder,
private val unifiedPushHelper: UnifiedPushHelper, private val unifiedPushHelper: UnifiedPushHelper,
private val registerUnifiedPushUseCase: RegisterUnifiedPushUseCase,
private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase,
) : TroubleshootTest(R.string.settings_troubleshoot_test_endpoint_registration_title) { ) : TroubleshootTest(R.string.settings_troubleshoot_test_endpoint_registration_title) {
override fun perform(testParameters: TestParameters) { override fun perform(testParameters: TestParameters) {
@ -56,12 +61,34 @@ class TestEndpointAsTokenRegistration @Inject constructor(
) )
quickFix = object : TroubleshootQuickFix(R.string.settings_troubleshoot_test_endpoint_registration_quick_fix) { quickFix = object : TroubleshootQuickFix(R.string.settings_troubleshoot_test_endpoint_registration_quick_fix) {
override fun doFix() { override fun doFix() {
unifiedPushHelper.forceRegister( unregisterThenRegister(testParameters, endpoint)
context, }
pushersManager }
) status = TestStatus.FAILED
val workId = pushersManager.enqueueRegisterPusherWithFcmKey(endpoint) } else {
WorkManager.getInstance(context).getWorkInfoByIdLiveData(workId).observe(context, Observer { workInfo -> description = stringProvider.getString(R.string.settings_troubleshoot_test_endpoint_registration_success)
status = TestStatus.SUCCESS
}
}
private fun unregisterThenRegister(testParameters: TestParameters, pushKey: String) {
activeSessionHolder.getSafeActiveSession()?.coroutineScope?.launch {
unregisterUnifiedPushUseCase.execute(pushersManager)
registerUnifiedPush(distributor = "", testParameters, pushKey)
}
}
private fun registerUnifiedPush(
distributor: String,
testParameters: TestParameters,
pushKey: String,
) {
when (val result = registerUnifiedPushUseCase.execute(distributor)) {
is RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.NeedToAskUserForDistributor ->
askUserForDistributor(result.distributors, testParameters, pushKey)
RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success -> {
val workId = pushersManager.enqueueRegisterPusherWithFcmKey(pushKey)
WorkManager.getInstance(context).getWorkInfoByIdLiveData(workId).observe(context) { workInfo ->
if (workInfo != null) { if (workInfo != null) {
if (workInfo.state == WorkInfo.State.SUCCEEDED) { if (workInfo.state == WorkInfo.State.SUCCEEDED) {
manager?.retry(testParameters) manager?.retry(testParameters)
@ -69,14 +96,18 @@ class TestEndpointAsTokenRegistration @Inject constructor(
manager?.retry(testParameters) manager?.retry(testParameters)
} }
} }
}) }
}
} }
} }
status = TestStatus.FAILED private fun askUserForDistributor(
} else { distributors: List<String>,
description = stringProvider.getString(R.string.settings_troubleshoot_test_endpoint_registration_success) testParameters: TestParameters,
status = TestStatus.SUCCESS pushKey: String,
) {
unifiedPushHelper.showSelectDistributorDialog(context, distributors) { selection ->
registerUnifiedPush(distributor = selection, testParameters, pushKey)
} }
} }
} }