Bluetooth: Only show devices when their names are resolved

* Add a developer menu option to allow name-less devices to be shown
  when a Bluetooth developer needs it, but hide it for non-developer
  users.
* Set BluetoothDevicePreference to invisible when CachedBluetoothDevice
  does not have a name besides MAC address and the above developer option
  is false.
* This affects BluetoothPairingDetail and DevicePickerFragment, but does
  not affect BluetoothSettings. BluetoothSettings will show all paired
  devices regardless whether an user friendly name exists.

Bug: 34685932
Test: pair Bluetooth device, send file over Bluetooth, unit tests
Change-Id: Idd7ad4b1671dfdcf3204efb50eddb6dae1065aa5
This commit is contained in:
Jack He
2017-05-31 18:37:28 -07:00
parent 77bd8c3a73
commit 19ba320263
6 changed files with 102 additions and 5 deletions

View File

@@ -39,6 +39,8 @@ import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -52,6 +54,8 @@ public class BluetoothDevicePreferenceTest {
private Context mContext;
@Mock
private CachedBluetoothDevice mCachedBluetoothDevice;
@Mock
private DeviceListPreferenceFragment mDeviceListPreferenceFragment;
private FakeFeatureFactory mFakeFeatureFactory;
private MetricsFeatureProvider mMetricsFeatureProvider;
@@ -64,7 +68,8 @@ public class BluetoothDevicePreferenceTest {
FakeFeatureFactory.setupForTest(mContext);
mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider();
mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice);
mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
mDeviceListPreferenceFragment);
}
@Test
@@ -151,4 +156,49 @@ public class BluetoothDevicePreferenceTest {
assertThat(mPreference.getIcon()).isEqualTo(
mContext.getDrawable(R.drawable.ic_settings_print));
}
@Test
public void testVisible_notVisibleThenVisible() {
when(mDeviceListPreferenceFragment.shouldShowDevicesWithoutNames()).thenReturn(false);
final boolean[] humanReadableName = {false};
doAnswer(invocation -> humanReadableName[0]).when(mCachedBluetoothDevice)
.hasHumanReadableName();
BluetoothDevicePreference preference =
new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
mDeviceListPreferenceFragment);
assertThat(preference.isVisible()).isFalse();
humanReadableName[0] = true;
preference.onDeviceAttributesChanged();
assertThat(preference.isVisible()).isTrue();
}
@Test
public void testVisible_visibleThenNotVisible() {
when(mDeviceListPreferenceFragment.shouldShowDevicesWithoutNames()).thenReturn(false);
final boolean[] humanReadableName = {true};
doAnswer(invocation -> humanReadableName[0]).when(mCachedBluetoothDevice)
.hasHumanReadableName();
BluetoothDevicePreference preference =
new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
mDeviceListPreferenceFragment);
assertThat(preference.isVisible()).isTrue();
humanReadableName[0] = false;
preference.onDeviceAttributesChanged();
assertThat(preference.isVisible()).isFalse();
}
@Test
public void testVisible_alwaysVisibleWhenEnabled() {
when(mDeviceListPreferenceFragment.shouldShowDevicesWithoutNames()).thenReturn(true);
final boolean[] humanReadableName = {true};
doAnswer(invocation -> humanReadableName[0]).when(mCachedBluetoothDevice)
.hasHumanReadableName();
BluetoothDevicePreference preference =
new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
mDeviceListPreferenceFragment);
assertThat(preference.isVisible()).isTrue();
humanReadableName[0] = false;
preference.onDeviceAttributesChanged();
assertThat(preference.isVisible()).isTrue();
}
}