Migrate BluetoothHeadset GTS tests to CTS

Bug: 227548535
Test: atest BluetoothHeadsetTest
Change-Id: I3504772aacf87d13f7d1344560fc21e92c67fb5e
diff --git a/tests/tests/bluetooth/src/android/bluetooth/cts/BluetoothHeadsetTest.java b/tests/tests/bluetooth/src/android/bluetooth/cts/BluetoothHeadsetTest.java
index 6925d9e..cb2c2b4 100644
--- a/tests/tests/bluetooth/src/android/bluetooth/cts/BluetoothHeadsetTest.java
+++ b/tests/tests/bluetooth/src/android/bluetooth/cts/BluetoothHeadsetTest.java
@@ -17,6 +17,7 @@
 package android.bluetooth.cts;
 
 import static android.Manifest.permission.BLUETOOTH_CONNECT;
+import static android.Manifest.permission.BLUETOOTH_PRIVILEGED;
 
 import android.app.UiAutomation;
 import android.bluetooth.BluetoothAdapter;
@@ -24,6 +25,7 @@
 import android.bluetooth.BluetoothHeadset;
 import android.bluetooth.BluetoothManager;
 import android.bluetooth.BluetoothProfile;
+import android.bluetooth.BluetoothStatusCodes;
 import android.content.pm.PackageManager;
 import android.test.AndroidTestCase;
 import android.util.Log;
@@ -198,6 +200,181 @@
         assertFalse(mBluetoothHeadset.sendVendorSpecificResultCode(testDevice, "", ""));
     }
 
