Test for MediaStore_Video_Thumbnails

Bug 5810254

Change-Id: I30428f84f1925c9c52ce965ee435b6486c336bda
diff --git a/tests/src/android/provider/cts/FileCopyHelper.java b/tests/src/android/provider/cts/FileCopyHelper.java
index 4ee93ac..114c3ad 100644
--- a/tests/src/android/provider/cts/FileCopyHelper.java
+++ b/tests/src/android/provider/cts/FileCopyHelper.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -57,36 +58,36 @@
      * @param fileName the file name
      *
      * @return the absolute path of the destination file
+     * @throws IOException
      */
-    public String copy(int resId, String fileName) {
-        InputStream source = null;
-        OutputStream target = null;
+    public String copy(int resId, String fileName) throws IOException {
+        InputStream source = mContext.getResources().openRawResource(resId);
+        OutputStream target = mContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
+        copyFile(source, target);
+        mFilesList.add(fileName);
+        return mContext.getFileStreamPath(fileName).getAbsolutePath();
+    }
 
+    public void copyToExternalStorage(int resId, File path) throws IOException {
+        InputStream source = mContext.getResources().openRawResource(resId);
+        OutputStream target = new FileOutputStream(path);
+        copyFile(source, target);
+    }
+
+    private void copyFile(InputStream source, OutputStream target) throws IOException {
         try {
-            source = mContext.getResources().openRawResource(resId);
-            target = mContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
-
             byte[] buffer = new byte[1024];
             for (int len = source.read(buffer); len > 0; len = source.read(buffer)) {
                 target.write(buffer, 0, len);
             }
-        } catch (IOException e) {
-            e.printStackTrace();
         } finally {
-            try {
-                if (source != null) {
-                    source.close();
-                }
-                if (target != null) {
-                    target.close();
-                }
-            } catch (IOException e) {
-                // Ignore the IOException.
+            if (source != null) {
+                source.close();
+            }
+            if (target != null) {
+                target.close();
             }
         }
-
-        mFilesList.add(fileName);
-        return mContext.getFileStreamPath(fileName).getAbsolutePath();
     }
 
     /**
diff --git a/tests/tests/provider/src/android/provider/cts/MediaStore_Images_MediaTest.java b/tests/tests/provider/src/android/provider/cts/MediaStore_Images_MediaTest.java
index 8e8d650..5203326 100644
--- a/tests/tests/provider/src/android/provider/cts/MediaStore_Images_MediaTest.java
+++ b/tests/tests/provider/src/android/provider/cts/MediaStore_Images_MediaTest.java
@@ -18,11 +18,6 @@
 
 import com.android.cts.stub.R;
 
-import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetClass;
-import dalvik.annotation.TestTargetNew;
-import dalvik.annotation.TestTargets;
-
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
@@ -31,18 +26,14 @@
 import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.os.Environment;
-import android.provider.MediaStore;
 import android.provider.MediaStore.Images.Media;
 import android.provider.MediaStore.Images.Thumbnails;
 import android.test.InstrumentationTestCase;
 
 import java.io.File;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
 import java.util.ArrayList;
 
-@TestTargetClass(MediaStore.Images.Media.class)
 public class MediaStore_Images_MediaTest extends InstrumentationTestCase {
     private static final String MIME_TYPE_JPEG = "image/jpeg";
 
@@ -87,33 +78,7 @@
         mRowsAdded = new ArrayList<Uri>();
     }
 
-    @TestTargets({
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        notes = "This test will fail if there is no sdcard attached because the method "
-                    + "{@link Images#Media#insertImage(ContentResolver, String, String, String)} "
-                    + "will store images on the sdcard",
-        method = "insertImage",
-        args = {ContentResolver.class, String.class, String.class, String.class}
-      ),
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        method = "query",
-        args = {ContentResolver.class, Uri.class, String[].class}
-      ),
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        method = "query",
-        args = {ContentResolver.class, Uri.class, String[].class, String.class, String.class}
-      ),
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        method = "query",
-        args = {ContentResolver.class, Uri.class, String[].class,  String.class, String[].class,
-                String.class}
-      )
-    })
-    public void testInsertImageWithImagePath() {
+    public void testInsertImageWithImagePath() throws Exception {
         Cursor c = Media.query(mContentResolver, Media.EXTERNAL_CONTENT_URI, null, null,
                 "_id ASC");
         int previousCount = c.getCount();
@@ -184,21 +149,7 @@
         c.close();
     }
 
-    @TestTargets({
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        notes = "This test will fail if there is no sdcard attached because the method "
-                + "will store images on the sdcard",
-        method = "insertImage",
-        args = {ContentResolver.class, Bitmap.class, String.class, String.class}
-      ),
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        method = "getBitmap",
-        args = {ContentResolver.class, Uri.class}
-      )
-    })
-    public void testInsertImageWithBitmap() {
+    public void testInsertImageWithBitmap() throws Exception {
         // insert the image by bitmap
         Bitmap src = BitmapFactory.decodeResource(mContext.getResources(), R.raw.scenery);
         String stringUrl = null;
@@ -215,27 +166,15 @@
                 null, "_id ASC");
         c.moveToFirst();
         // get the bimap by the path
-        Bitmap result = null;
-        try {
-            result = Media.getBitmap(mContentResolver,
+        Bitmap result = Media.getBitmap(mContentResolver,
                     Uri.fromFile(new File(c.getString(c.getColumnIndex(Media.DATA)))));
-        } catch (FileNotFoundException e) {
-            fail(e.getMessage());
-        } catch (IOException e) {
-            fail(e.getMessage());
-        }
+
         // can not check the identity between the result and source bitmap because
         // source bitmap is compressed before it is saved as result bitmap
-        assertNotNull(result);
         assertEquals(src.getWidth(), result.getWidth());
         assertEquals(src.getHeight(), result.getHeight());
     }
 
-    @TestTargetNew(
-      level = TestLevel.COMPLETE,
-      method = "getContentUri",
-      args = {String.class}
-    )
     public void testGetContentUri() {
         assertNotNull(mContentResolver.query(Media.getContentUri("internal"), null, null, null,
                 null));
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 98ad62e..7ec7937 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
@@ -18,11 +18,6 @@
 
 import com.android.cts.stub.R;
 
-import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetClass;
-import dalvik.annotation.TestTargetNew;
-import dalvik.annotation.TestTargets;
-
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
@@ -31,15 +26,12 @@
 import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.os.Environment;
-import android.provider.MediaStore;
 import android.provider.MediaStore.Images.Media;
 import android.provider.MediaStore.Images.Thumbnails;
 import android.test.InstrumentationTestCase;
-import android.util.Log;
 
 import java.util.ArrayList;
 
-@TestTargetClass(MediaStore.Images.Thumbnails.class)
 public class MediaStore_Images_ThumbnailsTest extends InstrumentationTestCase {
     private ArrayList<Uri> mRowsAdded;
 
@@ -75,19 +67,7 @@
         mRowsAdded = new ArrayList<Uri>();
     }
 
-    @TestTargets({
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        method = "queryMiniThumbnails",
-        args = {ContentResolver.class, Uri.class, int.class, String[].class}
-      ),
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        method = "query",
-        args = {ContentResolver.class, Uri.class, String[].class}
-      )
-    })
-    public void testQueryInternalThumbnails() {
+    public void testQueryInternalThumbnails() throws Exception {
         Cursor c = Thumbnails.queryMiniThumbnails(mContentResolver,
                 Thumbnails.INTERNAL_CONTENT_URI, Thumbnails.MICRO_KIND, null);
         int previousMicroKindCount = c.getCount();
@@ -125,18 +105,6 @@
         c.close();
     }
 
-    @TestTargets({
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        method = "queryMiniThumbnail",
-        args = {ContentResolver.class, long.class, int.class, String[].class}
-      ),
-      @TestTargetNew(
-        level = TestLevel.COMPLETE,
-        method = "query",
-        args = {ContentResolver.class, Uri.class, String[].class}
-      )
-    })
     public void testQueryExternalMiniThumbnails() {
         // insert the image by bitmap
         Bitmap src = BitmapFactory.decodeResource(mContext.getResources(), R.raw.scenery);
@@ -174,11 +142,6 @@
         c.close();
     }
 
-    @TestTargetNew(
-      level = TestLevel.COMPLETE,
-      method = "getContentUri",
-      args = {String.class}
-    )
     public void testGetContentUri() {
         assertNotNull(mContentResolver.query(Thumbnails.getContentUri("internal"), null, null,
                 null, null));
diff --git a/tests/tests/provider/src/android/provider/cts/MediaStore_VideoTest.java b/tests/tests/provider/src/android/provider/cts/MediaStore_VideoTest.java
index 31d9f2c..366fc57 100644
--- a/tests/tests/provider/src/android/provider/cts/MediaStore_VideoTest.java
+++ b/tests/tests/provider/src/android/provider/cts/MediaStore_VideoTest.java
@@ -18,23 +18,17 @@
 
 import com.android.cts.stub.R;
 
-import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetClass;
-import dalvik.annotation.TestTargetNew;
-
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.net.Uri;
-import android.provider.MediaStore;
 import android.provider.MediaStore.Video;
 import android.provider.MediaStore.Video.VideoColumns;
 import android.test.InstrumentationTestCase;
 
 import java.util.ArrayList;
 
-@TestTargetClass(MediaStore.Video.class)
 public class MediaStore_VideoTest extends InstrumentationTestCase {
     private static final String TEST_VIDEO_3GP = "testVideo.3gp";
 
@@ -64,12 +58,7 @@
         mRowsAdded = new ArrayList<Uri>();
     }
 
-    @TestTargetNew(
-      level = TestLevel.COMPLETE,
-      method = "query",
-      args = {ContentResolver.class, Uri.class, String[].class}
-    )
-    public void testQuery() {
+    public void testQuery() throws Exception {
         ContentValues values = new ContentValues();
         String valueOfData = mHelper.copy(R.raw.testvideo, TEST_VIDEO_3GP);
         values.put(VideoColumns.DATA, valueOfData);
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
new file mode 100644
index 0000000..935b255
--- /dev/null
+++ b/tests/tests/provider/src/android/provider/cts/MediaStore_Video_ThumbnailsTest.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2012 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.provider.cts;
+
+import com.android.cts.stub.R;
+
+import android.content.ContentResolver;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Environment;
+import android.provider.MediaStore.Video.Media;
+import android.provider.MediaStore.Video.Thumbnails;
+import android.provider.MediaStore.Video.VideoColumns;
+import android.test.AndroidTestCase;
+
+import java.io.File;
+import java.io.IOException;
+
+public class MediaStore_Video_ThumbnailsTest extends AndroidTestCase {
+
+    private ContentResolver mResolver;
+
+    private FileCopyHelper mFileHelper;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mResolver = mContext.getContentResolver();
+        mFileHelper = new FileCopyHelper(mContext);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        mFileHelper.clear();
+        super.tearDown();
+    }
+
+    public void testGetContentUri() {
+        Uri internalUri = Thumbnails.getContentUri(MediaStoreAudioTestHelper.INTERNAL_VOLUME_NAME);
+        Uri externalUri = Thumbnails.getContentUri(MediaStoreAudioTestHelper.EXTERNAL_VOLUME_NAME);
+        assertEquals(Thumbnails.INTERNAL_CONTENT_URI, internalUri);
+        assertEquals(Thumbnails.EXTERNAL_CONTENT_URI, externalUri);
+    }
+
+    public void testGetThumbnail() throws Exception {
+        // Insert a video into the provider.
+        Uri videoUri = insertVideo();
+        long videoId = ContentUris.parseId(videoUri);
+        assertTrue(videoId != -1);
+        assertEquals(ContentUris.withAppendedId(Media.EXTERNAL_CONTENT_URI, videoId),
+                videoUri);
+
+        // Get the current thumbnail count for future comparison.
+        int count = getThumbnailCount(Thumbnails.EXTERNAL_CONTENT_URI);
+
+        // Calling getThumbnail should generate a new thumbnail.
+        assertNotNull(Thumbnails.getThumbnail(mResolver, videoId, Thumbnails.MINI_KIND, null));
+        assertNotNull(Thumbnails.getThumbnail(mResolver, videoId, Thumbnails.MICRO_KIND, null));
+
+        try {
+            Thumbnails.getThumbnail(mResolver, videoId, Thumbnails.FULL_SCREEN_KIND, null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // Full screen thumbnails not supported by getThumbnail...
+        }
+
+        // Check that an additional thumbnails have been registered.
+        int count2 = getThumbnailCount(Thumbnails.EXTERNAL_CONTENT_URI);
+        assertTrue(count2 > count);
+    }
+
+    private Uri insertVideo() throws IOException {
+        File file = new File(Environment.getExternalStorageDirectory(), "testVideo.3gp");
+        mFileHelper.copyToExternalStorage(R.raw.testvideo, file);
+
+        ContentValues values = new ContentValues();
+        values.put(VideoColumns.DATA, file.getAbsolutePath());
+        return mResolver.insert(Media.EXTERNAL_CONTENT_URI, values);
+    }
+
+    private int getThumbnailCount(Uri uri) {
+        Cursor cursor = mResolver.query(uri, null, null, null, null);
+        try {
+            return cursor.getCount();
+        } finally {
+            cursor.close();
+        }
+    }
+}