Search in sources :

Example 6 with DataSource

use of org.robolectric.shadows.util.DataSource in project robolectric by robolectric.

the class ShadowMediaPlayerTest method testSetDataSource_withUri.

@Test
public void testSetDataSource_withUri() throws IOException {
    Uri uri = Uri.parse("file:/test");
    DataSource ds = toDataSource(ApplicationProvider.getApplicationContext(), uri);
    ShadowMediaPlayer.addMediaInfo(ds, info);
    mediaPlayer.setDataSource(ApplicationProvider.getApplicationContext(), uri);
    assertWithMessage("sourceUri").that(shadowMediaPlayer.getSourceUri()).isSameInstanceAs(uri);
    assertWithMessage("dataSource").that(shadowMediaPlayer.getDataSource()).isEqualTo(ds);
}
Also used : Uri(android.net.Uri) DataSource(org.robolectric.shadows.util.DataSource) MediaDataSource(android.media.MediaDataSource) DataSource.toDataSource(org.robolectric.shadows.util.DataSource.toDataSource) Test(org.junit.Test)

Example 7 with DataSource

use of org.robolectric.shadows.util.DataSource in project robolectric by robolectric.

the class ShadowMediaPlayerTest method testSetDataSourceFD.

@Test
public void testSetDataSourceFD() throws IOException {
    File tmpFile = File.createTempFile("MediaPlayerTest", null);
    try {
        tmpFile.deleteOnExit();
        FileInputStream is = new FileInputStream(tmpFile);
        try {
            FileDescriptor fd = is.getFD();
            DataSource ds = toDataSource(fd, 23, 524);
            ShadowMediaPlayer.addMediaInfo(ds, info);
            mediaPlayer.setDataSource(fd, 23, 524);
            assertWithMessage("sourceUri").that(shadowMediaPlayer.getSourceUri()).isNull();
            assertWithMessage("dataSource").that(shadowMediaPlayer.getDataSource()).isEqualTo(ds);
        } finally {
            is.close();
        }
    } finally {
        tmpFile.delete();
    }
}
Also used : File(java.io.File) FileInputStream(java.io.FileInputStream) AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileDescriptor(java.io.FileDescriptor) DataSource(org.robolectric.shadows.util.DataSource) MediaDataSource(android.media.MediaDataSource) DataSource.toDataSource(org.robolectric.shadows.util.DataSource.toDataSource) Test(org.junit.Test)

Example 8 with DataSource

use of org.robolectric.shadows.util.DataSource in project robolectric by robolectric.

the class ShadowMediaPlayerTest method testSetDataSourceString.

@Test
public void testSetDataSourceString() throws IOException {
    DataSource ds = toDataSource("dummy");
    ShadowMediaPlayer.addMediaInfo(ds, info);
    mediaPlayer.setDataSource("dummy");
    assertWithMessage("dataSource").that(shadowMediaPlayer.getDataSource()).isEqualTo(ds);
}
Also used : DataSource(org.robolectric.shadows.util.DataSource) MediaDataSource(android.media.MediaDataSource) DataSource.toDataSource(org.robolectric.shadows.util.DataSource.toDataSource) Test(org.junit.Test)

Example 9 with DataSource

use of org.robolectric.shadows.util.DataSource in project robolectric by robolectric.

the class ShadowMediaPlayerTest method testResetStaticState.

@Test
public void testResetStaticState() {
    ShadowMediaPlayer.CreateListener createListener = Mockito.mock(ShadowMediaPlayer.CreateListener.class);
    ShadowMediaPlayer.setCreateListener(createListener);
    assertWithMessage("createListener").that(ShadowMediaPlayer.createListener).isSameInstanceAs(createListener);
    DataSource dummy = toDataSource("stuff");
    IOException e = new IOException();
    addException(dummy, e);
    try {
        shadowMediaPlayer.setState(IDLE);
        shadowMediaPlayer.setDataSource(dummy);
        fail("Expected exception thrown");
    } catch (IOException e2) {
        assertWithMessage("thrown exception").that(e2).isSameInstanceAs(e);
    }
    // Check that the mediaInfo was cleared
    shadowMediaPlayer.doSetDataSource(defaultSource);
    assertWithMessage("mediaInfo:before").that(shadowMediaPlayer.getMediaInfo()).isNotNull();
    ShadowMediaPlayer.resetStaticState();
    // Check that the listener was cleared.
    assertWithMessage("createListener").that(ShadowMediaPlayer.createListener).isNull();
    // Check that the mediaInfo was cleared.
    try {
        shadowMediaPlayer.doSetDataSource(defaultSource);
        fail("Expected exception thrown");
    } catch (IllegalArgumentException ie) {
    // We expect this if the static state has been cleared.
    }
    // Check that the exception was cleared.
    try {
        shadowMediaPlayer.setState(IDLE);
        ShadowMediaPlayer.addMediaInfo(dummy, info);
        shadowMediaPlayer.setDataSource(dummy);
    } catch (IOException e2) {
        fail("Exception was not cleared by resetStaticState() for <" + dummy + ">" + e2);
    }
}
Also used : IOException(java.io.IOException) DataSource(org.robolectric.shadows.util.DataSource) MediaDataSource(android.media.MediaDataSource) DataSource.toDataSource(org.robolectric.shadows.util.DataSource.toDataSource) Test(org.junit.Test)

Example 10 with DataSource

use of org.robolectric.shadows.util.DataSource in project robolectric by robolectric.

the class ShadowMediaPlayerTest method testSetDataSourceExceptionWithWrongExceptionTypeAsserts.

@Test
public void testSetDataSourceExceptionWithWrongExceptionTypeAsserts() {
    boolean fail = false;
    Map<DataSource, Exception> exceptions = ReflectionHelpers.getStaticField(ShadowMediaPlayer.class, "exceptions");
    DataSource ds = toDataSource("dummy");
    // just a convenient, non-RuntimeException in java.lang
    Exception e = new CloneNotSupportedException();
    exceptions.put(ds, e);
    try {
        shadowMediaPlayer.setDataSource(ds);
        fail = true;
    } catch (AssertionError a) {
    } catch (IOException ioe) {
        fail("Got exception <" + ioe + ">; expecting assertion");
    }
    if (fail) {
        fail("setDataSource() should assert with non-IOException,non-RuntimeException");
    }
}
Also used : IOException(java.io.IOException) ShadowMediaPlayer.addException(org.robolectric.shadows.ShadowMediaPlayer.addException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DataSource(org.robolectric.shadows.util.DataSource) MediaDataSource(android.media.MediaDataSource) DataSource.toDataSource(org.robolectric.shadows.util.DataSource.toDataSource) Test(org.junit.Test)

Aggregations

DataSource (org.robolectric.shadows.util.DataSource)13 Test (org.junit.Test)12 DataSource.toDataSource (org.robolectric.shadows.util.DataSource.toDataSource)10 MediaDataSource (android.media.MediaDataSource)9 Uri (android.net.Uri)3 AssetFileDescriptor (android.content.res.AssetFileDescriptor)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Clip (org.odk.collect.audioclips.Clip)2 RobolectricHelpers.setupMediaPlayerDataSource (org.odk.collect.testshared.RobolectricHelpers.setupMediaPlayerDataSource)2 Config (org.robolectric.annotation.Config)2 Application (android.app.Application)1 MediaPlayer (android.media.MediaPlayer)1 File (java.io.File)1 FileDescriptor (java.io.FileDescriptor)1 FileInputStream (java.io.FileInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Implementation (org.robolectric.annotation.Implementation)1 ShadowMediaPlayer (org.robolectric.shadows.ShadowMediaPlayer)1