Merge "Fix unit tests" into nyc-dev
am: 09c900e

* commit '09c900ef10510038772e172f5c3e553a3f0d9366':
  Fix unit tests

Change-Id: Ic63da1b33c6b3c3f1068f4a854fff6eacd5bb465
diff --git a/src/com/android/server/telecom/CallAudioManager.java b/src/com/android/server/telecom/CallAudioManager.java
index 8fd2705..38cd36b 100644
--- a/src/com/android/server/telecom/CallAudioManager.java
+++ b/src/com/android/server/telecom/CallAudioManager.java
@@ -411,6 +411,11 @@
                 CallAudioRouteStateMachine.SWITCH_FOCUS, focusState);
     }
 
+    @VisibleForTesting
+    public CallAudioRouteStateMachine getCallAudioRouteStateMachine() {
+        return mCallAudioRouteStateMachine;
+    }
+
     void dump(IndentingPrintWriter pw) {
         pw.println("All calls:");
         pw.increaseIndent();
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index b187358..9368d95 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -582,7 +582,7 @@
         return mCurrentUserHandle;
     }
 
-    CallAudioManager getCallAudioManager() {
+    public CallAudioManager getCallAudioManager() {
         return mCallAudioManager;
     }
 
diff --git a/src/com/android/server/telecom/InCallController.java b/src/com/android/server/telecom/InCallController.java
index 12805a3..e5a9bac 100644
--- a/src/com/android/server/telecom/InCallController.java
+++ b/src/com/android/server/telecom/InCallController.java
@@ -134,11 +134,12 @@
             }
 
             Log.i(this, "Attempting to bind to InCall %s, with %s", mComponentName, intent);
