2019-09-10 22:29:21 +02:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
@file:Suppress("DEPRECATION")
|
|
|
|
|
2019-09-14 06:29:11 +02:00
|
|
|
package org.mozilla.fenix.settings.deletebrowsingdata
|
2019-09-10 22:29:21 +02:00
|
|
|
|
|
|
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
|
|
|
import io.mockk.every
|
|
|
|
import io.mockk.mockk
|
|
|
|
import io.mockk.verify
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
2019-10-10 20:31:07 +02:00
|
|
|
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
2019-09-10 22:29:21 +02:00
|
|
|
import kotlinx.coroutines.newSingleThreadContext
|
|
|
|
import kotlinx.coroutines.test.resetMain
|
|
|
|
import kotlinx.coroutines.test.runBlockingTest
|
|
|
|
import kotlinx.coroutines.test.setMain
|
|
|
|
import mozilla.components.browser.storage.sync.PlacesHistoryStorage
|
|
|
|
import mozilla.components.concept.engine.Engine
|
|
|
|
import mozilla.components.feature.tabs.TabsUseCases
|
|
|
|
import mozilla.components.support.test.robolectric.testContext
|
|
|
|
import org.junit.After
|
|
|
|
import org.junit.Before
|
2019-09-23 16:04:15 +02:00
|
|
|
import org.junit.Ignore
|
2019-09-10 22:29:21 +02:00
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.runner.RunWith
|
|
|
|
import org.mozilla.fenix.HomeActivity
|
|
|
|
import org.mozilla.fenix.TestApplication
|
|
|
|
import org.mozilla.fenix.components.PermissionStorage
|
|
|
|
import org.mozilla.fenix.ext.clearAndCommit
|
|
|
|
import org.mozilla.fenix.ext.components
|
2019-09-14 06:29:11 +02:00
|
|
|
import org.mozilla.fenix.utils.Settings
|
2019-09-10 22:29:21 +02:00
|
|
|
import org.robolectric.annotation.Config
|
|
|
|
|
|
|
|
@ExperimentalCoroutinesApi
|
2019-10-10 20:31:07 +02:00
|
|
|
@UseExperimental(ObsoleteCoroutinesApi::class)
|
2019-09-10 22:29:21 +02:00
|
|
|
@RunWith(AndroidJUnit4::class)
|
|
|
|
@Config(application = TestApplication::class)
|
|
|
|
class DeleteAndQuitTest {
|
|
|
|
|
|
|
|
private val mainThreadSurrogate = newSingleThreadContext("UI thread")
|
|
|
|
|
2019-09-14 20:51:12 +02:00
|
|
|
private var activity: HomeActivity = mockk(relaxed = true)
|
2019-09-10 22:29:21 +02:00
|
|
|
lateinit var settings: Settings
|
|
|
|
private val tabUseCases: TabsUseCases = mockk(relaxed = true)
|
|
|
|
private val historyStorage: PlacesHistoryStorage = mockk(relaxed = true)
|
|
|
|
private val permissionStorage: PermissionStorage = mockk(relaxed = true)
|
|
|
|
private val engine: Engine = mockk(relaxed = true)
|
|
|
|
private val removeAllTabsUseCases: TabsUseCases.RemoveAllTabsUseCase = mockk(relaxed = true)
|
|
|
|
|
|
|
|
@Before
|
|
|
|
fun setUp() {
|
|
|
|
settings = Settings.getInstance(testContext).apply {
|
|
|
|
clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
Dispatchers.setMain(mainThreadSurrogate)
|
|
|
|
|
2019-09-14 20:51:12 +02:00
|
|
|
every { activity.components.core.historyStorage } returns historyStorage
|
|
|
|
every { activity.components.core.permissionStorage } returns permissionStorage
|
|
|
|
every { activity.components.useCases.tabsUseCases } returns tabUseCases
|
2019-09-10 22:29:21 +02:00
|
|
|
every { tabUseCases.removeAllTabs } returns removeAllTabsUseCases
|
2019-09-14 20:51:12 +02:00
|
|
|
every { activity.components.core.engine } returns engine
|
2019-09-10 22:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
fun tearDown() {
|
|
|
|
Dispatchers.resetMain() // reset main dispatcher to the original Main dispatcher
|
|
|
|
mainThreadSurrogate.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun Settings.clear() {
|
|
|
|
preferences.clearAndCommit()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `delete only tabs and quit`() = runBlockingTest {
|
|
|
|
// When
|
2019-09-14 21:35:17 +02:00
|
|
|
settings.setDeleteDataOnQuit(DeleteBrowsingDataOnQuitType.TABS, true)
|
2019-09-10 22:29:21 +02:00
|
|
|
|
2019-09-14 20:51:12 +02:00
|
|
|
deleteAndQuit(activity, this)
|
2019-09-10 22:29:21 +02:00
|
|
|
|
|
|
|
verify {
|
|
|
|
removeAllTabsUseCases.invoke()
|
2019-09-14 20:51:12 +02:00
|
|
|
activity.finish()
|
2019-09-10 22:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
verify(exactly = 0) {
|
|
|
|
historyStorage
|
|
|
|
|
|
|
|
engine.clearData(
|
|
|
|
Engine.BrowsingData.select(
|
|
|
|
Engine.BrowsingData.COOKIES
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
permissionStorage.deleteAllSitePermissions()
|
|
|
|
|
|
|
|
engine.clearData(Engine.BrowsingData.allCaches())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 16:04:15 +02:00
|
|
|
@Ignore("Intermittently failing; will be fixed with #5406.")
|
2019-09-10 22:29:21 +02:00
|
|
|
@Test
|
|
|
|
fun `delete everything and quit`() = runBlockingTest {
|
|
|
|
// When
|
2019-09-14 21:35:17 +02:00
|
|
|
DeleteBrowsingDataOnQuitType.values().forEach {
|
|
|
|
settings.setDeleteDataOnQuit(it, true)
|
|
|
|
}
|
2019-09-10 22:29:21 +02:00
|
|
|
|
2019-09-14 20:51:12 +02:00
|
|
|
deleteAndQuit(activity, this)
|
2019-09-10 22:29:21 +02:00
|
|
|
|
|
|
|
verify(exactly = 1) {
|
|
|
|
engine.clearData(Engine.BrowsingData.allCaches())
|
|
|
|
|
|
|
|
removeAllTabsUseCases.invoke()
|
|
|
|
|
|
|
|
engine.clearData(
|
|
|
|
Engine.BrowsingData.select(Engine.BrowsingData.ALL_SITE_SETTINGS)
|
|
|
|
)
|
|
|
|
|
|
|
|
permissionStorage.deleteAllSitePermissions()
|
|
|
|
|
|
|
|
engine.clearData(
|
|
|
|
Engine.BrowsingData.select(
|
|
|
|
Engine.BrowsingData.COOKIES,
|
|
|
|
Engine.BrowsingData.AUTH_SESSIONS
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
engine.clearData(Engine.BrowsingData.select(Engine.BrowsingData.DOM_STORAGES))
|
|
|
|
|
|
|
|
historyStorage
|
|
|
|
|
2019-09-14 20:51:12 +02:00
|
|
|
activity.finish()
|
2019-09-10 22:29:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|