Merge "Add OnBringToForegroundTest" into mnc-dev
diff --git a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
index c574320..750ef3d 100644
--- a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
+++ b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
@@ -28,6 +28,7 @@
 import android.graphics.Color;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.SystemClock;
 import android.telecom.Call;
 import android.telecom.CallAudioState;
 import android.telecom.Conference;
@@ -75,6 +76,7 @@
 
     Context mContext;
     TelecomManager mTelecomManager;
+    InvokeCounter mOnBringToForegroundCounter;
     InCallServiceCallbacks mInCallCallbacks;
     String mPreviousDefaultDialer = null;
     MockConnectionService connectionService = null;
@@ -185,9 +187,17 @@
             public void onCallStateChanged(Call call, int newState) {
                 Log.i(TAG, "onCallStateChanged, Call: " + call + ", New State: " + newState);
             }
+            @Override
+            public void onBringToForeground(boolean showDialpad) {
+                mOnBringToForegroundCounter.invoke(showDialpad);
+            }
         };
 
         MockInCallService.setCallbacks(mInCallCallbacks);
+
+        // TODO: If more InvokeCounters are added in the future, consider consolidating them into a
+        // single Collection.
+        mOnBringToForegroundCounter = new InvokeCounter("OnBringToForeground");
     }
 
     /**
@@ -740,4 +750,68 @@
     protected interface Work {
         void doWork();
     }
+
+    /**
+     * Utility class used to track the number of times a callback was invoked, and the arguments it
+     * was invoked with. This class is prefixed Invoke rather than the more typical Call for
+     * disambiguation purposes.
+     */
+    protected final class InvokeCounter {
+        private final String mName;
+        private final Object mLock = new Object();
+        private final ArrayList<Object[]> mInvokeArgs = new ArrayList<>();
+
+        private int mInvokeCount;
+
+        public InvokeCounter(String callbackName) {
+            mName = callbackName;
+        }
+
+        public void invoke(Object... args) {
+            synchronized (mLock) {
+                mInvokeCount++;
+                mInvokeArgs.add(args);
+                mLock.notifyAll();
+            }
+        }
+
+        public Object[] getArgs(int index) {
+            synchronized (mLock) {
+                return mInvokeArgs.get(index);
+            }
+        }
+
+        public int getInvokeCount() {
+            synchronized (mLock) {
+                return mInvokeCount;
+            }
+        }
+
+        public void waitForCount(int count, long timeoutMillis) {
+            waitForCount(count, timeoutMillis, null);
+        }
+
+        public void waitForCount(int count, long timeoutMillis, String message) {
+            synchronized (mLock) {
+                final long startTimeMillis = SystemClock.uptimeMillis();
+                while (mInvokeCount < count) {
+                    try {
+                        final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis;
+                        final long remainingTimeMillis = timeoutMillis - elapsedTimeMillis;
+                        if (remainingTimeMillis <= 0) {
+                            if (message != null) {
+                                fail(message);
+                            } else {
+                                fail(String.format("Expected %s to be called %d times.", mName,
+                                        count));
+                            }
+                        }
+                        mLock.wait(timeoutMillis);
+                    } catch (InterruptedException ie) {
+                        /* ignore */
+                    }
+                }
+            }
+        }
+    }
 }
diff --git a/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java b/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java
index a18654a..2c9ca01 100644
--- a/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java
@@ -18,13 +18,14 @@
 
 import static android.telecom.cts.TestUtils.*;
 
+import android.content.Context;
 import android.telecom.CallAudioState;
 import android.telecom.Call;
 import android.telecom.Connection;
 import android.telecom.ConnectionService;
 import android.telecom.InCallService;
+import android.telecom.TelecomManager;
 import android.telecom.VideoProfile;
-import android.telecom.cts.BaseTelecomTestWithMockServices.Condition;
 
 /**
  * Extended suite of tests that use {@link CtsConnectionService} and {@link MockInCallService} to
@@ -300,6 +301,38 @@
                 "Should not be able to add call with two calls already present");
     }
 
+    public void testOnBringToForeground() {
+        if (!shouldTestTelecom(mContext)) {
+            return;
+        }
+
+        placeAndVerifyCall();
+        verifyConnectionForOutgoingCall();
+
+        final MockInCallService inCallService = mInCallCallbacks.getService();
+
+        final Call call = inCallService.getLastCall();
+
+        assertCallState(call, Call.STATE_DIALING);
+
+        assertEquals(0, mOnBringToForegroundCounter.getInvokeCount());
+
+        final TelecomManager tm =
+            (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
+
+        tm.showInCallScreen(false);
+
+        mOnBringToForegroundCounter.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
+
+        assertFalse((Boolean) mOnBringToForegroundCounter.getArgs(0)[0]);
+
+        tm.showInCallScreen(true);
+
+        mOnBringToForegroundCounter.waitForCount(2, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
+
+        assertTrue((Boolean) mOnBringToForegroundCounter.getArgs(1)[0]);
+    }
+
     private void assertCanAddCall(final InCallService inCallService, final boolean canAddCall,
             String message) {
         waitUntilConditionIsTrueOrTimeout(
diff --git a/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java b/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java
index b876b3f..af3b9c3 100644
--- a/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java
+++ b/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java
@@ -47,6 +47,7 @@
         public void onCallDestroyed(Call call) {};
         public void onDetailsChanged(Call call, Call.Details details) {};
         public void onCanAddCallsChanged(boolean canAddCalls) {}
+        public void onBringToForeground(boolean showDialpad) {}
 
         final public MockInCallService getService() {
             return mService;
@@ -182,6 +183,14 @@
         }
     }
 
+    @Override
+    public void onBringToForeground(boolean showDialpad) {
+        super.onBringToForeground(showDialpad);
+        if (getCallbacks() != null) {
+            getCallbacks().onBringToForeground(showDialpad);
+        }
+    }
+
     /**
      * @return the number of calls currently added to the {@code InCallService}.
      */