FileDocCategorySizeDatePackage
MediaMetadataRetrieverTest.javaAPI DocAndroid 1.5 API11861Wed May 06 22:42:00 BST 2009com.android.mediaframeworktest.unit

MediaMetadataRetrieverTest

public class MediaMetadataRetrieverTest extends android.test.AndroidTestCase
WARNING: Currently, captureFrame() does not work, due to hardware access permission problem. We are currently only testing the metadata/album art retrieval features.

Fields Summary
private static final String
TAG
Constructors Summary
Methods Summary
public static voiddisableTestThumbnailCapture()

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        Log.v(TAG, "Thumbnail processing starts");
        long startedAt = System.currentTimeMillis();
        for(int i = 0, n = MediaNames.THUMBNAIL_CAPTURE_TEST_FILES.length; i < n; ++i) {
            try {
                Log.v(TAG, "File " + i + ": " + MediaNames.THUMBNAIL_CAPTURE_TEST_FILES[i]);
                retriever.setDataSource(MediaNames.THUMBNAIL_CAPTURE_TEST_FILES[i]);
                Bitmap bitmap = retriever.captureFrame();
                assertTrue(bitmap != null);
                try {
                    java.io.OutputStream stream = new FileOutputStream(MediaNames.THUMBNAIL_CAPTURE_TEST_FILES[i] + ".jpg");
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
                    stream.close();
                } catch (Exception e) {
                    throw new Exception("Fails to convert the bitmap to a JPEG file for " + MediaNames.THUMBNAIL_CAPTURE_TEST_FILES[i], e);
                }
            } catch(Exception e) {
                throw new Exception("Fails to setDataSource for file " + MediaNames.THUMBNAIL_CAPTURE_TEST_FILES[i], e);
            }
            Thread.yield();  // Don't be evil
        }
        long endedAt = System.currentTimeMillis();
        Log.v(TAG, "Average processing time per thumbnail: " + (endedAt - startedAt)/MediaNames.THUMBNAIL_CAPTURE_TEST_FILES.length + " ms");
        retriever.release();
    
private static voidextractAllSupportedMetadataValues(android.media.MediaMetadataRetriever retriever)

        String value = null;
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_AUTHOR)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)) == null? "not found": value);
        Log.v(TAG, (value = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR)) == null? "not found": value);
    
public static voidtestAlbumArt()

   
    // Test album art extraction.
    
          
        Log.v(TAG, "testAlbumArt starts.");
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
        for (int i = 0, n = MediaNames.ALBUMART_TEST_FILES.length; i < n; ++i) {
            try {
                Log.v(TAG, "File " + i + ": " + MediaNames.ALBUMART_TEST_FILES[i]);
                retriever.setDataSource(MediaNames.ALBUMART_TEST_FILES[i]);
                byte[] albumArt = retriever.extractAlbumArt();

                // TODO:
                // A better test would be to compare the retrieved album art with the
                // known result.
                if (albumArt == null) {  // Do we have expect in JUnit?
                    fail("Fails to extract album art for " + MediaNames.ALBUMART_TEST_FILES[i]);
                }
            } catch(Exception e) {
                throw new Exception("Fails to setDataSource for " + MediaNames.ALBUMART_TEST_FILES[i], e);
            }
            Thread.yield();  // Don't be evil
        }
        retriever.release();
        Log.v(TAG, "testAlbumArt completes.");
    
public static voidtestBasicAbnormalMethodCallSequence()

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
        assertTrue(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM) == null);
        assertTrue(retriever.captureFrame() == null);
    
public static voidtestBasicNormalMethodCallSequence()

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
        try {
            retriever.setDataSource(MediaNames.TEST_PATH_1);
            /*
             * captureFrame() fails due to lack of permission to access hardware decoder devices
            Bitmap bitmap = retriever.captureFrame();
            assertTrue(bitmap != null);
            try {
                java.io.OutputStream stream = new FileOutputStream("/sdcard/thumbnailout.jpg");
                bitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
                stream.close();
            } catch (Exception e) {
                throw new Exception("Fails to convert the bitmap to a JPEG file for " + MediaNames.TEST_PATH_1, e);
            }
            */
            extractAllSupportedMetadataValues(retriever);
        } catch(Exception e) {
            throw new Exception("Fails to setDataSource for " + MediaNames.TEST_PATH_1, e);
        }
        retriever.release();
    
public static voidtestIntendedUsage()

        // By default, capture frame and retrieve metadata
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        // retriever.setDataSource(MediaNames.TEST_PATH_1);
        // assertTrue(retriever.captureFrame() != null);
        // assertTrue(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS) != null);

        // Do not capture frame or retrieve metadata
        retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY & MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
        retriever.setDataSource(MediaNames.TEST_PATH_1);
        assertTrue(retriever.captureFrame() == null);
        assertTrue(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS) == null);

        // Capture frame only
        // retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
        // retriever.setDataSource(MediaNames.TEST_PATH_1);
        // assertTrue(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS) == null);

        // Retriever metadata only
        retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
        retriever.setDataSource(MediaNames.TEST_PATH_1);
        assertTrue(retriever.captureFrame() == null);

        // Capture frame and retrieve metadata
        // retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY | MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
        // retriever.setDataSource(MediaNames.TEST_PATH_1);
        // assertTrue(retriever.captureFrame() != null);
        // assertTrue(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS) != null);
        retriever.release();
    
public static voidtestMetadataRetrieval()

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
        for(int i = 0, n = MediaNames.METADATA_RETRIEVAL_TEST_FILES.length; i < n; ++i) {
            try {
                retriever.setDataSource(MediaNames.METADATA_RETRIEVAL_TEST_FILES[i]);
                extractAllSupportedMetadataValues(retriever);
            } catch(Exception e) {
                throw new Exception("Fails to setDataSource for file " + MediaNames.METADATA_RETRIEVAL_TEST_FILES[i], e);
            }
            Thread.yield();  // Don't be evil
        }
        retriever.release();
    
public static voidtestSetDataSource()

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);

        // Null pointer argument
        try {
            String path = null;
            retriever.setDataSource(path);
            fail("IllegalArgumentException must be thrown.");
        } catch(Exception e) {
            assertTrue(e instanceof IllegalArgumentException);
        }

        // Use mem:// path
        try {
            retriever.setDataSource(MediaNames.TEST_PATH_5);
            fail("IllegalArgumentException must be thrown.");
        } catch(Exception e) {
            assertTrue(e instanceof IllegalArgumentException);
        }

        // The pathname does not correspond to any existing file
        try {
            retriever.setDataSource(MediaNames.TEST_PATH_4);
            fail("Runtime exception must be thrown.");
        } catch(Exception e) {
            assertTrue(e instanceof RuntimeException);
        }

        // The pathname does correspond to a file, but this file
        // is not a valid media file
        try {
            retriever.setDataSource(MediaNames.TEST_PATH_3);
            fail("Runtime exception must be thrown.");
        } catch(Exception e) {
            assertTrue(e instanceof RuntimeException);
        }
        
        retriever.release();