Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Close #20796: Additional fixes for intermittent failing test
Browse files Browse the repository at this point in the history
Fixes for TrackingProtectionExceptionsInteractorTest to remove the lamba
mocking and test actual BrowserState changes instead (where possible).
  • Loading branch information
jonalmeida authored and mergify[bot] committed Aug 16, 2021
1 parent 293092f commit 0090381
Showing 1 changed file with 66 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,94 @@

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,
trackingProtectionUseCases = trackingProtectionUseCases
)

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
Expand All @@ -67,14 +109,13 @@ class TrackingProtectionExceptionsInteractorTest {
)
}
}

@Test
fun onDeleteAll() {
interactor.onDeleteAll()

verifySequence {
trackingStorage.removeAll(any())
trackingStorage.fetchAll(any())
}
assertTrue(removedAll)
assertTrue(fetchedAll)

exceptionsStore.waitUntilIdle()

Expand All @@ -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)
}
}

0 comments on commit 0090381

Please sign in to comment.