Commit 6f6baa4f authored by Theresa Wellington's avatar Theresa Wellington Committed by Commit Bot

[Home] Exit search if bookmark deleted while search query empty

If the user initiates a search, then deletes a folder without entering
a query, we were previously crashing. Folders are not typically shown
in search view except when the query is empty. Prevent the crash by
exiting search if a bookmark is removed while the search query is empty.

BUG=789910

Change-Id: If4c3586deb72ce5cd61c87a903ba594d1a706351
Reviewed-on: https://chromium-review.googlesource.com/804419
Commit-Queue: Theresa <twellington@chromium.org>
Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521157}
parent 08800cd1
...@@ -73,6 +73,12 @@ class BookmarkItemsAdapter ...@@ -73,6 +73,12 @@ class BookmarkItemsAdapter
public void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, BookmarkItem node, public void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, BookmarkItem node,
boolean isDoingExtensiveChanges) { boolean isDoingExtensiveChanges) {
assert mDelegate != null; assert mDelegate != null;
if (mDelegate.getCurrentState() == BookmarkUIState.STATE_SEARCHING
&& TextUtils.equals(mSearchText, EMPTY_QUERY)) {
mDelegate.closeSearchUI();
}
if (node.isFolder()) { if (node.isFolder()) {
mDelegate.notifyStateChange(BookmarkItemsAdapter.this); mDelegate.notifyStateChange(BookmarkItemsAdapter.this);
} else { } else {
......
...@@ -410,15 +410,7 @@ public class BookmarkManager implements BookmarkDelegate, SearchDelegate, ...@@ -410,15 +410,7 @@ public class BookmarkManager implements BookmarkDelegate, SearchDelegate,
@Override @Override
public void closeSearchUI() { public void closeSearchUI() {
mSelectableListLayout.onEndSearch(); mToolbar.hideSearchView();
// Pop the search state off the stack.
mStateStack.pop();
// Set the state back to the folder that was previously being viewed. Listeners, including
// the BookmarkItemsAdapter, will be notified of the change and the list of bookmarks will
// be updated.
setState(mStateStack.pop());
} }
@Override @Override
...@@ -456,7 +448,15 @@ public class BookmarkManager implements BookmarkDelegate, SearchDelegate, ...@@ -456,7 +448,15 @@ public class BookmarkManager implements BookmarkDelegate, SearchDelegate,
@Override @Override
public void onEndSearch() { public void onEndSearch() {
closeSearchUI(); mSelectableListLayout.onEndSearch();
// Pop the search state off the stack.
mStateStack.pop();
// Set the state back to the folder that was previously being viewed. Listeners, including
// the BookmarkItemsAdapter, will be notified of the change and the list of bookmarks will
// be updated.
setState(mStateStack.pop());
} }
// Testing methods // Testing methods
...@@ -466,6 +466,11 @@ public class BookmarkManager implements BookmarkDelegate, SearchDelegate, ...@@ -466,6 +466,11 @@ public class BookmarkManager implements BookmarkDelegate, SearchDelegate,
return mToolbar; return mToolbar;
} }
@VisibleForTesting
public BookmarkUndoController getUndoControllerForTests() {
return mUndoController;
}
/** /**
* @param preventLoading Whether to prevent the bookmark model from fully loading for testing. * @param preventLoading Whether to prevent the bookmark model from fully loading for testing.
*/ */
......
...@@ -317,6 +317,62 @@ public class BookmarkTest { ...@@ -317,6 +317,62 @@ public class BookmarkTest {
Assert.assertEquals(BookmarkUIState.STATE_FOLDER, delegate.getCurrentState()); Assert.assertEquals(BookmarkUIState.STATE_FOLDER, delegate.getCurrentState());
} }
@Test
@MediumTest
public void testSearchBookmarks_Delete() throws Exception {
BookmarkPromoHeader.forcePromoStateForTests(BookmarkPromoHeader.PromoState.PROMO_NONE);
BookmarkId testFolder = addFolder(TEST_FOLDER_TITLE);
BookmarkId testBookmark = addBookmark(TEST_PAGE_TITLE_GOOGLE, mTestPage);
addBookmark(TEST_PAGE_TITLE_FOO, mTestPageFoo);
openBookmarkManager();
BookmarkItemsAdapter adapter = ((BookmarkItemsAdapter) mItemsContainer.getAdapter());
BookmarkManager manager = (BookmarkManager) adapter.getDelegateForTesting();
Assert.assertEquals(BookmarkUIState.STATE_FOLDER, manager.getCurrentState());
assertBookmarkItems("Wrong number of items before starting search.", 3, adapter, manager);
// Start searching without entering a query.
ThreadUtils.runOnUiThreadBlocking(manager::openSearchUI);
Assert.assertEquals(BookmarkUIState.STATE_SEARCHING, manager.getCurrentState());
// Select the folder and delete it.
ThreadUtils.runOnUiThreadBlocking(
() -> manager.getSelectionDelegate().toggleSelectionForItem(adapter.getItem(0)));
ThreadUtils.runOnUiThreadBlocking(
()
-> manager.getToolbarForTests().onMenuItemClick(
manager.getToolbarForTests().getMenu().findItem(
R.id.selection_mode_delete_menu_id)));
// Search should be exited and the folder should be gone.
Assert.assertEquals(BookmarkUIState.STATE_FOLDER, manager.getCurrentState());
assertBookmarkItems("Wrong number of items before starting search.", 2, adapter, manager);
// Start searching, enter a query.
ThreadUtils.runOnUiThreadBlocking(manager::openSearchUI);
Assert.assertEquals(BookmarkUIState.STATE_SEARCHING, manager.getCurrentState());
searchBookmarks("Google");
assertBookmarkItems(
"Wrong number of items after searching.", 1, mItemsContainer.getAdapter(), manager);
// Remove the bookmark.
removeBookmark(testBookmark);
// The user should still be searching, and the bookmark should be gone.
Assert.assertEquals(BookmarkUIState.STATE_SEARCHING, manager.getCurrentState());
assertBookmarkItems(
"Wrong number of items after searching.", 0, mItemsContainer.getAdapter(), manager);
// Undo the deletion.
ThreadUtils.runOnUiThreadBlocking(() -> manager.getUndoControllerForTests().onAction(null));
// The user should still be searching, and the bookmark should reappear.
Assert.assertEquals(BookmarkUIState.STATE_SEARCHING, manager.getCurrentState());
assertBookmarkItems(
"Wrong number of items after searching.", 1, mItemsContainer.getAdapter(), manager);
}
@Test @Test
@MediumTest @MediumTest
@Feature({"RenderTest"}) @Feature({"RenderTest"})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment