GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

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

Issue #8300: Ensure that the default top sites are added in order

Adds a new method `addAllPinnedSites` in `PinnedSitesStorage` to execute adding all the default top sites in order.
parent 28b335f9
......@@ -59,6 +59,35 @@ class PinnedSitesStorageTest {
executor.shutdown()
}
@Test
fun testAddingAllDefaultSites() = runBlocking {
val defaultTopSites = listOf(
Pair("Mozilla", "https://www.mozilla.org"),
Pair("Firefox", "https://www.firefox.com"),
Pair("Wikipedia", "https://www.wikipedia.com"),
Pair("Pocket", "https://www.getpocket.com")
)
storage.addAllPinnedSites(defaultTopSites, isDefault = true)
val topSites = storage.getPinnedSites()
assertEquals(4, topSites.size)
assertEquals("Mozilla", topSites[0].title)
assertEquals("https://www.mozilla.org", topSites[0].url)
assertEquals(DEFAULT, topSites[0].type)
assertEquals("Firefox", topSites[1].title)
assertEquals("https://www.firefox.com", topSites[1].url)
assertEquals(DEFAULT, topSites[2].type)
assertEquals("Wikipedia", topSites[2].title)
assertEquals("https://www.wikipedia.com", topSites[2].url)
assertEquals(DEFAULT, topSites[2].type)
assertEquals("Pocket", topSites[3].title)
assertEquals("https://www.getpocket.com", topSites[3].url)
assertEquals(DEFAULT, topSites[3].type)
}
@Test
fun testAddingPinnedSite() = runBlocking {
storage.addPinnedSite("Mozilla", "https://www.mozilla.org")
......
......@@ -8,9 +8,9 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.browser.storage.sync.PlacesHistoryStorage
import mozilla.components.feature.top.sites.ext.toTopSite
import mozilla.components.feature.top.sites.TopSite.Type.FRECENT
import mozilla.components.feature.top.sites.ext.hasUrl
import mozilla.components.feature.top.sites.ext.toTopSite
import mozilla.components.support.base.observer.Observable
import mozilla.components.support.base.observer.ObserverRegistry
import kotlin.coroutines.CoroutineContext
......@@ -39,9 +39,7 @@ class DefaultTopSitesStorage(
init {
if (defaultTopSites.isNotEmpty()) {
scope.launch {
defaultTopSites.forEach { (title, url) ->
addTopSite(title, url, isDefault = true)
}
pinnedSitesStorage.addAllPinnedSites(defaultTopSites, isDefault = true)
}
}
}
......
......@@ -5,10 +5,11 @@
package mozilla.components.feature.top.sites
import android.content.Context
import androidx.room.withTransaction
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import mozilla.components.feature.top.sites.db.TopSiteDatabase
import mozilla.components.feature.top.sites.db.PinnedSiteEntity
import mozilla.components.feature.top.sites.db.TopSiteDatabase
import mozilla.components.feature.top.sites.db.toPinnedSite
/**
......@@ -19,6 +20,30 @@ class PinnedSiteStorage(context: Context) {
internal var database: Lazy<TopSiteDatabase> = lazy { TopSiteDatabase.get(context) }
private val pinnedSiteDao by lazy { database.value.pinnedSiteDao() }
/**
* Adds the given list pinned sites.
*
* @param topSites A list containing a title to url pair of top sites to be added.
* @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.
*/
suspend fun addAllPinnedSites(
topSites: List<Pair<String, String>>,
isDefault: Boolean = false
) = withContext(IO) {
database.value.withTransaction {
topSites.forEach { (title, url) ->
val entity = PinnedSiteEntity(
title = title,
url = url,
isDefault = isDefault,
createdAt = System.currentTimeMillis()
)
entity.id = pinnedSiteDao.insertPinnedSite(entity)
}
}
}
/**
* Adds a new pinned site.
*
......@@ -27,15 +52,16 @@ class PinnedSiteStorage(context: Context) {
* @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.
*/
suspend fun addPinnedSite(title: String, url: String, isDefault: Boolean = false) = withContext(IO) {
val entity = PinnedSiteEntity(
title = title,
url = url,
isDefault = isDefault,
createdAt = System.currentTimeMillis()
)
entity.id = pinnedSiteDao.insertPinnedSite(entity)
}
suspend fun addPinnedSite(title: String, url: String, isDefault: Boolean = false) =
withContext(IO) {
val entity = PinnedSiteEntity(
title = title,
url = url,
isDefault = isDefault,
createdAt = System.currentTimeMillis()
)
entity.id = pinnedSiteDao.insertPinnedSite(entity)
}
/**
* Returns a list of all the pinned sites.
......
......@@ -40,17 +40,7 @@ class DefaultTopSitesStorageTest {
coroutineContext
)
verify(pinnedSitesStorage).addPinnedSite(
"Mozilla",
"https://mozilla.com",
isDefault = true
)
verify(pinnedSitesStorage).addPinnedSite(
"Firefox",
"https://firefox.com",
isDefault = true
)
verify(pinnedSitesStorage).addAllPinnedSites(defaultTopSites, isDefault = true)
}
@Test
......
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