Merge "Bluetooth device slice improvement"

This commit is contained in:
TreeHugger Robot
2020-02-24 06:14:59 +00:00
committed by Android (Google) Code Review
5 changed files with 18 additions and 142 deletions

View File

@@ -16,7 +16,6 @@
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;
@@ -38,7 +37,6 @@ 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;
@@ -117,91 +115,17 @@ public class BluetoothDevicesSliceTest {
@Test
@Config(shadows = ShadowBluetoothAdapter.class)
public void getSlice_bluetoothOff_shouldHaveToggle() {
final ShadowBluetoothAdapter adapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
adapter.setState(BluetoothAdapter.STATE_OFF);
public void getSlice_hasBluetoothHardware_shouldHaveBluetoothDevicesTitleAndPairNewDevice() {
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();
assertThat(metadata.getTitle()).isEqualTo(mContext.getString(R.string.bluetooth_devices));
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 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() {
@@ -280,16 +204,6 @@ public class BluetoothDevicesSliceTest {
}
}
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

View File

@@ -242,16 +242,6 @@ 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.
*