Test uninstall package and keep data with reboot

Add a test to ensure that system could boot without exception after
a package is uninstalled with keeping data.

Bug: 187383694
Test: atest CtsPackageManagerBootTestCases
Change-Id: Idb88ccdb4045e420949b2e71eb69bd912cfe1bfa
diff --git a/hostsidetests/packagemanager/boottest/Android.bp b/hostsidetests/packagemanager/boottest/Android.bp
new file mode 100644
index 0000000..9d1f96e
--- /dev/null
+++ b/hostsidetests/packagemanager/boottest/Android.bp
@@ -0,0 +1,33 @@
+// 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"],
+}
+
+java_test_host {
+    name: "CtsPackageManagerBootTestCases",
+    defaults: ["cts_defaults"],
+    srcs: ["src/**/*.java"],
+    // Tag this module as a cts test artifact
+    test_suites: [
+        "cts",
+        "general-tests",
+    ],
+    libs: [
+        "cts-tradefed",
+        "tradefed",
+        "compatibility-host-util",
+    ],
+}
\ No newline at end of file
diff --git a/hostsidetests/packagemanager/boottest/AndroidTest.xml b/hostsidetests/packagemanager/boottest/AndroidTest.xml
new file mode 100644
index 0000000..9889a52
--- /dev/null
+++ b/hostsidetests/packagemanager/boottest/AndroidTest.xml
@@ -0,0 +1,28 @@
+<?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 the CTS PackageManager boot tests">
+    <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" />
+    <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
+        <option name="jar" value="CtsPackageManagerBootTestCases.jar" />
+        <option name="runtime-hint" value="3m" />
+    </test>
+
+</configuration>
\ No newline at end of file
diff --git a/hostsidetests/packagemanager/boottest/app/Android.bp b/hostsidetests/packagemanager/boottest/app/Android.bp
new file mode 100644
index 0000000..d1a3271
--- /dev/null
+++ b/hostsidetests/packagemanager/boottest/app/Android.bp
@@ -0,0 +1,31 @@
+// 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_helper_app {
+    name: "CtsPackageManagerBootTestStubApp",
+    defaults: ["cts_support_defaults"],
+    srcs: ["src/**/*.java"],
+    // Tag this module as a cts test artifact
+    test_suites: [
+        "cts",
+        "general-tests",
+    ],
+    sdk_version: "current",
+    manifest: "AndroidManifest.xml",
+    compile_multilib: "both",
+}
\ No newline at end of file
diff --git a/hostsidetests/packagemanager/boottest/app/AndroidManifest.xml b/hostsidetests/packagemanager/boottest/app/AndroidManifest.xml
new file mode 100644
index 0000000..06b1e34
--- /dev/null
+++ b/hostsidetests/packagemanager/boottest/app/AndroidManifest.xml
@@ -0,0 +1,26 @@
+<?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="android.packagemanager.boottest.stub">
+
+    <application>
+        <activity android:name=".StubActivity"
+                  android:exported="true"/>
+    </application>
+
+</manifest>
\ No newline at end of file
diff --git a/hostsidetests/packagemanager/boottest/src/android/packagemanager/boot/cts/BootTest.java b/hostsidetests/packagemanager/boottest/src/android/packagemanager/boot/cts/BootTest.java
new file mode 100644
index 0000000..f86f5cf
--- /dev/null
+++ b/hostsidetests/packagemanager/boottest/src/android/packagemanager/boot/cts/BootTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.packagemanager.boot.cts;
+
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class BootTest extends BaseHostJUnit4Test {
+    private static final String TEST_APK = "CtsPackageManagerBootTestStubApp.apk";
+    private static final String TEST_PACKAGE = "android.packagemanager.boottest.stub";
+
+    @Before
+    public void setUp() throws Exception {
+        installPackage(TEST_APK);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        uninstallPackage(getDevice(), TEST_PACKAGE);
+    }
+
+    @Test
+    public void testUninstallPackageWithKeepDataAndReboot() throws Exception {
+        Assert.assertTrue(isPackageInstalled(TEST_PACKAGE));
+        uninstallPackageWithKeepData(TEST_PACKAGE);
+        getDevice().rebootUntilOnline();
+        waitForBootCompleted();
+    }
+
+    private void uninstallPackageWithKeepData(String packageName)
+            throws DeviceNotAvailableException {
+        getDevice().executeShellCommand("pm uninstall -k " + packageName);
+    }
+
+    private void waitForBootCompleted() throws Exception {
+        for (int i = 0; i < 45; i++) {
+            if (isBootCompleted()) {
+                return;
+            }
+            Thread.sleep(1000);
+        }
+        throw new AssertionError("System failed to become ready!");
+    }
+
+    private boolean isBootCompleted() throws Exception {
+        return "1".equals(getDevice().executeShellCommand("getprop sys.boot_completed").trim());
+    }
+}
\ No newline at end of file