diff --git a/app/metrics.yaml b/app/metrics.yaml index d8a329771466..c9907011d22d 100644 --- a/app/metrics.yaml +++ b/app/metrics.yaml @@ -2476,6 +2476,22 @@ bookmarks_management: metadata: tags: - Bookmarks + open_all_in_new_tabs: + type: event + description: | + A user opened a folder of bookmarks at once in new tabs. + bugs: + - https://github.com/mozilla-mobile/fenix/issues/11404 + data_reviews: + - https://github.com/mozilla-mobile/fenix/pull/27138 + data_sensitivity: + - interaction + notification_emails: + - android-probes@mozilla.com + expires: 120 + metadata: + tags: + - Bookmarks open_in_private_tab: type: event description: | @@ -2520,6 +2536,22 @@ bookmarks_management: metadata: tags: - Bookmarks + open_all_in_private_tabs: + type: event + description: | + A user opened a folder of bookmarks at once in new private tabs. + bugs: + - https://github.com/mozilla-mobile/fenix/issues/11404 + data_reviews: + - https://github.com/mozilla-mobile/fenix/pull/27138 + data_sensitivity: + - interaction + notification_emails: + - android-probes@mozilla.com + expires: 120 + metadata: + tags: + - Bookmarks edited: type: event description: | diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/BookmarksTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/BookmarksTest.kt index c1c4a425270e..02eca8a74c4b 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/BookmarksTest.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/BookmarksTest.kt @@ -260,6 +260,96 @@ class BookmarksTest { } } + @Test + fun openAllInTabsTest() { + val nbPages = 4 + val webPages = List(nbPages) { + TestAssetHelper.getGenericAsset(mockWebServer, it + 1) + } + + homeScreen { + }.openThreeDotMenu { + }.openBookmarks { + createFolder("root") + createFolder("sub", "root") + createFolder("empty", "root") + }.closeMenu { + } + + browserScreen { + createBookmark(webPages[0].url, "root") + createBookmark(webPages[1].url, "root") + createBookmark(webPages[2].url, "sub") + // out of folder and should not be opened + createBookmark(webPages[3].url) + }.openTabDrawer { + closeTab() + } + + browserScreen { + }.openThreeDotMenu { + }.openBookmarks { + }.openThreeDotMenu("root") { + }.clickOpenAllInTabs { + verifyTabTrayIsOpened() + verifyNormalModeSelected() + + verifyExistingOpenTabs("Test_Page_1") + verifyExistingOpenTabs("Test_Page_2") + verifyExistingOpenTabs("Test_Page_3") + swipeTabRight("Test_Page_1") + swipeTabRight("Test_Page_2") + swipeTabRight("Test_Page_3") + // no more tabs should be presents and auto close tab tray + verifyTabTrayIsClosed() + } + } + + @Test + fun openAllInPrivateTabsTest() { + val nbPages = 4 + val webPages = List(nbPages) { + TestAssetHelper.getGenericAsset(mockWebServer, it + 1) + } + + homeScreen { + }.openThreeDotMenu { + }.openBookmarks { + createFolder("root") + createFolder("sub", "root") + createFolder("empty", "root") + }.closeMenu { + } + + browserScreen { + createBookmark(webPages[0].url, "root") + createBookmark(webPages[1].url, "root") + createBookmark(webPages[2].url, "sub") + // out of folder and should not be opened + createBookmark(webPages[3].url) + }.openTabDrawer { + closeTab() + } + + browserScreen { + }.openThreeDotMenu { + }.openBookmarks { + }.openThreeDotMenu("root") { + }.clickOpenAllInPrivateTabs { + verifyTabTrayIsOpened() + verifyPrivateModeSelected() + + verifyExistingOpenTabs("Test_Page_1") + verifyExistingOpenTabs("Test_Page_2") + verifyExistingOpenTabs("Test_Page_3") + swipeTabRight("Test_Page_1") + swipeTabRight("Test_Page_2") + swipeTabRight("Test_Page_3") + // no more tabs should be presents and auto close tab tray + verifyTabTrayIsClosed() + } + } + @Test fun openBookmarkInPrivateTabTest() { val defaultWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1) diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BookmarksRobot.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BookmarksRobot.kt index 0b4a6d043138..4b4f881fe99b 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BookmarksRobot.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BookmarksRobot.kt @@ -150,12 +150,21 @@ class BookmarksRobot { .click() } - fun createFolder(name: String) { + fun createFolder(name: String, parent: String? = null) { clickAddFolderButton() addNewFolderName(name) + if (!parent.isNullOrBlank()) { + setParentFolder(parent) + } saveNewFolder() } + fun setParentFolder(parentName: String) { + clickParentFolderSelector() + selectFolder(parentName) + navigateUp() + } + fun clickAddFolderButton() { mDevice.waitNotNull( Until.findObject(By.desc("Add folder")), diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt index bab7ef9ac96c..94fdcb927e15 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt @@ -334,13 +334,21 @@ class BrowserRobot { menuSaveImage.click() } - fun createBookmark(url: Uri) { + fun createBookmark(url: Uri, folder: String? = null) { navigationToolbar { }.enterURLAndEnterToBrowser(url) { // needs to wait for the right url to load before saving a bookmark verifyUrl(url.toString()) }.openThreeDotMenu { - }.bookmarkPage { } + }.bookmarkPage { + // continue only if a folder is defined + }.takeIf { !folder.isNullOrBlank() }?.let { + it.openThreeDotMenu { + }.editBookmarkPage { + setParentFolder(folder!!) + saveEditBookmark() + } + } } fun clickLinkMatchingText(expectedText: String) { diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/ThreeDotMenuBookmarksRobot.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/ThreeDotMenuBookmarksRobot.kt index 2c13ba626bda..035aa87ffa4d 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/ThreeDotMenuBookmarksRobot.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/ThreeDotMenuBookmarksRobot.kt @@ -52,6 +52,20 @@ class ThreeDotMenuBookmarksRobot { return TabDrawerRobot.Transition() } + fun clickOpenAllInTabs(interact: TabDrawerRobot.() -> Unit): TabDrawerRobot.Transition { + openAllInTabsButton().click() + + TabDrawerRobot().interact() + return TabDrawerRobot.Transition() + } + + fun clickOpenAllInPrivateTabs(interact: TabDrawerRobot.() -> Unit): TabDrawerRobot.Transition { + openAllInPrivateTabsButton().click() + + TabDrawerRobot().interact() + return TabDrawerRobot.Transition() + } + fun clickDelete(interact: BookmarksRobot.() -> Unit): BookmarksRobot.Transition { deleteButton().click() @@ -71,4 +85,8 @@ private fun openInNewTabButton() = onView(withText("Open in new tab")) private fun openInPrivateTabButton() = onView(withText("Open in private tab")) +private fun openAllInTabsButton() = onView(withText("Open all in new tabs")) + +private fun openAllInPrivateTabsButton() = onView(withText("Open all in private tabs")) + private fun deleteButton() = onView(withText("Delete")) diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/ThreeDotMenuMainRobot.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/ThreeDotMenuMainRobot.kt index 865e17adafc0..12dc1d71665e 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/ThreeDotMenuMainRobot.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/ThreeDotMenuMainRobot.kt @@ -208,6 +208,14 @@ class ThreeDotMenuMainRobot { return BrowserRobot.Transition() } + fun editBookmarkPage(interact: BookmarksRobot.() -> Unit): BookmarksRobot.Transition { + mDevice.waitNotNull(Until.findObject(By.text("Bookmarks")), waitingTime) + editBookmarkButton().click() + + BookmarksRobot().interact() + return BookmarksRobot.Transition() + } + fun openHelp(interact: BrowserRobot.() -> Unit): BrowserRobot.Transition { mDevice.waitNotNull(Until.findObject(By.text("Help")), waitingTime) helpButton().click() diff --git a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkController.kt b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkController.kt index 4f96e26ac694..7226d03ade01 100644 --- a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkController.kt +++ b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkController.kt @@ -7,6 +7,7 @@ package org.mozilla.fenix.library.bookmarks import android.content.ClipData import android.content.ClipboardManager import android.content.res.Resources +import androidx.annotation.VisibleForTesting import androidx.navigation.NavController import androidx.navigation.NavDirections import kotlinx.coroutines.CoroutineScope @@ -15,6 +16,7 @@ import mozilla.appservices.places.BookmarkRoot import mozilla.components.concept.engine.EngineSession import mozilla.components.concept.engine.prompt.ShareData import mozilla.components.concept.storage.BookmarkNode +import mozilla.components.concept.storage.BookmarkNodeType import mozilla.components.feature.tabs.TabsUseCases import mozilla.components.service.fxa.sync.SyncReason import org.mozilla.fenix.BrowserDirection @@ -44,6 +46,7 @@ interface BookmarkController { fun handleCopyUrl(item: BookmarkNode) fun handleBookmarkSharing(item: BookmarkNode) fun handleOpeningBookmark(item: BookmarkNode, mode: BrowsingMode) + fun handleBookmarkFolderOpening(folder: BookmarkNode, mode: BrowsingMode) /** * Handle bookmark nodes deletion @@ -73,8 +76,9 @@ class DefaultBookmarkController( private val store: BookmarkFragmentStore, private val sharedViewModel: BookmarksSharedViewModel, private val tabsUseCases: TabsUseCases?, - private val loadBookmarkNode: suspend (String) -> BookmarkNode?, + private val loadBookmarkNode: suspend (String, Boolean) -> BookmarkNode?, private val showSnackbar: (String) -> Unit, + private val alertHeavyOpen: (Int, () -> Unit) -> Unit, private val deleteBookmarkNodes: (Set, BookmarkRemoveType) -> Unit, private val deleteBookmarkFolder: (Set) -> Unit, private val showTabTray: () -> Unit, @@ -83,6 +87,10 @@ class DefaultBookmarkController( private val resources: Resources = activity.resources + @Suppress("MagicNumber") + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + internal val maxOpenBeforeWarn = 15 + override fun handleBookmarkChanged(item: BookmarkNode) { sharedViewModel.selectedFolder = item store.dispatch(BookmarkFragmentAction.Change(item)) @@ -105,7 +113,7 @@ class DefaultBookmarkController( override fun handleBookmarkExpand(folder: BookmarkNode) { handleAllBookmarksDeselected() scope.launch { - val node = loadBookmarkNode.invoke(folder.guid) ?: return@launch + val node = loadBookmarkNode.invoke(folder.guid, false) ?: return@launch sharedViewModel.selectedFolder = node store.dispatch(BookmarkFragmentAction.Change(node)) } @@ -158,6 +166,59 @@ class DefaultBookmarkController( showTabTray() } + private fun extractURLsFromTree(node: BookmarkNode): MutableList { + val urls = mutableListOf() + + when (node.type) { + BookmarkNodeType.FOLDER -> { + // if not leaf (empty) folder + if (!node.children.isNullOrEmpty()) { + node.children!!.iterator().forEach { + urls.addAll(extractURLsFromTree(it)) + } + } + } + BookmarkNodeType.ITEM -> { + node.url?.let { urls.add(it) } + } + BookmarkNodeType.SEPARATOR -> {} + } + + return urls + } + + override fun handleBookmarkFolderOpening(folder: BookmarkNode, mode: BrowsingMode) { + // potentially heavy function with a lot of bookmarks to open => use a coroutine + scope.launch { + // if more than maxOpenBeforeWarn (15) elements + val tree = loadBookmarkNode.invoke(folder.guid, true) ?: return@launch + val urls = extractURLsFromTree(tree) + + val openAll = { load: Boolean -> + for (url in urls) { + tabsUseCases?.addTab?.invoke( + url, + private = (mode == BrowsingMode.Private), + startLoading = load, + ) + } + activity.browsingModeManager.mode = + BrowsingMode.fromBoolean(mode == BrowsingMode.Private) + showTabTray() + } + + // warn user if more than maxOpenBeforeWarn (15) + if (urls.size >= maxOpenBeforeWarn) { + alertHeavyOpen(urls.size) { + // do not load bookmarks when more than maxOpenBeforeWarn (15) + openAll(false) + } + } else { + openAll(true) + } + } + } + override fun handleBookmarkDeletion(nodes: Set, removeType: BookmarkRemoveType) { deleteBookmarkNodes(nodes, removeType) } diff --git a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragment.kt b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragment.kt index 5eb7f3898d4c..ac791c0291b8 100644 --- a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragment.kt @@ -103,6 +103,7 @@ class BookmarkFragment : LibraryPageFragment(), UserInteractionHan tabsUseCases = activity?.components?.useCases?.tabsUseCases, loadBookmarkNode = ::loadBookmarkNode, showSnackbar = ::showSnackBarWithText, + alertHeavyOpen = ::alertHeavyOpen, deleteBookmarkNodes = ::deleteMulti, deleteBookmarkFolder = ::showRemoveFolderDialog, showTabTray = ::showTabTray, @@ -272,11 +273,11 @@ class BookmarkFragment : LibraryPageFragment(), UserInteractionHan return bookmarkView.onBackPressed() } - private suspend fun loadBookmarkNode(guid: String): BookmarkNode? = withContext(IO) { + private suspend fun loadBookmarkNode(guid: String, recursive: Boolean = false): BookmarkNode? = withContext(IO) { // Only runs if the fragment is attached same as [runIfFragmentIsAttached] context?.let { requireContext().bookmarkStorage - .getTree(guid, false) + .getTree(guid, recursive) ?.let { desktopFolders.withOptionalDesktopFolders(it) } } } @@ -293,6 +294,27 @@ class BookmarkFragment : LibraryPageFragment(), UserInteractionHan } } + private fun alertHeavyOpen(n: Int, function: () -> (Unit)) { + AlertDialog.Builder(requireContext()).apply { + setTitle(R.string.open_all_warning_title) + setMessage(String.format(context.getString(R.string.open_all_warning_message), n)) + setPositiveButton( + R.string.open_all_warning_confirm, + ) { dialog, _ -> + function() + dialog.dismiss() + } + setNegativeButton( + R.string.open_all_warning_cancel, + ) { dialog: DialogInterface, _ -> + dialog.dismiss() + } + setCancelable(false) + create() + show() + } + } + private fun deleteMulti( selected: Set, eventType: BookmarkRemoveType = BookmarkRemoveType.MULTIPLE, diff --git a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragmentInteractor.kt b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragmentInteractor.kt index 4fec702ac7f5..e607e407bfc6 100644 --- a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragmentInteractor.kt +++ b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkFragmentInteractor.kt @@ -80,6 +80,16 @@ class BookmarkFragmentInteractor( } } + override fun onOpenAllInTabs(folder: BookmarkNode) { + require(folder.type == BookmarkNodeType.FOLDER) + bookmarksController.handleBookmarkFolderOpening(folder, BrowsingMode.Normal) + } + + override fun onOpenAllInPrivateTabs(folder: BookmarkNode) { + require(folder.type == BookmarkNodeType.FOLDER) + bookmarksController.handleBookmarkFolderOpening(folder, BrowsingMode.Private) + } + override fun onDelete(nodes: Set) { if (nodes.find { it.type == BookmarkNodeType.SEPARATOR } != null) { throw IllegalStateException("Cannot delete separators") diff --git a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkItemMenu.kt b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkItemMenu.kt index e437490ca2de..24916822aaf6 100644 --- a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkItemMenu.kt +++ b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkItemMenu.kt @@ -13,6 +13,7 @@ import mozilla.components.concept.menu.candidate.TextStyle import mozilla.components.concept.storage.BookmarkNodeType import mozilla.components.support.ktx.android.content.getColorFromAttr import org.mozilla.fenix.R +import org.mozilla.fenix.ext.bookmarkStorage class BookmarkItemMenu( private val context: Context, @@ -25,6 +26,8 @@ class BookmarkItemMenu( Share, OpenInNewTab, OpenInPrivateTab, + OpenAllInTabs, + OpenAllInPrivateTabs, Delete, ; } @@ -32,7 +35,11 @@ class BookmarkItemMenu( val menuController: MenuController by lazy { BrowserMenuController() } @VisibleForTesting - internal fun menuItems(itemType: BookmarkNodeType): List { + @SuppressWarnings("LongMethod") + internal suspend fun menuItems(itemType: BookmarkNodeType, itemId: String): List { + // if have at least one child + val hasAtLeastOneChild = !context.bookmarkStorage.getTree(itemId)?.children.isNullOrEmpty() + return listOfNotNull( if (itemType != BookmarkNodeType.SEPARATOR) { TextMenuCandidate( @@ -79,6 +86,32 @@ class BookmarkItemMenu( } else { null }, + if (hasAtLeastOneChild) { + if (itemType == BookmarkNodeType.FOLDER) { + TextMenuCandidate( + text = context.getString(R.string.bookmark_menu_open_all_in_tabs_button), + ) { + onItemTapped.invoke(Item.OpenAllInTabs) + } + } else { + null + } + } else { + null + }, + if (hasAtLeastOneChild) { + if (itemType == BookmarkNodeType.FOLDER) { + TextMenuCandidate( + text = context.getString(R.string.bookmark_menu_open_all_in_private_tabs_button), + ) { + onItemTapped.invoke(Item.OpenAllInPrivateTabs) + } + } else { + null + } + } else { + null + }, TextMenuCandidate( text = context.getString(R.string.bookmark_menu_delete_button), textStyle = TextStyle(color = context.getColorFromAttr(R.attr.textWarning)), @@ -88,7 +121,10 @@ class BookmarkItemMenu( ) } - fun updateMenu(itemType: BookmarkNodeType) { - menuController.submitList(menuItems(itemType)) + /** + * Update the menu items for the type of bookmark. + */ + suspend fun updateMenu(itemType: BookmarkNodeType, itemId: String) { + menuController.submitList(menuItems(itemType, itemId)) } } diff --git a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkView.kt b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkView.kt index b6cbcf292f04..c609cfdd72ab 100644 --- a/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkView.kt +++ b/app/src/main/java/org/mozilla/fenix/library/bookmarks/BookmarkView.kt @@ -79,6 +79,20 @@ interface BookmarkViewInteractor : SelectionInteractor { */ fun onOpenInPrivateTab(item: BookmarkNode) + /** + * Opens all bookmark items in new tabs. + * + * @param folder the bookmark folder containing all items to open in new tabs + */ + fun onOpenAllInTabs(folder: BookmarkNode) + + /** + * Opens all bookmark items in new private tabs. + * + * @param folder the bookmark folder containing all items to open in new private tabs + */ + fun onOpenAllInPrivateTabs(folder: BookmarkNode) + /** * Deletes a set of bookmark nodes. * diff --git a/app/src/main/java/org/mozilla/fenix/library/bookmarks/viewholders/BookmarkNodeViewHolder.kt b/app/src/main/java/org/mozilla/fenix/library/bookmarks/viewholders/BookmarkNodeViewHolder.kt index dc1d8c8b9bc7..153238c39387 100644 --- a/app/src/main/java/org/mozilla/fenix/library/bookmarks/viewholders/BookmarkNodeViewHolder.kt +++ b/app/src/main/java/org/mozilla/fenix/library/bookmarks/viewholders/BookmarkNodeViewHolder.kt @@ -6,6 +6,9 @@ package org.mozilla.fenix.library.bookmarks.viewholders import androidx.core.view.isVisible import androidx.recyclerview.widget.RecyclerView +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import mozilla.components.concept.storage.BookmarkNode import mozilla.components.concept.storage.BookmarkNodeType import org.mozilla.fenix.R @@ -42,6 +45,8 @@ class BookmarkNodeViewHolder( BookmarkItemMenu.Item.Share -> interactor.onSharePressed(item) BookmarkItemMenu.Item.OpenInNewTab -> interactor.onOpenInNormalTab(item) BookmarkItemMenu.Item.OpenInPrivateTab -> interactor.onOpenInPrivateTab(item) + BookmarkItemMenu.Item.OpenAllInTabs -> interactor.onOpenAllInTabs(item) + BookmarkItemMenu.Item.OpenAllInPrivateTabs -> interactor.onOpenAllInPrivateTabs(item) BookmarkItemMenu.Item.Delete -> interactor.onDelete(setOf(item)) } } @@ -58,7 +63,10 @@ class BookmarkNodeViewHolder( containerView.urlView.isVisible = item.type == BookmarkNodeType.ITEM containerView.setSelectionInteractor(item, mode, interactor) - menu.updateMenu(item.type) + + CoroutineScope(Dispatchers.Default).launch { + menu.updateMenu(item.type, item.guid) + } // Hide menu button if this item is a root folder or is selected if (item.type == BookmarkNodeType.FOLDER && item.inRoots()) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 97f13d4a4a6b..fd7e994ea2b7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -622,6 +622,16 @@ Close + + Confirm open + + You are about to open %d tabs. This might slow down Firefox while the pages are loading. Are you sure you want to continue? + + Open tabs + + Cancel + %d site @@ -858,6 +868,10 @@ Open in new tab Open in private tab + + Open all in new tabs + + Open all in private tabs Delete diff --git a/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkControllerTest.kt b/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkControllerTest.kt index 510389f5f6a3..195c7ec04f28 100644 --- a/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkControllerTest.kt @@ -17,9 +17,11 @@ import io.mockk.coVerify import io.mockk.every import io.mockk.just import io.mockk.mockk +import io.mockk.mockkConstructor import io.mockk.runs import io.mockk.slot import io.mockk.spyk +import io.mockk.unmockkConstructor import io.mockk.verify import io.mockk.verifyOrder import mozilla.appservices.places.BookmarkRoot @@ -193,7 +195,7 @@ class BookmarkControllerTest { fun `handleBookmarkExpand should refresh and change the active bookmark node`() = runTestOnMain { var loadBookmarkNodeInvoked = false createController( - loadBookmarkNode = { + loadBookmarkNode = { _: String, _: Boolean -> loadBookmarkNodeInvoked = true tree }, @@ -358,6 +360,95 @@ class BookmarkControllerTest { } } + @Test + fun `handleBookmarkFolderOpening should open all bookmarks in normal tabs`() { + var showTabTrayInvoked = false + createController( + showTabTray = { + showTabTrayInvoked = true + }, + loadBookmarkNode = { guid: String, _: Boolean -> + fun recurseFind(item: BookmarkNode, guid: String): BookmarkNode? { + if (item.guid == guid) { + return item + } else { + item.children?.iterator()?.forEach { + val res = recurseFind(it, guid) + if (res != null) { + return res + } + } + return null + } + } + recurseFind(tree, guid) + }, + ).handleBookmarkFolderOpening(tree, BrowsingMode.Normal) + + assertTrue(showTabTrayInvoked) + verifyOrder { + addNewTabUseCase.invoke(item.url!!, private = false) + addNewTabUseCase.invoke(item.url!!, private = false) + addNewTabUseCase.invoke(childItem.url!!, private = false) + homeActivity.browsingModeManager.mode = BrowsingMode.Normal + } + } + + @Test + fun `handleBookmarkFolderOpening should open all bookmarks in private tabs`() { + var showTabTrayInvoked = false + createController( + showTabTray = { + showTabTrayInvoked = true + }, + loadBookmarkNode = { guid: String, _: Boolean -> + fun recurseFind(item: BookmarkNode, guid: String): BookmarkNode? { + if (item.guid == guid) { + return item + } else { + item.children?.iterator()?.forEach { + val res = recurseFind(it, guid) + if (res != null) { + return res + } + } + return null + } + } + recurseFind(tree, guid) + }, + ).handleBookmarkFolderOpening(tree, BrowsingMode.Private) + + assertTrue(showTabTrayInvoked) + verifyOrder { + addNewTabUseCase.invoke(item.url!!, private = true) + addNewTabUseCase.invoke(item.url!!, private = true) + addNewTabUseCase.invoke(childItem.url!!, private = true) + homeActivity.browsingModeManager.mode = BrowsingMode.Private + } + } + + @Test + fun `handleBookmarkFolderOpening for more than maxOpenBeforeWarn items should show alert`() { + var alertHeavyOpenInvoked = false + + mockkConstructor(DefaultBookmarkController::class) + every { + anyConstructed() getProperty "maxOpenBeforeWarn" + } propertyType Int::class returns 1 + + createController( + alertHeavyOpen = { _: Int, _: () -> Unit -> alertHeavyOpenInvoked = true }, + loadBookmarkNode = { _: String, _: Boolean -> + tree + }, + ).handleBookmarkFolderOpening(tree, BrowsingMode.Normal) + + unmockkConstructor(DefaultBookmarkController::class) + + assertTrue(alertHeavyOpenInvoked) + } + @Test fun `handleBookmarkDeletion for an item should properly call a passed in delegate`() { var deleteBookmarkNodesInvoked = false @@ -426,8 +517,9 @@ class BookmarkControllerTest { @Suppress("LongParameterList") private fun createController( - loadBookmarkNode: suspend (String) -> BookmarkNode? = { _ -> null }, + loadBookmarkNode: suspend (String, Boolean) -> BookmarkNode? = { _, _ -> null }, showSnackbar: (String) -> Unit = { _ -> }, + alertHeavyOpen: (Int, () -> Unit) -> Unit = { _: Int, _: () -> Unit -> }, deleteBookmarkNodes: (Set, BookmarkRemoveType) -> Unit = { _, _ -> }, deleteBookmarkFolder: (Set) -> Unit = { _ -> }, showTabTray: () -> Unit = { }, @@ -442,6 +534,7 @@ class BookmarkControllerTest { tabsUseCases = tabsUseCases, loadBookmarkNode = loadBookmarkNode, showSnackbar = showSnackbar, + alertHeavyOpen = alertHeavyOpen, deleteBookmarkNodes = deleteBookmarkNodes, deleteBookmarkFolder = deleteBookmarkFolder, showTabTray = showTabTray, diff --git a/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkFragmentInteractorTest.kt b/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkFragmentInteractorTest.kt index a11357e9edae..52f417f5ad39 100644 --- a/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkFragmentInteractorTest.kt +++ b/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkFragmentInteractorTest.kt @@ -170,6 +170,24 @@ class BookmarkFragmentInteractorTest { assertNull(BookmarksManagement.openInPrivateTab.testGetValue()!!.single().extra) } + @Test + fun `open all bookmarks item in new tabs`() { + interactor.onOpenAllInTabs(tree) + + verify { + bookmarkController.handleBookmarkFolderOpening(tree, BrowsingMode.Normal) + } + } + + @Test + fun `open all bookmarks item in new private tabs`() { + interactor.onOpenAllInPrivateTabs(tree) + + verify { + bookmarkController.handleBookmarkFolderOpening(tree, BrowsingMode.Private) + } + } + @Test fun `delete a bookmark item`() { interactor.onDelete(setOf(item)) diff --git a/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkItemMenuTest.kt b/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkItemMenuTest.kt index f251132bf516..a356018983b4 100644 --- a/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkItemMenuTest.kt +++ b/app/src/test/java/org/mozilla/fenix/library/bookmarks/BookmarkItemMenuTest.kt @@ -6,15 +6,23 @@ package org.mozilla.fenix.library.bookmarks import android.content.Context import androidx.appcompat.view.ContextThemeWrapper +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import kotlinx.coroutines.runBlocking +import mozilla.components.concept.menu.candidate.TextMenuCandidate import mozilla.components.concept.menu.candidate.TextStyle import mozilla.components.concept.storage.BookmarkNodeType import mozilla.components.support.ktx.android.content.getColorFromAttr import mozilla.components.support.test.robolectric.testContext import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mozilla.fenix.R +import org.mozilla.fenix.ext.bookmarkStorage import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.mozilla.fenix.library.bookmarks.BookmarkItemMenu.Item @@ -34,37 +42,78 @@ class BookmarkItemMenuTest { } @Test - fun `delete item has special styling`() { - val deleteItem = menu.menuItems(BookmarkNodeType.SEPARATOR).last() - assertEquals("Delete", deleteItem.text) + fun `delete item has special styling`() = runBlocking { + var deleteItem: TextMenuCandidate? = null + mockkStatic("org.mozilla.fenix.ext.BookmarkNodeKt") { + every { any().bookmarkStorage } returns mockk(relaxed = true) + + deleteItem = menu.menuItems(BookmarkNodeType.SEPARATOR, "").last() + } + assertNotNull(deleteItem) + assertEquals("Delete", deleteItem!!.text) assertEquals( TextStyle(color = context.getColorFromAttr(R.attr.textWarning)), - deleteItem.textStyle, + deleteItem!!.textStyle, ) - deleteItem.onClick() + deleteItem!!.onClick() assertEquals(Item.Delete, lastItemTapped) } @Test - fun `edit item appears for folders`() { - val folderItems = menu.menuItems(BookmarkNodeType.FOLDER) - assertEquals(2, folderItems.size) - val (edit, delete) = folderItems + fun `edit item appears for folders`() = runBlocking { + // empty folder + var emptyFolderItems: List? = null + mockkStatic("org.mozilla.fenix.ext.BookmarkNodeKt") { + every { any().bookmarkStorage } returns mockk(relaxed = true) + + emptyFolderItems = menu.menuItems(BookmarkNodeType.FOLDER, "") + } + assertNotNull(emptyFolderItems) + assertEquals(2, emptyFolderItems!!.size) + + // not empty + var folderItems: List? = null + mockkStatic("org.mozilla.fenix.ext.BookmarkNodeKt") { + coEvery { any().bookmarkStorage.getTree("")?.children } returns listOf(mockk(relaxed = true)) + + folderItems = menu.menuItems(BookmarkNodeType.FOLDER, "") + } + assertNotNull(folderItems) + assertEquals(4, folderItems!!.size) + + val (edit, openAll, openAllPrivate, delete) = folderItems!! assertEquals("Edit", edit.text) - edit.onClick() + assertEquals("Open all in new tabs", openAll.text) + assertEquals("Open all in private tabs", openAllPrivate.text) + assertEquals("Delete", delete.text) + edit.onClick() assertEquals(Item.Edit, lastItemTapped) - assertEquals("Delete", delete.text) + + openAll.onClick() + assertEquals(Item.OpenAllInTabs, lastItemTapped) + + openAllPrivate.onClick() + assertEquals(Item.OpenAllInPrivateTabs, lastItemTapped) + + delete.onClick() + assertEquals(Item.Delete, lastItemTapped) } @Test - fun `all item appears for sites`() { - val siteItems = menu.menuItems(BookmarkNodeType.ITEM) - assertEquals(6, siteItems.size) - val (edit, copy, share, openInNewTab, openInPrivateTab, delete) = siteItems + fun `all item appears for sites`() = runBlocking { + var siteItems: List? = null + mockkStatic("org.mozilla.fenix.ext.BookmarkNodeKt") { + every { any().bookmarkStorage } returns mockk(relaxed = true) + + siteItems = menu.menuItems(BookmarkNodeType.ITEM, "") + } + assertNotNull(siteItems) + assertEquals(6, siteItems!!.size) + val (edit, copy, share, openInNewTab, openInPrivateTab, delete) = siteItems!! assertEquals("Edit", edit.text) assertEquals("Copy", copy.text) diff --git a/detekt-baseline.xml b/detekt-baseline.xml index 41a921e1ed3d..e1800681350a 100644 --- a/detekt-baseline.xml +++ b/detekt-baseline.xml @@ -543,11 +543,11 @@ UndocumentedPublicFunction:BookmarkController.kt$BookmarkController$fun handleBookmarkTapped(item: BookmarkNode) UndocumentedPublicFunction:BookmarkController.kt$BookmarkController$fun handleCopyUrl(item: BookmarkNode) UndocumentedPublicFunction:BookmarkController.kt$BookmarkController$fun handleOpeningBookmark(item: BookmarkNode, mode: BrowsingMode) + UndocumentedPublicFunction:BookmarkController.kt$BookmarkController$fun handleBookmarkFolderOpening(folder: BookmarkNode, mode: BrowsingMode) UndocumentedPublicFunction:BookmarkController.kt$BookmarkController$fun handleRequestSync() UndocumentedPublicFunction:BookmarkController.kt$BookmarkController$fun handleSearch() UndocumentedPublicFunction:BookmarkController.kt$BookmarkController$fun handleSelectionModeSwitch() UndocumentedPublicFunction:BookmarkFragmentStore.kt$operator fun BookmarkNode.contains(item: BookmarkNode): Boolean - UndocumentedPublicFunction:BookmarkItemMenu.kt$BookmarkItemMenu$fun updateMenu(itemType: BookmarkNodeType) UndocumentedPublicFunction:BookmarkNodeViewHolder.kt$BookmarkNodeViewHolder$fun bind( item: BookmarkNode, mode: BookmarkFragmentState.Mode, payload: BookmarkPayload, ) UndocumentedPublicFunction:BookmarkSearchController.kt$BookmarkSearchController$fun handleEditingCancelled() UndocumentedPublicFunction:BookmarkSearchController.kt$BookmarkSearchController$fun handleTextChanged(text: String)