Set max num of slices allowed in panel view

Setup a max allowed num for PanelSlicesAdapter to prevent too
many slices showing in single panel.

Test: Manual verify
Test: atest PanelSlicesAdapterTest
Fixes: 129358092
Change-Id: I7b72a29489e597b8309d74841eaeab0fe42aace6
This commit is contained in:
lindatseng
2019-04-16 16:07:36 -07:00
parent cef4e42dc2
commit 634d8b3ee4
2 changed files with 44 additions and 16 deletions

View File

@@ -44,6 +44,12 @@ import java.util.List;
public class PanelSlicesAdapter public class PanelSlicesAdapter
extends RecyclerView.Adapter<PanelSlicesAdapter.SliceRowViewHolder> { extends RecyclerView.Adapter<PanelSlicesAdapter.SliceRowViewHolder> {
/**
* Maximum number of slices allowed on the panel view.
*/
@VisibleForTesting
static final int MAX_NUM_OF_SLICES = 5;
private final List<LiveData<Slice>> mSliceLiveData; private final List<LiveData<Slice>> mSliceLiveData;
private final int mMetricsCategory; private final int mMetricsCategory;
private final PanelFragment mPanelFragment; private final PanelFragment mPanelFragment;
@@ -70,14 +76,21 @@ public class PanelSlicesAdapter
sliceRowViewHolder.onBind(mSliceLiveData.get(position)); sliceRowViewHolder.onBind(mSliceLiveData.get(position));
} }
/**
* Return the number of available items in the adapter with max number of slices enforced.
*/
@Override @Override
public int getItemCount() { public int getItemCount() {
return mSliceLiveData.size(); return Math.min(mSliceLiveData.size(), MAX_NUM_OF_SLICES);
} }
/**
* Return the available data from the adapter. If the number of Slices over the max number
* allowed, the list will only have the first MAX_NUM_OF_SLICES of slices.
*/
@VisibleForTesting @VisibleForTesting
List<LiveData<Slice>> getData() { List<LiveData<Slice>> getData() {
return mSliceLiveData; return mSliceLiveData.subList(0, getItemCount());
} }
/** /**

View File

@@ -16,12 +16,14 @@
package com.android.settings.panel; package com.android.settings.panel;
import static com.android.settings.panel.PanelSlicesAdapter.MAX_NUM_OF_SLICES;
import static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_INDICATOR_SLICE_URI; import static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_INDICATOR_SLICE_URI;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -40,7 +42,6 @@ import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before; import org.junit.Before;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
@@ -62,11 +63,6 @@ public class PanelSlicesAdapterTest {
private FakePanelContent mFakePanelContent; private FakePanelContent mFakePanelContent;
private List<LiveData<Slice>> mData = new ArrayList<>(); private List<LiveData<Slice>> mData = new ArrayList<>();
@Mock
private LiveData<Slice> mLiveData;
private Slice mSlice;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
@@ -91,17 +87,18 @@ public class PanelSlicesAdapterTest {
} }
private void constructTestLiveData(Uri uri) { private void addTestLiveData(Uri uri) {
// Create a slice to return for the LiveData // Create a slice to return for the LiveData
mSlice = spy(new Slice()); final Slice slice = spy(new Slice());
doReturn(uri).when(mSlice).getUri(); doReturn(uri).when(slice).getUri();
when(mLiveData.getValue()).thenReturn(mSlice); final LiveData<Slice> liveData = mock(LiveData.class);
mData.add(mLiveData); when(liveData.getValue()).thenReturn(slice);
mData.add(liveData);
} }
@Test @Test
public void onCreateViewHolder_returnsSliceRowViewHolder() { public void onCreateViewHolder_returnsSliceRowViewHolder() {
constructTestLiveData(DATA_URI); addTestLiveData(DATA_URI);
final PanelSlicesAdapter adapter = final PanelSlicesAdapter adapter =
new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */); new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */);
final ViewGroup view = new FrameLayout(mContext); final ViewGroup view = new FrameLayout(mContext);
@@ -111,9 +108,27 @@ public class PanelSlicesAdapterTest {
assertThat(viewHolder.sliceView).isNotNull(); assertThat(viewHolder.sliceView).isNotNull();
} }
@Test
public void sizeOfAdapter_shouldNotExceedMaxNum() {
for (int i = 0; i < MAX_NUM_OF_SLICES + 2; i++) {
addTestLiveData(DATA_URI);
}
assertThat(mData.size()).isEqualTo(MAX_NUM_OF_SLICES + 2);
final PanelSlicesAdapter adapter =
new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */);
final ViewGroup view = new FrameLayout(mContext);
final PanelSlicesAdapter.SliceRowViewHolder viewHolder =
adapter.onCreateViewHolder(view, 0);
assertThat(adapter.getItemCount()).isEqualTo(MAX_NUM_OF_SLICES);
assertThat(adapter.getData().size()).isEqualTo(MAX_NUM_OF_SLICES);
}
@Test @Test
public void nonMediaOutputIndicatorSlice_shouldAllowDividerAboveAndBelow() { public void nonMediaOutputIndicatorSlice_shouldAllowDividerAboveAndBelow() {
constructTestLiveData(DATA_URI); addTestLiveData(DATA_URI);
final PanelSlicesAdapter adapter = final PanelSlicesAdapter adapter =
new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */); new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */);
final int position = 0; final int position = 0;
@@ -129,7 +144,7 @@ public class PanelSlicesAdapterTest {
@Test @Test
public void mediaOutputIndicatorSlice_shouldNotAllowDividerAbove() { public void mediaOutputIndicatorSlice_shouldNotAllowDividerAbove() {
constructTestLiveData(MEDIA_OUTPUT_INDICATOR_SLICE_URI); addTestLiveData(MEDIA_OUTPUT_INDICATOR_SLICE_URI);
final PanelSlicesAdapter adapter = final PanelSlicesAdapter adapter =
new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */); new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */);