2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-03 23:15:31 +00:00

[fenix] Close https://github.com/mozilla-mobile/fenix/issues/20796: Additional fixes for intermittent failing test

Fixes for TrackingProtectionExceptionsInteractorTest to remove the lamba
mocking and test actual BrowserState changes instead (where possible).
This commit is contained in:
Jonathan Almeida 2021-08-16 12:56:31 -04:00 committed by mergify[bot]
parent d643af098a
commit 591d6744df

View File

@ -4,42 +4,83 @@
package org.mozilla.fenix.exceptions.trackingprotection
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.verify
import io.mockk.verifySequence
import mozilla.components.browser.state.selector.findTab
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.TrackingProtectionState
import mozilla.components.browser.state.state.createTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.content.blocking.TrackingProtectionException
import mozilla.components.concept.engine.content.blocking.TrackingProtectionExceptionStorage
import mozilla.components.feature.session.TrackingProtectionUseCases
import mozilla.components.support.test.libstate.ext.waitUntilIdle
import mozilla.components.support.test.middleware.CaptureActionsMiddleware
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.settings.SupportUtils
@RunWith(FenixRobolectricTestRunner::class)
class TrackingProtectionExceptionsInteractorTest {
@MockK(relaxed = true) private lateinit var activity: HomeActivity
private lateinit var interactor: TrackingProtectionExceptionsInteractor
private val results: List<TrackingProtectionException> = emptyList()
private val activity: HomeActivity = mockk(relaxed = true)
private val engine: Engine = mockk(relaxed = true)
private val store = BrowserStore()
private val capture = CaptureActionsMiddleware<ExceptionsFragmentState, ExceptionsFragmentAction>()
private val results: List<TrackingProtectionException> = emptyList()
private val store = BrowserStore(
BrowserState(
tabs = listOf(
// Using copy to add TP state because `createTab` doesn't support it right now.
createTab("https://mozilla.org", false, "tab1")
.copy(trackingProtection = TrackingProtectionState(ignoredOnTrackingProtection = true)),
createTab("https://firefox.com", false, "tab2")
.copy(trackingProtection = TrackingProtectionState(ignoredOnTrackingProtection = true))
)
)
)
private val capture =
CaptureActionsMiddleware<ExceptionsFragmentState, ExceptionsFragmentAction>()
private val exceptionsStore = ExceptionsFragmentStore(middlewares = listOf(capture))
private val trackingProtectionUseCases = TrackingProtectionUseCases(store, engine)
private val trackingStorage: TrackingProtectionExceptionStorage = mockk(relaxed = true)
private val trackingStorage: TrackingProtectionExceptionStorage =
object : TrackingProtectionExceptionStorage {
override fun fetchAll(onResult: (List<TrackingProtectionException>) -> Unit) {
fetchedAll = true
onResult(results)
}
override fun removeAll(activeSessions: List<EngineSession>?) {
removedAll = true
}
override fun add(session: EngineSession) = Unit
override fun contains(session: EngineSession, onResult: (Boolean) -> Unit) = Unit
override fun remove(session: EngineSession) = Unit
override fun remove(exception: TrackingProtectionException) = Unit
override fun restore() = Unit
}
private val exceptionsItem: (String) -> TrackingProtectionException = {
object : TrackingProtectionException {
override val url = it
}
}
private var fetchedAll: Boolean = false
private var removedAll: Boolean = false
@Before
fun setup() {
MockKAnnotations.init(this)
interactor = DefaultTrackingProtectionExceptionsInteractor(
activity = activity,
exceptionsStore = exceptionsStore,
@ -47,9 +88,10 @@ class TrackingProtectionExceptionsInteractorTest {
)
every { engine.trackingProtectionExceptionStore } returns trackingStorage
every { trackingStorage.fetchAll(any()) } answers {
firstArg<(List<TrackingProtectionException>) -> Unit>()(results)
}
// Re-setting boolean checks in case they are not re-initialized per test run.
fetchedAll = false
removedAll = false
}
@Test
@ -67,14 +109,13 @@ class TrackingProtectionExceptionsInteractorTest {
)
}
}
@Test
fun onDeleteAll() {
interactor.onDeleteAll()
verifySequence {
trackingStorage.removeAll(any())
trackingStorage.fetchAll(any())
}
assertTrue(removedAll)
assertTrue(fetchedAll)
exceptionsStore.waitUntilIdle()
@ -85,18 +126,21 @@ class TrackingProtectionExceptionsInteractorTest {
@Test
fun onDeleteOne() {
val exceptionsItem = mockk<TrackingProtectionException>()
interactor.onDeleteOne(exceptionsItem)
interactor.onDeleteOne(exceptionsItem.invoke("https://mozilla.org"))
verifySequence {
trackingStorage.remove(exceptionsItem)
trackingStorage.fetchAll(any())
}
assertTrue(fetchedAll)
exceptionsStore.waitUntilIdle()
store.waitUntilIdle()
capture.assertLastAction(ExceptionsFragmentAction.Change::class) {
assertEquals(results, it.list)
}
val tab = store.state.findTab("tab1")!!
assertFalse(tab.trackingProtection.ignoredOnTrackingProtection)
val tab2 = store.state.findTab("tab2")!!
assertTrue(tab2.trackingProtection.ignoredOnTrackingProtection)
}
}