Add tests for the application's load label api

Verify that the length of an application's or component's label
should not exceed the maximum safe length.

Bug: 67013844
Bug: 197298717
Test: atest PackageManagerTest
Change-Id: I0da1959e6ea33925b11831f5597bbe0a4fa8bd48
Merged-in: I0da1959e6ea33925b11831f5597bbe0a4fa8bd48
diff --git a/tests/tests/content/AndroidTest.xml b/tests/tests/content/AndroidTest.xml
index e39bf36..c03dd80 100644
--- a/tests/tests/content/AndroidTest.xml
+++ b/tests/tests/content/AndroidTest.xml
@@ -39,6 +39,7 @@
         <option name="push" value="CtsContentLongSharedUserIdTestApp.apk->/data/local/tmp/cts/content/CtsContentLongSharedUserIdTestApp.apk" />
         <option name="push" value="CtsContentMaxPackageNameTestApp.apk->/data/local/tmp/cts/content/CtsContentMaxPackageNameTestApp.apk" />
         <option name="push" value="CtsContentMaxSharedUserIdTestApp.apk->/data/local/tmp/cts/content/CtsContentMaxSharedUserIdTestApp.apk" />
+        <option name="push" value="CtsContentLongLabelNameTestApp.apk->/data/local/tmp/cts/content/CtsContentLongLabelNameTestApp.apk" />
     </target_preparer>
 
     <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
diff --git a/tests/tests/content/emptytestapp/Android.bp b/tests/tests/content/emptytestapp/Android.bp
index 42f36d0..f66ccdf 100644
--- a/tests/tests/content/emptytestapp/Android.bp
+++ b/tests/tests/content/emptytestapp/Android.bp
@@ -77,3 +77,16 @@
         "general-tests",
     ],
 }
+
+android_test_helper_app {
+    name: "CtsContentLongLabelNameTestApp",
+    defaults: ["cts_defaults"],
+    sdk_version: "current",
+    manifest: "AndroidManifestLongLabelName.xml",
+    // tag this module as a cts test artifact
+    test_suites: [
+        "cts",
+        "general-tests",
+    ],
+    min_sdk_version : "29"
+}
diff --git a/tests/tests/content/emptytestapp/AndroidManifestLongLabelName.xml b/tests/tests/content/emptytestapp/AndroidManifestLongLabelName.xml
new file mode 100644
index 0000000..eaa69b1
--- /dev/null
+++ b/tests/tests/content/emptytestapp/AndroidManifestLongLabelName.xml
@@ -0,0 +1,23 @@
+<?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.
+-->
+<!-- Test that the maximum length of the label returned from the load label api should be 1000 -->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+        package="android.content.cts.emptytestapp" >
+    <application android:hasCode="false" android:label="@string/long_app_name">
+        <activity android:name=".MockActivity" android:label="@string/long_app_name"
+                  android:exported="true" android:enabled="true" />
+    </application>
+</manifest>
diff --git a/tests/tests/content/emptytestapp/res/values/strings.xml b/tests/tests/content/emptytestapp/res/values/strings.xml
new file mode 100644
index 0000000..8c06d18
--- /dev/null
+++ b/tests/tests/content/emptytestapp/res/values/strings.xml
@@ -0,0 +1,31 @@
+<?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.
+-->
+
+<resources>
+    <string name="long_app_name">
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+EmptyTestApp123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
+    </string>
+</resources>
diff --git a/tests/tests/content/src/android/content/pm/cts/PackageManagerTest.java b/tests/tests/content/src/android/content/pm/cts/PackageManagerTest.java
index 598e882..6ca8a01 100644
--- a/tests/tests/content/src/android/content/pm/cts/PackageManagerTest.java
+++ b/tests/tests/content/src/android/content/pm/cts/PackageManagerTest.java
@@ -154,6 +154,8 @@
             + "CtsContentMaxPackageNameTestApp.apk";
     private static final String MAX_SHARED_USER_ID_APK = SAMPLE_APK_BASE
             + "CtsContentMaxSharedUserIdTestApp.apk";
+    private static final String LONG_LABEL_NAME_APK = SAMPLE_APK_BASE
+            + "CtsContentLongLabelNameTestApp.apk";
     private static final String EMPTY_APP_PACKAGE_NAME = "android.content.cts.emptytestapp";
     private static final String EMPTY_APP_MAX_PACKAGE_NAME = "android.content.cts.emptytestapp27j"
             + "EBRNRG3ozwBsGr1sVIM9U0bVTI2TdyIyeRkZgW4JrJefwNIBAmCg4AzqXiCvG6JjqA0uTCWSFu2YqAVxVd"
@@ -163,6 +165,8 @@
     private static final String HELLO_WORLD_PACKAGE_NAME = "com.example.helloworld";
     private static final String HELLO_WORLD_APK = SAMPLE_APK_BASE + "HelloWorld5.apk";
 
+    private static final int MAX_SAFE_LABEL_LENGTH = 1000;
+
     @Before
     public void setup() throws Exception {
         mContext = InstrumentationRegistry.getContext();
@@ -1464,4 +1468,33 @@
     private void uninstallPackage(String packageName) {
         SystemUtil.runShellCommand("pm uninstall " + packageName);
     }
+
+    @Test
+    public void loadApplicationLabel_withLongLabelName_truncated() throws Exception {
+        assertThat(installPackage(LONG_LABEL_NAME_APK)).isTrue();
+        final ApplicationInfo info = mPackageManager.getApplicationInfo(
+                EMPTY_APP_PACKAGE_NAME, 0 /* flags */);
+        final CharSequence resLabel = mPackageManager.getText(
+                EMPTY_APP_PACKAGE_NAME, info.labelRes, info);
+
+        assertThat(resLabel.length()).isGreaterThan(MAX_SAFE_LABEL_LENGTH);
+        assertThat(info.loadLabel(mPackageManager).length()).isEqualTo(MAX_SAFE_LABEL_LENGTH);
+    }
+
+    @Test
+    public void loadComponentLabel_withLongLabelName_truncated() throws Exception {
+        assertThat(installPackage(LONG_LABEL_NAME_APK)).isTrue();
+        final ComponentName componentName = ComponentName.createRelative(
+                EMPTY_APP_PACKAGE_NAME, ".MockActivity");
+        final ApplicationInfo appInfo = mPackageManager.getApplicationInfo(
+                EMPTY_APP_PACKAGE_NAME, 0 /* flags */);
+        final ActivityInfo activityInfo = mPackageManager.getActivityInfo(
+                componentName, 0 /* flags */);
+        final CharSequence resLabel = mPackageManager.getText(
+                EMPTY_APP_PACKAGE_NAME, activityInfo.labelRes, appInfo);
+
+        assertThat(resLabel.length()).isGreaterThan(MAX_SAFE_LABEL_LENGTH);
+        assertThat(activityInfo.loadLabel(mPackageManager).length())
+                .isEqualTo(MAX_SAFE_LABEL_LENGTH);
+    }
 }