diff --git a/app/src/main/java/org/mozilla/fenix/crashes/CrashReporterController.kt b/app/src/main/java/org/mozilla/fenix/crashes/CrashReporterController.kt index 3a18fa07a..795101d2b 100644 --- a/app/src/main/java/org/mozilla/fenix/crashes/CrashReporterController.kt +++ b/app/src/main/java/org/mozilla/fenix/crashes/CrashReporterController.kt @@ -5,6 +5,10 @@ package org.mozilla.fenix.crashes import androidx.navigation.NavController +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch import mozilla.components.browser.session.Session import mozilla.components.lib.crash.Crash import org.mozilla.fenix.R @@ -29,22 +33,25 @@ class CrashReporterController( * Closes the crash reporter fragment and tries to recover the session. * * @param sendCrash If true, submit a crash report. + * @return Job if report is submitted through an IO thread, null otherwise */ - fun handleCloseAndRestore(sendCrash: Boolean) { - submitReportIfNecessary(sendCrash) + fun handleCloseAndRestore(sendCrash: Boolean): Job? { + val job = submitReportIfNecessary(sendCrash) components.useCases.sessionUseCases.crashRecovery.invoke() navController.popBackStack() + return job } /** * Closes the crash reporter fragment and the tab. * * @param sendCrash If true, submit a crash report. + * @return Job if report is submitted through an IO thread, null otherwise */ - fun handleCloseAndRemove(sendCrash: Boolean) { - session ?: return - submitReportIfNecessary(sendCrash) + fun handleCloseAndRemove(sendCrash: Boolean): Job? { + session ?: return null + val job = submitReportIfNecessary(sendCrash) components.useCases.tabsUseCases.removeTab(session) components.useCases.sessionUseCases.crashRecovery.invoke() @@ -52,19 +59,28 @@ class CrashReporterController( R.id.crashReporterFragment, CrashReporterFragmentDirections.actionCrashReporterFragmentToHomeFragment() ) + + return job } /** * Submits the crash report if the "Send crash" checkbox was checked and the setting is enabled. + * + * @param sendCrash If true, submit a crash report. + * @return Job if report is submitted through an IO thread, null otherwise */ - private fun submitReportIfNecessary(sendCrash: Boolean) { + private fun submitReportIfNecessary(sendCrash: Boolean): Job? { + var job: Job? = null val didSubmitReport = if (sendCrash && settings.isCrashReportingEnabled) { - components.analytics.crashReporter.submitReport(crash) + job = GlobalScope.launch(Dispatchers.IO) { + components.analytics.crashReporter.submitReport(crash) + } true } else { false } components.analytics.metrics.track(Event.CrashReporterClosed(didSubmitReport)) + return job } } diff --git a/app/src/test/java/org/mozilla/fenix/crashes/CrashReporterControllerTest.kt b/app/src/test/java/org/mozilla/fenix/crashes/CrashReporterControllerTest.kt index 0f3048d4a..e3dfaad30 100644 --- a/app/src/test/java/org/mozilla/fenix/crashes/CrashReporterControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/crashes/CrashReporterControllerTest.kt @@ -11,6 +11,7 @@ import io.mockk.mockk import io.mockk.verify import mozilla.components.browser.session.Session import mozilla.components.lib.crash.Crash +import mozilla.components.support.test.ext.joinBlocking import org.junit.Before import org.junit.Test import org.mozilla.fenix.R @@ -49,7 +50,7 @@ class CrashReporterControllerTest { @Test fun `handle close and restore tab`() { val controller = CrashReporterController(crash, session, navContoller, components, settings) - controller.handleCloseAndRestore(sendCrash = false) + controller.handleCloseAndRestore(sendCrash = false)?.joinBlocking() verify { components.analytics.metrics.track(Event.CrashReporterClosed(false)) } verify { components.useCases.sessionUseCases.crashRecovery.invoke() } @@ -59,7 +60,7 @@ class CrashReporterControllerTest { @Test fun `handle close and remove tab`() { val controller = CrashReporterController(crash, session, navContoller, components, settings) - controller.handleCloseAndRemove(sendCrash = false) + controller.handleCloseAndRemove(sendCrash = false)?.joinBlocking() verify { components.analytics.metrics.track(Event.CrashReporterClosed(false)) } verify { components.useCases.tabsUseCases.removeTab(session) } @@ -72,7 +73,7 @@ class CrashReporterControllerTest { every { settings.isCrashReportingEnabled } returns false val controller = CrashReporterController(crash, session, navContoller, components, settings) - controller.handleCloseAndRestore(sendCrash = true) + controller.handleCloseAndRestore(sendCrash = true)?.joinBlocking() verify { components.analytics.metrics.track(Event.CrashReporterClosed(false)) } } @@ -82,7 +83,7 @@ class CrashReporterControllerTest { every { settings.isCrashReportingEnabled } returns true val controller = CrashReporterController(crash, session, navContoller, components, settings) - controller.handleCloseAndRestore(sendCrash = true) + controller.handleCloseAndRestore(sendCrash = true)?.joinBlocking() verify { components.analytics.crashReporter.submitReport(crash) } verify { components.analytics.metrics.track(Event.CrashReporterClosed(true)) }