Skip to content

Commit

Permalink
Reuse RecyclerViewSelectionController with ActionChips.
Browse files Browse the repository at this point in the history
This change
- adds CycleThroughNoSelection variant to
  RecyclerViewSelectionController (used by ActionChips), and
- uses the RecyclerViewSelectionController with ActionChips,
  obsoleting the AcionChipsAdapter.

Bug: 1490333
Change-Id: Iabbb3c69116a2cc730bbe296141d042d766412c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4985629
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Fred Mello <fredmello@chromium.org>
Auto-Submit: Tomasz Wiszkowski <ender@google.com>
Commit-Queue: Tomasz Wiszkowski <ender@google.com>
Cr-Commit-Position: refs/heads/main@{#1217053}
  • Loading branch information
tomasz-wiszkowski authored and Chromium LUCI CQ committed Oct 30, 2023
1 parent 83cb046 commit 84b2c85
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 336 deletions.
2 changes: 0 additions & 2 deletions chrome/browser/ui/android/omnibox/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ android_library("java") {
"java/src/org/chromium/chrome/browser/omnibox/suggestions/answer/AnswerSuggestionViewProperties.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/answer/AnswerText.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/answer/AnswerTextNewLayout.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/base/ActionChipsAdapter.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/base/ActionChipsBinder.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/base/ActionChipsProcessor.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/base/ActionChipsProperties.java",
Expand Down Expand Up @@ -453,7 +452,6 @@ robolectric_library("junit") {
"java/src/org/chromium/chrome/browser/omnibox/suggestions/action/OmniboxPedalUnitTest.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/answer/AnswerSuggestionProcessorUnitTest.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/answer/AnswerTextNewLayoutUnitTest.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/base/ActionChipsAdapterUnitTest.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/base/ActionChipsProcessorUnitTest.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/base/ActionChipsViewUnitTest.java",
"java/src/org/chromium/chrome/browser/omnibox/suggestions/base/BaseSuggestionProcessorUnitTest.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,33 @@ public class RecyclerViewSelectionController
private int mSelectedItem = RecyclerView.NO_POSITION;
private LayoutManager mLayoutManager;

/** When true, cycling to next/previous item will go through null selection. */
private boolean mCycleThroughNoSelection;

public RecyclerViewSelectionController(LayoutManager layoutManager) {
mLayoutManager = layoutManager;
}

/**
* Specifies whether advancing to previous/next element should go through no selection.
*
* <p>Note that advancing from no selection always proceeds to the next element:
*
* <p>- If the flag is set to `false`, once the last possible element is selected, the user
* cannot advance any further.
*
* <pre>[A] -> [B] -> [C] -> [C] -> [C] -> [C] ...</pre>
*
* <p>- if the flag is set to `true`, once the last possible element is selected and the user
* advances to the next item, selection will re-start from no selection, and advance to the
* first selectable item afterwards.
*
* <pre>[A] -> [B] -> [C] -> [∅] -> [A] -> [B] ...</pre>
*/
public void setCycleThroughNoSelection(boolean cycleThroughNoSelection) {
mCycleThroughNoSelection = cycleThroughNoSelection;
}

@Override
public void onChildViewAttachedToWindow(View view) {
// Force update selection of the view that might come from a recycle pool.
Expand Down Expand Up @@ -49,8 +72,10 @@ public void selectNextItem() {
int newSelectedItem;
if (mSelectedItem == RecyclerView.NO_POSITION) {
newSelectedItem = 0;
} else if (mSelectedItem < mLayoutManager.getItemCount()) {
} else if (mSelectedItem < mLayoutManager.getItemCount() - 1) {
newSelectedItem = mSelectedItem + 1;
} else if (mCycleThroughNoSelection) {
newSelectedItem = RecyclerView.NO_POSITION;
} else {
newSelectedItem = mLayoutManager.getItemCount() - 1;
}
Expand All @@ -67,6 +92,8 @@ public void selectPreviousItem() {
newSelectedItem = mLayoutManager.getItemCount() - 1;
} else if (mSelectedItem > 0) {
newSelectedItem = mSelectedItem - 1;
} else if (mCycleThroughNoSelection) {
newSelectedItem = RecyclerView.NO_POSITION;
} else {
newSelectedItem = 0;
}
Expand All @@ -87,6 +114,7 @@ public View getSelectedView() {
* @param force Whether to apply the selection even if the current selected item has not
* changed.
*/
@VisibleForTesting
public void setSelectedItem(int index, boolean force) {
if (mLayoutManager == null) return;
if (index != RecyclerView.NO_POSITION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.robolectric.annotation.Config;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.annotation.Config;

import org.chromium.base.test.BaseRobolectricTestRunner;

Expand Down Expand Up @@ -77,6 +77,13 @@ public void selectNextItem_fromLast() {
mSelectionController.selectNextItem();
Assert.assertEquals(2, mSelectionController.getSelectedItemForTest());
Assert.assertEquals(mChildView3, mSelectionController.getSelectedView());

// Permit cycling through.
mSelectionController.setCycleThroughNoSelection(true);
mSelectionController.selectNextItem();
Assert.assertEquals(
RecyclerView.NO_POSITION, mSelectionController.getSelectedItemForTest());
Assert.assertEquals(null, mSelectionController.getSelectedView());
}

@Test
Expand Down Expand Up @@ -106,6 +113,13 @@ public void selectPreviousItem_fromFirst() {
mSelectionController.selectPreviousItem();
Assert.assertEquals(0, mSelectionController.getSelectedItemForTest());
Assert.assertEquals(mChildView1, mSelectionController.getSelectedView());

// Permit cycling through.
mSelectionController.setCycleThroughNoSelection(true);
mSelectionController.selectPreviousItem();
Assert.assertEquals(
RecyclerView.NO_POSITION, mSelectionController.getSelectedItemForTest());
Assert.assertEquals(null, mSelectionController.getSelectedView());
}

@Test
Expand Down

This file was deleted.

0 comments on commit 84b2c85

Please sign in to comment.