1 package com.xtremelabs.robolectric.shadows;
2 
3 import static org.junit.Assert.assertThat;
4 import android.media.MediaPlayer;
5 import android.net.Uri;
6 import android.widget.VideoView;
7 
8 import com.xtremelabs.robolectric.internal.Implementation;
9 import com.xtremelabs.robolectric.internal.Implements;
10 
11 @Implements(VideoView.class)
12 @SuppressWarnings({"UnusedDeclaration"})
13 public class ShadowVideoView extends ShadowSurfaceView {
14 
15 	private MediaPlayer.OnCompletionListener completionListner;
16 	private MediaPlayer.OnErrorListener errorListener;
17 	private MediaPlayer.OnPreparedListener preparedListener;
18 
19 	private Uri uri;
20 	private String path;
21 
22 	public static final int STOP = 0;
23 	public static final int START = 1;
24 	public static final int SUSPEND = 2;
25 	public static final int PAUSE = 3;
26 	public static final int RESUME = 4;
27 
28 	private int currentState = -1;
29 	private int prevState;
30 
31 	@Implementation
setOnPreparedListener(MediaPlayer.OnPreparedListener l)32 	public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
33 		preparedListener = l;
34 	}
35 
36 	@Implementation
setOnErrorListener(MediaPlayer.OnErrorListener l)37 	public void setOnErrorListener(MediaPlayer.OnErrorListener l) {
38 		errorListener = l;
39 	}
40 
41 	@Implementation
setOnCompletionListener(MediaPlayer.OnCompletionListener l)42 	public void setOnCompletionListener(MediaPlayer.OnCompletionListener l) {
43 		completionListner = l;
44 	}
45 
46 	@Implementation
setVideoPath(String path)47 	public void setVideoPath(String path) {
48 		this.path = path;
49 	}
50 
51 	@Implementation
setVideoURI(Uri uri)52 	public void setVideoURI(Uri uri) {
53 		this.uri = uri;
54 	}
55 
56 	@Implementation
start()57 	public void start() {
58 		savePrevState();
59 		currentState = ShadowVideoView.START;
60 	}
61 
62 	@Implementation
stopPlayback()63 	public void stopPlayback() {
64 		savePrevState();
65 		currentState = ShadowVideoView.STOP;
66 	}
67 
68 	@Implementation
suspend()69 	public void suspend() {
70 		savePrevState();
71 		currentState = ShadowVideoView.SUSPEND;
72 	}
73 
74 	@Implementation
pause()75 	public void pause() {
76 		savePrevState();
77 		currentState = ShadowVideoView.PAUSE;
78 	}
79 
80 	@Implementation
resume()81 	public void resume() {
82 		savePrevState();
83 		currentState = ShadowVideoView.RESUME;
84 	}
85 
86 	@Implementation
isPlaying()87 	public boolean isPlaying() {
88 		return (currentState == ShadowVideoView.START);
89 	}
90 
91 	@Implementation
canPause()92 	public boolean canPause() {
93 		return (currentState != ShadowVideoView.PAUSE &&
94 				currentState != ShadowVideoView.STOP &&
95 				currentState != ShadowVideoView.SUSPEND);
96 	}
97 
98 	/**
99 	 * Non-Android accessor.
100 	 */
getOnPreparedListener()101 	public MediaPlayer.OnPreparedListener getOnPreparedListener() {
102 		return preparedListener;
103 	}
104 
105 	/**
106 	 * Non-Android accessor.
107 	 * @return
108 	 */
getOnErrorListener()109 	public MediaPlayer.OnErrorListener getOnErrorListener() {
110 		return errorListener;
111 	}
112 
113 	/**
114 	 * Non-Android accessor.
115 	 * @return
116 	 */
getOnCompletionListener()117 	public MediaPlayer.OnCompletionListener getOnCompletionListener() {
118 		return completionListner;
119 	}
120 
121 	/**
122 	 * Non-Android accessor.
123 	 * @return
124 	 */
getVideoPath()125 	public String getVideoPath() {
126 		return path;
127 	}
128 
129 	/**
130 	 * Non-Android accessor.
131 	 * @return
132 	 */
getVideoURIString()133 	public String getVideoURIString() {
134 		return uri == null ? null : uri.toString();
135 	}
136 
137 	/**
138 	 * Non-Android accessor.
139 	 * @return
140 	 */
getCurrentVideoState()141 	public int getCurrentVideoState() {
142 		return currentState;
143 	}
144 
145 	/**
146 	 * Non-Android accessor.
147 	 * @return
148 	 */
getPrevVideoState()149 	public int getPrevVideoState() {
150 		return prevState;
151 	}
152 
153 	/**
154 	 * Non-Android accessor.
155 	 */
savePrevState()156 	private void savePrevState() {
157 		prevState = currentState;
158 	}
159 
160 }
161