[fenix] For https://github.com/mozilla-mobile/fenix/issues/20890 when TP is off globally hide TP section on quick settings.
parent
1928220c0d
commit
a82a02a9e7
@ -0,0 +1,182 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.settings.quicksettings
|
||||
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.LifecycleRegistry
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.slot
|
||||
import io.mockk.spyk
|
||||
import io.mockk.verify
|
||||
import junit.framework.TestCase.assertNotSame
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.TestCoroutineDispatcher
|
||||
import mozilla.components.browser.state.action.TabListAction
|
||||
import mozilla.components.browser.state.action.TrackingProtectionAction.TrackerBlockedAction
|
||||
import mozilla.components.browser.state.action.TrackingProtectionAction.TrackerLoadedAction
|
||||
import mozilla.components.browser.state.selector.findTab
|
||||
import mozilla.components.browser.state.state.TabSessionState
|
||||
import mozilla.components.browser.state.state.createTab
|
||||
import mozilla.components.browser.state.store.BrowserStore
|
||||
import mozilla.components.concept.engine.content.blocking.TrackerLog
|
||||
import mozilla.components.feature.session.TrackingProtectionUseCases
|
||||
import mozilla.components.support.test.ext.joinBlocking
|
||||
import mozilla.components.support.test.rule.MainCoroutineRule
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class QuickSettingsSheetDialogFragmentTest {
|
||||
|
||||
private val testDispatcher = TestCoroutineDispatcher()
|
||||
|
||||
@get:Rule
|
||||
val coroutinesTestRule = MainCoroutineRule(testDispatcher)
|
||||
private lateinit var lifecycleOwner: MockedLifecycleOwner
|
||||
private lateinit var fragment: QuickSettingsSheetDialogFragment
|
||||
private lateinit var store: BrowserStore
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
fragment = spyk(QuickSettingsSheetDialogFragment())
|
||||
lifecycleOwner = MockedLifecycleOwner(Lifecycle.State.STARTED)
|
||||
|
||||
store = BrowserStore()
|
||||
every { fragment.view } returns mockk(relaxed = true)
|
||||
every { fragment.lifecycle } returns lifecycleOwner.lifecycle
|
||||
every { fragment.activity } returns mockk(relaxed = true)
|
||||
}
|
||||
|
||||
@After
|
||||
fun cleanUp() {
|
||||
testDispatcher.cleanupTestCoroutines()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN a tracker is loaded THEN trackers view is updated`() {
|
||||
val tab = createTab("mozilla.org")
|
||||
|
||||
every { fragment.provideTabId() } returns tab.id
|
||||
every { fragment.updateTrackers(any()) } returns Unit
|
||||
|
||||
fragment.observeTrackersChange(store)
|
||||
|
||||
addAndSelectTab(tab)
|
||||
|
||||
verify(exactly = 1) {
|
||||
fragment.updateTrackers(tab)
|
||||
}
|
||||
|
||||
store.dispatch(TrackerLoadedAction(tab.id, mockk())).joinBlocking()
|
||||
|
||||
val updatedTab = store.state.findTab(tab.id)!!
|
||||
|
||||
assertNotSame(updatedTab, tab)
|
||||
|
||||
verify(exactly = 1) {
|
||||
fragment.updateTrackers(updatedTab)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN a tracker is blocked THEN trackers view is updated`() {
|
||||
val tab = createTab("mozilla.org")
|
||||
|
||||
every { fragment.provideTabId() } returns tab.id
|
||||
every { fragment.updateTrackers(any()) } returns Unit
|
||||
|
||||
fragment.observeTrackersChange(store)
|
||||
|
||||
addAndSelectTab(tab)
|
||||
|
||||
verify(exactly = 1) {
|
||||
fragment.updateTrackers(tab)
|
||||
}
|
||||
|
||||
store.dispatch(TrackerBlockedAction(tab.id, mockk())).joinBlocking()
|
||||
|
||||
val updatedTab = store.state.findTab(tab.id)!!
|
||||
|
||||
assertNotSame(updatedTab, tab)
|
||||
|
||||
verify(exactly = 1) {
|
||||
fragment.updateTrackers(updatedTab)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN no trackers WHEN calling updateTrackers THEN hide the details section`() {
|
||||
val tab = createTab("mozilla.org")
|
||||
val trackingProtectionUseCases: TrackingProtectionUseCases = mockk(relaxed = true)
|
||||
val trackingProtectionView: TrackingProtectionView = mockk(relaxed = true)
|
||||
|
||||
val onComplete = slot<(List<TrackerLog>) -> Unit>()
|
||||
|
||||
every { fragment.trackingProtectionView } returns trackingProtectionView
|
||||
|
||||
every {
|
||||
trackingProtectionUseCases.fetchTrackingLogs.invoke(
|
||||
any(),
|
||||
capture(onComplete),
|
||||
any()
|
||||
)
|
||||
}.answers { onComplete.captured.invoke(emptyList()) }
|
||||
|
||||
every { fragment.provideTrackingProtectionUseCases() } returns trackingProtectionUseCases
|
||||
|
||||
fragment.updateTrackers(tab)
|
||||
|
||||
verify {
|
||||
trackingProtectionView.updateDetailsSection(false)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN trackers WHEN calling updateTrackers THEN show the details section`() {
|
||||
val tab = createTab("mozilla.org")
|
||||
val trackingProtectionUseCases: TrackingProtectionUseCases = mockk(relaxed = true)
|
||||
val trackingProtectionView: TrackingProtectionView = mockk(relaxed = true)
|
||||
|
||||
val onComplete = slot<(List<TrackerLog>) -> Unit>()
|
||||
|
||||
every { fragment.trackingProtectionView } returns trackingProtectionView
|
||||
|
||||
every {
|
||||
trackingProtectionUseCases.fetchTrackingLogs.invoke(
|
||||
any(),
|
||||
capture(onComplete),
|
||||
any()
|
||||
)
|
||||
}.answers { onComplete.captured.invoke(listOf(TrackerLog(""))) }
|
||||
|
||||
every { fragment.provideTrackingProtectionUseCases() } returns trackingProtectionUseCases
|
||||
|
||||
fragment.updateTrackers(tab)
|
||||
|
||||
verify {
|
||||
trackingProtectionView.updateDetailsSection(true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun addAndSelectTab(tab: TabSessionState) {
|
||||
store.dispatch(TabListAction.AddTabAction(tab)).joinBlocking()
|
||||
store.dispatch(TabListAction.SelectTabAction(tab.id)).joinBlocking()
|
||||
}
|
||||
|
||||
internal class MockedLifecycleOwner(initialState: Lifecycle.State) : LifecycleOwner {
|
||||
private val lifecycleRegistry = LifecycleRegistry(this).apply {
|
||||
currentState = initialState
|
||||
}
|
||||
|
||||
override fun getLifecycle(): Lifecycle = lifecycleRegistry
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.settings.quicksettings
|
||||
|
||||
import android.widget.FrameLayout
|
||||
import androidx.core.view.isVisible
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.every
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import io.mockk.mockk
|
||||
import io.mockk.spyk
|
||||
import mozilla.components.browser.state.state.createTab
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
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.databinding.QuicksettingsTrackingProtectionBinding
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
import org.mozilla.fenix.trackingprotection.TrackingProtectionState
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class TrackingProtectionViewTest {
|
||||
|
||||
private lateinit var view: TrackingProtectionView
|
||||
private lateinit var binding: QuicksettingsTrackingProtectionBinding
|
||||
private lateinit var interactor: TrackingProtectionInteractor
|
||||
|
||||
@MockK(relaxed = true)
|
||||
private lateinit var settings: Settings
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
MockKAnnotations.init(this)
|
||||
interactor = mockk(relaxed = true)
|
||||
view = spyk(TrackingProtectionView(FrameLayout(testContext), interactor, settings))
|
||||
binding = view.binding
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN updating THEN bind checkbox`() {
|
||||
val websiteUrl = "https://mozilla.org"
|
||||
val state = TrackingProtectionState(
|
||||
tab = createTab(url = websiteUrl),
|
||||
url = websiteUrl,
|
||||
isTrackingProtectionEnabled = true,
|
||||
listTrackers = listOf(),
|
||||
mode = TrackingProtectionState.Mode.Normal,
|
||||
lastAccessedCategory = ""
|
||||
)
|
||||
|
||||
every { settings.shouldUseTrackingProtection } returns true
|
||||
|
||||
view.update(state)
|
||||
|
||||
assertTrue(binding.root.isVisible)
|
||||
assertTrue(binding.trackingProtectionSwitch.switchWidget.isChecked)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN TP is globally off WHEN updating THEN hide the TP section`() {
|
||||
val websiteUrl = "https://mozilla.org"
|
||||
val state = TrackingProtectionState(
|
||||
tab = createTab(url = websiteUrl),
|
||||
url = websiteUrl,
|
||||
isTrackingProtectionEnabled = true,
|
||||
listTrackers = listOf(),
|
||||
mode = TrackingProtectionState.Mode.Normal,
|
||||
lastAccessedCategory = ""
|
||||
)
|
||||
|
||||
every { settings.shouldUseTrackingProtection } returns false
|
||||
|
||||
view.update(state)
|
||||
|
||||
assertFalse(binding.root.isVisible)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN updateDetailsSection is called THEN update the visibility of the section`() {
|
||||
every { settings.shouldUseTrackingProtection } returns false
|
||||
|
||||
view.updateDetailsSection(false)
|
||||
|
||||
assertFalse(binding.trackingProtectionDetails.isVisible)
|
||||
|
||||
view.updateDetailsSection(true)
|
||||
|
||||
assertTrue(binding.trackingProtectionDetails.isVisible)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue