mercredi 24 août 2016

Failure while unit testing playback in Robolectric because of prepareAsync() method of MediaPlayer

I have this problem while unit testing the playback using Robolectric. I have a play button which has code something like this::

Btn_Play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaPlayer mPlayer = new MediaPlayer();
    mPlayer.setDataSource(String.valueOf(list.get(position)));
    mPlayer.prepareAsync();

    mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {

            mPlayer.start();
            System.out.println("Playing....");
        }
     });
   }
});

Roboletric Unit Test code is:

@Test
public void varifyPlayButton()
{
    ShadowMediaPlayer.MediaInfo mediaInfo= new ShadowMediaPlayer.MediaInfo();
    play=(Button)fragment.getView().findViewById(R.id.play_Btn);
    ShadowMediaPlayer shadowMediaPlayer=new ShadowMediaPlayer();
    shadowMediaPlayer.addMediaInfo(DataSource.toDataSource(fragmentOne.list.get(0).getAbsolutePath()), mediaInfo);
    play.performClick();
    assertThat(play.isPressed(),equalsTo(true)); //This assertion fails.
}

The test passes with no exceptions but it won't execute the mPlayer.setOnPreparedListener() method. What can be possible issue??. I am able to playback the audio in the real devices. Is it a correct approach to write a unit test in Robolectric? I searched a lot regarding this issue. But I am not able to resolve this. Any suggestion or tutorial is appreciated. Thank you.

Aucun commentaire:

Enregistrer un commentaire