Test that upgrading target apk invalidates idmap

This change adds tests that run in presubmit to verify that updating
the target APK invalidates the idmap for overlays targeting the
target.

Bug: 147794117
Bug: 150877400
Test: atest PackagedUpgradedTest
Change-Id: I0268ea99dc7beb4bbc7009a67d9929e2d82862cb
diff --git a/cmds/idmap2/TEST_MAPPING b/cmds/idmap2/TEST_MAPPING
index 26ccf03..9e0fb84 100644
--- a/cmds/idmap2/TEST_MAPPING
+++ b/cmds/idmap2/TEST_MAPPING
@@ -3,5 +3,10 @@
     {
       "name" : "idmap2_tests"
     }
+  ],
+  "imports": [
+    {
+      "path": "frameworks/base/services/core/java/com/android/server/om"
+    }
   ]
 }
diff --git a/core/tests/overlaytests/remount/host/Android.bp b/core/tests/overlaytests/remount/Android.bp
similarity index 91%
rename from core/tests/overlaytests/remount/host/Android.bp
rename to core/tests/overlaytests/remount/Android.bp
index 3825c55..9baedf8 100644
--- a/core/tests/overlaytests/remount/host/Android.bp
+++ b/core/tests/overlaytests/remount/Android.bp
@@ -24,5 +24,7 @@
         ":OverlayRemountedTest_SharedLibrary",
         ":OverlayRemountedTest_SharedLibraryOverlay",
         ":OverlayRemountedTest_Target",
+        ":OverlayRemountedTest_TargetUpgrade",
+        ":OverlayRemountedTest_Overlay",
     ],
 }