-            if (mContext.bindServiceAsUser(intent, mServiceConnection,
+            mIsConnected = true;
+            if (!mContext.bindServiceAsUser(intent, mServiceConnection,
                         Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
                         UserHandle.CURRENT)) {
-                Log.i(this, "It's connecting!");
-                mIsConnected = true;
+                Log.w(this, "Failed to connect.");
+                mIsConnected = false;
             }
 
             return mIsConnected;
diff --git a/src/com/android/server/telecom/TelecomSystem.java b/src/com/android/server/telecom/TelecomSystem.java
index 817ad07..9da8a24 100644
--- a/src/com/android/server/telecom/TelecomSystem.java
+++ b/src/com/android/server/telecom/TelecomSystem.java
@@ -233,6 +233,11 @@
         return mPhoneAccountRegistrar;
     }
 
+    @VisibleForTesting
+    public CallsManager getCallsManager() {
+        return mCallsManager;
+    }
+
     public BluetoothPhoneServiceImpl getBluetoothPhoneServiceImpl() {
         return mBluetoothPhoneServiceImpl;
     }
diff --git a/src/com/android/server/telecom/Timeouts.java b/src/com/android/server/telecom/Timeouts.java
index f3d3e21..88a3a06 100644
--- a/src/com/android/server/telecom/Timeouts.java
+++ b/src/com/android/server/telecom/Timeouts.java
@@ -26,6 +26,14 @@
  * These methods are safe to call from any thread, including the UI thread.
  */
 public final class Timeouts {
+    public class Adapter {
+        public Adapter() { }
+
+        public long getCallScreeningTimeoutMillis(ContentResolver cr) {
+            return Timeouts.getCallScreeningTimeoutMillis(cr);
+        }
+    }
+
     /** A prefix to use for all keys so to not clobber the global namespace. */
     private static final String PREFIX = "telecom.";
 
diff --git a/src/com/android/server/telecom/callfiltering/IncomingCallFilter.java b/src/com/android/server/telecom/callfiltering/IncomingCallFilter.java
index f49a502..0a046b6 100644
--- a/src/com/android/server/telecom/callfiltering/IncomingCallFilter.java
+++ b/src/com/android/server/telecom/callfiltering/IncomingCallFilter.java
@@ -41,6 +41,7 @@
     private final List<CallFilter> mFilters;
     private final Call mCall;
     private final CallFilterResultCallback mListener;
+    private final Timeouts.Adapter mTimeoutsAdapter;
 
     private CallFilteringResult mResult = new CallFilteringResult(
             true, // shouldAllowCall
@@ -53,13 +54,15 @@
     private int mNumPendingFilters;
 
     public IncomingCallFilter(Context context, CallFilterResultCallback listener, Call call,
-            TelecomSystem.SyncRoot lock, List<CallFilter> filters) {
+            TelecomSystem.SyncRoot lock, Timeouts.Adapter timeoutsAdapter,
+            List<CallFilter> filters) {
         mContext = context;
         mListener = listener;
         mCall = call;
         mLock = lock;
         mFilters = filters;
         mNumPendingFilters = filters.size();
+        mTimeoutsAdapter = timeoutsAdapter;
     }
 
     public void performFiltering() {
@@ -77,7 +80,7 @@
                     }
                 }
             }
-        }.prepare(), Timeouts.getCallScreeningTimeoutMillis(mContext.getContentResolver()));
+        }.prepare(), mTimeoutsAdapter.getCallScreeningTimeoutMillis(mContext.getContentResolver()));
     }
 
     public void onCallFilteringComplete(Call call, CallFilteringResult result) {
diff --git a/tests/src/com/android/server/telecom/tests/IncomingCallFilterTest.java b/tests/src/com/android/server/telecom/tests/IncomingCallFilterTest.java
index 2889cdb..e766786 100644
--- a/tests/src/com/android/server/telecom/tests/IncomingCallFilterTest.java
+++ b/tests/src/com/android/server/telecom/tests/IncomingCallFilterTest.java
@@ -16,13 +16,13 @@
 
 package com.android.server.telecom.tests;
 
+import android.content.ContentResolver;
 import android.content.IContentProvider;
 import android.net.Uri;
-import android.os.Bundle;
-import android.os.Handler;
 import android.test.suitebuilder.annotation.SmallTest;
 
 import com.android.server.telecom.Call;
+import com.android.server.telecom.Timeouts;
 import com.android.server.telecom.callfiltering.CallFilterResultCallback;
 import com.android.server.telecom.callfiltering.CallFilteringResult;
 import com.android.server.telecom.callfiltering.IncomingCallFilter;
@@ -33,19 +33,15 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.concurrent.CountDownLatch;
 
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
-import static org.mockito.Matchers.isNull;
 import static org.mockito.Mockito.atMost;
 import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 public class IncomingCallFilterTest extends TelecomTestCase {
-    private IContentProvider mSettingsContentProvider;
     @Mock private CallFilterResultCallback mResultCallback;
     @Mock private Call mCall;
     private final TelecomSystem.SyncRoot mLock = new TelecomSystem.SyncRoot() {};
@@ -54,11 +50,11 @@
     @Mock private IncomingCallFilter.CallFilter mFilter2;
     @Mock private IncomingCallFilter.CallFilter mFilter3;
 
+    @Mock private Timeouts.Adapter mTimeoutsAdapter;
+
     private static final Uri TEST_HANDLE = Uri.parse("tel:1235551234");
-    private static final int LONG_TIMEOUT = 1000000;
-    private static final int SHORT_TIMEOUT = 100;
-    private static final String PACKAGE_NAME = "com.android.server.telecom.tests";
-    private static final String TIMEOUT_NAME = "telecom.call_screening_timeout";
+    private static final long LONG_TIMEOUT = 1000000;
+    private static final long SHORT_TIMEOUT = 100;
 
     private static final CallFilteringResult RESULT1 =
             new CallFilteringResult(
@@ -90,19 +86,18 @@
         super.setUp();
         mContext = mComponentContextFixture.getTestDouble().getApplicationContext();
         when(mCall.getHandle()).thenReturn(TEST_HANDLE);
-        mSettingsContentProvider = mContext.getContentResolver().acquireProvider("settings");
         setTimeoutLength(LONG_TIMEOUT);
     }
 
     @SmallTest
     public void testSingleFilter() {
         IncomingCallFilter testFilter = new IncomingCallFilter(mContext, mResultCallback, mCall,
-                mLock, Collections.singletonList(mFilter1));
+                mLock, mTimeoutsAdapter, Collections.singletonList(mFilter1));
         testFilter.performFiltering();
         verify(mFilter1).startFilterLookup(mCall, testFilter);
 
         testFilter.onCallFilteringComplete(mCall, RESULT1);
-        waitForHandlerAction(testFilter.getHandler());
+        waitForHandlerAction(testFilter.getHandler(), SHORT_TIMEOUT * 2);
         verify(mResultCallback).onCallFilteringComplete(eq(mCall), eq(RESULT1));
     }
 
@@ -115,7 +110,7 @@
                     add(mFilter3);
                 }};
         IncomingCallFilter testFilter = new IncomingCallFilter(mContext, mResultCallback, mCall,
-                mLock, filters);
+                mLock, mTimeoutsAdapter, filters);
         testFilter.performFiltering();
         verify(mFilter1).startFilterLookup(mCall, testFilter);
         verify(mFilter2).startFilterLookup(mCall, testFilter);