+    public void test_connect() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        // Verify returns false when invalid input is given
+        assertFalse(mBluetoothHeadset.connect(null));
+
+        // Verify it returns false for a device that has CONNECTION_POLICY_FORBIDDEN
+        BluetoothDevice testDevice = mAdapter.getRemoteDevice("00:11:22:AA:BB:CC");
+        assertFalse(mBluetoothHeadset.connect(testDevice));
+
+        // Verify returns false if bluetooth is not enabled
+        assertTrue(BTAdapterUtils.disableAdapter(mAdapter, mContext));
+        assertFalse(mBluetoothHeadset.connect(testDevice));
+    }
+
+    public void test_disconnect() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        // Verify returns false when invalid input is given
+        assertFalse(mBluetoothHeadset.disconnect(null));
+
+        // Verify it returns false for a device that has CONNECTION_POLICY_FORBIDDEN
+        BluetoothDevice testDevice = mAdapter.getRemoteDevice("00:11:22:AA:BB:CC");
+        assertFalse(mBluetoothHeadset.disconnect(testDevice));
+
+        // Verify returns false if bluetooth is not enabled
+        assertTrue(BTAdapterUtils.disableAdapter(mAdapter, mContext));
+        assertFalse(mBluetoothHeadset.disconnect(testDevice));
+    }
+
+    public void test_getConnectionPolicy() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        BluetoothDevice testDevice = mAdapter.getRemoteDevice("00:11:22:AA:BB:CC");
+
+        // Verify returns false when invalid input is given
+        assertEquals(
+                BluetoothProfile.CONNECTION_POLICY_FORBIDDEN,
+                mBluetoothHeadset.getConnectionPolicy(null));
+
+        // Verify returns CONNECTION_POLICY_FORBIDDEN if bluetooth is not enabled
+        assertTrue(BTAdapterUtils.disableAdapter(mAdapter, mContext));
+        assertEquals(
+                BluetoothProfile.CONNECTION_POLICY_FORBIDDEN,
+                mBluetoothHeadset.getConnectionPolicy(testDevice));
+    }
+
+    public void test_setConnectionPolicy() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        BluetoothDevice testDevice = mAdapter.getRemoteDevice("00:11:22:AA:BB:CC");
+
+        // Verify returns false when invalid input is given
+        assertFalse(
+                mBluetoothHeadset.setConnectionPolicy(
+                        testDevice, BluetoothProfile.CONNECTION_POLICY_UNKNOWN));
+        assertFalse(
+                mBluetoothHeadset.setConnectionPolicy(
+                        null, BluetoothProfile.CONNECTION_POLICY_ALLOWED));
+
+        // Verify returns false if bluetooth is not enabled
+        assertTrue(BTAdapterUtils.disableAdapter(mAdapter, mContext));
+        assertFalse(
+                mBluetoothHeadset.setConnectionPolicy(
+                        testDevice, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN));
+    }
+
+    public void test_getAudioState() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        mUiAutomation.adoptShellPermissionIdentity(BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED);
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        try {
+            mBluetoothHeadset.getAudioState(null);
+            fail("Calling getAudioState on a null device should throw a NullPointerException");
+        } catch (NullPointerException ignored) {
+        }
+
+        BluetoothDevice testDevice = mAdapter.getRemoteDevice("00:11:22:AA:BB:CC");
+        assertEquals(
+                BluetoothHeadset.STATE_AUDIO_DISCONNECTED,
+                mBluetoothHeadset.getAudioState(testDevice));
+    }
+
+    public void test_connectAudio() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        mUiAutomation.adoptShellPermissionIdentity(BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED);
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        assertEquals(
+                BluetoothStatusCodes.ERROR_NO_ACTIVE_DEVICES, mBluetoothHeadset.connectAudio());
+
+        assertTrue(BTAdapterUtils.disableAdapter(mAdapter, mContext));
+        assertEquals(
+                BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND,
+                mBluetoothHeadset.connectAudio());
+    }
+
+    public void test_disconnectAudio() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        mUiAutomation.adoptShellPermissionIdentity(BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED);
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        assertEquals(
+                BluetoothStatusCodes.ERROR_NO_ACTIVE_DEVICES, mBluetoothHeadset.disconnectAudio());
+
+        assertTrue(BTAdapterUtils.disableAdapter(mAdapter, mContext));
+        assertEquals(
+                BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND,
+                mBluetoothHeadset.disconnectAudio());
+    }
+
+    public void test_startScoUsingVirtualVoiceCall() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        mUiAutomation.adoptShellPermissionIdentity(BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED);
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        assertFalse(mBluetoothHeadset.startScoUsingVirtualVoiceCall());
+    }
+
+    public void test_stopScoUsingVirtualVoiceCall() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        mUiAutomation.adoptShellPermissionIdentity(BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED);
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        assertFalse(mBluetoothHeadset.stopScoUsingVirtualVoiceCall());
+    }
+
+    public void test_isInbandRingingEnabled() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        mUiAutomation.adoptShellPermissionIdentity(BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED);
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        assertTrue(BTAdapterUtils.disableAdapter(mAdapter, mContext));
+        assertFalse(mBluetoothHeadset.isInbandRingingEnabled());
+    }
+
+    public void test_setGetAudioRouteAllowed() {
+        if (!(mHasBluetooth && mIsHeadsetSupported)) return;
+
+        mUiAutomation.adoptShellPermissionIdentity(BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED);
+        assertTrue(waitForProfileConnect());
+        assertNotNull(mBluetoothHeadset);
+
+        assertEquals(BluetoothStatusCodes.SUCCESS, mBluetoothHeadset.setAudioRouteAllowed(true));
+        assertEquals(BluetoothStatusCodes.ALLOWED, mBluetoothHeadset.getAudioRouteAllowed());
+
+        assertEquals(BluetoothStatusCodes.SUCCESS, mBluetoothHeadset.setAudioRouteAllowed(false));
+        assertEquals(BluetoothStatusCodes.NOT_ALLOWED, mBluetoothHeadset.getAudioRouteAllowed());
+    }
+
     private boolean waitForProfileConnect() {
         mProfileConnectedlock.lock();
         try {