Loading src/com/android/settings/bluetooth/BluetoothSummaryUpdater.java +32 −1 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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: Loading Loading @@ -145,4 +153,27 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu : null; } @VisibleForTesting String getConnectedDeviceSummary() { String deviceName = null; int count = 0; final Set<BluetoothDevice> 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); } } tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryUpdaterTest.java +64 −13 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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); Loading @@ -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 Loading @@ -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 Loading @@ -125,6 +141,8 @@ public class BluetoothSummaryUpdaterTest { when(mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()) .thenReturn(devices); when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED); prepareConnectedDevice(false); mSummaryUpdater.register(true); when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTED); Loading @@ -132,7 +150,7 @@ public class BluetoothSummaryUpdaterTest { BluetoothAdapter.STATE_CONNECTED); verify(mListener).onSummaryChanged( mContext.getString(R.string.bluetooth_connected_summary)); mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME)); } @Test Loading Loading @@ -165,6 +183,39 @@ public class BluetoothSummaryUpdaterTest { 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; Loading Loading
src/com/android/settings/bluetooth/BluetoothSummaryUpdater.java +32 −1 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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: Loading Loading @@ -145,4 +153,27 @@ public final class BluetoothSummaryUpdater extends SummaryUpdater implements Blu : null; } @VisibleForTesting String getConnectedDeviceSummary() { String deviceName = null; int count = 0; final Set<BluetoothDevice> 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); } }
tests/robotests/src/com/android/settings/bluetooth/BluetoothSummaryUpdaterTest.java +64 −13 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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); Loading @@ -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 Loading @@ -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 Loading @@ -125,6 +141,8 @@ public class BluetoothSummaryUpdaterTest { when(mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()) .thenReturn(devices); when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_DISCONNECTED); prepareConnectedDevice(false); mSummaryUpdater.register(true); when(mBtAdapter.getConnectionState()).thenReturn(BluetoothAdapter.STATE_CONNECTED); Loading @@ -132,7 +150,7 @@ public class BluetoothSummaryUpdaterTest { BluetoothAdapter.STATE_CONNECTED); verify(mListener).onSummaryChanged( mContext.getString(R.string.bluetooth_connected_summary)); mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME)); } @Test Loading Loading @@ -165,6 +183,39 @@ public class BluetoothSummaryUpdaterTest { 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; Loading