From 591d6744dfd3892fe66653df8d07237534087187 Mon Sep 17 00:00:00 2001 From: Jonathan Almeida Date: Mon, 16 Aug 2021 12:56:31 -0400 Subject: [PATCH] [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). --- ...ckingProtectionExceptionsInteractorTest.kt | 88 ++++++++++++++----- 1 file changed, 66 insertions(+), 22 deletions(-) diff --git a/app/src/test/java/org/mozilla/fenix/exceptions/trackingprotection/TrackingProtectionExceptionsInteractorTest.kt b/app/src/test/java/org/mozilla/fenix/exceptions/trackingprotection/TrackingProtectionExceptionsInteractorTest.kt index 7e1a69b5c7..caf26dcb52 100644 --- a/app/src/test/java/org/mozilla/fenix/exceptions/trackingprotection/TrackingProtectionExceptionsInteractorTest.kt +++ b/app/src/test/java/org/mozilla/fenix/exceptions/trackingprotection/TrackingProtectionExceptionsInteractorTest.kt @@ -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 = emptyList() + private val activity: HomeActivity = mockk(relaxed = true) private val engine: Engine = mockk(relaxed = true) - private val store = BrowserStore() - private val capture = CaptureActionsMiddleware() + + private val results: List = 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() 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) -> Unit) { + fetchedAll = true + onResult(results) + } + + override fun removeAll(activeSessions: List?) { + 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) -> 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() - 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) } }