Implement new design of Bluetooth card

- show only up to 2 connected devices
- show the Bluetooth toggle only when it's off
- remove subtext
- when bluetooth is on but there is no connected device, show the row
  "Pair new device"

Test: robotest
Bug: 142927894
Fixes: 147892635
Change-Id: I1559fc0a4cb0b42bf447d25417ac454c29fe86c3
This commit is contained in:
Jason Chiu
2020-01-17 12:31:56 +08:00
parent 09f634edfc
commit 75e8f5ea6c
6 changed files with 211 additions and 65 deletions

View File

@@ -16,6 +16,7 @@
package com.android.settings.homepage.contextualcards.slices;
import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
import static android.app.slice.Slice.HINT_LIST_ITEM;
import static android.app.slice.SliceItem.FORMAT_SLICE;
@@ -26,9 +27,9 @@ import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
@@ -37,11 +38,13 @@ import androidx.slice.Slice;
import androidx.slice.SliceItem;
import androidx.slice.SliceMetadata;
import androidx.slice.SliceProvider;
import androidx.slice.core.SliceAction;
import androidx.slice.core.SliceQuery;
import androidx.slice.widget.SliceLiveData;
import com.android.settings.R;
import com.android.settings.testutils.SliceTester;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.After;
@@ -52,6 +55,10 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;
import java.util.ArrayList;
import java.util.List;
@@ -101,18 +108,105 @@ public class BluetoothDevicesSliceTest {
}
@Test
public void getSlice_hasBluetoothDevices_shouldHaveBluetoothDevicesTitle() {
@Config(shadows = ShadowNoBluetoothAdapter.class)
public void getSlice_noBluetoothHardware_shouldReturnNull() {
final Slice slice = mBluetoothDevicesSlice.getSlice();
assertThat(slice).isNull();
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_bluetoothOff_shouldHaveToggle() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_OFF);
final Slice slice = mBluetoothDevicesSlice.getSlice();
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
assertTitleAndIcon(metadata);
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).hasSize(1);
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_bluetoothOn_shouldNotHaveToggle() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_ON);
final Slice slice = mBluetoothDevicesSlice.getSlice();
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
assertTitleAndIcon(metadata);
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).isEmpty();
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_bluetoothTurningOff_shouldHaveToggle() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_ON);
final Intent intent = new Intent().putExtra(EXTRA_TOGGLE_STATE, false);
mBluetoothDevicesSlice.onNotifyChange(intent);
final Slice slice = mBluetoothDevicesSlice.getSlice();
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).hasSize(1);
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_bluetoothTurningOn_shouldHaveToggle() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_OFF);
final Intent intent = new Intent().putExtra(EXTRA_TOGGLE_STATE, true);
mBluetoothDevicesSlice.onNotifyChange(intent);
final Slice slice = mBluetoothDevicesSlice.getSlice();
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).isEmpty();
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_noBluetoothDevice_shouldHavePairNewDeviceRow() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_ON);
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
final Slice slice = mBluetoothDevicesSlice.getSlice();
final List<SliceItem> sliceItems = slice.getItems();
SliceTester.assertAnySliceItemContainsTitle(sliceItems, mContext.getString(
R.string.bluetooth_pairing_pref_title));
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_hasBluetoothDevices_shouldNotHavePairNewDeviceRow() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_ON);
mockBluetoothDeviceList(1);
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
final Slice slice = mBluetoothDevicesSlice.getSlice();
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.bluetooth_devices));
final List<SliceItem> sliceItems = slice.getItems();
SliceTester.assertNoSliceItemContainsTitle(sliceItems, mContext.getString(
R.string.bluetooth_pairing_pref_title));
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_hasBluetoothDevices_shouldMatchBluetoothMockTitle() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_ON);
mockBluetoothDeviceList(1);
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
@@ -123,7 +217,10 @@ public class BluetoothDevicesSliceTest {
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_hasMediaBluetoothDevice_shouldBuildMediaBluetoothAction() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_ON);
mockBluetoothDeviceList(1 /* deviceCount */);
doReturn(true).when(mBluetoothDeviceList.get(0)).isConnectedA2dpDevice();
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
@@ -134,7 +231,10 @@ public class BluetoothDevicesSliceTest {
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_noMediaBluetoothDevice_shouldNotBuildMediaBluetoothAction() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_ON);
mockBluetoothDeviceList(1 /* deviceCount */);
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
@@ -144,18 +244,10 @@ public class BluetoothDevicesSliceTest {
}
@Test
public void getSlice_noBluetoothDevices_shouldHaveNoBluetoothDevicesTitle() {
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
final Slice slice = mBluetoothDevicesSlice.getSlice();
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
assertThat(metadata.getTitle()).isEqualTo(
mContext.getString(R.string.no_bluetooth_devices));
}
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_exceedDefaultRowCount_shouldOnlyShowDefaultRows() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_ON);
mockBluetoothDeviceList(BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT + 1);
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
@@ -166,20 +258,6 @@ public class BluetoothDevicesSliceTest {
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(1);
@@ -201,4 +279,22 @@ public class BluetoothDevicesSliceTest {
mBluetoothDeviceList.add(mCachedBluetoothDevice);
}
}
private void assertTitleAndIcon(SliceMetadata metadata) {
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(
R.string.bluetooth_settings_title));
final SliceAction primaryAction = metadata.getPrimaryAction();
final IconCompat expectedToggleIcon = IconCompat.createWithResource(mContext,
com.android.internal.R.drawable.ic_settings_bluetooth);
assertThat(primaryAction.getIcon().toString()).isEqualTo(expectedToggleIcon.toString());
}
@Implements(BluetoothAdapter.class)
public static class ShadowNoBluetoothAdapter extends ShadowBluetoothAdapter {
@Implementation
protected static BluetoothAdapter getDefaultAdapter() {
return null;
}
}
}

View File

@@ -20,7 +20,6 @@ import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context;

View File

@@ -242,6 +242,16 @@ public class SliceTester {
assertThat(hasText(sliceItems, title, HINT_TITLE)).isTrue();
}
/**
* Assert no slice item contains title.
*
* @param sliceItems All slice items of a Slice.
* @param title Title for asserting.
*/
public static void assertNoSliceItemContainsTitle(List<SliceItem> sliceItems, String title) {
assertThat(hasText(sliceItems, title, HINT_TITLE)).isFalse();
}
/**
* Assert any slice item contains subtitle.
*