Add tests for thumbnail cleanup

Bug: 63766886
Test: build and run
Change-Id: Ie86c4d6059bc5d3c5f39bc27c8bcc1e0313b25ca
(cherry picked from commit 9245c121b6395e728aa05a1557fb956087119777)
diff --git a/tests/tests/provider/src/android/provider/cts/MediaStore_Images_ThumbnailsTest.java b/tests/tests/provider/src/android/provider/cts/MediaStore_Images_ThumbnailsTest.java
index b0f5426..f57af32 100644
--- a/tests/tests/provider/src/android/provider/cts/MediaStore_Images_ThumbnailsTest.java
+++ b/tests/tests/provider/src/android/provider/cts/MediaStore_Images_ThumbnailsTest.java
@@ -274,6 +274,87 @@
         assertEquals(1, mContentResolver.delete(uri, null, null));
     }
 
+    public void testThumbnailGenerationAndCleanup() throws Exception {
+        // insert an image
+        Bitmap src = BitmapFactory.decodeResource(mContext.getResources(), R.raw.scenery);
+        Uri uri = Uri.parse(Media.insertImage(mContentResolver, src, "test", "test description"));
+
+        // query its thumbnail
+        Cursor c = mContentResolver.query(
+                Thumbnails.EXTERNAL_CONTENT_URI,
+                new String [] {Thumbnails.DATA},
+                "image_id=?",
+                new String[] {uri.getLastPathSegment()},
+                null /* sort */
+                );
+        assertTrue("couldn't find thumbnail", c.moveToNext());
+        String path = c.getString(0);
+        c.close();
+        assertTrue("thumbnail does not exist", new File(path).exists());
+
+        // delete the source image and check that the thumbnail is gone too
+        mContentResolver.delete(uri, null /* where clause */, null /* where args */);
+        assertFalse("thumbnail still exists after source file delete", new File(path).exists());
+
+        // insert again
+        uri = Uri.parse(Media.insertImage(mContentResolver, src, "test", "test description"));
+
+        // query its thumbnail again
+        c = mContentResolver.query(
+                Thumbnails.EXTERNAL_CONTENT_URI,
+                new String [] {Thumbnails.DATA},
+                "image_id=?",
+                new String[] {uri.getLastPathSegment()},
+                null /* sortOrder */
+                );
+        assertTrue("couldn't find thumbnail", c.moveToNext());
+        path = c.getString(0);
+        c.close();
+        assertTrue("thumbnail does not exist", new File(path).exists());
+
+        // update the media type
+        ContentValues values = new ContentValues();
+        values.put("media_type", 0);
+        assertEquals("unexpected number of updated rows",
+                1, mContentResolver.update(uri, values, null /* where */, null /* where args */));
+
+        // image was marked as regular file in the database, which should have deleted its thumbnail
+
+        // query its thumbnail again
+        c = mContentResolver.query(
+                Thumbnails.EXTERNAL_CONTENT_URI,
+                new String [] {Thumbnails.DATA},
+                "image_id=?",
+                new String[] {uri.getLastPathSegment()},
+                null /* sort */
+                );
+        if (c != null) {
+            assertFalse("thumbnail entry exists for non-thumbnail file", c.moveToNext());
+            c.close();
+        }
+        assertFalse("thumbnail remains after source file type change", new File(path).exists());
+
+        // check source no longer exists as image
+        c = mContentResolver.query(uri,
+                null /* projection */, null /* where */, null /* where args */, null /* sort */);
+        assertFalse("source entry should be gone", c.moveToNext());
+        c.close();
+
+        // check source still exists as file
+        Uri fileUri = ContentUris.withAppendedId(
+                MediaStore.Files.getContentUri("external"),
+                Long.valueOf(uri.getLastPathSegment()));
+        c = mContentResolver.query(fileUri,
+                null /* projection */, null /* where */, null /* where args */, null /* sort */);
+        assertTrue("source entry is gone", c.moveToNext());
+        String sourcePath = c.getString(c.getColumnIndex("_data"));
+        c.close();
+
+        // clean up
+        mContentResolver.delete(uri, null /* where */, null /* where args */);
+        new File(sourcePath).delete();
+    }
+
     public void testStoreImagesMediaInternal() {
         // can not insert any data, so other operations can not be tested
         try {
diff --git a/tests/tests/provider/src/android/provider/cts/MediaStore_Video_ThumbnailsTest.java b/tests/tests/provider/src/android/provider/cts/MediaStore_Video_ThumbnailsTest.java
index 26f353f..8b62530 100644
--- a/tests/tests/provider/src/android/provider/cts/MediaStore_Video_ThumbnailsTest.java
+++ b/tests/tests/provider/src/android/provider/cts/MediaStore_Video_ThumbnailsTest.java
@@ -26,6 +26,7 @@
 import android.media.MediaCodecList;
 import android.net.Uri;
 import android.os.Environment;
+import android.provider.MediaStore.Files;
 import android.provider.MediaStore.Video.Media;
 import android.provider.MediaStore.Video.Thumbnails;
 import android.provider.MediaStore.Video.VideoColumns;
@@ -123,6 +124,94 @@
         assertEquals(1, mResolver.delete(videoUri, null, null));
     }
 
+    public void testThumbnailGenerationAndCleanup() throws Exception {
+        // insert a video
+        Uri uri = insertVideo();
+
+        // request thumbnail creation
+        Thumbnails.getThumbnail(mResolver, Long.valueOf(uri.getLastPathSegment()),
+                Thumbnails.MINI_KIND, null /* options */);
+
+        // query the thumbnail
+        Cursor c = mResolver.query(
+                Thumbnails.EXTERNAL_CONTENT_URI,
+                new String [] {Thumbnails.DATA},
+                "video_id=?",
+                new String[] {uri.getLastPathSegment()},
+                null /* sort */
+                );
+        assertTrue("couldn't find thumbnail", c.moveToNext());
+        String path = c.getString(0);
+        c.close();
+        assertTrue("thumbnail does not exist", new File(path).exists());
+
+        // delete the source video and check that the thumbnail is gone too
+        mResolver.delete(uri, null /* where clause */, null /* where args */);
+        assertFalse("thumbnail still exists after source file delete", new File(path).exists());
+
+        // insert again
+        uri = insertVideo();
+
+        // request thumbnail creation
+        Thumbnails.getThumbnail(mResolver, Long.valueOf(uri.getLastPathSegment()),
+                Thumbnails.MINI_KIND, null);
+
+        // query its thumbnail again
+        c = mResolver.query(
+                Thumbnails.EXTERNAL_CONTENT_URI,
+                new String [] {Thumbnails.DATA},
+                "video_id=?",
+                new String[] {uri.getLastPathSegment()},
+                null /* sort */
+                );
+        assertTrue("couldn't find thumbnail", c.moveToNext());
+        path = c.getString(0);
+        c.close();
+        assertTrue("thumbnail does not exist", new File(path).exists());
+
+        // update the media type
+        ContentValues values = new ContentValues();
+        values.put("media_type", 0);
+        assertEquals("unexpected number of updated rows",
+                1, mResolver.update(uri, values, null /* where */, null /* where args */));
+
+        // video was marked as regular file in the database, which should have deleted its thumbnail
+
+        // query its thumbnail again
+        c = mResolver.query(
+                Thumbnails.EXTERNAL_CONTENT_URI,
+                new String [] {Thumbnails.DATA},
+                "video_id=?",
+                new String[] {uri.getLastPathSegment()},
+                null /* sort */
+                );
+        if (c != null) {
+            assertFalse("thumbnail entry exists for non-thumbnail file", c.moveToNext());
+            c.close();
+        }
+        assertFalse("thumbnail remains after source file type change", new File(path).exists());
+
+        // check source no longer exists as video
+        c = mResolver.query(uri,
+                null /* projection */, null /* where */, null /* where args */, null /* sort */);
+        assertFalse("source entry should be gone", c.moveToNext());
+        c.close();
+
+        // check source still exists as file
+        Uri fileUri = ContentUris.withAppendedId(
+                Files.getContentUri("external"),
+                Long.valueOf(uri.getLastPathSegment()));
+        c = mResolver.query(fileUri,
+                null /* projection */, null /* where */, null /* where args */, null /* sort */);
+        assertTrue("source entry should be gone", c.moveToNext());
+        String sourcePath = c.getString(c.getColumnIndex("_data"));
+        c.close();
+
+        // clean up
+        mResolver.delete(uri, null /* where */, null /* where args */);
+        new File(sourcePath).delete();
+    }
+
     private Uri insertVideo() throws IOException {
         File file = new File(Environment.getExternalStorageDirectory(), "testVideo.3gp");
         // clean up any potential left over entries from a previous aborted run