Added unit tests for Translation.

Fixes: 176868784
Test: atest CtsTranslationTestCases
Change-Id: Id7b3a3d5e2ca60cb4f1cfa8d59d6ae5483866a0b
diff --git a/tests/translation/Android.bp b/tests/translation/Android.bp
new file mode 100644
index 0000000..dfe1a849
--- /dev/null
+++ b/tests/translation/Android.bp
@@ -0,0 +1,38 @@
+// 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.
+
+android_test {
+    name: "CtsTranslationTestCases",
+    defaults: ["cts_defaults"],
+    // Tag this module as a cts test artifact
+    test_suites: [
+        "cts",
+        "general-tests",
+    ],
+    libs: ["android.test.base"],
+    static_libs: [
+        "androidx.test.core",
+        "androidx.test.rules",
+        "compatibility-device-util-axt",
+        "ctstestrunner-axt",
+        "truth-prebuilt",
+        "androidx.test.ext.junit",
+        "testng",
+    ],
+    srcs: [
+        "src/**/*.java",
+    ],
+    sdk_version: "test_current",
+    min_sdk_version: "30",
+}
diff --git a/tests/translation/AndroidManifest.xml b/tests/translation/AndroidManifest.xml
new file mode 100644
index 0000000..946625d
--- /dev/null
+++ b/tests/translation/AndroidManifest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          package="android.translation.cts">
+
+    <application android:label="Translation TestCase">
+        <uses-library android:name="android.test.runner"/>
+
+    </application>
+
+    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+                     android:targetPackage="android.translation.cts"
+                     android:label="CTS tests of android.translation">
+    </instrumentation>
+
+</manifest>
diff --git a/tests/translation/AndroidTest.xml b/tests/translation/AndroidTest.xml
new file mode 100644
index 0000000..f83ed7f
--- /dev/null
+++ b/tests/translation/AndroidTest.xml
@@ -0,0 +1,32 @@
+<?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.
+-->
+<configuration description="Config for Translation CTS">
+  <option name="test-suite-tag" value="cts" />
+  <option name="config-descriptor:metadata" key="component" value="framework" />
+  <option name="config-descriptor:metadata" key="parameter" value="instant_app" />
+  <option name="config-descriptor:metadata" key="parameter" value="multi_abi" />
+  <option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
+  <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+    <option name="cleanup-apks" value="true" />
+    <option name="test-file-name" value="CtsTranslationTestCases.apk" />
+  </target_preparer>
+  <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
+    <option name="package" value="android.translation.cts" />
+    <option name="runtime-hint" value="1m" />
+    <option name="hidden-api-checks" value="false" />
+    <option name="isolated-storage" value="false" />
+  </test>
+</configuration>
diff --git a/tests/translation/OWNERS b/tests/translation/OWNERS
new file mode 100644
index 0000000..a1e663a
--- /dev/null
+++ b/tests/translation/OWNERS
@@ -0,0 +1,8 @@
+# Bug component: 994311
+
+adamhe@google.com
+augale@google.com
+joannechung@google.com
+lpeter@google.com
+svetoslavganov@google.com
+tymtsai@google.com
diff --git a/tests/translation/src/android/translation/cts/unittests/InternalTranslationRequestTest.java b/tests/translation/src/android/translation/cts/unittests/InternalTranslationRequestTest.java
new file mode 100644
index 0000000..1255647
--- /dev/null
+++ b/tests/translation/src/android/translation/cts/unittests/InternalTranslationRequestTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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 android.translation.cts.unittests;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.testng.Assert.assertThrows;
+
+import android.os.Parcel;
+import android.service.translation.TranslationRequest;
+import android.view.autofill.AutofillId;
+import android.view.translation.TranslationSpec;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.ArrayList;
+
+@RunWith(AndroidJUnit4.class)
+public class InternalTranslationRequestTest {
+
+    private final TranslationSpec mSourceSpec =
+            new TranslationSpec("en", TranslationSpec.DATA_FORMAT_TEXT);
+    private final TranslationSpec mDestSpec =
+            new TranslationSpec("zh", TranslationSpec.DATA_FORMAT_TEXT);
+    private final android.view.translation.TranslationRequest mRequest =
+            new android.view.translation.TranslationRequest.Builder()
+            .setAutofillId(new AutofillId(17))
+            .setTranslationText("sample text")
+            .build();
+
+    @Test
+    public void testBuilder_nullSpecs() {
+        final ArrayList<android.view.translation.TranslationRequest> requests = new ArrayList<>();
+        assertThrows(NullPointerException.class, () -> {
+            final TranslationRequest request =
+                    new TranslationRequest.Builder(0, null, mDestSpec, requests).build();
+        });
+        assertThrows(NullPointerException.class, () -> {
+            final TranslationRequest request =
+                    new TranslationRequest.Builder(0, mSourceSpec, null, requests).build();
+        });
+    }
+
+    @Test
+    public void testBuilder_nullRequests() {
+        assertThrows(NullPointerException.class, () -> {
+            final TranslationRequest request =
+                    new TranslationRequest.Builder(0, mSourceSpec, mDestSpec, null).build();
+        });
+    }
+
+    @Test
+    public void testBuilder_validRequests() {
+        final ArrayList<android.view.translation.TranslationRequest> requests = new ArrayList<>();
+        requests.add(mRequest);
+        final TranslationRequest request =
+                new TranslationRequest.Builder(0, mSourceSpec, mDestSpec, requests).build();
+
+        assertThat(request.getRequestId()).isEqualTo(0);
+        assertThat(request.getSourceSpec()).isEqualTo(mSourceSpec);
+        assertThat(request.getDestSpec()).isEqualTo(mDestSpec);
+        assertThat(request.getTranslationRequests().size()).isEqualTo(1);
+
+        final android.view.translation.TranslationRequest request1 =
+                request.getTranslationRequests().get(0);
+        assertThat(request1.getAutofillId()).isEqualTo(new AutofillId(17));
+        assertThat(request1.getTranslationText()).isEqualTo("sample text");
+    }
+
+    @Test
+    public void testBuilder_validAddRequests() {
+        final ArrayList<android.view.translation.TranslationRequest> requests = new ArrayList<>();
+        final TranslationRequest request =
+                new TranslationRequest.Builder(0, mSourceSpec, mDestSpec, requests)
+                .addTranslationRequests(mRequest)
+                .build();
+
+        assertThat(request.getRequestId()).isEqualTo(0);
+        assertThat(request.getSourceSpec()).isEqualTo(mSourceSpec);
+        assertThat(request.getDestSpec()).isEqualTo(mDestSpec);
+        assertThat(request.getTranslationRequests().size()).isEqualTo(1);
+
+        final android.view.translation.TranslationRequest request1 =
+                request.getTranslationRequests().get(0);
+        assertThat(request1.getAutofillId()).isEqualTo(new AutofillId(17));
+        assertThat(request1.getTranslationText()).isEqualTo("sample text");
+    }
+
+    @Test
+    public void testParceledRequest() {
+        final ArrayList<android.view.translation.TranslationRequest> requests = new ArrayList<>();
+        requests.add(mRequest);
+        final TranslationRequest request =
+                new TranslationRequest.Builder(0, mSourceSpec, mDestSpec, requests).build();
+
+        final Parcel parcel = Parcel.obtain();
+        request.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        final TranslationRequest parceledRequest =
+                TranslationRequest.CREATOR.createFromParcel(parcel);
+
+        assertThat(request.getRequestId()).isEqualTo(0);
+        assertThat(request.getSourceSpec()).isEqualTo(mSourceSpec);
+        assertThat(request.getDestSpec()).isEqualTo(mDestSpec);
+        assertThat(request.getTranslationRequests().size()).isEqualTo(1);
+
+        final android.view.translation.TranslationRequest request1 =
+                parceledRequest.getTranslationRequests().get(0);
+        assertThat(request1.getAutofillId()).isEqualTo(new AutofillId(17));
+        assertThat(request1.getTranslationText()).isEqualTo("sample text");
+    }
+}
\ No newline at end of file
diff --git a/tests/translation/src/android/translation/cts/unittests/TranslationRequestTest.java b/tests/translation/src/android/translation/cts/unittests/TranslationRequestTest.java
new file mode 100644
index 0000000..eb3b772
--- /dev/null
+++ b/tests/translation/src/android/translation/cts/unittests/TranslationRequestTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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 android.translation.cts.unittests;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.os.Parcel;
+import android.view.autofill.AutofillId;
+import android.view.translation.TranslationRequest;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class TranslationRequestTest {
+
+    private final AutofillId mAutofillId = new AutofillId(17);
+    private final String mTranslationText = "sample text";
+
+    @Test
+    public void testBuilder_validRequest() {
+        final TranslationRequest request = new TranslationRequest.Builder()
+                .setAutofillId(mAutofillId)
+                .setTranslationText(mTranslationText)
+                .build();
+
+        assertThat(request.getAutofillId()).isEqualTo(new AutofillId(17));
+        assertThat(request.getTranslationText()).isEqualTo("sample text");
+    }
+
+    @Test
+    public void testParceledRequest() {
+        final TranslationRequest request = new TranslationRequest.Builder()
+                .setAutofillId(mAutofillId)
+                .setTranslationText(mTranslationText)
+                .build();
+
+        final Parcel parcel = Parcel.obtain();
+        request.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        final TranslationRequest parceledRequest =
+                TranslationRequest.CREATOR.createFromParcel(parcel);
+
+        assertThat(request.getAutofillId()).isEqualTo(new AutofillId(17));
+        assertThat(request.getTranslationText()).isEqualTo("sample text");
+    }
+}
\ No newline at end of file
diff --git a/tests/translation/src/android/translation/cts/unittests/TranslationResponseTest.java b/tests/translation/src/android/translation/cts/unittests/TranslationResponseTest.java
new file mode 100644
index 0000000..85548e1
--- /dev/null
+++ b/tests/translation/src/android/translation/cts/unittests/TranslationResponseTest.java
@@ -0,0 +1,146 @@
+/*
+ * 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 android.translation.cts.unittests;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.os.Parcel;
+import android.view.autofill.AutofillId;
+import android.view.translation.TranslationRequest;
+import android.view.translation.TranslationResponse;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.ArrayList;
+
+@RunWith(AndroidJUnit4.class)
+public class TranslationResponseTest {
+
+    private final TranslationRequest mRequest = new TranslationRequest.Builder()
+            .setAutofillId(new AutofillId(17))
+            .setTranslationText("text1")
+            .build();
+    private final TranslationRequest mRequest2 = new TranslationRequest.Builder()
+            .setAutofillId(new AutofillId(42))
+            .setTranslationText("text2")
+            .build();
+
+    @Test
+    public void testBuilder_nullTranslation() {
+        final TranslationResponse response =
+                new TranslationResponse.Builder(TranslationResponse.TRANSLATION_STATUS_SUCCESS)
+                .addTranslations(null)
+                .build();
+
+        assertThat(response.getTranslationStatus())
+                .isEqualTo(TranslationResponse.TRANSLATION_STATUS_SUCCESS);
+        assertThat(response.getTranslations().size()).isEqualTo(1);
+        assertThat(response.getTranslations().get(0)).isNull();
+    }
+
+    @Test
+    public void testBuilder_emptyTranslations() {
+        final TranslationResponse response =
+                new TranslationResponse.Builder(TranslationResponse.TRANSLATION_STATUS_SUCCESS)
+                .build();
+
+        assertThat(response.getTranslationStatus())
+                .isEqualTo(TranslationResponse.TRANSLATION_STATUS_SUCCESS);
+        assertThat(response.getTranslations().size()).isEqualTo(0);
+    }
+
+    @Test
+    public void testBuilder_changeTranslationStatus() {
+        final TranslationResponse response =
+                new TranslationResponse.Builder(TranslationResponse.TRANSLATION_STATUS_SUCCESS)
+                .setTranslationStatus(TranslationResponse.TRANSLATION_STATUS_UNKNOWN_ERROR)
+                .build();
+
+        assertThat(response.getTranslationStatus())
+                .isEqualTo(TranslationResponse.TRANSLATION_STATUS_UNKNOWN_ERROR);
+    }
+
+    @Test
+    public void testBuilder_validAddRequests() {
+        final TranslationResponse response =
+                new TranslationResponse.Builder(TranslationResponse.TRANSLATION_STATUS_SUCCESS)
+                .addTranslations(mRequest)
+                .addTranslations(mRequest2)
+                .build();
+
+        assertThat(response.getTranslationStatus())
+                .isEqualTo(TranslationResponse.TRANSLATION_STATUS_SUCCESS);
+        assertThat(response.getTranslations().size()).isEqualTo(2);
+
+        final TranslationRequest request1 = response.getTranslations().get(0);
+        assertThat(request1.getAutofillId()).isEqualTo(new AutofillId(17));
+        assertThat(request1.getTranslationText()).isEqualTo("text1");
+
+        final TranslationRequest request2 = response.getTranslations().get(1);
+        assertThat(request2.getAutofillId()).isEqualTo(new AutofillId(42));
+        assertThat(request2.getTranslationText()).isEqualTo("text2");
+    }
+
+    @Test
+    public void testBuilder_validSetRequests() {
+        final ArrayList<TranslationRequest> requests = new ArrayList<>();
+        requests.add(mRequest);
+        requests.add(mRequest2);
+        final TranslationResponse response =
+                new TranslationResponse.Builder(TranslationResponse.TRANSLATION_STATUS_SUCCESS)
+                .setTranslations(requests)
+                .build();
+
+        assertThat(response.getTranslationStatus())
+                .isEqualTo(TranslationResponse.TRANSLATION_STATUS_SUCCESS);
+        assertThat(response.getTranslations().size()).isEqualTo(2);
+
+        final TranslationRequest request1 = response.getTranslations().get(0);
+        assertThat(request1.getAutofillId()).isEqualTo(new AutofillId(17));
+        assertThat(request1.getTranslationText()).isEqualTo("text1");
+
+        final TranslationRequest request2 = response.getTranslations().get(1);
+        assertThat(request2.getAutofillId()).isEqualTo(new AutofillId(42));
+        assertThat(request2.getTranslationText()).isEqualTo("text2");
+    }
+
+    @Test
+    public void testParceledResponse() {
+        final TranslationResponse response =
+                new TranslationResponse.Builder(TranslationResponse.TRANSLATION_STATUS_SUCCESS)
+                .addTranslations(mRequest)
+                .build();
+
+        final Parcel parcel = Parcel.obtain();
+        response.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        final TranslationResponse parceledResponse =
+                TranslationResponse.CREATOR.createFromParcel(parcel);
+
+        assertThat(response.getTranslationStatus())
+                .isEqualTo(TranslationResponse.TRANSLATION_STATUS_SUCCESS);
+        assertThat(response.getTranslations().size()).isEqualTo(1);
+
+        final TranslationRequest request1 = response.getTranslations().get(0);
+        assertThat(request1.getAutofillId()).isEqualTo(new AutofillId(17));
+        assertThat(request1.getTranslationText()).isEqualTo("text1");
+    }
+
+}
\ No newline at end of file
diff --git a/tests/translation/src/android/translation/cts/unittests/TranslationSpecTest.java b/tests/translation/src/android/translation/cts/unittests/TranslationSpecTest.java
new file mode 100644
index 0000000..5cf9ab1
--- /dev/null
+++ b/tests/translation/src/android/translation/cts/unittests/TranslationSpecTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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 android.translation.cts.unittests;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.testng.Assert.assertThrows;
+
+import android.os.Parcel;
+import android.view.translation.TranslationSpec;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class TranslationSpecTest {
+
+    @Test
+    public void testSpec_nullLanguage() {
+        assertThrows(NullPointerException.class, () -> {
+            final TranslationSpec spec =
+                    new TranslationSpec(null, TranslationSpec.DATA_FORMAT_TEXT);
+        });
+    }
+
+    @Test
+    public void testSpec_validSpec() {
+        final TranslationSpec spec = new TranslationSpec("en", TranslationSpec.DATA_FORMAT_TEXT);
+
+        assertThat(spec.getDataFormat()).isEqualTo(TranslationSpec.DATA_FORMAT_TEXT);
+        assertThat(spec.getLanguage()).isEqualTo("en");
+    }
+
+    @Test
+    public void testParceledSpec() {
+        final TranslationSpec spec = new TranslationSpec("en", TranslationSpec.DATA_FORMAT_TEXT);
+
+        assertThat(spec.getDataFormat()).isEqualTo(TranslationSpec.DATA_FORMAT_TEXT);
+        assertThat(spec.getLanguage()).isEqualTo("en");
+
+        final Parcel parcel = Parcel.obtain();
+        spec.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+        final TranslationSpec parceledSpec = TranslationSpec.CREATOR.createFromParcel(parcel);
+        parcel.recycle();
+
+        assertThat(parceledSpec.getDataFormat()).isEqualTo(TranslationSpec.DATA_FORMAT_TEXT);
+        assertThat(parceledSpec.getLanguage()).isEqualTo("en");
+    }
+
+}
\ No newline at end of file