Commit 1d529b69 authored by Arturo Mejia's avatar Arturo Mejia
Browse files

Closes #2687: Added support for deleting all site permissions and

get paged all site permissions.
parent 78d8b68c
......@@ -46,6 +46,10 @@ dependencies {
implementation Dependencies.room_runtime
kapt Dependencies.room_compiler
implementation Dependencies.arch_paging
implementation Dependencies.arch_lifecycle
kapt Dependencies.arch_lifecycl_compiler
implementation Dependencies.kotlin_coroutines
......
......@@ -14,6 +14,7 @@ import mozilla.components.support.ktx.kotlin.toUri
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
......@@ -46,6 +47,23 @@ class SitePermissionsDaoTest {
assertEquals(BLOCKED, siteFromDb.camera)
}
@Test
fun testRemoveAllSitePermissions() {
for (index in 1..4) {
val origin = insertMockSitePermissions("https://www.mozilla$index.org")
val sitePermissionFromDb = dao.getSitePermissionsBy(origin)
assertEquals(origin, sitePermissionFromDb!!.origin)
}
dao.deleteAllSitePermissions()
val isEmpty = dao.getSitePermissions().isEmpty()
assertTrue(isEmpty)
}
@Test
fun testUpdateAndDeleteSitePermissions() {
val origin = insertMockSitePermissions("https://www.mozilla.org")
......
......@@ -4,6 +4,7 @@
package mozilla.components.feature.sitepermissions
import android.arch.paging.DataSource
import android.content.Context
import android.support.annotation.VisibleForTesting
import mozilla.components.feature.sitepermissions.SitePermissions.Status
......@@ -66,6 +67,24 @@ class SitePermissionsStorage(
?.toSitePermission()
}
/**
* Returns all saved [SitePermissions] instances as a [DataSource.Factory].
*
* A consuming app can transform the data source into a `LiveData<PagedList>` of when using RxJava2 into a
* `Flowable<PagedList>` or `Observable<PagedList>`, that can be observed.
*
* - https://developer.android.com/topic/libraries/architecture/paging/data
* - https://developer.android.com/topic/libraries/architecture/paging/ui
*/
fun getSitePermissionsPaged(): DataSource.Factory<Int, SitePermissions> {
return database
.sitePermissionsDao()
.getSitePermissionsPaged()
.map { entity ->
entity.toSitePermission()
}
}
/**
* Finds all SitePermissions grouped by [Permission].
* @return a map of site grouped by [Permission].
......@@ -99,6 +118,15 @@ class SitePermissionsStorage(
)
}
/**
* Deletes all sitePermissions sitePermissions.
*/
fun removeAll() {
return database
.sitePermissionsDao()
.deleteAllSitePermissions()
}
private fun all(): List<SitePermissions> {
return database
.sitePermissionsDao()
......
......@@ -4,6 +4,7 @@
package mozilla.components.feature.sitepermissions.db
import android.arch.paging.DataSource
import android.arch.persistence.room.Dao
import android.arch.persistence.room.Delete
import android.arch.persistence.room.Insert
......@@ -30,4 +31,10 @@ internal interface SitePermissionsDao {
@Delete
fun deleteSitePermissions(entity: SitePermissionsEntity)
@Query("DELETE FROM site_permissions")
fun deleteAllSitePermissions()
@Query("SELECT * FROM site_permissions ORDER BY saved_at DESC")
fun getSitePermissionsPaged(): DataSource.Factory<Int, SitePermissionsEntity>
}
......@@ -4,6 +4,7 @@
package mozilla.components.feature.sitepermissions
import android.arch.paging.DataSource
import android.arch.persistence.db.SupportSQLiteOpenHelper
import android.arch.persistence.room.DatabaseConfiguration
import android.arch.persistence.room.InvalidationTracker
......@@ -96,6 +97,28 @@ class SitePermissionsStorageTest {
verify(mockDAO).deleteSitePermissions(any())
}
@Test
fun `remove all SitePermissions`() {
storage.removeAll()
verify(mockDAO).deleteAllSitePermissions()
}
@Test
fun `get all SitePermissions paged`() {
val mockDataSource: DataSource<Int, SitePermissionsEntity> = mock()
doReturn(object : DataSource.Factory<Int, SitePermissionsEntity>() {
override fun create(): DataSource<Int, SitePermissionsEntity> {
return mockDataSource
}
}).`when`(mockDAO).getSitePermissionsPaged()
storage.getSitePermissionsPaged()
verify(mockDAO).getSitePermissionsPaged()
}
private fun createNewSitePermission(): SitePermissions {
return SitePermissions(
origin = "mozilla.dev",
......
Supports Markdown
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