[fenix] Move BookmarkNode extensions to helper class (https://github.com/mozilla-mobile/fenix/pull/4752)
parent
317000247f
commit
e823891c6b
@ -0,0 +1,120 @@
|
||||
/* 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 org.mozilla.fenix.library.bookmarks
|
||||
|
||||
import android.content.Context
|
||||
import mozilla.appservices.places.BookmarkRoot
|
||||
import mozilla.components.concept.storage.BookmarkNode
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.components
|
||||
|
||||
class DesktopFolders(
|
||||
context: Context,
|
||||
private val showMobileRoot: Boolean
|
||||
) {
|
||||
|
||||
private val bookmarksStorage = context.components.core.bookmarksStorage
|
||||
private val accountManager = context.components.backgroundServices.accountManager
|
||||
|
||||
private val bookmarksTitle = context.getString(R.string.library_bookmarks)
|
||||
|
||||
/**
|
||||
* Map of [BookmarkNode.title] to translated strings.
|
||||
*/
|
||||
private val rootTitles: Map<String, String> = if (showMobileRoot) {
|
||||
mapOf(
|
||||
"root" to bookmarksTitle,
|
||||
"mobile" to bookmarksTitle,
|
||||
"menu" to context.getString(R.string.library_desktop_bookmarks_menu),
|
||||
"toolbar" to context.getString(R.string.library_desktop_bookmarks_toolbar),
|
||||
"unfiled" to context.getString(R.string.library_desktop_bookmarks_unfiled)
|
||||
)
|
||||
} else {
|
||||
mapOf(
|
||||
"root" to context.getString(R.string.library_desktop_bookmarks_root),
|
||||
"menu" to context.getString(R.string.library_desktop_bookmarks_menu),
|
||||
"toolbar" to context.getString(R.string.library_desktop_bookmarks_toolbar),
|
||||
"unfiled" to context.getString(R.string.library_desktop_bookmarks_unfiled)
|
||||
)
|
||||
}
|
||||
|
||||
fun withRootTitle(node: BookmarkNode): BookmarkNode =
|
||||
if (rootTitles.containsKey(node.title)) node.copy(title = rootTitles[node.title]) else node
|
||||
|
||||
suspend fun withOptionalDesktopFolders(node: BookmarkNode): BookmarkNode {
|
||||
val loggedIn = accountManager.authenticatedAccount() != null
|
||||
|
||||
return when (node.guid) {
|
||||
BookmarkRoot.Mobile.id -> if (loggedIn) {
|
||||
// We're going to make a copy of the mobile node, and add-in a synthetic child folder to the top of the
|
||||
// children's list that contains all of the desktop roots.
|
||||
val childrenWithVirtualFolder =
|
||||
listOfNotNull(virtualDesktopFolder()) + node.children.orEmpty()
|
||||
|
||||
node.copy(children = childrenWithVirtualFolder)
|
||||
} else {
|
||||
node
|
||||
}
|
||||
BookmarkRoot.Root.id ->
|
||||
node.copy(
|
||||
title = rootTitles[node.title],
|
||||
children = if (showMobileRoot) {
|
||||
restructureMobileRoots(node.children)
|
||||
} else {
|
||||
restructureDesktopRoots(node.children)
|
||||
}
|
||||
)
|
||||
BookmarkRoot.Menu.id, BookmarkRoot.Toolbar.id, BookmarkRoot.Unfiled.id ->
|
||||
// If we're looking at one of the desktop roots, change their titles to friendly names.
|
||||
node.copy(title = rootTitles[node.title])
|
||||
else ->
|
||||
// Otherwise, just return the node as-is.
|
||||
node
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun virtualDesktopFolder(): BookmarkNode? {
|
||||
val rootNode = bookmarksStorage.getTree(BookmarkRoot.Root.id, recursive = false) ?: return null
|
||||
return rootNode.copy(title = rootTitles[rootNode.title])
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes 'mobile' root (to avoid a cyclical bookmarks tree in the UI) and renames other roots to friendly titles.
|
||||
*/
|
||||
private fun restructureDesktopRoots(roots: List<BookmarkNode>?): List<BookmarkNode>? {
|
||||
roots ?: return null
|
||||
|
||||
return roots.filter { rootTitles.containsKey(it.title) }
|
||||
.map { it.copy(title = rootTitles[it.title]) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Restructures roots to place desktop roots underneath the mobile root and renames them to friendly titles.
|
||||
* This provides a recognizable bookmark tree when offering destinations to move a bookmark.
|
||||
*/
|
||||
private fun restructureMobileRoots(roots: List<BookmarkNode>?): List<BookmarkNode>? {
|
||||
roots ?: return null
|
||||
|
||||
val loggedIn = accountManager.authenticatedAccount() != null
|
||||
|
||||
val others = if (loggedIn) {
|
||||
roots.filter { it.guid != BookmarkRoot.Mobile.id }
|
||||
.map { it.copy(title = rootTitles[it.title]) }
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
val mobileRoot = roots.find { it.guid == BookmarkRoot.Mobile.id } ?: return roots
|
||||
val mobileChildren = others + mobileRoot.children.orEmpty()
|
||||
|
||||
// Note that the desktop bookmarks folder does not appear because it is not selectable as a parent
|
||||
return listOf(
|
||||
mobileRoot.copy(
|
||||
children = mobileChildren,
|
||||
title = bookmarksTitle
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
/* 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 org.mozilla.fenix.library.bookmarks
|
||||
|
||||
import android.content.Context
|
||||
import assertk.assertThat
|
||||
import assertk.assertions.isEqualTo
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.spyk
|
||||
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import mozilla.appservices.places.BookmarkRoot
|
||||
import mozilla.components.browser.storage.sync.PlacesBookmarksStorage
|
||||
import mozilla.components.concept.storage.BookmarkNode
|
||||
import mozilla.components.concept.storage.BookmarkNodeType
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.TestApplication
|
||||
import org.mozilla.fenix.ext.components
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@ObsoleteCoroutinesApi
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(application = TestApplication::class)
|
||||
class DesktopFoldersTest {
|
||||
|
||||
private lateinit var context: Context
|
||||
private lateinit var bookmarksStorage: PlacesBookmarksStorage
|
||||
|
||||
private val basicNode = BookmarkNode(
|
||||
type = BookmarkNodeType.FOLDER,
|
||||
guid = BookmarkRoot.Root.id,
|
||||
parentGuid = null,
|
||||
title = BookmarkRoot.Root.name,
|
||||
position = 0,
|
||||
url = null,
|
||||
children = null
|
||||
)
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
context = spyk(testContext)
|
||||
bookmarksStorage = mockk()
|
||||
every { context.components.core.bookmarksStorage } returns bookmarksStorage
|
||||
every { context.components.backgroundServices.accountManager.authenticatedAccount() } returns null
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withRootTitle and do showMobileRoot`() {
|
||||
val desktopFolders = DesktopFolders(context, showMobileRoot = true)
|
||||
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("root")).title)
|
||||
.isEqualTo(testContext.getString(R.string.library_bookmarks))
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("mobile")).title)
|
||||
.isEqualTo(testContext.getString(R.string.library_bookmarks))
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("menu")).title)
|
||||
.isEqualTo(testContext.getString(R.string.library_desktop_bookmarks_menu))
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("toolbar")).title)
|
||||
.isEqualTo(testContext.getString(R.string.library_desktop_bookmarks_toolbar))
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("unfiled")).title)
|
||||
.isEqualTo(testContext.getString(R.string.library_desktop_bookmarks_unfiled))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withRootTitle and do not showMobileRoot`() {
|
||||
val desktopFolders = DesktopFolders(context, showMobileRoot = false)
|
||||
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("root")).title)
|
||||
.isEqualTo(testContext.getString(R.string.library_desktop_bookmarks_root))
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("mobile")))
|
||||
.isEqualTo(mockNodeWithTitle("mobile"))
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("menu")).title)
|
||||
.isEqualTo(testContext.getString(R.string.library_desktop_bookmarks_menu))
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("toolbar")).title)
|
||||
.isEqualTo(testContext.getString(R.string.library_desktop_bookmarks_toolbar))
|
||||
assertThat(desktopFolders.withRootTitle(mockNodeWithTitle("unfiled")).title)
|
||||
.isEqualTo(testContext.getString(R.string.library_desktop_bookmarks_unfiled))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withOptionalDesktopFolders mobile node and logged out`() = runBlocking {
|
||||
every { context.components.backgroundServices.accountManager.authenticatedAccount() } returns null
|
||||
val node = basicNode.copy(guid = BookmarkRoot.Mobile.id, title = BookmarkRoot.Mobile.name)
|
||||
val desktopFolders = DesktopFolders(context, showMobileRoot = true)
|
||||
|
||||
assertSame(node, desktopFolders.withOptionalDesktopFolders(node))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withOptionalDesktopFolders other node`() = runBlocking {
|
||||
val node = basicNode.copy(guid = "12345")
|
||||
val desktopFolders = DesktopFolders(context, showMobileRoot = true)
|
||||
|
||||
assertSame(node, desktopFolders.withOptionalDesktopFolders(node))
|
||||
}
|
||||
|
||||
private fun mockNodeWithTitle(title: String) = basicNode.copy(title = title)
|
||||
}
|
Loading…
Reference in New Issue