diff --git a/src/com/android/settings/bluetooth/BluetoothSummaryUpdater.java b/src/com/android/settings/bluetooth/BluetoothSummaryUpdater.java index 282f6130e14..5a2dcb2a787 100644 --- a/src/com/android/settings/bluetooth/BluetoothSummaryUpdater.java +++ b/src/com/android/settings/bluetooth/BluetoothSummaryUpdater.java @@ -17,14 +17,22 @@ package com.android.settings.bluetooth; import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; import android.content.Context; +import android.support.annotation.VisibleForTesting; +import android.text.TextUtils; + import com.android.settings.R; import com.android.settings.widget.SummaryUpdater; import com.android.settingslib.bluetooth.BluetoothCallback; import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.LocalBluetoothAdapter; import com.android.settingslib.bluetooth.LocalBluetoothManager; + +import java.util.ArrayList; import java.util.Collection; +import java.util.List; +import java.util.Set; /** * Helper class that listeners to bluetooth callback and notify client when there is update in @@ -98,7 +106,7 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu } switch (mConnectionState) { case BluetoothAdapter.STATE_CONNECTED: - return mContext.getString(R.string.bluetooth_connected_summary); + return getConnectedDeviceSummary(); case BluetoothAdapter.STATE_CONNECTING: return mContext.getString(R.string.bluetooth_connecting); case BluetoothAdapter.STATE_DISCONNECTING: @@ -145,4 +153,27 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu : null; } + @VisibleForTesting + String getConnectedDeviceSummary() { + String deviceName = null; + int count = 0; + final Set devices = mBluetoothAdapter.getBondedDevices(); + if (devices == null || devices.isEmpty()) { + return null; + } + + for (BluetoothDevice device : devices) { + if (device.isConnected()) { + deviceName = device.getName(); + count++; + if (count > 1) { + break; + } + } + } + + return count > 1 ? mContext.getString(R.string.bluetooth_connected_multiple_devices_summary) + : mContext.getString(R.string.bluetooth_connected_summary, deviceName); + } + } diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryUpdaterTest.java index cc059ad9886..79daa0e7c86 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryUpdaterTest.java @@ -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 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;