diff --git a/core/tests/overlaytests/remount/host/AndroidTest.xml b/core/tests/overlaytests/remount/AndroidTest.xml
similarity index 100%
rename from core/tests/overlaytests/remount/host/AndroidTest.xml
rename to core/tests/overlaytests/remount/AndroidTest.xml
diff --git a/core/tests/overlaytests/remount/host/src/com/android/overlaytest/remounted/OverlaySharedLibraryTest.java b/core/tests/overlaytests/remount/host/src/com/android/overlaytest/remounted/OverlaySharedLibraryTest.java
deleted file mode 100644
index 06b2ac8..0000000
--- a/core/tests/overlaytests/remount/host/src/com/android/overlaytest/remounted/OverlaySharedLibraryTest.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright (C) 2019 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.overlaytest.remounted;
-
-import static org.junit.Assert.assertTrue;
-
-import com.android.tradefed.device.DeviceNotAvailableException;
-import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
-import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
-
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.RuleChain;
-import org.junit.rules.TemporaryFolder;
-import org.junit.runner.RunWith;
-
-@RunWith(DeviceJUnit4ClassRunner.class)
-public class OverlaySharedLibraryTest extends BaseHostJUnit4Test {
-    private static final String TARGET_APK = "OverlayRemountedTest_Target.apk";
-    private static final String TARGET_PACKAGE = "com.android.overlaytest.remounted.target";
-    private static final String SHARED_LIBRARY_APK =
-            "OverlayRemountedTest_SharedLibrary.apk";
-    private static final String SHARED_LIBRARY_PACKAGE =
-            "com.android.overlaytest.remounted.shared_library";
-    private static final String SHARED_LIBRARY_OVERLAY_APK =
-            "OverlayRemountedTest_SharedLibraryOverlay.apk";
-    private static final String SHARED_LIBRARY_OVERLAY_PACKAGE =
-            "com.android.overlaytest.remounted.shared_library.overlay";
-
-    public final TemporaryFolder temporaryFolder = new TemporaryFolder();
-    public final SystemPreparer preparer = new SystemPreparer(temporaryFolder, this::getDevice);
-
-    @Rule
-    public final RuleChain ruleChain = RuleChain.outerRule(temporaryFolder).around(preparer);
-
-    @Before
-    public void startBefore() throws DeviceNotAvailableException {
-        getDevice().waitForDeviceAvailable();
-    }
-
-    @Test
-    public void testSharedLibrary() throws Exception {
-        final String targetResource = resourceName(TARGET_PACKAGE, "bool",
-                "uses_shared_library_overlaid");
-        final String libraryResource = resourceName(SHARED_LIBRARY_PACKAGE, "bool",
-                "shared_library_overlaid");
-
-        preparer.pushResourceFile(SHARED_LIBRARY_APK, "/product/app/SharedLibrary.apk")
-                .installResourceApk(SHARED_LIBRARY_OVERLAY_APK, SHARED_LIBRARY_OVERLAY_PACKAGE)
-                .reboot()
-                .setOverlayEnabled(SHARED_LIBRARY_OVERLAY_PACKAGE, false)
-                .installResourceApk(TARGET_APK, TARGET_PACKAGE);
-
-        // The shared library resource is not currently overlaid.
-        assertResource(targetResource, "false");
-        assertResource(libraryResource, "false");
-
-        // Overlay the shared library resource.
-        preparer.setOverlayEnabled(SHARED_LIBRARY_OVERLAY_PACKAGE, true);
-        assertResource(targetResource, "true");
-        assertResource(libraryResource, "true");
-    }
-
-    @Test
-    public void testSharedLibraryPreEnabled() throws Exception {
-        final String targetResource = resourceName(TARGET_PACKAGE, "bool",
-                "uses_shared_library_overlaid");
-        final String libraryResource = resourceName(SHARED_LIBRARY_PACKAGE, "bool",
-                "shared_library_overlaid");
-
-        preparer.pushResourceFile(SHARED_LIBRARY_APK, "/product/app/SharedLibrary.apk")
-                .installResourceApk(SHARED_LIBRARY_OVERLAY_APK, SHARED_LIBRARY_OVERLAY_PACKAGE)
-                .setOverlayEnabled(SHARED_LIBRARY_OVERLAY_PACKAGE, true)
-                .reboot()
-                .installResourceApk(TARGET_APK, TARGET_PACKAGE);
-
-        assertResource(targetResource, "true");
-        assertResource(libraryResource, "true");
-    }
-
-    /** Builds the full name of a resource in the form package:type/entry. */
-    String resourceName(String pkg, String type, String entry) {
-        return String.format("%s:%s/%s", pkg, type, entry);
-    }
-
-    void assertResource(String resourceName, String expectedValue)
-            throws DeviceNotAvailableException {
-        final String result = getDevice().executeShellCommand(
-                String.format("cmd overlay lookup %s %s", TARGET_PACKAGE, resourceName));
-        assertTrue(String.format("expected: <[%s]> in: <[%s]>", expectedValue, result),
-                result.equals(expectedValue + "\n") ||
-                result.endsWith("-> " + expectedValue + "\n"));
-    }
-}
diff --git a/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/OverlayRemountedTestBase.java b/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/OverlayRemountedTestBase.java
new file mode 100644
index 0000000..00a53c4
--- /dev/null
+++ b/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/OverlayRemountedTestBase.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2020 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.overlaytest.remounted;
+
+import static org.junit.Assert.fail;
+
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TemporaryFolder;
+
+public class OverlayRemountedTestBase extends BaseHostJUnit4Test {
+    static final long ASSERT_RESOURCE_TIMEOUT_MS = 30000;
+
+    static final String TARGET_APK = "OverlayRemountedTest_Target.apk";
+    static final String TARGET_PACKAGE = "com.android.overlaytest.remounted.target";
+    static final String TARGET_UPGRADE_APK = "OverlayRemountedTest_TargetUpgrade.apk";
+    static final String OVERLAY_APK = "OverlayRemountedTest_Overlay.apk";
+    static final String OVERLAY_PACKAGE = "com.android.overlaytest.remounted.target.overlay";
+    static final String SHARED_LIBRARY_APK =
+            "OverlayRemountedTest_SharedLibrary.apk";
+    static final String SHARED_LIBRARY_PACKAGE =
+            "com.android.overlaytest.remounted.shared_library";
+    static final String SHARED_LIBRARY_OVERLAY_APK =
+            "OverlayRemountedTest_SharedLibraryOverlay.apk";
+    static final String SHARED_LIBRARY_OVERLAY_PACKAGE =
+            "com.android.overlaytest.remounted.shared_library.overlay";
+
+    private final TemporaryFolder mTemporaryFolder = new TemporaryFolder();
+    protected final SystemPreparer mPreparer = new SystemPreparer(mTemporaryFolder,
+            this::getDevice);
+
+    @Rule
+    public final RuleChain ruleChain = RuleChain.outerRule(mTemporaryFolder).around(mPreparer);
+
+    @Before
+    public void startBefore() throws DeviceNotAvailableException {
+        getDevice().waitForDeviceAvailable();
+    }
+
+    /** Builds the full name of a resource in the form package:type/entry. */
+    String resourceName(String pkg, String type, String entry) {
+        return String.format("%s:%s/%s", pkg, type, entry);
+    }
+
+    void assertResource(String resourceName, String expectedValue)
+            throws DeviceNotAvailableException {
+        String result = null;
+
+        final long endMillis = System.currentTimeMillis() + ASSERT_RESOURCE_TIMEOUT_MS;
+        while (System.currentTimeMillis() <= endMillis) {
+            result = getDevice().executeShellCommand(
+                    String.format("cmd overlay lookup %s %s", TARGET_PACKAGE, resourceName));
+            if (result.equals(expectedValue + "\n") ||
+                    result.endsWith("-> " + expectedValue + "\n")) {
+                return;
+            }
+
+            try {
+                Thread.sleep(200);
+            } catch (InterruptedException ignore) {
+            }
+        }
+
+        fail(String.format("expected: <[%s]> in: <[%s]>", expectedValue, result));
+    }
+}
diff --git a/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/OverlaySharedLibraryTest.java b/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/OverlaySharedLibraryTest.java
new file mode 100644
index 0000000..49d1894
--- /dev/null
+++ b/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/OverlaySharedLibraryTest.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2019 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.overlaytest.remounted;
+
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class OverlaySharedLibraryTest extends OverlayRemountedTestBase {
+
+    @Test
+    public void testSharedLibrary() throws Exception {
+        final String targetResource = resourceName(TARGET_PACKAGE, "bool",
+                "uses_shared_library_overlaid");
+        final String libraryResource = resourceName(SHARED_LIBRARY_PACKAGE, "bool",
+                "shared_library_overlaid");
+
+        mPreparer.pushResourceFile(SHARED_LIBRARY_APK, "/product/app/SharedLibrary.apk")
+                .installResourceApk(SHARED_LIBRARY_OVERLAY_APK, SHARED_LIBRARY_OVERLAY_PACKAGE)
+                .reboot()
+                .setOverlayEnabled(SHARED_LIBRARY_OVERLAY_PACKAGE, false)
+                .installResourceApk(TARGET_APK, TARGET_PACKAGE);
+
+        // The shared library resource is not currently overlaid.
+        assertResource(targetResource, "false");
+        assertResource(libraryResource, "false");
+
+        // Overlay the shared library resource.
+        mPreparer.setOverlayEnabled(SHARED_LIBRARY_OVERLAY_PACKAGE, true);
+        assertResource(targetResource, "true");
+        assertResource(libraryResource, "true");
+    }
+
+    @Test
+    public void testSharedLibraryPreEnabled() throws Exception {
+        final String targetResource = resourceName(TARGET_PACKAGE, "bool",
+                "uses_shared_library_overlaid");
+        final String libraryResource = resourceName(SHARED_LIBRARY_PACKAGE, "bool",
+                "shared_library_overlaid");
+
+        mPreparer.pushResourceFile(SHARED_LIBRARY_APK, "/product/app/SharedLibrary.apk")
+                .installResourceApk(SHARED_LIBRARY_OVERLAY_APK, SHARED_LIBRARY_OVERLAY_PACKAGE)
+                .setOverlayEnabled(SHARED_LIBRARY_OVERLAY_PACKAGE, true)
+                .reboot()
+                .installResourceApk(TARGET_APK, TARGET_PACKAGE);
+
+        assertResource(targetResource, "true");
+        assertResource(libraryResource, "true");
+    }
+}
diff --git a/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/PackagedUpgradedTest.java b/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/PackagedUpgradedTest.java
new file mode 100644
index 0000000..f685ec1
--- /dev/null
+++ b/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/PackagedUpgradedTest.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2020 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.overlaytest.remounted;
+
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class PackagedUpgradedTest extends OverlayRemountedTestBase {
+
+    @Test
+    public void testTargetUpgrade() throws Exception {
+        final String targetOverlaid = resourceName(TARGET_PACKAGE, "bool", "target_overlaid");
+        final String targetReference = resourceName(TARGET_PACKAGE, "bool", "target_reference");
+
+        mPreparer.pushResourceFile(TARGET_APK, "/product/app/OverlayTarget.apk")
+                .reboot()
+                .installResourceApk(OVERLAY_APK, OVERLAY_PACKAGE)
+                .setOverlayEnabled(OVERLAY_PACKAGE, true);
+
+        assertResource(targetReference, "@" + 0x7f010000 + " -> true");
+        assertResource(targetOverlaid, "true");
+
+        mPreparer.installResourceApk(TARGET_UPGRADE_APK, TARGET_PACKAGE);
+
+        assertResource(targetReference, "@" + 0x7f0100ff + " -> true");
+        assertResource(targetOverlaid, "true");
+    }
+
+    @Test
+    public void testTargetRelocated() throws Exception {
+        final String targetOverlaid = resourceName(TARGET_PACKAGE, "bool", "target_overlaid");
+        final String originalPath = "/product/app/OverlayTarget.apk";
+
+        mPreparer.pushResourceFile(TARGET_APK, originalPath)
+                .reboot()
+                .installResourceApk(OVERLAY_APK, OVERLAY_PACKAGE)
+                .setOverlayEnabled(OVERLAY_PACKAGE, true);
+
+        assertResource(targetOverlaid, "true");
+
+        mPreparer.remount();
+        getDevice().deleteFile(originalPath);
+        mPreparer.pushResourceFile(TARGET_UPGRADE_APK, "/product/app/OverlayTarget2.apk")
+                .reboot();
+
+        assertResource(targetOverlaid, "true");
+    }
+}
diff --git a/core/tests/overlaytests/remount/host/src/com/android/overlaytest/remounted/SystemPreparer.java b/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/SystemPreparer.java
similarity index 76%
rename from core/tests/overlaytests/remount/host/src/com/android/overlaytest/remounted/SystemPreparer.java
rename to core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/SystemPreparer.java
index 8696091..b45b8ea 100644
--- a/core/tests/overlaytests/remount/host/src/com/android/overlaytest/remounted/SystemPreparer.java
+++ b/core/tests/overlaytests/remount/src/com/android/overlaytest/remounted/SystemPreparer.java
@@ -18,8 +18,6 @@
 
 import static org.junit.Assert.assertTrue;
 
