|
|
|
@ -11,23 +11,29 @@ import io.mockk.mockk
|
|
|
|
|
import kotlinx.coroutines.test.runBlockingTest
|
|
|
|
|
import mozilla.appservices.places.BookmarkRoot
|
|
|
|
|
import mozilla.components.concept.storage.BookmarkNode
|
|
|
|
|
import mozilla.components.concept.storage.BookmarkNodeType
|
|
|
|
|
import mozilla.components.concept.storage.BookmarksStorage
|
|
|
|
|
import mozilla.components.concept.storage.HistoryStorage
|
|
|
|
|
import mozilla.components.concept.storage.VisitInfo
|
|
|
|
|
import mozilla.components.concept.storage.VisitType
|
|
|
|
|
import org.junit.Assert.assertEquals
|
|
|
|
|
import org.junit.Assert.assertFalse
|
|
|
|
|
import org.junit.Assert.assertTrue
|
|
|
|
|
import org.junit.Test
|
|
|
|
|
import org.mozilla.fenix.home.recentbookmarks.RecentBookmark
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
|
|
|
|
class BookmarksUseCaseTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `WHEN adding existing bookmark THEN no new item is stored`() = runBlockingTest {
|
|
|
|
|
val storage = mockk<BookmarksStorage>()
|
|
|
|
|
val bookmarksStorage = mockk<BookmarksStorage>()
|
|
|
|
|
val historyStorage = mockk<HistoryStorage>()
|
|
|
|
|
val bookmarkNode = mockk<BookmarkNode>()
|
|
|
|
|
val useCase = BookmarksUseCase(storage)
|
|
|
|
|
val useCase = BookmarksUseCase(bookmarksStorage, historyStorage)
|
|
|
|
|
|
|
|
|
|
every { bookmarkNode.url }.answers { "https://mozilla.org" }
|
|
|
|
|
coEvery { storage.getBookmarksWithUrl(any()) }.coAnswers { listOf(bookmarkNode) }
|
|
|
|
|
coEvery { bookmarksStorage.getBookmarksWithUrl(any()) }.coAnswers { listOf(bookmarkNode) }
|
|
|
|
|
|
|
|
|
|
val result = useCase.addBookmark("https://mozilla.org", "Mozilla")
|
|
|
|
|
|
|
|
|
@ -36,26 +42,49 @@ class BookmarksUseCaseTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `WHEN adding bookmark THEN new item is stored`() = runBlockingTest {
|
|
|
|
|
val storage = mockk<BookmarksStorage>(relaxed = true)
|
|
|
|
|
val useCase = BookmarksUseCase(storage)
|
|
|
|
|
val bookmarksStorage = mockk<BookmarksStorage>(relaxed = true)
|
|
|
|
|
val historyStorage = mockk<HistoryStorage>(relaxed = true)
|
|
|
|
|
val useCase = BookmarksUseCase(bookmarksStorage, historyStorage)
|
|
|
|
|
|
|
|
|
|
coEvery { storage.getBookmarksWithUrl(any()) }.coAnswers { emptyList() }
|
|
|
|
|
coEvery { bookmarksStorage.getBookmarksWithUrl(any()) }.coAnswers { emptyList() }
|
|
|
|
|
|
|
|
|
|
val result = useCase.addBookmark("https://mozilla.org", "Mozilla")
|
|
|
|
|
|
|
|
|
|
assertTrue(result)
|
|
|
|
|
|
|
|
|
|
coVerify { storage.addItem(BookmarkRoot.Mobile.id, "https://mozilla.org", "Mozilla", null) }
|
|
|
|
|
coVerify { bookmarksStorage.addItem(BookmarkRoot.Mobile.id, "https://mozilla.org", "Mozilla", null) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `WHEN recently saved bookmarks exist THEN retrieve the list from storage`() = runBlockingTest {
|
|
|
|
|
val storage = mockk<BookmarksStorage>(relaxed = true)
|
|
|
|
|
val useCase = BookmarksUseCase(storage)
|
|
|
|
|
val bookmarkNode = mockk<BookmarkNode>()
|
|
|
|
|
val bookmarksStorage = mockk<BookmarksStorage>(relaxed = true)
|
|
|
|
|
val historyStorage = mockk<HistoryStorage>(relaxed = true)
|
|
|
|
|
val useCase = BookmarksUseCase(bookmarksStorage, historyStorage)
|
|
|
|
|
|
|
|
|
|
val visitInfo = VisitInfo(
|
|
|
|
|
url = "https://www.firefox.com",
|
|
|
|
|
title = "firefox",
|
|
|
|
|
visitTime = 2,
|
|
|
|
|
visitType = VisitType.LINK,
|
|
|
|
|
previewImageUrl = "http://firefox.com/image1"
|
|
|
|
|
)
|
|
|
|
|
val bookmarkNode = BookmarkNode(
|
|
|
|
|
BookmarkNodeType.ITEM,
|
|
|
|
|
"987",
|
|
|
|
|
"123",
|
|
|
|
|
2,
|
|
|
|
|
"Firefox",
|
|
|
|
|
"https://www.firefox.com",
|
|
|
|
|
0,
|
|
|
|
|
null
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
coEvery {
|
|
|
|
|
historyStorage.getDetailedVisits(any(), any())
|
|
|
|
|
}.coAnswers { listOf(visitInfo) }
|
|
|
|
|
|
|
|
|
|
coEvery {
|
|
|
|
|
storage.getRecentBookmarks(
|
|
|
|
|
bookmarksStorage.getRecentBookmarks(
|
|
|
|
|
any(),
|
|
|
|
|
any(),
|
|
|
|
|
any()
|
|
|
|
@ -64,10 +93,19 @@ class BookmarksUseCaseTest {
|
|
|
|
|
|
|
|
|
|
val result = useCase.retrieveRecentBookmarks(BookmarksUseCase.DEFAULT_BOOKMARKS_TO_RETRIEVE, 22)
|
|
|
|
|
|
|
|
|
|
assertEquals(listOf(bookmarkNode), result)
|
|
|
|
|
assertEquals(
|
|
|
|
|
listOf(
|
|
|
|
|
RecentBookmark(
|
|
|
|
|
title = bookmarkNode.title,
|
|
|
|
|
url = bookmarkNode.url,
|
|
|
|
|
previewImageUrl = visitInfo.previewImageUrl
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
result
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
coVerify {
|
|
|
|
|
storage.getRecentBookmarks(
|
|
|
|
|
bookmarksStorage.getRecentBookmarks(
|
|
|
|
|
BookmarksUseCase.DEFAULT_BOOKMARKS_TO_RETRIEVE,
|
|
|
|
|
22,
|
|
|
|
|
any()
|
|
|
|
@ -77,17 +115,18 @@ class BookmarksUseCaseTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `WHEN there are no recently saved bookmarks THEN retrieve the empty list from storage`() = runBlockingTest {
|
|
|
|
|
val storage = mockk<BookmarksStorage>(relaxed = true)
|
|
|
|
|
val useCase = BookmarksUseCase(storage)
|
|
|
|
|
val bookmarksStorage = mockk<BookmarksStorage>(relaxed = true)
|
|
|
|
|
val historyStorage = mockk<HistoryStorage>(relaxed = true)
|
|
|
|
|
val useCase = BookmarksUseCase(bookmarksStorage, historyStorage)
|
|
|
|
|
|
|
|
|
|
coEvery { storage.getRecentBookmarks(any(), any(), any()) }.coAnswers { listOf() }
|
|
|
|
|
coEvery { bookmarksStorage.getRecentBookmarks(any(), any(), any()) }.coAnswers { listOf() }
|
|
|
|
|
|
|
|
|
|
val result = useCase.retrieveRecentBookmarks(BookmarksUseCase.DEFAULT_BOOKMARKS_TO_RETRIEVE)
|
|
|
|
|
|
|
|
|
|
assertEquals(listOf<BookmarkNode>(), result)
|
|
|
|
|
|
|
|
|
|
coVerify {
|
|
|
|
|
storage.getRecentBookmarks(
|
|
|
|
|
bookmarksStorage.getRecentBookmarks(
|
|
|
|
|
BookmarksUseCase.DEFAULT_BOOKMARKS_TO_RETRIEVE,
|
|
|
|
|
TimeUnit.DAYS.toMillis(BookmarksUseCase.DEFAULT_BOOKMARKS_DAYS_AGE_TO_RETRIEVE),
|
|
|
|
|
any()
|
|
|
|
|