Merge "Add PROPERTY_VOIP_AUDIO_MODE cts test."
diff --git a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
index d9c21e3..44545c4 100644
--- a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
+++ b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
@@ -26,6 +26,7 @@
 
 import android.content.Context;
 import android.content.Intent;
+import android.media.AudioManager;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
@@ -1132,6 +1133,54 @@
     }
 
     /**
+     * Asserts that a call does not have any of the specified call property bits specified.
+     *
+     * @param call The call.
+     * @param properties The property or properties which are not expected.
+     */
+    public void assertDoesNotHaveCallProperties(final Call call, final int properties) {
+        waitUntilConditionIsTrueOrTimeout(
+                new Condition() {
+                    @Override
+                    public Object expected() {
+                        return true;
+                    }
+
+                    @Override
+                    public Object actual() {
+                        return !call.getDetails().hasProperty(properties);
+                    }
+                },
+                TestUtils.WAIT_FOR_STATE_CHANGE_TIMEOUT_MS,
+                "Call should not have properties " + properties
+        );
+    }
+
+    /**
+     * Asserts that the audio manager reports the specified audio mode.
+     *
+     * @param audioManager The audio manager to check.
+     * @param expectedMode The expected audio mode.
+     */
+    public void assertAudioMode(final AudioManager audioManager, final int expectedMode) {
+        waitUntilConditionIsTrueOrTimeout(
+                new Condition() {
+                    @Override
+                    public Object expected() {
+                        return true;
+                    }
+
+                    @Override
+                    public Object actual() {
+                        return audioManager.getMode() == expectedMode;
+                    }
+                },
+                TestUtils.WAIT_FOR_STATE_CHANGE_TIMEOUT_MS,
+                "Audio mode was expected to be " + expectedMode
+        );
+    }
+
+    /**
      * Asserts that a call's capabilities are as expected.
      *
      * @param call The call.
diff --git a/tests/tests/telecom/src/android/telecom/cts/CallDetailsTest.java b/tests/tests/telecom/src/android/telecom/cts/CallDetailsTest.java
index 77f16ec..9e131ae 100644
--- a/tests/tests/telecom/src/android/telecom/cts/CallDetailsTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/CallDetailsTest.java
@@ -21,7 +21,9 @@
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.junit.Assert.assertThat;
 
+import android.content.Context;
 import android.graphics.drawable.Icon;
+import android.media.AudioManager;
 import android.os.Bundle;
 import android.net.Uri;
 import android.telecom.Call;
@@ -658,6 +660,24 @@
     }
 
     /**
+     * Verifies that the AudioManager audio mode changes as expected based on whether a connection
+     * is using voip audio mode or not.
+     */
+    public void testSetVoipAudioMode() {
+        if (!mShouldTestTelecom) {
+            return;
+        }
+        mConnection.setAudioModeIsVoip(true);
+        assertCallProperties(mCall, Call.Details.PROPERTY_VOIP_AUDIO_MODE);
+        AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+        assertAudioMode(audioManager, AudioManager.MODE_IN_COMMUNICATION);
+
+        mConnection.setAudioModeIsVoip(false);
+        assertDoesNotHaveCallProperties(mCall, Call.Details.PROPERTY_VOIP_AUDIO_MODE);
+        assertAudioMode(audioManager, AudioManager.MODE_IN_CALL);
+    }
+
+    /**
      * Asserts that a call's extras contain a specified key.
      *
      * @param call The call.
diff --git a/tests/tests/telecom/src/android/telecom/cts/IncomingCallTest.java b/tests/tests/telecom/src/android/telecom/cts/IncomingCallTest.java
index 8317e3d..af74f88 100644
--- a/tests/tests/telecom/src/android/telecom/cts/IncomingCallTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/IncomingCallTest.java
@@ -20,10 +20,14 @@
 import static android.telecom.cts.TestUtils.PACKAGE;
 
 import android.content.ComponentName;
+import android.graphics.drawable.Icon;
 import android.net.Uri;
 import android.os.Bundle;
+import android.telecom.Call;
 import android.telecom.Connection;
+import android.telecom.ConnectionRequest;
 import android.telecom.PhoneAccountHandle;
+import android.telecom.StatusHints;
 import android.telecom.TelecomManager;
 import android.telephony.TelephonyManager;
 
@@ -107,4 +111,33 @@
 
         assertFalse(CtsConnectionService.isServiceRegisteredToTelecom());
     }
+
+    /**
+     * Ensure {@link Call.Details#PROPERTY_VOIP_AUDIO_MODE} is set for a ringing call which uses
+     * voip audio mode.
+     * @throws Exception
+     */
+    public void testAddNewIncomingCallVoipState() throws Exception {
+        if (!mShouldTestTelecom) {
+            return;
+        }
+        setupConnectionService(new MockConnectionService() {
+            @Override
+            public Connection onCreateIncomingConnection(
+                    PhoneAccountHandle connectionManagerPhoneAccount,
+                    ConnectionRequest request) {
+                Connection connection = super.onCreateIncomingConnection(
+                        connectionManagerPhoneAccount,
+                        request);
+                connection.setAudioModeIsVoip(true);
+                lock.release();
+                return connection;
+            }
+        }, FLAG_REGISTER | FLAG_ENABLE);
+        addAndVerifyNewIncomingCall(createTestNumber(), null);
+        verifyConnectionForIncomingCall();
+
+        assertTrue((mInCallCallbacks.getService().getLastCall().getDetails().getCallProperties()
+                & Call.Details.PROPERTY_VOIP_AUDIO_MODE) != 0);
+    }
 }