-import static java.util.concurrent.TimeUnit.MILLISECONDS;
-
 import com.android.tradefed.device.DeviceNotAvailableException;
 import com.android.tradefed.device.ITestDevice;
 
@@ -32,10 +30,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Executor;
-import java.util.concurrent.FutureTask;
-import java.util.concurrent.TimeoutException;
 
 class SystemPreparer extends ExternalResource {
     private static final long OVERLAY_ENABLE_TIMEOUT_MS = 30000;
@@ -58,7 +52,7 @@
     SystemPreparer pushResourceFile(String resourcePath,
             String outputPath) throws DeviceNotAvailableException, IOException {
         final ITestDevice device = mDeviceProvider.getDevice();
-        device.executeAdbCommand("remount");
+        remount();
         assertTrue(device.pushFile(copyResourceToTemp(resourcePath), outputPath));
         mPushedFiles.add(outputPath);
         return this;
@@ -69,7 +63,7 @@
             throws DeviceNotAvailableException, IOException {
         final ITestDevice device = mDeviceProvider.getDevice();
         final File tmpFile = copyResourceToTemp(resourcePath);
-        final String result = device.installPackage(tmpFile, true);
+        final String result = device.installPackage(tmpFile, true /* reinstall */);
         Assert.assertNull(result);
         mInstalledPackages.add(packageName);
         return this;
@@ -77,34 +71,29 @@
 
     /** Sets the enable state of an overlay pacakage. */
     SystemPreparer setOverlayEnabled(String packageName, boolean enabled)
-            throws ExecutionException, DeviceNotAvailableException {
+            throws DeviceNotAvailableException {
         final ITestDevice device = mDeviceProvider.getDevice();
+        final String enable = enabled ? "enable" : "disable";
 
         // Wait for the overlay to change its enabled state.
-        final FutureTask<Boolean> enabledListener = new FutureTask<>(() -> {
-            while (true) {
-                device.executeShellCommand(String.format("cmd overlay %s %s",
-                        enabled ? "enable" : "disable", packageName));
-
-                final String result = device.executeShellCommand("cmd overlay dump " + packageName);
-                final int startIndex = result.indexOf("mIsEnabled");
-                final int endIndex = result.indexOf('\n', startIndex);
-                if (result.substring(startIndex, endIndex).contains((enabled) ? "true" : "false")) {
-                    return true;
-                }
+        final long endMillis = System.currentTimeMillis() + OVERLAY_ENABLE_TIMEOUT_MS;
+        String result;
+        while (System.currentTimeMillis() <= endMillis) {
+            device.executeShellCommand(String.format("cmd overlay %s %s", enable, packageName));
+            result = device.executeShellCommand("cmd overlay dump isenabled "
+                    + packageName);
+            if (((enabled) ? "true\n" : "false\n").equals(result)) {
+                return this;
             }
-        });
 
-        final Executor executor = (cmd) -> new Thread(cmd).start();
-        executor.execute(enabledListener);
-        try {
-            enabledListener.get(OVERLAY_ENABLE_TIMEOUT_MS, MILLISECONDS);
-        } catch (InterruptedException ignored) {
-        } catch (TimeoutException e) {
-            throw new IllegalStateException(device.executeShellCommand("cmd overlay list"));
+            try {
+                Thread.sleep(200);
+            } catch (InterruptedException ignore) {
+            }
         }
 
-        return this;
+        throw new IllegalStateException(String.format("Failed to %s overlay %s:\n%s", enable,
+                packageName, device.executeShellCommand("cmd overlay list")));
     }
 
     /** Restarts the device and waits until after boot is completed. */
@@ -114,6 +103,11 @@
         return this;
     }
 
+    SystemPreparer remount() throws DeviceNotAvailableException {
+        mDeviceProvider.getDevice().executeAdbCommand("remount");
+        return this;
+    }
+
     /** Copies a file within the host test jar to a temporary file on the host machine. */
     private File copyResourceToTemp(String resourcePath) throws IOException {
         final File tempFile = mHostTempFolder.newFile(resourcePath);
@@ -138,7 +132,7 @@
     protected void after() {
         final ITestDevice device = mDeviceProvider.getDevice();
         try {
-            device.executeAdbCommand("remount");
+            remount();
             for (final String file : mPushedFiles) {
                 device.deleteFile(file);
             }
diff --git a/core/tests/overlaytests/remount/target/Android.bp b/core/tests/overlaytests/remount/test-apps/Overlay/Android.bp
similarity index 73%
copy from core/tests/overlaytests/remount/target/Android.bp
copy to core/tests/overlaytests/remount/test-apps/Overlay/Android.bp
index 83f9f28..4476019 100644
--- a/core/tests/overlaytests/remount/target/Android.bp
+++ b/core/tests/overlaytests/remount/test-apps/Overlay/Android.bp
@@ -1,4 +1,4 @@
-// Copyright (C) 2018 The Android Open Source Project
+// Copyright (C) 2019 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.
@@ -13,8 +13,6 @@
 // limitations under the License.
 
 android_test_helper_app {
-    name: "OverlayRemountedTest_Target",
-    srcs: ["src/**/*.java"],
-    sdk_version: "test_current",
-    libs: ["OverlayRemountedTest_SharedLibrary"],
+    name: "OverlayRemountedTest_Overlay",
+    sdk_version: "current",
 }
diff --git a/core/tests/overlaytests/remount/target/AndroidManifest.xml b/core/tests/overlaytests/remount/test-apps/Overlay/AndroidManifest.xml
similarity index 66%
copy from core/tests/overlaytests/remount/target/AndroidManifest.xml
copy to core/tests/overlaytests/remount/test-apps/Overlay/AndroidManifest.xml
index dc07dca..d6d706c 100644
--- a/core/tests/overlaytests/remount/target/AndroidManifest.xml
+++ b/core/tests/overlaytests/remount/test-apps/Overlay/AndroidManifest.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-  ~ Copyright (C) 2019 The Android Open Source Project
+  ~ Copyright (C) 2020 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.
@@ -16,11 +16,8 @@
   -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-          package="com.android.overlaytest.remounted.target">
-
-    <application>
-        <uses-library android:name="android.test.runner" />
-        <uses-library android:name="com.android.overlaytest.remounted.shared_library"
-                      android:required="true" />
-    </application>
-</manifest>
+    package="com.android.overlaytest.remounted.target.overlay">
+    <application android:hasCode="false" />
+    <overlay android:targetPackage="com.android.overlaytest.remounted.target"
+             android:targetName="TestResources" />
+</manifest>
\ No newline at end of file
diff --git a/core/tests/overlaytests/remount/target/res/values/values.xml b/core/tests/overlaytests/remount/test-apps/Overlay/res/values/values.xml
similarity index 70%
copy from core/tests/overlaytests/remount/target/res/values/values.xml
copy to core/tests/overlaytests/remount/test-apps/Overlay/res/values/values.xml
index b5f444a..675e44f 100644
--- a/core/tests/overlaytests/remount/target/res/values/values.xml
+++ b/core/tests/overlaytests/remount/test-apps/Overlay/res/values/values.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-  ~ Copyright (C) 2019 The Android Open Source Project
+  ~ Copyright (C) 2020 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.
@@ -15,6 +15,6 @@
   ~ limitations under the License.
   -->
 
-<resources xmlns:sharedlib="http://schemas.android.com/apk/res/com.android.overlaytest.remounted.shared_library">
-    <bool name="uses_shared_library_overlaid">@sharedlib:bool/shared_library_overlaid</bool>
+<resources>
+    <bool name="target_overlaid">true</bool>
 </resources>
diff --git a/core/tests/overlaytests/remount/host/test-apps/SharedLibrary/Android.bp b/core/tests/overlaytests/remount/test-apps/SharedLibrary/Android.bp
similarity index 100%
rename from core/tests/overlaytests/remount/host/test-apps/SharedLibrary/Android.bp
rename to core/tests/overlaytests/remount/test-apps/SharedLibrary/Android.bp
diff --git a/core/tests/overlaytests/remount/host/test-apps/SharedLibrary/AndroidManifest.xml b/core/tests/overlaytests/remount/test-apps/SharedLibrary/AndroidManifest.xml
similarity index 100%
rename from core/tests/overlaytests/remount/host/test-apps/SharedLibrary/AndroidManifest.xml
rename to core/tests/overlaytests/remount/test-apps/SharedLibrary/AndroidManifest.xml
diff --git a/core/tests/overlaytests/remount/host/test-apps/SharedLibrary/res/values/overlayable.xml b/core/tests/overlaytests/remount/test-apps/SharedLibrary/res/values/overlayable.xml
similarity index 100%
rename from core/tests/overlaytests/remount/host/test-apps/SharedLibrary/res/values/overlayable.xml
rename to core/tests/overlaytests/remount/test-apps/SharedLibrary/res/values/overlayable.xml
diff --git a/core/tests/overlaytests/remount/host/test-apps/SharedLibrary/res/values/public.xml b/core/tests/overlaytests/remount/test-apps/SharedLibrary/res/values/public.xml
similarity index 100%
rename from core/tests/overlaytests/remount/host/test-apps/SharedLibrary/res/values/public.xml
rename to core/tests/overlaytests/remount/test-apps/SharedLibrary/res/values/public.xml
diff --git a/core/tests/overlaytests/remount/host/test-apps/SharedLibrary/res/values/values.xml b/core/tests/overlaytests/remount/test-apps/SharedLibrary/res/values/values.xml
similarity index 100%
rename from core/tests/overlaytests/remount/host/test-apps/SharedLibrary/res/values/values.xml
rename to core/tests/overlaytests/remount/test-apps/SharedLibrary/res/values/values.xml
diff --git a/core/tests/overlaytests/remount/host/test-apps/SharedLibraryOverlay/Android.bp b/core/tests/overlaytests/remount/test-apps/SharedLibraryOverlay/Android.bp
similarity index 100%
rename from core/tests/overlaytests/remount/host/test-apps/SharedLibraryOverlay/Android.bp
rename to core/tests/overlaytests/remount/test-apps/SharedLibraryOverlay/Android.bp
diff --git a/core/tests/overlaytests/remount/host/test-apps/SharedLibraryOverlay/AndroidManifest.xml b/core/tests/overlaytests/remount/test-apps/SharedLibraryOverlay/AndroidManifest.xml
similarity index 100%
rename from core/tests/overlaytests/remount/host/test-apps/SharedLibraryOverlay/AndroidManifest.xml
rename to core/tests/overlaytests/remount/test-apps/SharedLibraryOverlay/AndroidManifest.xml
diff --git a/core/tests/overlaytests/remount/host/test-apps/SharedLibraryOverlay/res/values/values.xml b/core/tests/overlaytests/remount/test-apps/SharedLibraryOverlay/res/values/values.xml
similarity index 100%
rename from core/tests/overlaytests/remount/host/test-apps/SharedLibraryOverlay/res/values/values.xml
rename to core/tests/overlaytests/remount/test-apps/SharedLibraryOverlay/res/values/values.xml
diff --git a/core/tests/overlaytests/remount/target/Android.bp b/core/tests/overlaytests/remount/test-apps/Target/Android.bp
similarity index 77%
rename from core/tests/overlaytests/remount/target/Android.bp
rename to core/tests/overlaytests/remount/test-apps/Target/Android.bp
index 83f9f28..a8910eb 100644
--- a/core/tests/overlaytests/remount/target/Android.bp
+++ b/core/tests/overlaytests/remount/test-apps/Target/Android.bp
@@ -1,4 +1,4 @@
-// Copyright (C) 2018 The Android Open Source Project
+// Copyright (C) 2019 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.
@@ -14,7 +14,12 @@
 
 android_test_helper_app {
     name: "OverlayRemountedTest_Target",
-    srcs: ["src/**/*.java"],
     sdk_version: "test_current",
     libs: ["OverlayRemountedTest_SharedLibrary"],
 }
+
+android_test_helper_app {
+    name: "OverlayRemountedTest_TargetUpgrade",
+    resource_dirs: ["res_upgrade"],
+    sdk_version: "test_current",
+}
diff --git a/core/tests/overlaytests/remount/target/AndroidManifest.xml b/core/tests/overlaytests/remount/test-apps/Target/AndroidManifest.xml
similarity index 89%
rename from core/tests/overlaytests/remount/target/AndroidManifest.xml
rename to core/tests/overlaytests/remount/test-apps/Target/AndroidManifest.xml
index dc07dca..d1c7b7e8 100644
--- a/core/tests/overlaytests/remount/target/AndroidManifest.xml
+++ b/core/tests/overlaytests/remount/test-apps/Target/AndroidManifest.xml
@@ -19,8 +19,7 @@
           package="com.android.overlaytest.remounted.target">
 
     <application>
-        <uses-library android:name="android.test.runner" />
         <uses-library android:name="com.android.overlaytest.remounted.shared_library"
-                      android:required="true" />
+                      android:required="false" />
     </application>
 </manifest>
diff --git a/core/tests/overlaytests/remount/target/res/values/values.xml b/core/tests/overlaytests/remount/test-apps/Target/res/values/overlayable.xml
similarity index 70%
copy from core/tests/overlaytests/remount/target/res/values/values.xml
copy to core/tests/overlaytests/remount/test-apps/Target/res/values/overlayable.xml
index b5f444a..4aa5bce 100644
--- a/core/tests/overlaytests/remount/target/res/values/values.xml
+++ b/core/tests/overlaytests/remount/test-apps/Target/res/values/overlayable.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-  ~ Copyright (C) 2019 The Android Open Source Project
+  ~ Copyright (C) 2020 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.
@@ -15,6 +15,10 @@
   ~ limitations under the License.
   -->
 
-<resources xmlns:sharedlib="http://schemas.android.com/apk/res/com.android.overlaytest.remounted.shared_library">
-    <bool name="uses_shared_library_overlaid">@sharedlib:bool/shared_library_overlaid</bool>
+<resources>
+    <overlayable name="TestResources">
+        <policy type="public">
+            <item type="bool" name="target_overlaid" />
+        </policy>
+    </overlayable>
 </resources>
diff --git a/core/tests/overlaytests/remount/target/res/values/values.xml b/core/tests/overlaytests/remount/test-apps/Target/res/values/values.xml
similarity index 72%
rename from core/tests/overlaytests/remount/target/res/values/values.xml
rename to core/tests/overlaytests/remount/test-apps/Target/res/values/values.xml
index b5f444a..76253a9 100644
--- a/core/tests/overlaytests/remount/target/res/values/values.xml
+++ b/core/tests/overlaytests/remount/test-apps/Target/res/values/values.xml
@@ -17,4 +17,10 @@
 
 <resources xmlns:sharedlib="http://schemas.android.com/apk/res/com.android.overlaytest.remounted.shared_library">
     <bool name="uses_shared_library_overlaid">@sharedlib:bool/shared_library_overlaid</bool>
+
+    <!-- This resource has a different id in the updated version of this target app to test that the
+         idmap is regenerated when the target is updated. -->
+    <bool name="target_overlaid">false</bool>
+    <public type="bool" name="target_overlaid" id="0x7f010000" />
+    <bool name="target_reference">@bool/target_overlaid</bool>
 </resources>
diff --git a/core/tests/overlaytests/remount/target/res/values/values.xml b/core/tests/overlaytests/remount/test-apps/Target/res_upgrade/values/overlayable.xml
similarity index 70%
copy from core/tests/overlaytests/remount/target/res/values/values.xml
copy to core/tests/overlaytests/remount/test-apps/Target/res_upgrade/values/overlayable.xml
index b5f444a..4aa5bce 100644
--- a/core/tests/overlaytests/remount/target/res/values/values.xml
+++ b/core/tests/overlaytests/remount/test-apps/Target/res_upgrade/values/overlayable.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-  ~ Copyright (C) 2019 The Android Open Source Project
+  ~ Copyright (C) 2020 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.
@@ -15,6 +15,10 @@
   ~ limitations under the License.
   -->
 
-<resources xmlns:sharedlib="http://schemas.android.com/apk/res/com.android.overlaytest.remounted.shared_library">
-    <bool name="uses_shared_library_overlaid">@sharedlib:bool/shared_library_overlaid</bool>
+<resources>
+    <overlayable name="TestResources">
+        <policy type="public">
+            <item type="bool" name="target_overlaid" />
+        </policy>
+    </overlayable>
 </resources>
diff --git a/core/tests/overlaytests/remount/target/res/values/values.xml b/core/tests/overlaytests/remount/test-apps/Target/res_upgrade/values/values.xml
similarity index 60%
copy from core/tests/overlaytests/remount/target/res/values/values.xml
copy to core/tests/overlaytests/remount/test-apps/Target/res_upgrade/values/values.xml
index b5f444a..f552cb0 100644
--- a/core/tests/overlaytests/remount/target/res/values/values.xml
+++ b/core/tests/overlaytests/remount/test-apps/Target/res_upgrade/values/values.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-  ~ Copyright (C) 2019 The Android Open Source Project
+  ~ Copyright (C) 2020 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.
@@ -14,7 +14,10 @@
   ~ See the License for the specific language governing permissions and
   ~ limitations under the License.
   -->
-
-<resources xmlns:sharedlib="http://schemas.android.com/apk/res/com.android.overlaytest.remounted.shared_library">
-    <bool name="uses_shared_library_overlaid">@sharedlib:bool/shared_library_overlaid</bool>
-</resources>
+<resources>
+    <!-- This resource has a different id in the updated target app than the base target app to test
+         that the idmap is regenerated when the target is updated. -->
+    <bool name="target_overlaid">false</bool>
+    <public type="bool" name="target_overlaid" id="0x7f0100ff" />
+    <bool name="target_reference">@bool/target_overlaid</bool>
+</resources>
\ No newline at end of file
diff --git a/services/core/java/com/android/server/om/TEST_MAPPING b/services/core/java/com/android/server/om/TEST_MAPPING
index 75229a1..6edd76f 100644
--- a/services/core/java/com/android/server/om/TEST_MAPPING
+++ b/services/core/java/com/android/server/om/TEST_MAPPING
@@ -15,6 +15,9 @@
       "name": "OverlayHostTests"
     },
     {
+      "name": "OverlayRemountedTest"
+    },
+    {
       "name": "CtsAppSecurityHostTestCases",
       "options": [
         {