Commit 4b74ff18 authored by mcarare's avatar mcarare Committed by Mihai Adrian Carare
Browse files

For #12565: Remove context from DeleteBrowsingDataController constructor

parent e2e684b4
......@@ -13,6 +13,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
/**
......@@ -21,7 +22,13 @@ import org.mozilla.fenix.ext.settings
fun deleteAndQuit(activity: Activity, coroutineScope: CoroutineScope, snackbar: FenixSnackbar?) {
coroutineScope.launch {
val settings = activity.settings()
val controller = DefaultDeleteBrowsingDataController(activity, coroutineContext)
val controller = DefaultDeleteBrowsingDataController(
activity.components.useCases.tabsUseCases.removeAllTabs,
activity.components.core.historyStorage,
activity.components.core.permissionStorage,
activity.components.core.engine,
coroutineContext
)
snackbar?.apply {
setText(activity.getString(R.string.deleting_browsing_data_in_progress))
......
......@@ -4,11 +4,12 @@
package org.mozilla.fenix.settings.deletebrowsingdata
import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import mozilla.components.concept.engine.Engine
import org.mozilla.fenix.ext.components
import mozilla.components.concept.storage.HistoryStorage
import mozilla.components.feature.tabs.TabsUseCases
import org.mozilla.fenix.components.PermissionStorage
import kotlin.coroutines.CoroutineContext
interface DeleteBrowsingDataController {
......@@ -21,13 +22,16 @@ interface DeleteBrowsingDataController {
}
class DefaultDeleteBrowsingDataController(
val context: Context,
val coroutineContext: CoroutineContext = Dispatchers.Main
private val removeAllTabs: TabsUseCases.RemoveAllTabsUseCase,
private val historyStorage: HistoryStorage,
private val permissionStorage: PermissionStorage,
private val engine: Engine,
private val coroutineContext: CoroutineContext = Dispatchers.Main
) : DeleteBrowsingDataController {
override suspend fun deleteTabs() {
withContext(coroutineContext) {
context.components.useCases.tabsUseCases.removeAllTabs.invoke()
removeAllTabs.invoke()
}
}
......@@ -37,14 +41,14 @@ class DefaultDeleteBrowsingDataController(
override suspend fun deleteHistoryAndDOMStorages() {
withContext(coroutineContext) {
context.components.core.engine.clearData(Engine.BrowsingData.select(Engine.BrowsingData.DOM_STORAGES))
engine.clearData(Engine.BrowsingData.select(Engine.BrowsingData.DOM_STORAGES))
}
context.components.core.historyStorage.deleteEverything()
historyStorage.deleteEverything()
}
override suspend fun deleteCookies() {
withContext(coroutineContext) {
context.components.core.engine.clearData(
engine.clearData(
Engine.BrowsingData.select(
Engine.BrowsingData.COOKIES,
Engine.BrowsingData.AUTH_SESSIONS
......@@ -55,7 +59,7 @@ class DefaultDeleteBrowsingDataController(
override suspend fun deleteCachedFiles() {
withContext(coroutineContext) {
context.components.core.engine.clearData(
engine.clearData(
Engine.BrowsingData.select(Engine.BrowsingData.ALL_CACHES)
)
}
......@@ -63,10 +67,10 @@ class DefaultDeleteBrowsingDataController(
override suspend fun deleteSitePermissions() {
withContext(coroutineContext) {
context.components.core.engine.clearData(
engine.clearData(
Engine.BrowsingData.select(Engine.BrowsingData.ALL_SITE_SETTINGS)
)
}
context.components.core.permissionStorage.deleteAllSitePermissions()
permissionStorage.deleteAllSitePermissions()
}
}
......@@ -25,6 +25,7 @@ import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifChanged
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
......@@ -40,7 +41,12 @@ class DeleteBrowsingDataFragment : Fragment(R.layout.fragment_delete_browsing_da
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
controller = DefaultDeleteBrowsingDataController(requireContext())
controller = DefaultDeleteBrowsingDataController(
requireContext().components.useCases.tabsUseCases.removeAllTabs,
requireContext().components.core.historyStorage,
requireContext().components.core.permissionStorage,
requireContext().components.core.engine
)
settings = requireContext().settings()
getCheckboxes().forEach {
......
......@@ -4,21 +4,21 @@
package org.mozilla.fenix.settings.deletebrowsingdata
import android.content.Context
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope.coroutineContext
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.runBlockingTest
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.storage.HistoryStorage
import mozilla.components.feature.tabs.TabsUseCases
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.components.PermissionStorage
@OptIn(ExperimentalCoroutinesApi::class)
class DefaultDeleteBrowsingDataControllerTest {
......@@ -26,47 +26,49 @@ class DefaultDeleteBrowsingDataControllerTest {
@get:Rule
val coroutinesTestRule = MainCoroutineRule(TestCoroutineDispatcher())
private val context: Context = mockk(relaxed = true)
private var removeAllTabs: TabsUseCases.RemoveAllTabsUseCase = mockk(relaxed = true)
private var historyStorage: HistoryStorage = mockk(relaxed = true)
private var permissionStorage: PermissionStorage = mockk(relaxed = true)
private val engine: Engine = mockk(relaxed = true)
private lateinit var controller: DefaultDeleteBrowsingDataController
@Before
fun setup() {
every { context.components.core.engine.clearData(any()) } just Runs
controller = DefaultDeleteBrowsingDataController(
removeAllTabs = removeAllTabs,
historyStorage = historyStorage,
permissionStorage = permissionStorage,
engine = engine,
coroutineContext = coroutineContext
)
}
@Test
fun deleteTabs() = runBlockingTest {
controller = DefaultDeleteBrowsingDataController(context, coroutineContext)
every { context.components.useCases.tabsUseCases.removeAllTabs.invoke() } just Runs
controller.deleteTabs()
verify {
context.components.useCases.tabsUseCases.removeAllTabs.invoke()
removeAllTabs.invoke()
}
}
@Test
fun deleteBrowsingData() = runBlockingTest {
controller = DefaultDeleteBrowsingDataController(context, coroutineContext)
every { context.components.core.historyStorage } returns mockk(relaxed = true)
controller.deleteBrowsingData()
verify {
context.components.core.engine.clearData(Engine.BrowsingData.select(Engine.BrowsingData.DOM_STORAGES))
context.components.core.historyStorage
engine.clearData(Engine.BrowsingData.select(Engine.BrowsingData.DOM_STORAGES))
}
verify { launch { historyStorage.deleteEverything() } }
}
@Test
fun deleteCookies() = runBlockingTest {
controller = DefaultDeleteBrowsingDataController(context, coroutineContext)
controller.deleteCookies()
verify {
context.components.core.engine.clearData(
engine.clearData(
Engine.BrowsingData.select(
Engine.BrowsingData.COOKIES,
Engine.BrowsingData.AUTH_SESSIONS
......@@ -77,25 +79,21 @@ class DefaultDeleteBrowsingDataControllerTest {
@Test
fun deleteCachedFiles() = runBlockingTest {
controller = DefaultDeleteBrowsingDataController(context, coroutineContext)
controller.deleteCachedFiles()
verify {
context.components.core.engine.clearData(Engine.BrowsingData.select(Engine.BrowsingData.ALL_CACHES))
engine.clearData(Engine.BrowsingData.select(Engine.BrowsingData.ALL_CACHES))
}
}
@Test
fun deleteSitePermissions() = runBlockingTest {
controller = DefaultDeleteBrowsingDataController(context, coroutineContext)
every { context.components.core.permissionStorage.deleteAllSitePermissions() } just Runs
controller.deleteSitePermissions()
verify {
context.components.core.engine.clearData(Engine.BrowsingData.select(Engine.BrowsingData.ALL_SITE_SETTINGS))
context.components.core.permissionStorage.deleteAllSitePermissions()
engine.clearData(Engine.BrowsingData.select(Engine.BrowsingData.ALL_SITE_SETTINGS))
permissionStorage.deleteAllSitePermissions()
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment