diff options
-rw-r--r-- | android/app/src/com/android/bluetooth/mcp/McpService.java | 25 | ||||
-rw-r--r-- | android/app/tests/unit/src/com/android/bluetooth/mcp/McpServiceTest.java | 31 |
2 files changed, 43 insertions, 13 deletions
diff --git a/android/app/src/com/android/bluetooth/mcp/McpService.java b/android/app/src/com/android/bluetooth/mcp/McpService.java index 1da7da7858..2508b2f38f 100644 --- a/android/app/src/com/android/bluetooth/mcp/McpService.java +++ b/android/app/src/com/android/bluetooth/mcp/McpService.java @@ -26,6 +26,7 @@ import android.util.Log; import com.android.bluetooth.Utils; import com.android.bluetooth.btservice.ProfileService; +import com.android.internal.annotations.VisibleForTesting; import java.util.HashMap; import java.util.Map; @@ -41,7 +42,7 @@ public class McpService extends ProfileService { private static McpService sMcpService; - private static MediaControlProfile mGmcs; + private static MediaControlProfile sGmcs; private Map<BluetoothDevice, Integer> mDeviceAuthorizations = new HashMap<>(); private Handler mHandler = new Handler(Looper.getMainLooper()); @@ -65,8 +66,13 @@ public class McpService extends ProfileService { return sMcpService; } + @VisibleForTesting + public static MediaControlProfile getMediaControlProfile() { + return sGmcs; + } + public static void setMediaControlProfileForTesting(MediaControlProfile mediaControlProfile) { - mGmcs = mediaControlProfile; + sGmcs = mediaControlProfile; } @Override @@ -94,11 +100,11 @@ public class McpService extends ProfileService { // Mark service as started setMcpService(this); - if (mGmcs == null) { + if (sGmcs == null) { // Initialize the Media Control Service Server - mGmcs = new MediaControlProfile(this); + sGmcs = new MediaControlProfile(this); // Requires this service to be already started thus we have to make it an async call - mHandler.post(() -> mGmcs.init()); + mHandler.post(() -> sGmcs.init()); } return true; @@ -115,8 +121,9 @@ public class McpService extends ProfileService { return true; } - if (mGmcs != null) { - mGmcs.cleanup(); + if (sGmcs != null) { + sGmcs.cleanup(); + sGmcs = null; } // Mark service as stopped @@ -141,7 +148,7 @@ public class McpService extends ProfileService { : BluetoothDevice.ACCESS_REJECTED; mDeviceAuthorizations.put(device, authorization); - mGmcs.onDeviceAuthorizationSet(device); + sGmcs.onDeviceAuthorizationSet(device); } public int getDeviceAuthorization(BluetoothDevice device) { @@ -192,6 +199,6 @@ public class McpService extends ProfileService { @Override public void dump(StringBuilder sb) { super.dump(sb); - mGmcs.dump(sb); + sGmcs.dump(sb); } } diff --git a/android/app/tests/unit/src/com/android/bluetooth/mcp/McpServiceTest.java b/android/app/tests/unit/src/com/android/bluetooth/mcp/McpServiceTest.java index 5bb690ddb6..b467f04fd2 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/mcp/McpServiceTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/mcp/McpServiceTest.java @@ -50,11 +50,15 @@ public class McpServiceTest { private McpService mMcpService; private Context mTargetContext; - @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); + @Rule + public final ServiceTestRule mServiceRule = new ServiceTestRule(); - @Mock private AdapterService mAdapterService; - @Mock private MediaControlGattService mMockMcpService; - @Mock private MediaControlProfile mMediaControlProfile; + @Mock + private AdapterService mAdapterService; + @Mock + private MediaControlGattService mMockMcpService; + @Mock + private MediaControlProfile mMediaControlProfile; @Before public void setUp() throws Exception { @@ -115,4 +119,23 @@ public class McpServiceTest { Assert.assertEquals(BluetoothDevice.ACCESS_REJECTED, mMcpService.getDeviceAuthorization(device1)); } + + @Test + public void testStopMcpService() { + InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { + public void run() { + Assert.assertTrue(mMcpService.stop()); + } + }); + Assert.assertNull(McpService.getMcpService()); + Assert.assertNull(McpService.getMediaControlProfile()); + + McpService.setMediaControlProfileForTesting(mMediaControlProfile); + // Try to restart the service. Note: must be done on the main thread + InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { + public void run() { + Assert.assertTrue(mMcpService.start()); + } + }); + } } |