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