FileDocCategorySizeDatePackage
MediaPlayerMetadataParserTest.javaAPI DocAndroid 5.1 API13426Thu Mar 12 22:22:30 GMT 2015com.android.mediaframeworktest.unit

MediaPlayerMetadataParserTest

public class MediaPlayerMetadataParserTest extends android.test.AndroidTestCase

Fields Summary
private static final String
TAG
private static final int
kMarker
private static final int
kHeaderSize
private android.media.Metadata
mMetadata
private android.os.Parcel
mParcel
Constructors Summary
Methods Summary
private voidadjustSize()

        adjustSize(0);
    
private voidadjustSize(int offset)

        final int pos = mParcel.dataPosition();

        mParcel.setDataPosition(offset);
        mParcel.writeInt(pos - offset);
        mParcel.setDataPosition(pos);
    
private voidassertParse()

        mParcel.setDataPosition(0);
        assertTrue(mMetadata.parse(mParcel));
    
private voidassertParseFail()

        mParcel.setDataPosition(0);
        assertFalse(mMetadata.parse(mParcel));
        assertEquals(0, mParcel.dataPosition());
    
private voidresetParcel()

        mParcel.setDataPosition(0);
        // Most tests will use a properly formed parcel with a size
        // and the meta marker so we add them by default.
        mParcel.writeInt(-1);  // Placeholder for the size
        mParcel.writeInt(kMarker);
    
protected voidsetUp()


    
         
        super.setUp();
        mMetadata = new Metadata();
        mParcel = Parcel.obtain();

        resetParcel();
    
public voidtestBadMetadataType()

        final int start = mParcel.dataPosition();
        mParcel.writeInt(-1);  // Placeholder for the length
        mParcel.writeInt(Metadata.TITLE);
        mParcel.writeInt(0);  // Invalid type.
        mParcel.writeString("dummy");
        adjustSize(start);

        adjustSize();
        assertParseFail();
    
public voidtestEmptyIsOk()

        adjustSize();
        assertParse();
    
public voidtestGetBoolean()

        writeBooleanRecord(Metadata.PAUSE_AVAILABLE, true);
        writeBooleanRecord(Metadata.SEEK_AVAILABLE, true);
        writeBooleanRecord(Metadata.SEEK_BACKWARD_AVAILABLE, true);
        writeBooleanRecord(Metadata.SEEK_FORWARD_AVAILABLE, true);
        adjustSize();
        assertParse();

        assertEquals(true, mMetadata.getBoolean(Metadata.PAUSE_AVAILABLE));
        assertEquals(true, mMetadata.getBoolean(Metadata.SEEK_AVAILABLE));
        assertEquals(true, mMetadata.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE));
        assertEquals(true, mMetadata.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE));
    
public voidtestGetByteArray()

        byte data[] = new byte[]{1,2,3,4,5};

        writeByteArrayRecord(Metadata.ALBUM_ART, data);
        adjustSize();
        assertParse();

        byte res[] = mMetadata.getByteArray(Metadata.ALBUM_ART);
        for (int i = 0; i < data.length; ++i) {
            assertEquals(data[i], res[i]);
        }
    
public voidtestGetDate()

        writeDateRecord(Metadata.DATE, 0, "PST");
        adjustSize();
        assertParse();

        assertEquals(new Date(0), mMetadata.getDate(Metadata.DATE));
    
public voidtestGetDouble()

        writeDoubleRecord(Metadata.VIDEO_FRAME_RATE, 29.97);
        adjustSize();
        assertParse();

        assertEquals(29.97, mMetadata.getDouble(Metadata.VIDEO_FRAME_RATE));
    
public voidtestGetEmptyString()

        writeStringRecord(Metadata.TITLE, "");
        adjustSize();
        assertParse();

        assertEquals("", mMetadata.getString(Metadata.TITLE));
    
public voidtestGetInt()

        writeIntRecord(Metadata.CD_TRACK_NUM, 1);
        adjustSize();
        assertParse();

        assertEquals(1, mMetadata.getInt(Metadata.CD_TRACK_NUM));
    
public voidtestGetLong()

        writeLongRecord(Metadata.DURATION, 1L);
        adjustSize();
        assertParse();

        assertEquals(1L, mMetadata.getLong(Metadata.DURATION));
    
public voidtestGetNullString()

        writeStringRecord(Metadata.TITLE, null);
        adjustSize();
        assertParse();

        assertEquals(null, mMetadata.getString(Metadata.TITLE));
    
public voidtestGetString()

        writeStringRecord(Metadata.TITLE, "a title");
        writeStringRecord(Metadata.GENRE, "comedy");
        adjustSize();
        assertParse();

        assertEquals("a title", mMetadata.getString(Metadata.TITLE));
        assertEquals("comedy", mMetadata.getString(Metadata.GENRE));
    
public voidtestMissingData()

        final int size = 20;

        mParcel.writeInt(size);
        mParcel.setDataSize(size - 1);
        assertParseFail();
    
