Test for onCallAudioStateChanged callback

Bug: 20303674
Change-Id: Ida7d7669bfa6eb71e12911507e982f31008d5bd2
diff --git a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
index 750ef3d..04b523c 100644
--- a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
+++ b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
@@ -76,7 +76,10 @@
 
     Context mContext;
     TelecomManager mTelecomManager;
+
     InvokeCounter mOnBringToForegroundCounter;
+    InvokeCounter mOnCallAudioStateChangedCounter;
+
     InCallServiceCallbacks mInCallCallbacks;
     String mPreviousDefaultDialer = null;
     MockConnectionService connectionService = null;
@@ -191,6 +194,11 @@
             public void onBringToForeground(boolean showDialpad) {
                 mOnBringToForegroundCounter.invoke(showDialpad);
             }
+            @Override
+            public void onCallAudioStateChanged(CallAudioState audioState) {
+                Log.i(TAG, "onCallAudioStateChanged, audioState: " + audioState);
+                mOnCallAudioStateChangedCounter.invoke(audioState);
+            }
         };
 
         MockInCallService.setCallbacks(mInCallCallbacks);
@@ -198,6 +206,7 @@
         // TODO: If more InvokeCounters are added in the future, consider consolidating them into a
         // single Collection.
         mOnBringToForegroundCounter = new InvokeCounter("OnBringToForeground");
+        mOnCallAudioStateChangedCounter = new InvokeCounter("OnCallAudioStateChanged");
     }
 
     /**
diff --git a/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java b/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java
index 2c9ca01..382132e 100644
--- a/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java
@@ -95,15 +95,21 @@
         final Call call = inCallService.getLastCall();
         assertCallState(call, Call.STATE_DIALING);
 
+        final int currentInvokeCount = mOnCallAudioStateChangedCounter.getInvokeCount();
+
         // Only test speaker and earpiece modes because the other modes are dependent on having
         // a bluetooth headset or wired headset connected.
 
         // Explicitly call super implementation to enable detection of CTS coverage
         ((InCallService) inCallService).setAudioRoute(CallAudioState.ROUTE_SPEAKER);
+        mOnCallAudioStateChangedCounter.waitForCount(currentInvokeCount + 1,
+                WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
         assertAudioRoute(connection, CallAudioState.ROUTE_SPEAKER);
         assertAudioRoute(inCallService, CallAudioState.ROUTE_SPEAKER);
 
         inCallService.setAudioRoute(CallAudioState.ROUTE_EARPIECE);
+        mOnCallAudioStateChangedCounter.waitForCount(currentInvokeCount + 2,
+                WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
         assertAudioRoute(connection, CallAudioState.ROUTE_EARPIECE);
         assertAudioRoute(inCallService, CallAudioState.ROUTE_EARPIECE);
     }
diff --git a/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java b/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java
index af3b9c3..ecc3d10 100644
--- a/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java
+++ b/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java
@@ -17,6 +17,7 @@
 package android.telecom.cts;
 
 import android.telecom.Call;
+import android.telecom.CallAudioState;
 import android.telecom.InCallService;
 import android.util.ArrayMap;
 
@@ -48,6 +49,7 @@
         public void onDetailsChanged(Call call, Call.Details details) {};
         public void onCanAddCallsChanged(boolean canAddCalls) {}
         public void onBringToForeground(boolean showDialpad) {}
+        public void onCallAudioStateChanged(CallAudioState audioState) {}
 
         final public MockInCallService getService() {
             return mService;
@@ -191,6 +193,14 @@
         }
     }
 
+    @Override
+    public void onCallAudioStateChanged(CallAudioState audioState) {
+        super.onCallAudioStateChanged(audioState);
+        if (getCallbacks() != null) {
+            getCallbacks().onCallAudioStateChanged(audioState);
+        }
+    }
+
     /**
      * @return the number of calls currently added to the {@code InCallService}.
      */