com.android.settingslib.Utils.formatPercentage(mBatteryLevel)));
// Disconnect all profiles and test connection state summary
- mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_DISCONNECTED);
+ mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED);
assertThat(mCachedDevice.getConnectionSummary()).isNull();
}
+
+ @Test
+ public void testDeviceName_testAliasNameAvailable() {
+ when(mDevice.getAliasName()).thenReturn(DEVICE_ALIAS);
+ when(mDevice.getName()).thenReturn(DEVICE_NAME);
+ CachedBluetoothDevice cachedBluetoothDevice =
+ new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice);
+ // Verify alias is returned on getName
+ assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS);
+ // Verify device is visible
+ assertThat(cachedBluetoothDevice.hasHumanReadableName()).isTrue();
+ }
+
+ @Test
+ public void testDeviceName_testNameNotAvailable() {
+ CachedBluetoothDevice cachedBluetoothDevice =
+ new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice);
+ // Verify device address is returned on getName
+ assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ADDRESS);
+ // Verify device is not visible
+ assertThat(cachedBluetoothDevice.hasHumanReadableName()).isFalse();
+ }
+
+ @Test
+ public void testDeviceName_testRenameDevice() {
+ final String[] alias = {DEVICE_ALIAS};
+ doAnswer(invocation -> alias[0]).when(mDevice).getAliasName();
+ doAnswer(invocation -> {
+ alias[0] = (String) invocation.getArguments()[0];
+ return true;
+ }).when(mDevice).setAlias(anyString());
+ when(mDevice.getName()).thenReturn(DEVICE_NAME);
+ CachedBluetoothDevice cachedBluetoothDevice =
+ new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice);
+ // Verify alias is returned on getName
+ assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS);
+ // Verify null name does not get set
+ cachedBluetoothDevice.setName(null);
+ verify(mDevice, never()).setAlias(any());
+ // Verify new name is set properly
+ cachedBluetoothDevice.setName(DEVICE_ALIAS_NEW);
+ verify(mDevice).setAlias(DEVICE_ALIAS_NEW);
+ // Verify new alias is returned on getName
+ assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS_NEW);
+ }
}