public voidtestMissingSizeAndMarker()

        for (int i = 0; i < kHeaderSize; ++i) {
            mParcel.setDataPosition(0);
            mParcel.setDataSize(i);

            assertEquals(i, mParcel.dataAvail());
            assertParseFail();
        }
    
public voidtestParseClearState()

        writeStringRecord(Metadata.TITLE, "a title");
        writeStringRecord(Metadata.GENRE, "comedy");
        writeStringRecord(Metadata.firstCustomId(), "custom");
        adjustSize();
        assertParse();

        resetParcel();
        writeStringRecord(Metadata.MIME_TYPE, "audio/mpg");
        adjustSize();
        assertParse();

        // Only the mime type metadata should be present.
        assertEquals(1, mMetadata.keySet().size());
        assertTrue(mMetadata.has(Metadata.MIME_TYPE));

        assertFalse(mMetadata.has(Metadata.TITLE));
        assertFalse(mMetadata.has(Metadata.GENRE));
        assertFalse(mMetadata.has(Metadata.firstCustomId()));
    
public voidtestRecordMissingId()

        mParcel.writeInt(13); // record length
        // misses metadata id and metadata type.
        adjustSize();
        assertParseFail();
    
public voidtestRecordMissingPayload()

        mParcel.writeInt(12);
        mParcel.writeInt(Metadata.TITLE);
        mParcel.writeInt(Metadata.STRING_VAL);
        // misses payload
        adjustSize();
        assertParseFail();
    
public voidtestRecordMissingType()

        mParcel.writeInt(13); // record length lies
        mParcel.writeInt(Metadata.TITLE);
        // misses metadata type
        adjustSize();
        assertParseFail();
    
public voidtestRecordWithZeroPayload()

        mParcel.writeInt(0);
        adjustSize();
        assertParseFail();
    
public voidtestRecordsFound()

        writeStringRecord(Metadata.TITLE, "a title");
        writeStringRecord(Metadata.GENRE, "comedy");
        writeStringRecord(Metadata.firstCustomId(), "custom");
        adjustSize();
        assertParse();
        assertTrue(mMetadata.has(Metadata.TITLE));
        assertTrue(mMetadata.has(Metadata.GENRE));
        assertTrue(mMetadata.has(Metadata.firstCustomId()));
        assertFalse(mMetadata.has(Metadata.DRM_CRIPPLED));
        assertEquals(3, mMetadata.keySet().size());
    
public voidtestWrongType()

        writeIntRecord(Metadata.DURATION, 5);
        adjustSize();
        assertParse();

        try {
            mMetadata.getString(Metadata.DURATION);
        } catch (IllegalStateException ise) {
            return;
        }
        fail("Exception was not thrown");
    
private voidwriteBooleanRecord(int metadataId, boolean val)

        final int start = mParcel.dataPosition();
        mParcel.writeInt(-1);  // Placeholder for the length
        mParcel.writeInt(metadataId);
        mParcel.writeInt(Metadata.BOOLEAN_VAL);
        mParcel.writeInt(val ? 1 : 0);
        adjustSize(start);
    
private voidwriteByteArrayRecord(int metadataId, byte[] val)

        final int start = mParcel.dataPosition();
        mParcel.writeInt(-1);  // Placeholder for the length
        mParcel.writeInt(metadataId);
        mParcel.writeInt(Metadata.BYTE_ARRAY_VAL);
        mParcel.writeByteArray(val);
        adjustSize(start);
    
private voidwriteDateRecord(int metadataId, long time, java.lang.String tz)

        final int start = mParcel.dataPosition();
        mParcel.writeInt(-1);  // Placeholder for the length
        mParcel.writeInt(metadataId);
        mParcel.writeInt(Metadata.DATE_VAL);
        mParcel.writeLong(time);
        mParcel.writeString(tz);
        adjustSize(start);
    
private voidwriteDoubleRecord(int metadataId, double val)

        final int start = mParcel.dataPosition();
        mParcel.writeInt(-1);  // Placeholder for the length
        mParcel.writeInt(metadataId);
        mParcel.writeInt(Metadata.DOUBLE_VAL);
        mParcel.writeDouble(val);
        adjustSize(start);
    
private voidwriteIntRecord(int metadataId, int val)

        final int start = mParcel.dataPosition();
        mParcel.writeInt(-1);  // Placeholder for the length
        mParcel.writeInt(metadataId);
        mParcel.writeInt(Metadata.INTEGER_VAL);
        mParcel.writeInt(val);
        adjustSize(start);
    
private voidwriteLongRecord(int metadataId, long val)

        final int start = mParcel.dataPosition();
        mParcel.writeInt(-1);  // Placeholder for the length
        mParcel.writeInt(metadataId);
        mParcel.writeInt(Metadata.LONG_VAL);
        mParcel.writeLong(val);
        adjustSize(start);
    
private voidwriteStringRecord(int metadataId, java.lang.String val)

        final int start = mParcel.dataPosition();
        mParcel.writeInt(-1);  // Placeholder for the length
        mParcel.writeInt(metadataId);
        mParcel.writeInt(Metadata.STRING_VAL);
        mParcel.writeString(val);
        adjustSize(start);