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

Issue #7978: Part 5 - Add tests for the new TopSitesFeature and fix existing pinned site tests

parent c78e6a71
...@@ -6,21 +6,19 @@ package mozilla.components.feature.top.sites ...@@ -6,21 +6,19 @@ package mozilla.components.feature.top.sites
import android.content.Context import android.content.Context
import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.paging.PagedList
import androidx.room.Room import androidx.room.Room
import androidx.room.testing.MigrationTestHelper import androidx.room.testing.MigrationTestHelper
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import mozilla.components.feature.top.sites.TopSite.Type.DEFAULT
import mozilla.components.feature.top.sites.TopSite.Type.PINNED
import mozilla.components.feature.top.sites.db.Migrations import mozilla.components.feature.top.sites.db.Migrations
import mozilla.components.feature.top.sites.db.TopSiteDatabase import mozilla.components.feature.top.sites.db.TopSiteDatabase
import org.junit.After import org.junit.After
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
...@@ -62,34 +60,34 @@ class PinnedSitesStorageTest { ...@@ -62,34 +60,34 @@ class PinnedSitesStorageTest {
} }
@Test @Test
fun testAddingTopSite() { fun testAddingPinnedSite() = runBlocking {
storage.addPinnedSite("Mozilla", "https://www.mozilla.org") storage.addPinnedSite("Mozilla", "https://www.mozilla.org")
storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true) storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true)
val topSites = getAllTopSites() val topSites = storage.getPinnedSites()
assertEquals(2, topSites.size) assertEquals(2, topSites.size)
assertEquals("Mozilla", topSites[0].title) assertEquals("Mozilla", topSites[0].title)
assertEquals("https://www.mozilla.org", topSites[0].url) assertEquals("https://www.mozilla.org", topSites[0].url)
assertFalse(topSites[0].isDefault) assertEquals(PINNED, topSites[0].type)
assertEquals("Firefox", topSites[1].title) assertEquals("Firefox", topSites[1].title)
assertEquals("https://www.firefox.com", topSites[1].url) assertEquals("https://www.firefox.com", topSites[1].url)
assertTrue(topSites[1].isDefault) assertEquals(DEFAULT, topSites[1].type)
} }
@Test @Test
fun testRemovingTopSites() { fun testRemovingPinnedSites() = runBlocking {
storage.addPinnedSite("Mozilla", "https://www.mozilla.org") storage.addPinnedSite("Mozilla", "https://www.mozilla.org")
storage.addPinnedSite("Firefox", "https://www.firefox.com") storage.addPinnedSite("Firefox", "https://www.firefox.com")
getAllTopSites().let { topSites -> storage.getPinnedSites().let { topSites ->
assertEquals(2, topSites.size) assertEquals(2, topSites.size)
storage.removePinnedSite(topSites[0]) storage.removePinnedSite(topSites[0])
} }
getAllTopSites().let { topSites -> storage.getPinnedSites().let { topSites ->
assertEquals(1, topSites.size) assertEquals(1, topSites.size)
assertEquals("Firefox", topSites[0].title) assertEquals("Firefox", topSites[0].title)
...@@ -98,11 +96,11 @@ class PinnedSitesStorageTest { ...@@ -98,11 +96,11 @@ class PinnedSitesStorageTest {
} }
@Test @Test
fun testGettingTopSites() = runBlocking { fun testGettingPinnedSites() = runBlocking {
storage.addPinnedSite("Mozilla", "https://www.mozilla.org") storage.addPinnedSite("Mozilla", "https://www.mozilla.org")
storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true) storage.addPinnedSite("Firefox", "https://www.firefox.com", isDefault = true)
val topSites = storage.getPinnedSites().first() val topSites = storage.getPinnedSites()
assertNotNull(topSites) assertNotNull(topSites)
assertEquals(2, topSites.size) assertEquals(2, topSites.size)
...@@ -110,13 +108,13 @@ class PinnedSitesStorageTest { ...@@ -110,13 +108,13 @@ class PinnedSitesStorageTest {
with(topSites[0]) { with(topSites[0]) {
assertEquals("Mozilla", title) assertEquals("Mozilla", title)
assertEquals("https://www.mozilla.org", url) assertEquals("https://www.mozilla.org", url)
assertFalse(isDefault) assertEquals(DEFAULT, type)
} }
with(topSites[1]) { with(topSites[1]) {
assertEquals("Firefox", title) assertEquals("Firefox", title)
assertEquals("https://www.firefox.com", url) assertEquals("https://www.firefox.com", url)
assertTrue(isDefault) assertEquals(DEFAULT, type)
} }
} }
...@@ -246,15 +244,4 @@ class PinnedSitesStorageTest { ...@@ -246,15 +244,4 @@ class PinnedSitesStorageTest {
assertEquals(4, cursor.getInt(cursor.getColumnIndexOrThrow("created_at"))) assertEquals(4, cursor.getInt(cursor.getColumnIndexOrThrow("created_at")))
} }
} }
private fun getAllTopSites(): List<PinnedSite> {
val dataSource = storage.getPinnedSitesPaged().create()
val pagedList = PagedList.Builder(dataSource, 10)
.setNotifyExecutor(executor)
.setFetchExecutor(executor)
.build()
return pagedList.toList()
}
} }
...@@ -6,7 +6,6 @@ package mozilla.components.feature.top.sites.db ...@@ -6,7 +6,6 @@ package mozilla.components.feature.top.sites.db
import android.content.Context import android.content.Context
import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.paging.PagedList
import androidx.room.Room import androidx.room.Room
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import org.junit.After import org.junit.After
...@@ -52,15 +51,10 @@ class PinnedSiteDaoTest { ...@@ -52,15 +51,10 @@ class PinnedSiteDaoTest {
it.id = pinnedSiteDao.insertPinnedSite(it) it.id = pinnedSiteDao.insertPinnedSite(it)
} }
val dataSource = pinnedSiteDao.getPinnedSitesPaged().create() val pinnedSites = pinnedSiteDao.getPinnedSites()
val pagedList = PagedList.Builder(dataSource, 10) assertEquals(1, pinnedSites.size)
.setNotifyExecutor(executor) assertEquals(topSite, pinnedSites[0])
.setFetchExecutor(executor)
.build()
assertEquals(1, pagedList.size)
assertEquals(topSite, pagedList[0]!!)
} }
@Test @Test
...@@ -85,14 +79,9 @@ class PinnedSiteDaoTest { ...@@ -85,14 +79,9 @@ class PinnedSiteDaoTest {
pinnedSiteDao.deletePinnedSite(topSite1) pinnedSiteDao.deletePinnedSite(topSite1)
val dataSource = pinnedSiteDao.getPinnedSitesPaged().create() val pinnedSites = pinnedSiteDao.getPinnedSites()
val pagedList = PagedList.Builder(dataSource, 10)
.setNotifyExecutor(executor)
.setFetchExecutor(executor)
.build()
assertEquals(1, pagedList.size) assertEquals(1, pinnedSites.size)
assertEquals(topSite2, pagedList[0]) assertEquals(topSite2, pinnedSites[0])
} }
} }
/* 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/. */
package mozilla.components.feature.top.sites
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import mozilla.components.browser.storage.sync.PlacesHistoryStorage
import mozilla.components.concept.storage.TopFrecentSiteInfo
import mozilla.components.feature.top.sites.ext.toTopSite
import mozilla.components.support.test.mock
import mozilla.components.support.test.whenever
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.anyInt
import org.mockito.Mockito.verify
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class DefaultTopSitesStorageTest {
private val pinnedSitesStorage: PinnedSiteStorage = mock()
private val historyStorage: PlacesHistoryStorage = mock()
@Test
fun `default top sites are added to pinned site storage on init`() = runBlockingTest {
val defaultTopSites = listOf(
Pair("Mozilla", "https://mozilla.com"),
Pair("Firefox", "https://firefox.com")
)
DefaultTopSitesStorage(
pinnedSitesStorage,
historyStorage,
defaultTopSites,
coroutineContext
)
verify(pinnedSitesStorage).addPinnedSite(
"Mozilla",
"https://mozilla.com",
isDefault = true
)
verify(pinnedSitesStorage).addPinnedSite(
"Firefox",
"https://firefox.com",
isDefault = true
)
}
@Test
fun `addPinnedSite`() = runBlockingTest {
val defaultTopSitesStorage = DefaultTopSitesStorage(
pinnedSitesStorage,
historyStorage,
listOf(),
coroutineContext
)
defaultTopSitesStorage.addPinnedSite("Mozilla", "https://mozilla.com", isDefault = false)
verify(pinnedSitesStorage).addPinnedSite(
"Mozilla",
"https://mozilla.com",
isDefault = false
)
}
@Test
fun `removeTopSite`() = runBlockingTest {
val defaultTopSitesStorage = DefaultTopSitesStorage(
pinnedSitesStorage,
historyStorage,
listOf(),
coroutineContext
)
val frecentSite = TopSite(
id = 1,
title = "Mozilla",
url = "https://mozilla.com",
createdAt = 1,
type = TopSite.Type.FRECENT
)
defaultTopSitesStorage.removeTopSite(frecentSite)
verify(historyStorage).deleteVisitsFor(frecentSite.url)
val pinnedSite = TopSite(
id = 2,
title = "Firefox",
url = "https://firefox.com",
createdAt = 2,
type = TopSite.Type.PINNED
)
defaultTopSitesStorage.removeTopSite(pinnedSite)
verify(pinnedSitesStorage).removePinnedSite(pinnedSite)
val defaultSite = TopSite(
id = 3,
title = "Wikipedia",
url = "https://wikipedia.com",
createdAt = 3,
type = TopSite.Type.DEFAULT
)
defaultTopSitesStorage.removeTopSite(defaultSite)
verify(pinnedSitesStorage).removePinnedSite(defaultSite)
}
@Test
fun `getTopSites returns only default and pinned sites when includeFrecent is false`() = runBlockingTest {
val defaultTopSitesStorage = DefaultTopSitesStorage(
pinnedSitesStorage,
historyStorage,
listOf(),
coroutineContext
)
val defaultSite = TopSite(
id = 1,
title = "Firefox",
url = "https://firefox.com",
createdAt = 1,
type = TopSite.Type.DEFAULT
)
val pinnedSite = TopSite(
id = 2,
title = "Wikipedia",
url = "https://wikipedia.com",
createdAt = 2,
type = TopSite.Type.PINNED
)
whenever(pinnedSitesStorage.getPinnedSites()).thenReturn(
listOf(
defaultSite,
pinnedSite
)
)
var topSites = defaultTopSitesStorage.getTopSites(0, false)
assertTrue(topSites.isEmpty())
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
topSites = defaultTopSitesStorage.getTopSites(1, false)
assertEquals(1, topSites.size)
assertEquals(defaultSite, topSites[0])
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
topSites = defaultTopSitesStorage.getTopSites(2, false)
assertEquals(2, topSites.size)
assertEquals(defaultSite, topSites[0])
assertEquals(pinnedSite, topSites[1])
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
topSites = defaultTopSitesStorage.getTopSites(5, false)
assertEquals(2, topSites.size)
assertEquals(defaultSite, topSites[0])
assertEquals(pinnedSite, topSites[1])
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
}
@Test
fun `getTopSites returns pinned and frecent sites when includeFrecent is true`() = runBlockingTest {
val defaultTopSitesStorage = DefaultTopSitesStorage(
pinnedSitesStorage,
historyStorage,
listOf(),
coroutineContext
)
val defaultSite = TopSite(
id = 1,
title = "Firefox",
url = "https://firefox.com",
createdAt = 1,
type = TopSite.Type.DEFAULT
)
val pinnedSite = TopSite(
id = 2,
title = "Wikipedia",
url = "https://wikipedia.com",
createdAt = 2,
type = TopSite.Type.PINNED
)
whenever(pinnedSitesStorage.getPinnedSites()).thenReturn(
listOf(
defaultSite,
pinnedSite
)
)
val frecentSite1 = TopFrecentSiteInfo("https://mozilla.com", "Mozilla")
whenever(historyStorage.getTopFrecentSites(anyInt())).thenReturn(listOf(frecentSite1))
var topSites = defaultTopSitesStorage.getTopSites(0, true)
assertTrue(topSites.isEmpty())
topSites = defaultTopSitesStorage.getTopSites(1, true)
assertEquals(1, topSites.size)
assertEquals(defaultSite, topSites[0])
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
topSites = defaultTopSitesStorage.getTopSites(2, true)
assertEquals(2, topSites.size)
assertEquals(defaultSite, topSites[0])
assertEquals(pinnedSite, topSites[1])
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
topSites = defaultTopSitesStorage.getTopSites(5, true)
assertEquals(3, topSites.size)
assertEquals(defaultSite, topSites[0])
assertEquals(pinnedSite, topSites[1])
assertEquals(frecentSite1.toTopSite(), topSites[2])
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
val frecentSite2 = TopFrecentSiteInfo("https://example.com", "Example")
val frecentSite3 = TopFrecentSiteInfo("https://getpocket.com", "Pocket")
whenever(historyStorage.getTopFrecentSites(anyInt())).thenReturn(
listOf(
frecentSite1,
frecentSite2,
frecentSite3
)
)
topSites = defaultTopSitesStorage.getTopSites(5, true)
assertEquals(5, topSites.size)
assertEquals(defaultSite, topSites[0])
assertEquals(pinnedSite, topSites[1])
assertEquals(frecentSite1.toTopSite(), topSites[2])
assertEquals(frecentSite2.toTopSite(), topSites[3])
assertEquals(frecentSite3.toTopSite(), topSites[4])
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
val frecentSite4 = TopFrecentSiteInfo("https://example2.com", "Example2")
whenever(historyStorage.getTopFrecentSites(anyInt())).thenReturn(
listOf(
frecentSite1,
frecentSite2,
frecentSite3,
frecentSite4
)
)
topSites = defaultTopSitesStorage.getTopSites(5, true)
assertEquals(5, topSites.size)
assertEquals(defaultSite, topSites[0])
assertEquals(pinnedSite, topSites[1])
assertEquals(frecentSite1.toTopSite(), topSites[2])
assertEquals(frecentSite2.toTopSite(), topSites[3])
assertEquals(frecentSite3.toTopSite(), topSites[4])
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
}
@Test
fun `getTopSites filters out frecent sites that already exist in pinned sites`() = runBlockingTest {
val defaultTopSitesStorage = DefaultTopSitesStorage(
pinnedSitesStorage,
historyStorage,
listOf(),
coroutineContext
)
val defaultSiteFirefox = TopSite(
id = 1,
title = "Firefox",
url = "https://firefox.com",
createdAt = 1,
type = TopSite.Type.DEFAULT
)
val pinnedSite = TopSite(
id = 2,
title = "Wikipedia",
url = "https://wikipedia.com",
createdAt = 2,
type = TopSite.Type.PINNED
)
whenever(pinnedSitesStorage.getPinnedSites()).thenReturn(
listOf(
defaultSiteFirefox,
pinnedSite
)
)
val frecentSiteWithNoTitle = TopFrecentSiteInfo("https://mozilla.com", "")
val frecentSiteFirefox = TopFrecentSiteInfo("https://firefox.com", "Firefox")
val frecentSite = TopFrecentSiteInfo("https://getpocket.com", "Pocket")
whenever(historyStorage.getTopFrecentSites(anyInt())).thenReturn(
listOf(
frecentSiteWithNoTitle,
frecentSiteFirefox,
frecentSite
)
)
val topSites = defaultTopSitesStorage.getTopSites(5, true)
assertEquals(4, topSites.size)
assertEquals(defaultSiteFirefox, topSites[0])
assertEquals(pinnedSite, topSites[1])
assertEquals(frecentSiteWithNoTitle.toTopSite(), topSites[2])
assertEquals(frecentSite.toTopSite(), topSites[3])
assertEquals("mozilla.com", frecentSiteWithNoTitle.toTopSite().title)
assertEquals(defaultTopSitesStorage.cachedTopSites, topSites)
}
}
/* 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/. */
package mozilla.components.feature.top.sites
import mozilla.components.feature.top.sites.presenter.TopSitesPresenter
import mozilla.components.feature.top.sites.view.TopSitesView
import mozilla.components.support.test.mock
import org.junit.Test
import org.mockito.Mockito.verify
class TopSitesFeatureTest {
private val view: TopSitesView = mock()
private val storage: TopSitesStorage = mock()
private val presenter: TopSitesPresenter = mock()
private val config: () -> TopSitesConfig = mock()
private val feature: TopSitesFeature = TopSitesFeature(view, storage, config, presenter)
@Test
fun start() {