Snap for 4434599 from 43fb3695ddc259f12f1fc8b7b6fa13a04dcf2465 to pi-release

Change-Id: I87b6d1dfcff75386aad49a3e670ae022ba1c79b8
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index dc60913..fa9cd55 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -20,8 +20,19 @@
     <uses-permission android:name="android.permission.INTERNET"/>
 
     <!--
+        Install the activity and disable battery optimization (so the KeyChainServiceTest can be
+        run in the background):
+        adb install out/target/product/${TARGET_PRODUCT}/data/app/KeyChainTests/KeyChainTests.apk
+        Then navigate to Settings -> Battery -> ... -> Battery optimization -> select All Apps
+        Find com.android.keychain.tests and select Do Not Optimize.
+
+        Alternatively, the following command can be used to exclude the test services from
+        the background execution restriction for 2 minutes:
+        adb shell cmd deviceidle tempwhitelist -d 120000 com.android.keychain.tests
+
         To run service:
         adb shell am startservice -n com.android.keychain.tests/.KeyChainServiceTest
+        One has to inspect the ADB log to find out about test failures.
 
         To run activity:
         adb shell am start -n com.android.keychain.tests/com.android.keychain.tests.KeyChainTestActivity
diff --git a/tests/src/com/android/keychain/tests/KeyChainServiceTest.java b/tests/src/com/android/keychain/tests/KeyChainServiceTest.java
index 1f3f7de..7e4008a 100644
--- a/tests/src/com/android/keychain/tests/KeyChainServiceTest.java
+++ b/tests/src/com/android/keychain/tests/KeyChainServiceTest.java
@@ -21,6 +21,8 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.ServiceConnection;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.os.IBinder;
 import android.security.Credentials;
 import android.security.IKeyChainService;
@@ -75,16 +77,36 @@
         }
     };
 
+    private static void addComponentToIntent(PackageManager pm, Intent intent) {
+        ResolveInfo service = pm.resolveService(intent, 0);
+        if (service == null) {
+            Log.w(TAG, String.format("No service found for intent: %s", intent.getAction()));
+        } else {
+            Log.d(TAG, String.format("Found service: %s %s for action %s",
+                        service.serviceInfo.packageName, service.serviceInfo.name,
+                        intent.getAction()));
+            ComponentName comp = new ComponentName(
+                    service.serviceInfo.packageName, service.serviceInfo.name);
+            intent.setComponent(comp);
+        }
+    }
+
     private void bindSupport() {
-        mIsBoundSupport = bindService(new Intent(IKeyChainServiceTestSupport.class.getName()),
+        Intent serviceIntent = new Intent(IKeyChainServiceTestSupport.class.getName());
+        addComponentToIntent(getPackageManager(), serviceIntent);
+        mIsBoundSupport = bindService(serviceIntent,
                                       mSupportConnection,
                                       Context.BIND_AUTO_CREATE);
+        Log.d(TAG, String.format("Finished bindSupport with result: %b", mIsBoundSupport));
     }
 
     private void bindService() {
-        mIsBoundService = bindService(new Intent(IKeyChainService.class.getName()),
+        Intent serviceIntent = new Intent(IKeyChainService.class.getName());
+        addComponentToIntent(getPackageManager(), serviceIntent);
+        mIsBoundService = bindService(serviceIntent,
                                       mServiceConnection,
                                       Context.BIND_AUTO_CREATE);
+        Log.d(TAG, String.format("Finished bindService with result: %b", mIsBoundService));
     }
 
     private void unbindServices() {