@@ -124,7 +119,7 @@
         testFilter.onCallFilteringComplete(mCall, RESULT1);
         testFilter.onCallFilteringComplete(mCall, RESULT2);
         testFilter.onCallFilteringComplete(mCall, RESULT3);
-        waitForHandlerAction(testFilter.getHandler());
+        waitForHandlerAction(testFilter.getHandler(), SHORT_TIMEOUT * 2);
         verify(mResultCallback).onCallFilteringComplete(eq(mCall), eq(
                 new CallFilteringResult(
                         false, // shouldAllowCall
@@ -138,12 +133,12 @@
     public void testFilterTimeout() throws Exception {
         setTimeoutLength(SHORT_TIMEOUT);
         IncomingCallFilter testFilter = new IncomingCallFilter(mContext, mResultCallback, mCall,
-                mLock, Collections.singletonList(mFilter1));
+                mLock, mTimeoutsAdapter, Collections.singletonList(mFilter1));
         testFilter.performFiltering();
-        verify(mResultCallback, timeout(SHORT_TIMEOUT * 2)).onCallFilteringComplete(eq(mCall),
+        verify(mResultCallback, timeout((int) SHORT_TIMEOUT * 2)).onCallFilteringComplete(eq(mCall),
                 eq(DEFAULT_RESULT));
         testFilter.onCallFilteringComplete(mCall, RESULT1);
-        waitForHandlerAction(testFilter.getHandler());
+        waitForHandlerAction(testFilter.getHandler(), SHORT_TIMEOUT * 2);
         // verify that we don't report back again with the result
         verify(mResultCallback, atMost(1)).onCallFilteringComplete(any(Call.class),
                 any(CallFilteringResult.class));
@@ -153,31 +148,17 @@
     public void testFilterTimeoutDoesntTrip() throws Exception {
         setTimeoutLength(SHORT_TIMEOUT);
         IncomingCallFilter testFilter = new IncomingCallFilter(mContext, mResultCallback, mCall,
-                mLock, Collections.singletonList(mFilter1));
+                mLock, mTimeoutsAdapter, Collections.singletonList(mFilter1));
         testFilter.performFiltering();
         testFilter.onCallFilteringComplete(mCall, RESULT1);
-        waitForHandlerAction(testFilter.getHandler());
+        waitForHandlerAction(testFilter.getHandler(), SHORT_TIMEOUT * 2);
         Thread.sleep(SHORT_TIMEOUT);
         verify(mResultCallback, atMost(1)).onCallFilteringComplete(any(Call.class),
                 any(CallFilteringResult.class));
     }
 
-    private void setTimeoutLength(int length) throws Exception {
-        Bundle result = new Bundle();
-        result.putString(TIMEOUT_NAME, String.valueOf(length));
-        when(mSettingsContentProvider.call(eq(PACKAGE_NAME), anyString(), eq(TIMEOUT_NAME),
-                any(Bundle.class))).thenReturn(result);
-    }
-
-    protected final void waitForHandlerAction(Handler h) {
-        final CountDownLatch lock = new CountDownLatch(1);
-        h.post(lock::countDown);
-        while (lock.getCount() > 0) {
-            try {
-                lock.await();
-            } catch (InterruptedException e) {
-                // do nothing
-            }
-        }
+    private void setTimeoutLength(long length) throws Exception {
+        when(mTimeoutsAdapter.getCallScreeningTimeoutMillis(any(ContentResolver.class)))
+                .thenReturn(length);
     }
 }
diff --git a/tests/src/com/android/server/telecom/tests/TelecomTestCase.java b/tests/src/com/android/server/telecom/tests/TelecomTestCase.java
index a029b35..ed10e4b 100644
--- a/tests/src/com/android/server/telecom/tests/TelecomTestCase.java
+++ b/tests/src/com/android/server/telecom/tests/TelecomTestCase.java
@@ -20,8 +20,12 @@
 
 import org.mockito.MockitoAnnotations;
 
+import android.os.Handler;
 import android.test.AndroidTestCase;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
 public abstract class TelecomTestCase extends AndroidTestCase {
     protected static final String TESTING_TAG = "Telecom-TEST";
 
@@ -44,4 +48,16 @@
         mMockitoHelper.tearDown();
         Log.setTag(com.android.server.telecom.Log.TAG);
     }
+
+    protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
+        final CountDownLatch lock = new CountDownLatch(1);
+        h.post(lock::countDown);
+        while (lock.getCount() > 0) {
+            try {
+                lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
+            } catch (InterruptedException e) {
+                // do nothing
+            }
+        }
+    }
 }
diff --git a/tests/src/com/android/server/telecom/tests/VideoCallTests.java b/tests/src/com/android/server/telecom/tests/VideoCallTests.java
index 2a54034..c079857 100644
--- a/tests/src/com/android/server/telecom/tests/VideoCallTests.java
+++ b/tests/src/com/android/server/telecom/tests/VideoCallTests.java
@@ -21,10 +21,11 @@
 import android.telecom.CallAudioState;
 import android.telecom.VideoProfile;
 
+import com.android.server.telecom.CallAudioRouteStateMachine;
+
 import java.util.List;
 
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.verify;
 
 /**
@@ -44,7 +45,7 @@
                 mPhoneAccountA0.getAccountHandle(), mConnectionServiceFixtureA,
                 VideoProfile.STATE_BIDIRECTIONAL);
 
-        verifyAudioRoute(CallAudioState.ROUTE_SPEAKER, 2);
+        verifyAudioRoute(CallAudioState.ROUTE_SPEAKER);
     }
 
     /**
@@ -59,7 +60,7 @@
                 mPhoneAccountA0.getAccountHandle(), mConnectionServiceFixtureA,
                 VideoProfile.STATE_RX_ENABLED);
 
-        verifyAudioRoute(CallAudioState.ROUTE_SPEAKER, 2);
+        verifyAudioRoute(CallAudioState.ROUTE_SPEAKER);
     }
 
     /**
@@ -72,7 +73,7 @@
                 mPhoneAccountA0.getAccountHandle(), mConnectionServiceFixtureA,
                 VideoProfile.STATE_BIDIRECTIONAL);
 
-        verifyAudioRoute(CallAudioState.ROUTE_SPEAKER, 2);
+        verifyAudioRoute(CallAudioState.ROUTE_SPEAKER);
     }
 
     /**
@@ -86,7 +87,7 @@
                 mPhoneAccountA0.getAccountHandle(), mConnectionServiceFixtureA,
                 VideoProfile.STATE_TX_ENABLED);
 
-        verifyAudioRoute(CallAudioState.ROUTE_SPEAKER, 2);
+        verifyAudioRoute(CallAudioState.ROUTE_SPEAKER);
     }
 
     /**
@@ -100,7 +101,7 @@
                 mPhoneAccountA0.getAccountHandle(), mConnectionServiceFixtureA,
                 VideoProfile.STATE_AUDIO_ONLY);
 
-        verifyAudioRoute(CallAudioState.ROUTE_EARPIECE, 1);
+        verifyAudioRoute(CallAudioState.ROUTE_EARPIECE);
     }
 
     /**
@@ -113,7 +114,7 @@
                 mPhoneAccountA0.getAccountHandle(), mConnectionServiceFixtureA,
                 VideoProfile.STATE_AUDIO_ONLY);
 
-        verifyAudioRoute(CallAudioState.ROUTE_EARPIECE, 1);
+        verifyAudioRoute(CallAudioState.ROUTE_EARPIECE);
     }
 
     /**
@@ -122,18 +123,16 @@
      * called with an expected route and number of changes.
      *
      * @param expectedRoute The expected audio route on the latest change.
-     * @param audioStateChangeCount The number of audio state changes expected.  This is set based
-     *                              on how many times we expect the audio route to change when
-     *                              setting up a call.  For an audio-only call, we normally expect
-     *                              1 route change, and for a video call we expect an extra change.
      */
-    private void verifyAudioRoute(int expectedRoute, int audioStateChangeCount) throws Exception {
+    private void verifyAudioRoute(int expectedRoute) throws Exception {
         // Capture all onCallAudioStateChanged callbacks to InCall.
+        CallAudioRouteStateMachine carsm = mTelecomSystem.getCallsManager()
+                        .getCallAudioManager().getCallAudioRouteStateMachine();
+        waitForHandlerAction(carsm.getHandler(), TEST_TIMEOUT);
         ArgumentCaptor<CallAudioState> callAudioStateArgumentCaptor = ArgumentCaptor.forClass(
                 CallAudioState.class);
-        verify(mInCallServiceFixtureX.getTestDouble(),
-                timeout(TEST_TIMEOUT).times(audioStateChangeCount)).
-                onCallAudioStateChanged(callAudioStateArgumentCaptor.capture());
+        verify(mInCallServiceFixtureX.getTestDouble(), atLeastOnce()).onCallAudioStateChanged(
+                callAudioStateArgumentCaptor.capture());
         List<CallAudioState> changes = callAudioStateArgumentCaptor.getAllValues();
         assertEquals(expectedRoute, changes.get(changes.size() - 1).getRoute());
     }