Add device name for connect string

When there are more than one connected device, show
a general summary.

Bug: 37365660
Test: RunSettingsRoboTests
Change-Id: I0eed734e82750969bef97a61dd59167e679c0203
This commit is contained in:
jackqdyulei
2017-04-18 13:20:34 -07:00
parent 9e89d528f6
commit 471d71bae0
2 changed files with 96 additions and 14 deletions

View File

@@ -16,7 +16,10 @@
package com.android.settings.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import com.android.settings.R;
@@ -35,29 +38,38 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowBluetoothAdapter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BluetoothSummaryUpdaterTest {
private static final String DEVICE_NAME = "Nightshade";
private static final String DEVICE_KEYBOARD_NAME = "Bluetooth Keyboard";
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private LocalBluetoothManager mBluetoothManager;
@Mock
private LocalBluetoothAdapter mBtAdapter;
private BluetoothSummaryUpdater mSummaryUpdater;
@Mock
private BluetoothDevice mConnectedDevice;
@Mock
private BluetoothDevice mConnectedKeyBoardDevice;
@Mock
private SummaryListener mListener;
private BluetoothSummaryUpdater mSummaryUpdater;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
@@ -84,10 +96,12 @@ public class BluetoothSummaryUpdaterTest {
@Test
public void register_true_shouldSendSummaryChange() {
prepareConnectedDevice(false);
mSummaryUpdater.register(true);
verify(mListener).onSummaryChanged(
mContext.getString(R.string.bluetooth_connected_summary));
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME));
}
@Test
@@ -100,11 +114,13 @@ public class BluetoothSummaryUpdaterTest {
@Test
public void onBluetoothStateChanged_btEnabled_connected_shouldSendConnectedSummary() {
prepareConnectedDevice(false);
mSummaryUpdater.register(true);
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
verify(mListener).onSummaryChanged(
mContext.getString(R.string.bluetooth_connected_summary));
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME));
}
@Test
@@ -114,7 +130,7 @@ public class BluetoothSummaryUpdaterTest {
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_TURNING_ON);
verify(mListener).onSummaryChanged(
mContext.getString(R.string.disconnected));
mContext.getString(R.string.disconnected));
}
@Test
@@ -123,26 +139,28 @@ public class BluetoothSummaryUpdaterTest {
devices.add(mock(CachedBluetoothDevice.class));
when(devices.get(0).isConnected()).thenReturn(true);
when(mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy())
.thenReturn(devices);
.thenReturn(devices);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED);
prepareConnectedDevice(false);
mSummaryUpdater.register(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTED);
mSummaryUpdater.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_CONNECTED);
BluetoothAdapter.STATE_CONNECTED);
verify(mListener).onSummaryChanged(
mContext.getString(R.string.bluetooth_connected_summary));
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME));
}
@Test
public void onConnectionStateChanged_inconsistentState_shouldSendDisconnectedMessage() {
mSummaryUpdater.register(true);
mSummaryUpdater.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_CONNECTED);
BluetoothAdapter.STATE_CONNECTED);
verify(mListener).onSummaryChanged(
mContext.getString(R.string.disconnected));
mContext.getString(R.string.disconnected));
}
@Test
@@ -150,7 +168,7 @@ public class BluetoothSummaryUpdaterTest {
mSummaryUpdater.register(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTING);
mSummaryUpdater.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_CONNECTING);
BluetoothAdapter.STATE_CONNECTING);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_connecting));
}
@@ -160,11 +178,44 @@ public class BluetoothSummaryUpdaterTest {
mSummaryUpdater.register(true);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTING);
mSummaryUpdater.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_DISCONNECTING);
BluetoothAdapter.STATE_DISCONNECTING);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disconnecting));
}
@Test
public void getConnectedDeviceSummary_hasConnectedDevice_returnOneDeviceSummary() {
prepareConnectedDevice(false);
final String expectedSummary = mContext.getString(R.string.bluetooth_connected_summary,
DEVICE_NAME);
assertThat(mSummaryUpdater.getConnectedDeviceSummary()).isEqualTo(expectedSummary);
}
@Test
public void getConnectedDeviceSummary_multipleDevices_returnMultipleDevicesSummary() {
prepareConnectedDevice(true);
final String expectedSummary = mContext.getString(
R.string.bluetooth_connected_multiple_devices_summary);
assertThat(mSummaryUpdater.getConnectedDeviceSummary()).isEqualTo(expectedSummary);
}
private void prepareConnectedDevice(boolean multipleDevices) {
final Set<BluetoothDevice> devices = new HashSet<>();
doReturn(DEVICE_NAME).when(mConnectedDevice).getName();
doReturn(true).when(mConnectedDevice).isConnected();
devices.add(mConnectedDevice);
if (multipleDevices) {
// Add one more device if we need to test multiple devices
doReturn(DEVICE_KEYBOARD_NAME).when(mConnectedKeyBoardDevice).getName();
doReturn(true).when(mConnectedKeyBoardDevice).isConnected();
devices.add(mConnectedKeyBoardDevice);
}
doReturn(devices).when(mBtAdapter).getBondedDevices();
}
private class SummaryListener implements OnSummaryChangeListener {
String summary;