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

MediaInserterTest

public class MediaInserterTest extends android.test.InstrumentationTestCase

Fields Summary
private android.media.MediaInserter
mMediaInserter
private static final int
TEST_BUFFER_SIZE
private android.content.IContentProvider
mMockProvider
private String
mPackageName
private int
mFilesCounter
private int
mAudioCounter
private int
mVideoCounter
private int
mImagesCounter
private static final String
sVolumeName
private static final android.net.Uri
sAudioUri
private static final android.net.Uri
sVideoUri
private static final android.net.Uri
sImagesUri
private static final android.net.Uri
sFilesUri
Constructors Summary
Methods Summary
private android.content.ContentValuescreateAudioContent()

        ContentValues values = new ContentValues();
        values.put("_data", "/mnt/sdcard/audio" + ++mAudioCounter);
        return values;
    
private android.content.ContentValuescreateContent(android.net.Uri uri)

        if (uri == sFilesUri) return createFileContent();
        else if (uri == sAudioUri) return createAudioContent();
        else if (uri == sVideoUri) return createVideoContent();
        else if (uri == sImagesUri) return createImageContent();
        else throw new IllegalArgumentException("Unknown URL: " + uri.toString());
    
private android.content.ContentValuescreateFileContent()

        ContentValues values = new ContentValues();
        values.put("_data", "/mnt/sdcard/file" + ++mFilesCounter);
        return values;
    
private android.content.ContentValuescreateImageContent()

        ContentValues values = new ContentValues();
        values.put("_data", "/mnt/sdcard/image" + ++mImagesCounter);
        return values;
    
private android.content.ContentValuescreateVideoContent()

        ContentValues values = new ContentValues();
        values.put("_data", "/mnt/sdcard/video" + ++mVideoCounter);
        return values;
    
private voidfillBuffer(android.net.Uri uri, int numberOfFiles)

        ContentValues values;
        for (int i = 0; i < numberOfFiles; ++i) {
            values = createContent(uri);
            mMediaInserter.insert(uri, values);
        }
    
protected voidsetUp()

        super.setUp();
        mMockProvider = EasyMock.createMock(IContentProvider.class);
        mMediaInserter = new MediaInserter(mMockProvider,
        mPackageName, TEST_BUFFER_SIZE);
        mPackageName = getInstrumentation().getContext().getPackageName();
        mFilesCounter = 0;
        mAudioCounter = 0;
        mVideoCounter = 0;
        mImagesCounter = 0;
    
public voidtestFlushAllWithEmptyContents()

        EasyMock.replay(mMockProvider);

        mMediaInserter.flushAll();

        EasyMock.verify(mMockProvider);
    
public voidtestFlushAllWithSomeContents()

        EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
                (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1);
        EasyMock.expectLastCall().times(4);
        EasyMock.replay(mMockProvider);

        fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4);
        fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3);
        fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2);
        fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1);
        mMediaInserter.flushAll();

        EasyMock.verify(mMockProvider);
    
public voidtestInsertContentsAfterFlushAll()

        EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
                (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1);
        EasyMock.expectLastCall().times(8);
        EasyMock.replay(mMockProvider);

        fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4);
        fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3);
        fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2);
        fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1);
        mMediaInserter.flushAll();

        fillBuffer(sFilesUri, TEST_BUFFER_SIZE + 1);
        fillBuffer(sAudioUri, TEST_BUFFER_SIZE + 2);
        fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3);
        fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4);

        EasyMock.verify(mMockProvider);
    
public voidtestInsertContentsEqualToBufferSize()

        EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
                (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1);
        EasyMock.expectLastCall().times(4);
        EasyMock.replay(mMockProvider);

        fillBuffer(sFilesUri, TEST_BUFFER_SIZE);
        fillBuffer(sAudioUri, TEST_BUFFER_SIZE);
        fillBuffer(sVideoUri, TEST_BUFFER_SIZE);
        fillBuffer(sImagesUri, TEST_BUFFER_SIZE);

        EasyMock.verify(mMockProvider);
    
public voidtestInsertContentsLessThanBufferSize()

        EasyMock.replay(mMockProvider);

        fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4);
        fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3);
        fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2);
        fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1);

        EasyMock.verify(mMockProvider);
    
public voidtestInsertContentsMoreThanBufferSize()

        EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
                (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1);
        EasyMock.expectLastCall().times(4);
        EasyMock.replay(mMockProvider);

        fillBuffer(sFilesUri, TEST_BUFFER_SIZE + 1);
        fillBuffer(sAudioUri, TEST_BUFFER_SIZE + 2);
        fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3);
        fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4);

        EasyMock.verify(mMockProvider);
    
public voidtestInsertContentsWithDifferentSizePerContentType()

        EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
        MediaUriMatcher.expectMediaUri(sFilesUri),
                (ContentValues[]) EasyMock.anyObject())).andReturn(1);
        EasyMock.expectLastCall().times(1);
        EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
        MediaUriMatcher.expectMediaUri(sAudioUri),
                (ContentValues[]) EasyMock.anyObject())).andReturn(1);
        EasyMock.expectLastCall().times(2);
        EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
        MediaUriMatcher.expectMediaUri(sVideoUri),
                (ContentValues[]) EasyMock.anyObject())).andReturn(1);
        EasyMock.expectLastCall().times(3);
        EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
        MediaUriMatcher.expectMediaUri(sImagesUri),
                (ContentValues[]) EasyMock.anyObject())).andReturn(1);
        EasyMock.expectLastCall().times(4);
        EasyMock.replay(mMockProvider);

        for (int i = 0; i < TEST_BUFFER_SIZE; ++i) {
            fillBuffer(sFilesUri, 1);
            fillBuffer(sAudioUri, 2);
            fillBuffer(sVideoUri, 3);
            fillBuffer(sImagesUri, 4);
        }

        EasyMock.verify(mMockProvider);