Commit dc7ee797 authored by Gabriel Luong's avatar Gabriel Luong
Browse files

Issue #7978: Part 3 - Replace Top prefix to Pinned for TopSiteDao, TopSite, TopSiteAdapter

parent f3705791
......@@ -63,8 +63,8 @@ class PinnedSitesStorageTest {
@Test
fun testAddingTopSite() {
storage.addTopSite("Mozilla", "https://www.mozilla.org")
storage.addTopSite("Firefox", "https://www.firefox.com", isDefault = true)
storage.addPinnedSite("Mozilla", "https://www.mozilla.org")
storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true)
val topSites = getAllTopSites()
......@@ -80,13 +80,13 @@ class PinnedSitesStorageTest {
@Test
fun testRemovingTopSites() {
storage.addTopSite("Mozilla", "https://www.mozilla.org")
storage.addTopSite("Firefox", "https://www.firefox.com")
storage.addPinnedSite("Mozilla", "https://www.mozilla.org")
storage.addPinnedSite("Firefox", "https://www.firefox.com")
getAllTopSites().let { topSites ->
assertEquals(2, topSites.size)
storage.removeTopSite(topSites[0])
storage.removePinnedSite(topSites[0])
}
getAllTopSites().let { topSites ->
......@@ -99,10 +99,10 @@ class PinnedSitesStorageTest {
@Test
fun testGettingTopSites() = runBlocking {
storage.addTopSite("Mozilla", "https://www.mozilla.org")
storage.addTopSite("Firefox", "https://www.firefox.com", isDefault = true)
storage.addPinnedSite("Mozilla", "https://www.mozilla.org")
storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true)
val topSites = storage.getTopSites().first()
val topSites = storage.getPinnedSites().first()
assertNotNull(topSites)
assertEquals(2, topSites.size)
......@@ -247,8 +247,8 @@ class PinnedSitesStorageTest {
}
}
private fun getAllTopSites(): List<TopSite> {
val dataSource = storage.getTopSitesPaged().create()
private fun getAllTopSites(): List<PinnedSite> {
val dataSource = storage.getPinnedSitesPaged().create()
val pagedList = PagedList.Builder(dataSource, 10)
.setNotifyExecutor(executor)
......
......@@ -17,12 +17,12 @@ import org.junit.Test
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class TopSiteDaoTest {
class PinnedSiteDaoTest {
private val context: Context
get() = ApplicationProvider.getApplicationContext()
private lateinit var database: TopSiteDatabase
private lateinit var topSiteDao: TopSiteDao
private lateinit var pinnedSiteDao: PinnedSiteDao
private lateinit var executor: ExecutorService
@get:Rule
......@@ -31,7 +31,7 @@ class TopSiteDaoTest {
@Before
fun setUp() {
database = Room.inMemoryDatabaseBuilder(context, TopSiteDatabase::class.java).build()
topSiteDao = database.topSiteDao()
pinnedSiteDao = database.pinnedSiteDao()
executor = Executors.newSingleThreadExecutor()
}
......@@ -49,10 +49,10 @@ class TopSiteDaoTest {
isDefault = false,
createdAt = 200
).also {
it.id = topSiteDao.insertTopSite(it)
it.id = pinnedSiteDao.insertPinnedSite(it)
}
val dataSource = topSiteDao.getTopSitesPaged().create()
val dataSource = pinnedSiteDao.getPinnedSitesPaged().create()
val pagedList = PagedList.Builder(dataSource, 10)
.setNotifyExecutor(executor)
......@@ -71,7 +71,7 @@ class TopSiteDaoTest {
isDefault = false,
createdAt = 200
).also {
it.id = topSiteDao.insertTopSite(it)
it.id = pinnedSiteDao.insertPinnedSite(it)
}
val topSite2 = PinnedSiteEntity(
......@@ -80,12 +80,12 @@ class TopSiteDaoTest {
isDefault = false,
createdAt = 100
).also {
it.id = topSiteDao.insertTopSite(it)
it.id = pinnedSiteDao.insertPinnedSite(it)
}
topSiteDao.deleteTopSite(topSite1)
pinnedSiteDao.deletePinnedSite(topSite1)
val dataSource = topSiteDao.getTopSitesPaged().create()
val dataSource = pinnedSiteDao.getPinnedSitesPaged().create()
val pagedList = PagedList.Builder(dataSource, 10)
.setNotifyExecutor(executor)
......
......@@ -5,26 +5,26 @@
package mozilla.components.feature.top.sites
/**
* A top site.
* A pinned site.
*/
interface TopSite {
interface PinnedSite {
/**
* Unique ID of this top site.
* Unique ID of this pinned site.
*/
val id: Long
/**
* The title of the top site.
* The title of the pinned site.
*/
val title: String
/**
* The URL of the top site.
* The URL of the pinned site.
*/
val url: String
/**
* Whether or not the top site is a default top site (added as a default by the application).
* Whether or not the pinned site is a default pinned site (added as a default by the application).
*/
val isDefault: Boolean
}
......@@ -8,12 +8,12 @@ import android.content.Context
import androidx.paging.DataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import mozilla.components.feature.top.sites.adapter.TopSiteAdapter
import mozilla.components.feature.top.sites.adapter.PinnedSiteAdapter
import mozilla.components.feature.top.sites.db.TopSiteDatabase
import mozilla.components.feature.top.sites.db.PinnedSiteEntity
/**
* A storage implementation for organizing top sites.
* A storage implementation for organizing pinned sites.
*/
class PinnedSiteStorage(
context: Context
......@@ -21,46 +21,46 @@ class PinnedSiteStorage(
internal var database: Lazy<TopSiteDatabase> = lazy { TopSiteDatabase.get(context) }
/**
* Adds a new [TopSite].
* Adds a new [PinnedSite].
*
* @param title The title string.
* @param url The URL string.
* @param isDefault Whether or not the top site added should be a default top site. This is
* used to identify top sites that are added by the application.
* @param isDefault Whether or not the pinned site added should be a default pinned site. This
* is used to identify pinned sites that are added by the application.
*/
fun addTopSite(title: String, url: String, isDefault: Boolean = false) {
fun addPinnedSite(title: String, url: String, isDefault: Boolean = false) {
PinnedSiteEntity(
title = title,
url = url,
isDefault = isDefault,
createdAt = System.currentTimeMillis()
).also { entity ->
entity.id = database.value.topSiteDao().insertTopSite(entity)
entity.id = database.value.pinnedSiteDao().insertPinnedSite(entity)
}
}
/**
* Returns a [Flow] list of all the [TopSite] instances.
* Returns a [Flow] list of all the [PinnedSite] instances.
*/
fun getTopSites(): Flow<List<TopSite>> {
return database.value.topSiteDao().getTopSites().map { list ->
list.map { entity -> TopSiteAdapter(entity) }
fun getPinnedSites(): Flow<List<PinnedSite>> {
return database.value.pinnedSiteDao().getPinnedSites().map { list ->
list.map { entity -> PinnedSiteAdapter(entity) }
}
}
/**
* Returns all [TopSite]s as a [DataSource.Factory].
* Returns all [PinnedSite]s as a [DataSource.Factory].
*/
fun getTopSitesPaged(): DataSource.Factory<Int, TopSite> = database.value
.topSiteDao()
.getTopSitesPaged()
.map { entity -> TopSiteAdapter(entity) }
fun getPinnedSitesPaged(): DataSource.Factory<Int, PinnedSite> = database.value
.pinnedSiteDao()
.getPinnedSitesPaged()
.map { entity -> PinnedSiteAdapter(entity) }
/**
* Removes the given [TopSite].
* Removes the given [PinnedSite].
*/
fun removeTopSite(site: TopSite) {
val topSiteEntity = (site as TopSiteAdapter).entity
database.value.topSiteDao().deleteTopSite(topSiteEntity)
fun removePinnedSite(site: PinnedSite) {
val pinnedSiteEntity = (site as PinnedSiteAdapter).entity
database.value.pinnedSiteDao().deletePinnedSite(pinnedSiteEntity)
}
}
......@@ -4,12 +4,12 @@
package mozilla.components.feature.top.sites.adapter
import mozilla.components.feature.top.sites.TopSite
import mozilla.components.feature.top.sites.PinnedSite
import mozilla.components.feature.top.sites.db.PinnedSiteEntity
internal class TopSiteAdapter(
internal class PinnedSiteAdapter(
internal val entity: PinnedSiteEntity
) : TopSite {
) : PinnedSite {
override val id: Long
get() = entity.id!!
......@@ -23,7 +23,7 @@ internal class TopSiteAdapter(
get() = entity.isDefault
override fun equals(other: Any?): Boolean {
if (other !is TopSiteAdapter) {
if (other !is PinnedSiteAdapter) {
return false
}
......
......@@ -16,18 +16,18 @@ import kotlinx.coroutines.flow.Flow
* Internal DAO for accessing [PinnedSiteEntity] instances.
*/
@Dao
internal interface TopSiteDao {
internal interface PinnedSiteDao {
@Insert
fun insertTopSite(site: PinnedSiteEntity): Long
fun insertPinnedSite(site: PinnedSiteEntity): Long
@Delete
fun deleteTopSite(site: PinnedSiteEntity)
fun deletePinnedSite(site: PinnedSiteEntity)
@Transaction
@Query("SELECT * FROM top_sites")
fun getTopSites(): Flow<List<PinnedSiteEntity>>
fun getPinnedSites(): Flow<List<PinnedSiteEntity>>
@Transaction
@Query("SELECT * FROM top_sites")
fun getTopSitesPaged(): DataSource.Factory<Int, PinnedSiteEntity>
fun getPinnedSitesPaged(): DataSource.Factory<Int, PinnedSiteEntity>
}
......@@ -9,7 +9,7 @@ import androidx.room.Entity
import androidx.room.PrimaryKey
/**
* Internal entity representing a top site.
* Internal entity representing a pinned site.
*/
@Entity(tableName = "top_sites")
internal data class PinnedSiteEntity(
......
......@@ -16,7 +16,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
*/
@Database(entities = [PinnedSiteEntity::class], version = 3)
internal abstract class TopSiteDatabase : RoomDatabase() {
abstract fun topSiteDao(): TopSiteDao
abstract fun pinnedSiteDao(): PinnedSiteDao
companion object {
@Volatile
......
......@@ -1305,7 +1305,7 @@ class FennecMigrator private constructor(
// Reversed, so that first pinned site in Fennec ends up as the first one in Fenix, as well.
pinnedSitesWithUrl.reversed().forEach { pinnedSite ->
try {
pinnedSitesStorage.addTopSite(pinnedSite.title ?: "", pinnedSite.url!!)
pinnedSitesStorage.addPinnedSite(pinnedSite.title ?: "", pinnedSite.url!!)
} catch (e: Exception) {
failedToImport++
// Let's not spam Sentry and submit the same exception multiple times
......
......@@ -272,7 +272,7 @@ class FennecMigratorTest {
assertTrue(historyStore.getVisited().isEmpty())
assertTrue(bookmarksStore.searchBookmarks("mozilla").isEmpty())
verify(topSiteStorage, never()).addTopSite(any(), any(), anyBoolean())
verify(topSiteStorage, never()).addPinnedSite(any(), any(), anyBoolean())
// Can run once.
with(migrator.migrateAsync(mock()).await()) {
......@@ -300,12 +300,12 @@ class FennecMigratorTest {
assertEquals(5, historyStore.getVisited().size)
assertEquals(2, bookmarksStore.searchBookmarks("mozilla").size)
verify(topSiteStorage, times(2)).addTopSite(any(), any(), anyBoolean())
verify(topSiteStorage).addTopSite(
verify(topSiteStorage, times(2)).addPinnedSite(any(), any(), anyBoolean())
verify(topSiteStorage).addPinnedSite(
"Featured extensions for Android – Add-ons for Firefox Android (en-US)",
"https://addons.mozilla.org/en-US/android/collections/4757633/mob/?page=1&collection_sort=-popularity"
)
verify(topSiteStorage).addTopSite(
verify(topSiteStorage).addPinnedSite(
"Internet for people, not profit — Mozilla",
"https://www.mozilla.org/en-US/"
)
......
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