Loading android/app/src/com/android/bluetooth/map/BluetoothMapService.java +12 −6 Original line number Diff line number Diff line Loading @@ -104,10 +104,12 @@ public class BluetoothMapService extends ProfileService { static final int MSG_OBSERVER_REGISTRATION = 5008; private static final int START_LISTENER = 1; private static final int USER_TIMEOUT = 2; @VisibleForTesting static final int USER_TIMEOUT = 2; private static final int DISCONNECT_MAP = 3; private static final int SHUTDOWN = 4; private static final int UPDATE_MAS_INSTANCES = 5; @VisibleForTesting static final int UPDATE_MAS_INSTANCES = 5; private static final int RELEASE_WAKE_LOCK_DELAY = 10000; private PowerManager.WakeLock mWakeLock = null; Loading Loading @@ -148,7 +150,8 @@ public class BluetoothMapService extends ProfileService { private boolean mAccountChanged = false; private boolean mSdpSearchInitiated = false; private SdpMnsRecord mMnsRecord = null; private MapServiceMessageHandler mSessionStatusHandler; @VisibleForTesting Handler mSessionStatusHandler; private boolean mServiceStarted = false; private static BluetoothMapService sBluetoothMapService; Loading Loading @@ -834,7 +837,8 @@ public class BluetoothMapService extends ProfileService { * If the key 255 is in use, the first free masId will be returned. * @return a free MasId */ private int getNextMasId() { @VisibleForTesting int getNextMasId() { // Find the largest masId in use int largestMasId = 0; for (int i = 0, c = mMasInstances.size(); i < c; i++) { Loading Loading @@ -1026,7 +1030,8 @@ public class BluetoothMapService extends ProfileService { } // Can only be null during shutdown } private void sendConnectTimeoutMessage() { @VisibleForTesting void sendConnectTimeoutMessage() { if (DEBUG) { Log.d(TAG, "sendConnectTimeoutMessage()"); } Loading @@ -1036,7 +1041,8 @@ public class BluetoothMapService extends ProfileService { } // Can only be null during shutdown } private void sendConnectCancelMessage() { @VisibleForTesting void sendConnectCancelMessage() { if (mSessionStatusHandler != null) { Message msg = mSessionStatusHandler.obtainMessage(MSG_MAS_CONNECT_CANCEL); msg.sendToTarget(); Loading android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapServiceTest.java +89 −11 Original line number Diff line number Diff line Loading @@ -15,24 +15,38 @@ */ package com.android.bluetooth.map; import static com.android.bluetooth.map.BluetoothMapService.MSG_MAS_CONNECT_CANCEL; import static com.android.bluetooth.map.BluetoothMapService.UPDATE_MAS_INSTANCES; import static com.android.bluetooth.map.BluetoothMapService.USER_TIMEOUT; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.os.Handler; import android.os.Looper; import android.os.Message; import androidx.test.InstrumentationRegistry; import androidx.test.filters.MediumTest; import androidx.test.rule.ServiceTestRule; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.R; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.storage.DatabaseManager; import org.junit.After; import org.junit.Assert; import org.junit.Assume; import org.junit.Before; import org.junit.Rule; Loading @@ -44,9 +58,11 @@ import org.mockito.MockitoAnnotations; @MediumTest @RunWith(AndroidJUnit4.class) public class BluetoothMapServiceTest { private static final String REMOTE_DEVICE_ADDRESS = "00:00:00:00:00:00"; private BluetoothMapService mService = null; private BluetoothAdapter mAdapter = null; private Context mTargetContext; private BluetoothDevice mRemoteDevice; @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); Loading @@ -55,7 +71,6 @@ public class BluetoothMapServiceTest { @Before public void setUp() throws Exception { mTargetContext = InstrumentationRegistry.getTargetContext(); Assume.assumeTrue("Ignore test when BluetoothMapService is not enabled", BluetoothMapService.isEnabled()); MockitoAnnotations.initMocks(this); Loading @@ -64,10 +79,11 @@ public class BluetoothMapServiceTest { doReturn(true, false).when(mAdapterService).isStartedProfile(anyString()); TestUtils.startService(mServiceRule, BluetoothMapService.class); mService = BluetoothMapService.getBluetoothMapService(); Assert.assertNotNull(mService); assertThat(mService).isNotNull(); // Try getting the Bluetooth adapter mAdapter = BluetoothAdapter.getDefaultAdapter(); Assert.assertNotNull(mAdapter); assertThat(mAdapter).isNotNull(); mRemoteDevice = mAdapter.getRemoteDevice(REMOTE_DEVICE_ADDRESS); } @After Loading @@ -77,12 +93,74 @@ public class BluetoothMapServiceTest { } TestUtils.stopService(mServiceRule, BluetoothMapService.class); mService = BluetoothMapService.getBluetoothMapService(); Assert.assertNull(mService); assertThat(mService).isNull(); TestUtils.clearAdapterService(mAdapterService); } @Test public void testInitialize() { Assert.assertNotNull(BluetoothMapService.getBluetoothMapService()); public void initialize() { assertThat(BluetoothMapService.getBluetoothMapService()).isNotNull(); } @Test public void getDevicesMatchingConnectionStates_whenNoDeviceIsConnected_returnsEmptyList() { when(mAdapterService.getBondedDevices()).thenReturn(new BluetoothDevice[] {mRemoteDevice}); assertThat(mService.getDevicesMatchingConnectionStates( new int[] {BluetoothProfile.STATE_CONNECTED})).isEmpty(); } @Test public void getNextMasId_isInRange() { int masId = mService.getNextMasId(); assertThat(masId).isAtMost(0xff); assertThat(masId).isAtLeast(1); } @Test public void sendConnectCancelMessage() { TestableHandler handler = spy(new TestableHandler(Looper.getMainLooper())); mService.mSessionStatusHandler = handler; mService.sendConnectCancelMessage(); verify(handler, timeout(1_000)).messageArrived( eq(MSG_MAS_CONNECT_CANCEL), anyInt(), anyInt(), any()); } @Test public void sendConnectTimeoutMessage() { TestableHandler handler = spy(new TestableHandler(Looper.getMainLooper())); mService.mSessionStatusHandler = handler; mService.sendConnectTimeoutMessage(); verify(handler, timeout(1_000)).messageArrived( eq(USER_TIMEOUT), anyInt(), anyInt(), any()); } @Test public void updateMasInstances() { int action = 5; TestableHandler handler = spy(new TestableHandler(Looper.getMainLooper())); mService.mSessionStatusHandler = handler; mService.updateMasInstances(action); verify(handler, timeout(1_000)).messageArrived( eq(UPDATE_MAS_INSTANCES), eq(action), anyInt(), any()); } public static class TestableHandler extends Handler { public TestableHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { messageArrived(msg.what, msg.arg1, msg.arg2, msg.obj); } public void messageArrived(int what, int arg1, int arg2, Object obj) {} } } Loading
android/app/src/com/android/bluetooth/map/BluetoothMapService.java +12 −6 Original line number Diff line number Diff line Loading @@ -104,10 +104,12 @@ public class BluetoothMapService extends ProfileService { static final int MSG_OBSERVER_REGISTRATION = 5008; private static final int START_LISTENER = 1; private static final int USER_TIMEOUT = 2; @VisibleForTesting static final int USER_TIMEOUT = 2; private static final int DISCONNECT_MAP = 3; private static final int SHUTDOWN = 4; private static final int UPDATE_MAS_INSTANCES = 5; @VisibleForTesting static final int UPDATE_MAS_INSTANCES = 5; private static final int RELEASE_WAKE_LOCK_DELAY = 10000; private PowerManager.WakeLock mWakeLock = null; Loading Loading @@ -148,7 +150,8 @@ public class BluetoothMapService extends ProfileService { private boolean mAccountChanged = false; private boolean mSdpSearchInitiated = false; private SdpMnsRecord mMnsRecord = null; private MapServiceMessageHandler mSessionStatusHandler; @VisibleForTesting Handler mSessionStatusHandler; private boolean mServiceStarted = false; private static BluetoothMapService sBluetoothMapService; Loading Loading @@ -834,7 +837,8 @@ public class BluetoothMapService extends ProfileService { * If the key 255 is in use, the first free masId will be returned. * @return a free MasId */ private int getNextMasId() { @VisibleForTesting int getNextMasId() { // Find the largest masId in use int largestMasId = 0; for (int i = 0, c = mMasInstances.size(); i < c; i++) { Loading Loading @@ -1026,7 +1030,8 @@ public class BluetoothMapService extends ProfileService { } // Can only be null during shutdown } private void sendConnectTimeoutMessage() { @VisibleForTesting void sendConnectTimeoutMessage() { if (DEBUG) { Log.d(TAG, "sendConnectTimeoutMessage()"); } Loading @@ -1036,7 +1041,8 @@ public class BluetoothMapService extends ProfileService { } // Can only be null during shutdown } private void sendConnectCancelMessage() { @VisibleForTesting void sendConnectCancelMessage() { if (mSessionStatusHandler != null) { Message msg = mSessionStatusHandler.obtainMessage(MSG_MAS_CONNECT_CANCEL); msg.sendToTarget(); Loading
android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapServiceTest.java +89 −11 Original line number Diff line number Diff line Loading @@ -15,24 +15,38 @@ */ package com.android.bluetooth.map; import static com.android.bluetooth.map.BluetoothMapService.MSG_MAS_CONNECT_CANCEL; import static com.android.bluetooth.map.BluetoothMapService.UPDATE_MAS_INSTANCES; import static com.android.bluetooth.map.BluetoothMapService.USER_TIMEOUT; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.os.Handler; import android.os.Looper; import android.os.Message; import androidx.test.InstrumentationRegistry; import androidx.test.filters.MediumTest; import androidx.test.rule.ServiceTestRule; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.R; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.storage.DatabaseManager; import org.junit.After; import org.junit.Assert; import org.junit.Assume; import org.junit.Before; import org.junit.Rule; Loading @@ -44,9 +58,11 @@ import org.mockito.MockitoAnnotations; @MediumTest @RunWith(AndroidJUnit4.class) public class BluetoothMapServiceTest { private static final String REMOTE_DEVICE_ADDRESS = "00:00:00:00:00:00"; private BluetoothMapService mService = null; private BluetoothAdapter mAdapter = null; private Context mTargetContext; private BluetoothDevice mRemoteDevice; @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); Loading @@ -55,7 +71,6 @@ public class BluetoothMapServiceTest { @Before public void setUp() throws Exception { mTargetContext = InstrumentationRegistry.getTargetContext(); Assume.assumeTrue("Ignore test when BluetoothMapService is not enabled", BluetoothMapService.isEnabled()); MockitoAnnotations.initMocks(this); Loading @@ -64,10 +79,11 @@ public class BluetoothMapServiceTest { doReturn(true, false).when(mAdapterService).isStartedProfile(anyString()); TestUtils.startService(mServiceRule, BluetoothMapService.class); mService = BluetoothMapService.getBluetoothMapService(); Assert.assertNotNull(mService); assertThat(mService).isNotNull(); // Try getting the Bluetooth adapter mAdapter = BluetoothAdapter.getDefaultAdapter(); Assert.assertNotNull(mAdapter); assertThat(mAdapter).isNotNull(); mRemoteDevice = mAdapter.getRemoteDevice(REMOTE_DEVICE_ADDRESS); } @After Loading @@ -77,12 +93,74 @@ public class BluetoothMapServiceTest { } TestUtils.stopService(mServiceRule, BluetoothMapService.class); mService = BluetoothMapService.getBluetoothMapService(); Assert.assertNull(mService); assertThat(mService).isNull(); TestUtils.clearAdapterService(mAdapterService); } @Test public void testInitialize() { Assert.assertNotNull(BluetoothMapService.getBluetoothMapService()); public void initialize() { assertThat(BluetoothMapService.getBluetoothMapService()).isNotNull(); } @Test public void getDevicesMatchingConnectionStates_whenNoDeviceIsConnected_returnsEmptyList() { when(mAdapterService.getBondedDevices()).thenReturn(new BluetoothDevice[] {mRemoteDevice}); assertThat(mService.getDevicesMatchingConnectionStates( new int[] {BluetoothProfile.STATE_CONNECTED})).isEmpty(); } @Test public void getNextMasId_isInRange() { int masId = mService.getNextMasId(); assertThat(masId).isAtMost(0xff); assertThat(masId).isAtLeast(1); } @Test public void sendConnectCancelMessage() { TestableHandler handler = spy(new TestableHandler(Looper.getMainLooper())); mService.mSessionStatusHandler = handler; mService.sendConnectCancelMessage(); verify(handler, timeout(1_000)).messageArrived( eq(MSG_MAS_CONNECT_CANCEL), anyInt(), anyInt(), any()); } @Test public void sendConnectTimeoutMessage() { TestableHandler handler = spy(new TestableHandler(Looper.getMainLooper())); mService.mSessionStatusHandler = handler; mService.sendConnectTimeoutMessage(); verify(handler, timeout(1_000)).messageArrived( eq(USER_TIMEOUT), anyInt(), anyInt(), any()); } @Test public void updateMasInstances() { int action = 5; TestableHandler handler = spy(new TestableHandler(Looper.getMainLooper())); mService.mSessionStatusHandler = handler; mService.updateMasInstances(action); verify(handler, timeout(1_000)).messageArrived( eq(UPDATE_MAS_INSTANCES), eq(action), anyInt(), any()); } public static class TestableHandler extends Handler { public TestableHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { messageArrived(msg.what, msg.arg1, msg.arg2, msg.obj); } public void messageArrived(int what, int arg1, int arg2, Object obj) {} } }