Commit 1d5f74d8 authored by Christian Sadilek's avatar Christian Sadilek
Browse files

Closes #16153: Intermittent failures of ShareControllerTest.handleShareToApp

parent 0d37c8f2
......@@ -16,10 +16,11 @@ import android.net.Uri
import androidx.annotation.VisibleForTesting
import androidx.navigation.NavController
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import mozilla.components.concept.engine.prompt.ShareData
import mozilla.components.concept.sync.Device
......@@ -74,6 +75,7 @@ class DefaultShareController(
private val navController: NavController,
private val recentAppsStorage: RecentAppsStorage,
private val viewLifecycleScope: CoroutineScope,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO,
private val dismiss: (ShareController.Result) -> Unit
) : ShareController {
......@@ -88,7 +90,7 @@ class DefaultShareController(
}
override fun handleShareToApp(app: AppShareOption) {
viewLifecycleScope.launch(Dispatchers.IO) {
viewLifecycleScope.launch(dispatcher) {
recentAppsStorage.updateRecentApp(app.activityName)
}
......
......@@ -20,6 +20,7 @@ import io.mockk.verify
import io.mockk.verifyOrder
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import mozilla.components.concept.engine.prompt.ShareData
import mozilla.components.concept.sync.Device
......@@ -28,11 +29,13 @@ import mozilla.components.concept.sync.TabData
import mozilla.components.feature.accounts.push.SendTabUseCases
import mozilla.components.feature.share.RecentAppsStorage
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
......@@ -62,6 +65,7 @@ class ShareControllerTest {
TabData("title1", "url1")
)
private val textToShare = "${shareData[0].url}\n\n${shareData[1].url}"
private val testDispatcher = TestCoroutineDispatcher()
private val testCoroutineScope = TestCoroutineScope()
private val sendTabUseCases = mockk<SendTabUseCases>(relaxed = true)
private val snackbar = mockk<FenixSnackbar>(relaxed = true)
......@@ -70,9 +74,12 @@ class ShareControllerTest {
private val recentAppStorage = mockk<RecentAppsStorage>(relaxed = true)
private val controller = DefaultShareController(
context, shareSubject, shareData, sendTabUseCases, snackbar, navController,
recentAppStorage, testCoroutineScope, dismiss
recentAppStorage, testCoroutineScope, testDispatcher, dismiss
)
@get:Rule
val coroutinesTestRule = MainCoroutineRule(testDispatcher)
@Before
fun setUp() {
every { context.metrics } returns metrics
......@@ -102,12 +109,13 @@ class ShareControllerTest {
val activityContext: Context = mockk<Activity>()
val testController = DefaultShareController(
activityContext, shareSubject, shareData, mockk(),
mockk(), mockk(), recentAppStorage, testCoroutineScope, dismiss
mockk(), mockk(), recentAppStorage, testCoroutineScope, testDispatcher, dismiss
)
every { activityContext.startActivity(capture(shareIntent)) } just Runs
every { recentAppStorage.updateRecentApp(appShareOption.activityName) } just Runs
testController.handleShareToApp(appShareOption)
testDispatcher.advanceUntilIdle()
// Check that the Intent used for querying apps has the expected structure
assertTrue(shareIntent.isCaptured)
......@@ -138,7 +146,7 @@ class ShareControllerTest {
val activityContext: Context = mockk<Activity>()
val testController = DefaultShareController(
activityContext, shareSubject, shareData, mockk(),
snackbar, mockk(), recentAppStorage, testCoroutineScope, dismiss
snackbar, mockk(), recentAppStorage, testCoroutineScope, testDispatcher, dismiss
)
every { recentAppStorage.updateRecentApp(appShareOption.activityName) } just Runs
every { activityContext.startActivity(capture(shareIntent)) } throws SecurityException()
......@@ -166,7 +174,7 @@ class ShareControllerTest {
val activityContext: Context = mockk<Activity>()
val testController = DefaultShareController(
activityContext, shareSubject, shareData, mockk(),
snackbar, mockk(), recentAppStorage, testCoroutineScope, dismiss
snackbar, mockk(), recentAppStorage, testCoroutineScope, testDispatcher, dismiss
)
every { recentAppStorage.updateRecentApp(appShareOption.activityName) } just Runs
every { activityContext.startActivity(capture(shareIntent)) } throws ActivityNotFoundException()
......@@ -316,6 +324,7 @@ class ShareControllerTest {
mockk(),
mockk(),
mockk(),
mockk(),
mockk()
)
val controllerWithMoreSharedTabs = controller
......@@ -344,7 +353,7 @@ class ShareControllerTest {
)
val controller = DefaultShareController(
context, shareSubject, shareData, sendTabUseCases, snackbar, navController,
recentAppStorage, testCoroutineScope, dismiss
recentAppStorage, testCoroutineScope, testDispatcher, dismiss
)
val expectedShareText = "${shareData[0].url}\n\nurl0\n\n${shareData[2].url}"
......@@ -360,7 +369,7 @@ class ShareControllerTest {
fun `getShareSubject will return a concatenation of tab titles if "shareSubject" is null`() {
val controller = DefaultShareController(
context, null, shareData, sendTabUseCases, snackbar, navController,
recentAppStorage, testCoroutineScope, dismiss
recentAppStorage, testCoroutineScope, testDispatcher, dismiss
)
assertEquals("title0, title1", controller.getShareSubject())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment