Skip to content

Commit 9a0986e

Browse files
committed
Bug 2003434 - Fallback to mobile bookmarks root if last saved folder does not exist
1 parent 62d03d7 commit 9a0986e

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

mobile/android/fenix/app/src/main/java/org/mozilla/fenix/components/menu/middleware/MenuDialogMiddleware.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,16 @@ class MenuDialogMiddleware(
223223
val selectedTab = browserMenuState.selectedTab
224224
val url = selectedTab.getUrl() ?: return@launch
225225

226-
val parentGuid = lastSavedFolderCache.getGuid() ?: BookmarkRoot.Mobile.id
226+
// get the last saved folder id
227+
val targetParentFolderId = lastSavedFolderCache.getGuid() ?: BookmarkRoot.Mobile.id
227228

228-
val parentNode = bookmarksStorage.getBookmark(parentGuid).getOrNull()
229+
// get the corresponding bookmark and fallback to mobile root bookmark node
230+
// this is necessary because it's possible that the last saved folder no longer exists (
231+
// e.g. if the folder is removed through sync)
232+
val parentNode = bookmarksStorage.getBookmark(targetParentFolderId).getOrNull()
233+
?: bookmarksStorage.getBookmark(BookmarkRoot.Mobile.id).getOrNull()
234+
235+
val parentGuid = parentNode?.guid ?: BookmarkRoot.Mobile.id
229236

230237
val guidToEdit = addBookmarkUseCase(
231238
url = url,

mobile/android/fenix/app/src/test/java/org/mozilla/fenix/components/menu/MenuDialogMiddlewareTest.kt

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ class MenuDialogMiddlewareTest {
282282
}
283283

284284
@Test
285-
fun `GIVEN last save folder cache has a value WHEN add bookmark action is dispatched for a selected tab THEN bookmark is added with the caches value as its parent`() = runTest(testDispatcher) {
285+
fun `GIVEN last save folder cache has a value WHEN add bookmark action is dispatched for a selected tab THEN bookmark is added with the cached value as its parent`() = runTest(testDispatcher) {
286+
// given that the last saved folder actually exists
287+
val lastSavedFolderId = bookmarksStorage.addFolder(BookmarkRoot.Mobile.id, "last-folder")
288+
.getOrThrow()
289+
`when`(lastSavedFolderCache.getGuid()).thenReturn(lastSavedFolderId)
286290
val url = "https://www.mozilla.org"
287291
val title = "Mozilla"
288292
var dismissWasCalled = false
@@ -304,19 +308,46 @@ class MenuDialogMiddlewareTest {
304308
)
305309
testScheduler.advanceUntilIdle()
306310

307-
`when`(lastSavedFolderCache.getGuid()).thenReturn("cached-value")
308-
309311
store.dispatch(MenuAction.AddBookmark)
310312
testScheduler.advanceUntilIdle()
311313

312-
verify(addBookmarkUseCase).invoke(url = url, title = title, parentGuid = "cached-value")
314+
verify(addBookmarkUseCase).invoke(url = url, title = title, parentGuid = lastSavedFolderId)
313315

314316
captureMiddleware.assertLastAction(BookmarkAction.BookmarkAdded::class) { action: BookmarkAction.BookmarkAdded ->
315317
assertNotNull(action.guidToEdit)
316318
}
317319
assertTrue(dismissWasCalled)
318320
}
319321

322+
@Test
323+
fun `GIVEN last save folder cache has a value that is no longer available THEN a new bookmark is added to the mobile root`() = runTestOnMain {
324+
val url = "https://www.mozilla.org"
325+
val title = "Mozilla"
326+
327+
val browserMenuState = BrowserMenuState(
328+
selectedTab = createTab(
329+
url = url,
330+
title = title,
331+
),
332+
)
333+
val captureMiddleware = CaptureActionsMiddleware<AppState, AppAction>()
334+
val appStore = AppStore(middlewares = listOf(captureMiddleware))
335+
val store = createStore(
336+
appStore = appStore,
337+
menuState = MenuState(
338+
browserMenuState = browserMenuState,
339+
),
340+
onDismiss = { },
341+
)
342+
343+
`when`(lastSavedFolderCache.getGuid()).thenReturn("cached-value")
344+
345+
store.dispatch(MenuAction.AddBookmark)
346+
347+
// we fallback to the mobile root
348+
verify(addBookmarkUseCase).invoke(url = url, title = title, parentGuid = BookmarkRoot.Mobile.id)
349+
}
350+
320351
@Test
321352
fun `GIVEN the last added bookmark does not belong to a folder WHEN bookmark is added THEN bookmark is added to mobile root`() = runTest(testDispatcher) {
322353
val url = "https://www.mozilla.org"

0 commit comments

Comments
 (0)