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