diff --git a/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSlice.java b/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSlice.java index f8b7fe45fce..6a7527482ae 100644 --- a/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSlice.java +++ b/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSlice.java @@ -63,6 +63,13 @@ public class BluetoothDevicesSlice implements CustomSliceable { @VisibleForTesting static final String BLUETOOTH_DEVICE_HASH_CODE = "bluetooth_device_hash_code"; + /** + * Add the "Pair new device" in the end of slice, when the number of Bluetooth devices is less + * than {@link #DEFAULT_EXPANDED_ROW_COUNT}. + */ + @VisibleForTesting + static final int DEFAULT_EXPANDED_ROW_COUNT = 3; + /** * Refer {@link com.android.settings.bluetooth.BluetoothDevicePreference#compareTo} to sort the * Bluetooth devices by {@link CachedBluetoothDevice}. @@ -70,12 +77,6 @@ public class BluetoothDevicesSlice implements CustomSliceable { private static final Comparator COMPARATOR = Comparator.naturalOrder(); - /** - * Add the "Pair new device" in the end of slice, when the number of Bluetooth devices is less - * than {@link #DEFAULT_EXPANDED_ROW_COUNT}. - */ - private static final int DEFAULT_EXPANDED_ROW_COUNT = 3; - private static final String TAG = "BluetoothDevicesSlice"; private final Context mContext; @@ -127,15 +128,18 @@ public class BluetoothDevicesSlice implements CustomSliceable { .build(); } - // According the number of Bluetooth devices to set sub title of header. + // Get displayable device count. + final int deviceCount = Math.min(rows.size(), DEFAULT_EXPANDED_ROW_COUNT); + + // According to the displayable device count to set sub title of header. listBuilder.setHeader(new ListBuilder.HeaderBuilder() .setTitle(title) - .setSubtitle(getSubTitle(rows.size())) + .setSubtitle(getSubTitle(deviceCount)) .setPrimaryAction(primarySliceAction)); - // Add Bluetooth device rows. - for (ListBuilder.RowBuilder rowBuilder : rows) { - listBuilder.addRow(rowBuilder); + // According to the displayable device count to add bluetooth device rows. + for (int i = 0; i < deviceCount; i++) { + listBuilder.addRow(rows.get(i)); } // Add "Pair new device" if need. @@ -238,7 +242,7 @@ public class BluetoothDevicesSlice implements CustomSliceable { } private List getBluetoothRowBuilder() { - // According Bluetooth devices to create row builders. + // According to Bluetooth devices to create row builders. final List bluetoothRows = new ArrayList<>(); final List bluetoothDevices = getConnectedBluetoothDevices(); for (CachedBluetoothDevice bluetoothDevice : bluetoothDevices) { diff --git a/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSliceTest.java b/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSliceTest.java index ebdc5aa4165..12ae7077c60 100644 --- a/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSliceTest.java +++ b/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSliceTest.java @@ -16,6 +16,9 @@ package com.android.settings.homepage.contextualcards.slices; +import static android.app.slice.Slice.HINT_LIST_ITEM; +import static android.app.slice.SliceItem.FORMAT_SLICE; + import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -34,6 +37,7 @@ import androidx.slice.Slice; import androidx.slice.SliceItem; import androidx.slice.SliceMetadata; import androidx.slice.SliceProvider; +import androidx.slice.core.SliceQuery; import androidx.slice.widget.SliceLiveData; import com.android.settings.R; @@ -97,7 +101,7 @@ public class BluetoothDevicesSliceTest { @Test public void getSlice_hasBluetoothDevices_shouldHaveBluetoothDevicesTitle() { - mockBluetoothDeviceList(); + mockBluetoothDeviceList(1); doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices(); final Slice slice = mBluetoothDevicesSlice.getSlice(); @@ -108,7 +112,7 @@ public class BluetoothDevicesSliceTest { @Test public void getSlice_hasBluetoothDevices_shouldMatchBluetoothMockTitle() { - mockBluetoothDeviceList(); + mockBluetoothDeviceList(1); doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices(); final Slice slice = mBluetoothDevicesSlice.getSlice(); @@ -119,7 +123,7 @@ public class BluetoothDevicesSliceTest { @Test public void getSlice_hasBluetoothDevices_shouldHavePairNewDevice() { - mockBluetoothDeviceList(); + mockBluetoothDeviceList(1); doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices(); final Slice slice = mBluetoothDevicesSlice.getSlice(); @@ -150,9 +154,35 @@ public class BluetoothDevicesSliceTest { mContext.getString(R.string.bluetooth_pairing_pref_title))).isFalse(); } + @Test + public void getSlice_exceedDefaultRowCount_shouldOnlyShowDefaultRows() { + mockBluetoothDeviceList(BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT + 1); + doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices(); + + final Slice slice = mBluetoothDevicesSlice.getSlice(); + + // Get the number of RowBuilders from Slice. + final int rows = SliceQuery.findAll(slice, FORMAT_SLICE, HINT_LIST_ITEM, null).size(); + assertThat(rows).isEqualTo(BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT); + } + + @Test + public void getSlice_exceedDefaultRowCount_shouldContainDefaultCountInSubTitle() { + mockBluetoothDeviceList(BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT + 1); + doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices(); + + final Slice slice = mBluetoothDevicesSlice.getSlice(); + + final SliceMetadata metadata = SliceMetadata.from(mContext, slice); + assertThat(metadata.getSubtitle()).isEqualTo( + mContext.getResources().getQuantityString(R.plurals.show_bluetooth_devices, + BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT, + BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT)); + } + @Test public void onNotifyChange_mediaDevice_shouldActivateDevice() { - mockBluetoothDeviceList(); + mockBluetoothDeviceList(1); doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices(); final Intent intent = new Intent().putExtra( BluetoothDevicesSlice.BLUETOOTH_DEVICE_HASH_CODE, @@ -163,12 +193,14 @@ public class BluetoothDevicesSliceTest { verify(mCachedBluetoothDevice).setActive(); } - private void mockBluetoothDeviceList() { + private void mockBluetoothDeviceList(int deviceCount) { doReturn(BLUETOOTH_MOCK_TITLE).when(mCachedBluetoothDevice).getName(); doReturn(BLUETOOTH_MOCK_SUMMARY).when(mCachedBluetoothDevice).getConnectionSummary(); doReturn(BLUETOOTH_MOCK_ADDRESS).when(mCachedBluetoothDevice).getAddress(); doReturn(true).when(mCachedBluetoothDevice).isConnectedA2dpDevice(); - mBluetoothDeviceList.add(mCachedBluetoothDevice); + for (int i = 0; i < deviceCount; i++) { + mBluetoothDeviceList.add(mCachedBluetoothDevice); + } } private boolean hasTitle(SliceMetadata metadata, String title) {