2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-19 09:25:34 +00:00

[fenix] Closes https://github.com/mozilla-mobile/fenix/issues/20533: Fix intermittent failures of HistoryControllerTest

This commit is contained in:
Christian Sadilek 2021-07-28 10:54:47 -04:00 committed by mergify[bot]
parent d9faf84790
commit a29d89ef7a

View File

@ -41,14 +41,192 @@ class HistoryControllerTest {
private val resources: Resources = mockk(relaxed = true)
private val snackbar: FenixSnackbar = mockk(relaxed = true)
private val clipboardManager: ClipboardManager = mockk(relaxed = true)
private val openInBrowser: (HistoryItem) -> Unit = mockk(relaxed = true)
private val openAndShowTray: (HistoryItem, BrowsingMode) -> Unit = mockk(relaxed = true)
private val displayDeleteAll: () -> Unit = mockk(relaxed = true)
private val invalidateOptionsMenu: () -> Unit = mockk(relaxed = true)
private val deleteHistoryItems: (Set<HistoryItem>) -> Unit = mockk(relaxed = true)
private val syncHistory: suspend () -> Unit = mockk(relaxed = true)
private val metrics: MetricController = mockk(relaxed = true)
private val controller = DefaultHistoryController(
@Before
fun setUp() {
every { store.state } returns state
}
@After
fun cleanUp() {
scope.cleanupTestCoroutines()
}
@Test
fun onPressHistoryItemInNormalMode() {
var actualHistoryItem: HistoryItem? = null
val controller = createController(openInBrowser = {
actualHistoryItem = it
})
controller.handleOpen(historyItem)
assertEquals(historyItem, actualHistoryItem)
}
@Test
fun onOpenItemInNormalMode() {
var actualHistoryItem: HistoryItem? = null
var actualBrowsingMode: BrowsingMode? = null
val controller = createController(openAndShowTray = { historyItem, browsingMode ->
actualHistoryItem = historyItem
actualBrowsingMode = browsingMode
})
controller.handleOpenInNewTab(historyItem, BrowsingMode.Normal)
assertEquals(historyItem, actualHistoryItem)
assertEquals(BrowsingMode.Normal, actualBrowsingMode)
}
@Test
fun onOpenItemInPrivateMode() {
var actualHistoryItem: HistoryItem? = null
var actualBrowsingMode: BrowsingMode? = null
val controller = createController(openAndShowTray = { historyItem, browsingMode ->
actualHistoryItem = historyItem
actualBrowsingMode = browsingMode
})
controller.handleOpenInNewTab(historyItem, BrowsingMode.Private)
assertEquals(historyItem, actualHistoryItem)
assertEquals(BrowsingMode.Private, actualBrowsingMode)
}
@Test
fun onPressHistoryItemInEditMode() {
every { state.mode } returns HistoryFragmentState.Mode.Editing(setOf())
createController().handleSelect(historyItem)
verify {
store.dispatch(HistoryFragmentAction.AddItemForRemoval(historyItem))
}
}
@Test
fun onPressSelectedHistoryItemInEditMode() {
every { state.mode } returns HistoryFragmentState.Mode.Editing(setOf(historyItem))
createController().handleDeselect(historyItem)
verify {
store.dispatch(HistoryFragmentAction.RemoveItemForRemoval(historyItem))
}
}
@Test
fun onSelectHistoryItemDuringSync() {
every { state.mode } returns HistoryFragmentState.Mode.Syncing
createController().handleSelect(historyItem)
verify(exactly = 0) {
store.dispatch(HistoryFragmentAction.AddItemForRemoval(historyItem))
}
}
@Test
fun onBackPressedInNormalMode() {
every { state.mode } returns HistoryFragmentState.Mode.Normal
assertFalse(createController().handleBackPressed())
}
@Test
fun onBackPressedInEditMode() {
every { state.mode } returns HistoryFragmentState.Mode.Editing(setOf())
assertTrue(createController().handleBackPressed())
verify {
store.dispatch(HistoryFragmentAction.ExitEditMode)
}
}
@Test
fun onModeSwitched() {
var invalidateOptionsMenuInvoked = false
val controller = createController(invalidateOptionsMenu = {
invalidateOptionsMenuInvoked = true
})
controller.handleModeSwitched()
assertTrue(invalidateOptionsMenuInvoked)
}
@Test
fun onDeleteAll() {
var displayDeleteAllInvoked = false
val controller = createController(displayDeleteAll = {
displayDeleteAllInvoked = true
})
controller.handleDeleteAll()
assertTrue(displayDeleteAllInvoked)
}
@Test
fun onDeleteSome() {
val itemsToDelete = setOf(historyItem)
var actualItems: Set<HistoryItem>? = null
val controller = createController(deleteHistoryItems = { items ->
actualItems = items
})
controller.handleDeleteSome(itemsToDelete)
assertEquals(itemsToDelete, actualItems)
}
@Test
fun onCopyItem() {
val clipdata = slot<ClipData>()
createController().handleCopyUrl(historyItem)
verify {
clipboardManager.setPrimaryClip(capture(clipdata))
snackbar.show()
}
assertEquals(1, clipdata.captured.itemCount)
assertEquals(historyItem.url, clipdata.captured.description.label)
assertEquals(historyItem.url, clipdata.captured.getItemAt(0).text)
}
@Test
@Suppress("UNCHECKED_CAST")
fun onShareItem() {
createController().handleShare(historyItem)
verify {
navController.navigate(directionsEq(
HistoryFragmentDirections.actionGlobalShareFragment(
data = arrayOf(ShareData(url = historyItem.url, title = historyItem.title))
)
))
}
}
@Test
fun onRequestSync() {
var syncHistoryInvoked = false
createController(syncHistory = {
syncHistoryInvoked = true
}).handleRequestSync()
coVerifyOrder {
store.dispatch(HistoryFragmentAction.StartSync)
store.dispatch(HistoryFragmentAction.FinishSync)
}
assertTrue(syncHistoryInvoked)
}
@Suppress("LongParameterList")
private fun createController(
openInBrowser: (HistoryItem) -> Unit = { _ -> },
openAndShowTray: (HistoryItem, BrowsingMode) -> Unit = { _, _ -> },
displayDeleteAll: () -> Unit = { },
invalidateOptionsMenu: () -> Unit = { },
deleteHistoryItems: (Set<HistoryItem>) -> Unit = { _ -> },
syncHistory: suspend () -> Unit = { }
): HistoryController {
return DefaultHistoryController(
store,
navController,
resources,
@ -63,164 +241,5 @@ class HistoryControllerTest {
syncHistory,
metrics
)
@Before
fun setUp() {
every { store.state } returns state
}
@After
fun cleanUp() {
scope.cleanupTestCoroutines()
}
@Test
fun onPressHistoryItemInNormalMode() {
controller.handleOpen(historyItem)
verify {
openInBrowser(historyItem)
}
}
@Test
fun onOpenItemInNormalMode() {
controller.handleOpenInNewTab(historyItem, BrowsingMode.Normal)
verify {
openAndShowTray(historyItem, BrowsingMode.Normal)
}
}
@Test
fun onOpenItemInPrivateMode() {
controller.handleOpenInNewTab(historyItem, BrowsingMode.Private)
verify {
openAndShowTray(historyItem, BrowsingMode.Private)
}
}
@Test
fun onPressHistoryItemInEditMode() {
every { state.mode } returns HistoryFragmentState.Mode.Editing(setOf())
controller.handleSelect(historyItem)
verify {
store.dispatch(HistoryFragmentAction.AddItemForRemoval(historyItem))
}
}
@Test
fun onPressSelectedHistoryItemInEditMode() {
every { state.mode } returns HistoryFragmentState.Mode.Editing(setOf(historyItem))
controller.handleDeselect(historyItem)
verify {
store.dispatch(HistoryFragmentAction.RemoveItemForRemoval(historyItem))
}
}
@Test
fun onSelectHistoryItemDuringSync() {
every { state.mode } returns HistoryFragmentState.Mode.Syncing
controller.handleSelect(historyItem)
verify(exactly = 0) {
store.dispatch(HistoryFragmentAction.AddItemForRemoval(historyItem))
}
}
@Test
fun onBackPressedInNormalMode() {
every { state.mode } returns HistoryFragmentState.Mode.Normal
assertFalse(controller.handleBackPressed())
}
@Test
fun onBackPressedInEditMode() {
every { state.mode } returns HistoryFragmentState.Mode.Editing(setOf())
assertTrue(controller.handleBackPressed())
verify {
store.dispatch(HistoryFragmentAction.ExitEditMode)
}
}
@Test
fun onModeSwitched() {
controller.handleModeSwitched()
verify {
invalidateOptionsMenu.invoke()
}
}
@Test
fun onDeleteAll() {
controller.handleDeleteAll()
verify {
displayDeleteAll.invoke()
}
}
@Test
fun onDeleteSome() {
val itemsToDelete = setOf(historyItem)
controller.handleDeleteSome(itemsToDelete)
verify {
deleteHistoryItems(itemsToDelete)
}
}
@Test
fun onCopyItem() {
val clipdata = slot<ClipData>()
controller.handleCopyUrl(historyItem)
verify {
clipboardManager.setPrimaryClip(capture(clipdata))
snackbar.show()
}
assertEquals(1, clipdata.captured.itemCount)
assertEquals(historyItem.url, clipdata.captured.description.label)
assertEquals(historyItem.url, clipdata.captured.getItemAt(0).text)
}
@Test
@Suppress("UNCHECKED_CAST")
fun onShareItem() {
controller.handleShare(historyItem)
verify {
navController.navigate(directionsEq(
HistoryFragmentDirections.actionGlobalShareFragment(
data = arrayOf(ShareData(url = historyItem.url, title = historyItem.title))
)
))
}
}
@Test
fun onRequestSync() {
controller.handleRequestSync()
verify(exactly = 2) {
store.dispatch(any())
}
coVerifyOrder {
store.dispatch(HistoryFragmentAction.StartSync)
syncHistory.invoke()
store.dispatch(HistoryFragmentAction.FinishSync)
}
}
}