Limit displayable rows in BluetoothDevicesSlice

The maximum displayable row count in current design is 3.
i.e., There are 1 header and 3 rows in a Slice when the number of
Bluetooth device exceeds 3.

Change-Id: Ie585617da0665dde4cbe1ab60a2693e3e1199c27
Fixes: 121308955
Bug: 114807655
Test: visual, robotests
diff --git a/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSlice.java b/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSlice.java
index f8b7fe45..6a75274 100644
--- a/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSlice.java
+++ b/src/com/android/settings/homepage/contextualcards/slices/BluetoothDevicesSlice.java
@@ -64,18 +64,19 @@
     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}.
      */
     private static final Comparator<CachedBluetoothDevice> 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 @@
                     .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 @@
     }
 
     private List<ListBuilder.RowBuilder> getBluetoothRowBuilder() {
-        // According Bluetooth devices to create row builders.
+        // According to Bluetooth devices to create row builders.
         final List<ListBuilder.RowBuilder> bluetoothRows = new ArrayList<>();
         final List<CachedBluetoothDevice> 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 ebdc5aa..12ae707 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.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 @@
 
     @Test
     public void getSlice_hasBluetoothDevices_shouldHaveBluetoothDevicesTitle() {
-        mockBluetoothDeviceList();
+        mockBluetoothDeviceList(1);
         doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
 
         final Slice slice = mBluetoothDevicesSlice.getSlice();
@@ -108,7 +112,7 @@
 
     @Test
     public void getSlice_hasBluetoothDevices_shouldMatchBluetoothMockTitle() {
-        mockBluetoothDeviceList();
+        mockBluetoothDeviceList(1);
         doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
 
         final Slice slice = mBluetoothDevicesSlice.getSlice();
@@ -119,7 +123,7 @@
 
     @Test
     public void getSlice_hasBluetoothDevices_shouldHavePairNewDevice() {
-        mockBluetoothDeviceList();
+        mockBluetoothDeviceList(1);
         doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
 
         final Slice slice = mBluetoothDevicesSlice.getSlice();
@@ -151,8 +155,34 @@
     }
 
     @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 @@
         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) {