Snap for 4418112 from cdc828d2e0fefd417281e1e9b54867277160297f to pi-release

Change-Id: I58ad6103245be3402eace92455adef8e6597ae5c
diff --git a/robotests/Android.mk b/robotests/Android.mk
new file mode 100644
index 0000000..e0a4f86
--- /dev/null
+++ b/robotests/Android.mk
@@ -0,0 +1,45 @@
+#############################################
+#     KeyChain Robolectric test target.     #
+#############################################
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+# Include the testing libraries (JUnit4 + Robolectric libs).
+LOCAL_STATIC_JAVA_LIBRARIES := \
+    mockito-robolectric-prebuilt \
+    platform-robolectric-android-all-stubs \
+    truth-prebuilt
+
+LOCAL_JAVA_LIBRARIES := \
+    junit \
+    platform-robolectric-3.4.2-prebuilt \
+    telephony-common
+
+LOCAL_INSTRUMENTATION_FOR := KeyChain
+LOCAL_MODULE := KeyChainRoboTests
+
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+#############################################################
+# Settings runner target to run the previous target. #
+#############################################################
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := RunKeyChainRoboTests
+
+LOCAL_SDK_VERSION := current
+
+LOCAL_STATIC_JAVA_LIBRARIES := \
+    KeyChainRoboTests
+
+LOCAL_TEST_PACKAGE := KeyChain
+
+LOCAL_INSTRUMENT_SOURCE_DIRS := $(dir $(LOCAL_PATH))../src
+
+LOCAL_ROBOTEST_TIMEOUT := 36000
+
+include prebuilts/misc/common/robolectric/3.4.2/run_robotests.mk
diff --git a/robotests/src/com/android/keychain/AliasLoaderTest.java b/robotests/src/com/android/keychain/AliasLoaderTest.java
new file mode 100644
index 0000000..1c4c50c
--- /dev/null
+++ b/robotests/src/com/android/keychain/AliasLoaderTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.keychain;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import android.security.Credentials;
+import android.security.KeyStore;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowApplication;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public final class AliasLoaderTest {
+
+    @Before
+    public void setUp() {}
+
+    @Test
+    public void testAliasLoader_loadsAllAliases()
+            throws InterruptedException, ExecutionException, CancellationException,
+                    TimeoutException {
+        KeyStore keyStore = mock(KeyStore.class);
+        when(keyStore.list(Credentials.USER_PRIVATE_KEY)).thenReturn(new String[] {"b", "c", "a"});
+
+        KeyChainActivity.AliasLoader loader =
+                new KeyChainActivity.AliasLoader(keyStore, RuntimeEnvironment.application);
+        loader.execute();
+
+        ShadowApplication.runBackgroundTasks();
+        KeyChainActivity.CertificateAdapter result = loader.get(5, TimeUnit.SECONDS);
+        Assert.assertNotNull(result);
+        Assert.assertEquals(3, result.getCount());
+        Assert.assertEquals("a", result.getItem(0));
+        Assert.assertEquals("b", result.getItem(1));
+        Assert.assertEquals("c", result.getItem(2));
+    }
+}
diff --git a/robotests/src/com/android/keychain/TestConfig.java b/robotests/src/com/android/keychain/TestConfig.java
new file mode 100644
index 0000000..d31d2e5
--- /dev/null
+++ b/robotests/src/com/android/keychain/TestConfig.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.keychain;
+
+public class TestConfig {
+    public static final int SDK_VERSION = 26;
+    public static final String MANIFEST_PATH = "packages/apps/KeyChain/AndroidManifest.xml";
+}
diff --git a/src/com/android/keychain/KeyChainActivity.java b/src/com/android/keychain/KeyChainActivity.java
index 30d86ee..ed8cd8c 100644
--- a/src/com/android/keychain/KeyChainActivity.java
+++ b/src/com/android/keychain/KeyChainActivity.java
@@ -46,6 +46,7 @@
 import android.widget.ListView;
 import android.widget.RadioButton;
 import android.widget.TextView;
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.org.bouncycastle.asn1.x509.X509Name;
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
@@ -192,7 +193,8 @@
         }
     }
 
-    private static class AliasLoader extends AsyncTask<Void, Void, CertificateAdapter> {
+    @VisibleForTesting
+    static class AliasLoader extends AsyncTask<Void, Void, CertificateAdapter> {
         private final KeyStore mKeyStore;
         private final Context mContext;
         public AliasLoader(KeyStore keyStore, Context context) {
@@ -330,7 +332,8 @@
         dialog.show();
     }
 
-    private static class CertificateAdapter extends BaseAdapter {
+    @VisibleForTesting
+    static class CertificateAdapter extends BaseAdapter {
         private final List<String> mAliases;
         private final List<String> mSubjects = new ArrayList<String>();
         private final KeyStore mKeyStore;