Bluetooth: reset mConnectionState when adapter is OFF

* No device is connected when Bluetooth adapter is OFF
* BluetoothSummaryUpdater should reset its connection state tracker in
  order to display the correct summary message on ConnectedDevice
  preference
* Otherwise, "Connected to null" will be shown because no device is
  connected while BluetoothSummaryUpdater is still in CONNECTED state
* Removed unused imports from BluetoothSummaryUpdater
* Write additional unit test to verify the above behaviour
* Add additional logging when deviceName is null in CONNECTED state

Bug: 62492716
Test: Pair and connect to Bluetooth device, turning Bluetooth ON/OFF,
      unit tests
Change-Id: I30726636f5678d61d6052f5b8d211aa20f26f409
This commit is contained in:
Jack He
2017-07-25 14:50:11 -07:00
parent b5696a9162
commit 70f293ee50
2 changed files with 46 additions and 5 deletions

View File

@@ -44,8 +44,11 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -132,6 +135,36 @@ public class BluetoothSummaryUpdaterTest {
mContext.getString(R.string.disconnected));
}
@Test
public void onBluetoothStateChanged_ConnectedDisabledEnabled_shouldSendDisconnectedSummary() {
final boolean[] connected = {false};
final List<CachedBluetoothDevice> devices = new ArrayList<>();
devices.add(mock(CachedBluetoothDevice.class));
doAnswer(invocation -> connected[0]).when(devices.get(0)).isConnected();
when(mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy())
.thenReturn(devices);
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED);
prepareConnectedDevice(false);
mSummaryUpdater.register(true);
verify(mListener).onSummaryChanged(mContext.getString(R.string.disconnected));
connected[0] = true;
when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTED);
mSummaryUpdater.onConnectionStateChanged(null /* device */,
BluetoothAdapter.STATE_CONNECTED);
verify(mListener).onSummaryChanged(
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME));
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
connected[0] = false;
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_TURNING_ON);
verify(mListener, times(2)).onSummaryChanged(mContext.getString(R.string.disconnected));
verify(mListener, times(4)).onSummaryChanged(anyString());
}
@Test
public void onConnectionStateChanged_connected_shouldSendConnectedMessage() {
final List<CachedBluetoothDevice> devices = new ArrayList<>();