Set up Android.bp and gradle for tests

Fix: 205772173
Test: gradle build, atest CarMessengerUnitTests

Change-Id: I65b3cfd5b9c19b744fe71d8bd6e40d33bc9045d5
diff --git a/Android.bp b/Android.bp
index 750a871..1d9412d 100644
--- a/Android.bp
+++ b/Android.bp
@@ -25,10 +25,16 @@
 
     resource_dirs: ["res"],
 
+    min_sdk_version: "30",
+
+    target_sdk_version: "31",
+
     sdk_version: "system_current",
 
     required: ["allowed_privapp_com.android.car.messenger"],
 
+    certificate: "platform",
+
     overrides: ["messaging"],
 
     optimize: {
@@ -64,3 +70,66 @@
         },
     },
 }
+
+android_test_helper_app {
+    name: "CarMessengerAppForTesting",
+
+    srcs: ["src/**/*.java"],
+
+    resource_dirs: ["res"],
+
+    min_sdk_version: "30",
+
+    target_sdk_version: "31",
+
+    sdk_version: "system_current",
+
+    required: ["allowed_privapp_com.android.car.messenger"],
+
+    certificate: "platform",
+
+    overrides: ["messaging"],
+
+    optimize: {
+        enabled: false,
+    },
+
+    privileged: true,
+
+    libs: ["android.car-system-stubs"],
+
+    // must be unbundled dependencies
+    static_libs: [
+        "androidx-constraintlayout_constraintlayout",
+        "androidx.lifecycle_lifecycle-common-java8",
+        "androidx.lifecycle_lifecycle-extensions",
+        "androidx.legacy_legacy-support-v4",
+        "androidx.preference_preference",
+        "androidx.recyclerview_recyclerview",
+        "car-assist-lib",
+        "car-messenger-common",
+        "car-telephony-common",
+        "car-ui-lib",
+        "androidx.annotation_annotation",
+    ],
+
+    dex_preopt: {
+        enabled: false,
+    },
+
+    product_variables: {
+        pdk: {
+            enabled: false,
+        },
+    },
+
+    // runtime cc library which is used by mockito-target-extended.
+    jni_libs: [
+        // For mockito extended
+        "libdexmakerjvmtiagent",
+        "libstaticjvmtiagent",
+    ],
+
+    // Enforce the jni libraries to be grouped into the APK file.
+    use_embedded_native_libs: true,
+}
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index d48bd17..11014dd 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -16,7 +16,9 @@
   -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.android.car.messenger">
+    package="com.android.car.messenger"
+    android:versionCode="10000"
+    android:versionName="1.0.0">
 
     <application
         android:name="com.android.car.messenger.impl.CarMessengerApp"
@@ -131,7 +133,4 @@
     -->
     <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
 
-    <uses-sdk
-        android:minSdkVersion="30"
-        android:targetSdkVersion="30" />
 </manifest>
diff --git a/build.gradle b/build.gradle
index b0e8bc2..88e8ced 100644
--- a/build.gradle
+++ b/build.gradle
@@ -44,6 +44,29 @@
             renderscript.srcDirs = ['src']
             res.srcDirs = ['res']
         }
+
+        androidTest {
+            java.srcDirs = ['tests/src']
+            setRoot('tests')
+        }
+    }
+
+    testOptions {
+        animationsDisabled = true
+    }
+
+    buildTypes {
+        debug {
+            testCoverageEnabled = true
+        }
+    }
+
+    signingConfigs {
+        debug {
+            // The following signs the apk with platform key.
+            storeFile file('../../../../vendor/google/certs/devkeys/platform.keystore')
+            storePassword 'android'
+        }
     }
 }
 
@@ -65,4 +88,11 @@
     implementation project(":car-messenger-common:model")
     implementation project(":car-telephony-common")
     implementation project(":car-ui-lib")
+
+    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
+    androidTestImplementation 'androidx.test:core:1.4.0'
+    androidTestImplementation 'androidx.test:rules:1.4.0'
+    androidTestImplementation 'androidx.test:runner:1.4.0'
+    androidTestImplementation 'com.google.truth:truth:1.1.3'
+    androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.28.1'
 }
diff --git a/tests/Android.bp b/tests/Android.bp
new file mode 100644
index 0000000..8dac782
--- /dev/null
+++ b/tests/Android.bp
@@ -0,0 +1,43 @@
+//
+// Copyright (C) 2021 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 {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_test {
+    name: "CarMessengerUnitTests",
+
+    min_sdk_version: "30",
+
+    target_sdk_version: "31",
+
+    srcs: ["src/**/*.java"],
+
+    libs: ["android.car-system-stubs"],
+
+    static_libs: [
+        "androidx.test.core",
+        "androidx.test.rules",
+        "androidx.test.runner",
+        "androidx.test.ext.junit",
+        "androidx.test.ext.truth",
+        "mockito-target-extended-minus-junit4",
+        "truth-prebuilt",
+    ],
+
+    instrumentation_for: "CarMessengerAppForTesting",
+}
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
new file mode 100644
index 0000000..e59c7c4
--- /dev/null
+++ b/tests/AndroidManifest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2021 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.
+  -->
+
+<manifest
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.car.messenger.unittests">
+
+    <application android:testOnly="false"
+                 android:debuggable="true">
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+                     android:targetPackage="com.android.car.messenger"
+                     android:label="Car Messenger Unit Tests" />
+</manifest>
diff --git a/tests/src/com/android/car/messenger/BuildTest.java b/tests/src/com/android/car/messenger/BuildTest.java
new file mode 100644
index 0000000..3d35ddb
--- /dev/null
+++ b/tests/src/com/android/car/messenger/BuildTest.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2021 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.car.messenger;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class BuildTest {
+
+    @Test
+    public void test() {
+        assertThat(true).isEqualTo(true);
+    }
+}