1 package com.xtremelabs.robolectric.shadows; 2 3 import static org.hamcrest.CoreMatchers.equalTo; 4 import static org.hamcrest.CoreMatchers.sameInstance; 5 import static org.hamcrest.CoreMatchers.nullValue; 6 import static org.hamcrest.MatcherAssert.assertThat; 7 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 12 import android.media.MediaPlayer; 13 import android.net.Uri; 14 import android.widget.VideoView; 15 16 import com.xtremelabs.robolectric.Robolectric; 17 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 18 19 @RunWith(WithTestDefaultsRunner.class) 20 public class VideoViewTest { 21 22 private VideoView view; 23 setUp()24 @Before public void setUp() throws Exception { 25 view = new VideoView(null); 26 } 27 28 @Test shouldSetOnPreparedListener()29 public void shouldSetOnPreparedListener() throws Exception { 30 TestPreparedListener l = new TestPreparedListener(); 31 view.setOnPreparedListener(l); 32 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 33 assertThat((TestPreparedListener)(shadowVideoView.getOnPreparedListener()), sameInstance(l)); 34 } 35 36 @Test shouldSetOnErrorListener()37 public void shouldSetOnErrorListener() throws Exception { 38 TestErrorListener l = new TestErrorListener(); 39 view.setOnErrorListener(l); 40 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 41 assertThat((TestErrorListener)(shadowVideoView.getOnErrorListener()), sameInstance(l)); 42 } 43 44 @Test shouldSetOnCompletionListener()45 public void shouldSetOnCompletionListener() throws Exception { 46 TestCompletionListener l = new TestCompletionListener(); 47 view.setOnCompletionListener(l); 48 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 49 assertThat((TestCompletionListener)(shadowVideoView.getOnCompletionListener()), sameInstance(l)); 50 } 51 52 @Test shouldSetVideoPath()53 public void shouldSetVideoPath() throws Exception { 54 view.setVideoPath("video.mp4"); 55 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 56 assertThat(shadowVideoView.getVideoPath(), equalTo("video.mp4")); 57 view.setVideoPath(null); 58 assertThat(shadowVideoView.getVideoPath(), nullValue()); 59 } 60 61 @Test shouldSetVideoURI()62 public void shouldSetVideoURI() throws Exception { 63 view.setVideoURI(Uri.parse("video.mp4")); 64 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 65 assertThat(shadowVideoView.getVideoURIString(), equalTo("video.mp4")); 66 view.setVideoURI(null); 67 assertThat(shadowVideoView.getVideoURIString(), nullValue()); 68 } 69 70 @Test shoulDetermineIsPlaying()71 public void shoulDetermineIsPlaying() throws Exception { 72 assertThat(view.isPlaying(), equalTo(false)); 73 view.start(); 74 assertThat(view.isPlaying(), equalTo(true)); 75 view.stopPlayback(); 76 assertThat(view.isPlaying(), equalTo(false)); 77 } 78 79 @Test shouldStartPlaying()80 public void shouldStartPlaying() throws Exception { 81 view.start(); 82 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 83 assertThat(shadowVideoView.getCurrentVideoState(), equalTo(ShadowVideoView.START)); 84 } 85 86 @Test shouldStopPlayback()87 public void shouldStopPlayback() throws Exception { 88 view.stopPlayback(); 89 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 90 assertThat(shadowVideoView.getCurrentVideoState(), equalTo(ShadowVideoView.STOP)); 91 } 92 93 @Test shouldSuspendPlaying()94 public void shouldSuspendPlaying() throws Exception { 95 view.start(); 96 view.suspend(); 97 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 98 assertThat(shadowVideoView.getPrevVideoState(), equalTo(ShadowVideoView.START)); 99 assertThat(shadowVideoView.getCurrentVideoState(), equalTo(ShadowVideoView.SUSPEND)); 100 } 101 102 @Test shouldResumePlaying()103 public void shouldResumePlaying() throws Exception { 104 view.start(); 105 view.suspend(); 106 view.resume(); 107 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 108 assertThat(shadowVideoView.getPrevVideoState(), equalTo(ShadowVideoView.SUSPEND)); 109 assertThat(shadowVideoView.getCurrentVideoState(), equalTo(ShadowVideoView.RESUME)); 110 } 111 112 113 @Test shouldPausePlaying()114 public void shouldPausePlaying() throws Exception { 115 view.start(); 116 view.pause(); 117 ShadowVideoView shadowVideoView = Robolectric.shadowOf(view); 118 assertThat(shadowVideoView.getPrevVideoState(), equalTo(ShadowVideoView.START)); 119 assertThat(shadowVideoView.getCurrentVideoState(), equalTo(ShadowVideoView.PAUSE)); 120 } 121 122 @Test shouldDetermineIfPausable()123 public void shouldDetermineIfPausable() throws Exception { 124 view.start(); 125 assertThat(view.canPause(), equalTo(true)); 126 127 view.pause(); 128 assertThat(view.canPause(), equalTo(false)); 129 130 view.resume(); 131 assertThat(view.canPause(), equalTo(true)); 132 133 view.suspend(); 134 assertThat(view.canPause(), equalTo(false)); 135 } 136 137 /** 138 * Helper classes 139 */ 140 141 private class TestPreparedListener implements MediaPlayer.OnPreparedListener { 142 @Override onPrepared(MediaPlayer mp)143 public void onPrepared(MediaPlayer mp) {} 144 } 145 146 private class TestErrorListener implements MediaPlayer.OnErrorListener { 147 @Override onError(MediaPlayer mp, int what, int extra)148 public boolean onError(MediaPlayer mp, int what, int extra) { 149 return false; 150 } 151 } 152 153 private class TestCompletionListener implements MediaPlayer.OnCompletionListener { 154 @Override onCompletion(MediaPlayer mp)155 public void onCompletion(MediaPlayer mp) {} 156 } 157 } 158