Test update and replace

Test: atest MediaStore_Images_MediaTest#testUpdateAndReplace
Bug: 155120435
Bug: 155320967
Change-Id: I6b96518b2e34e1b8ebe8742a3e416d24644e2872
(cherry picked from commit badda6e0de1d3b5d83c741d41587a3cfcfa8eb77)
diff --git a/tests/tests/provider/src/android/provider/cts/media/MediaStore_Images_MediaTest.java b/tests/tests/provider/src/android/provider/cts/media/MediaStore_Images_MediaTest.java
index fd96d40..106cd6f 100644
--- a/tests/tests/provider/src/android/provider/cts/media/MediaStore_Images_MediaTest.java
+++ b/tests/tests/provider/src/android/provider/cts/media/MediaStore_Images_MediaTest.java
@@ -296,6 +296,43 @@
         }
     }
 
+    /**
+     * b/155320967 Test that update with conflict is resolved as replace.
+     */
+    @Test
+    public void testUpdateAndReplace() throws Exception {
+        File file = null;
+        try {
+            // Create file
+            file = copyResourceToFile(R.raw.scenery, DCIM_DIR, "cts" + System.nanoTime() + ".jpg");
+            final String externalPath = file.getAbsolutePath();
+            assertNotNull(MediaStore.scanFile(mContentResolver, file));
+
+            // Insert another file, insertedFile doesn't exist in lower file system.
+            ContentValues values = new ContentValues();
+            final File insertedFile = new File(DCIM_DIR, "cts" + System.nanoTime() + ".jpg");
+            values.put(Media.DATA, insertedFile.getAbsolutePath());
+
+            final Uri uri = mContentResolver.insert(mExternalImages, values);
+            assertNotNull(uri);
+
+            // Now update the second file to the same file path as the first file.
+            values.put(Media.DATA, externalPath);
+            // This update is implemented as update and replace on conflict and shouldn't throw
+            // UNIQUE constraint error.
+            assertEquals(1, mContentResolver.update(uri, values, Bundle.EMPTY));
+            Uri scannedUri = MediaStore.scanFile(mContentResolver, file);
+            assertNotNull(scannedUri);
+
+            // _id in inserted uri and scannedUri should be same.
+            assertEquals(uri.getLastPathSegment(), scannedUri.getLastPathSegment());
+        } finally {
+            if (file != null) {
+                file.delete();
+            }
+        }
+    }
+
     @Test
     public void testUpsert() throws Exception {
         File file = null;