1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.widget.cts;
18 
19 import android.widget.cts.R;
20 
21 
22 import org.xmlpull.v1.XmlPullParser;
23 
24 import android.app.Activity;
25 import android.app.Instrumentation;
26 import android.content.Context;
27 import android.cts.util.PollingCheck;
28 import android.test.ActivityInstrumentationTestCase2;
29 import android.test.UiThreadTest;
30 import android.util.AttributeSet;
31 import android.util.Xml;
32 import android.view.MotionEvent;
33 import android.view.View;
34 import android.view.View.OnClickListener;
35 import android.widget.MediaController;
36 import android.widget.VideoView;
37 
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.OutputStream;
41 
42 /**
43  * Test {@link MediaController}.
44  */
45 public class MediaControllerTest extends
46         ActivityInstrumentationTestCase2<MediaControllerCtsActivity> {
47     private MediaController mMediaController;
48     private Activity mActivity;
49     private Instrumentation mInstrumentation;
50     private static final long DEFAULT_TIMEOUT = 3000;
51 
MediaControllerTest()52     public MediaControllerTest() {
53         super("android.widget.cts", MediaControllerCtsActivity.class);
54     }
55 
56     @Override
setUp()57     protected void setUp() throws Exception {
58         super.setUp();
59         mActivity = getActivity();
60         mInstrumentation = getInstrumentation();
61     }
62 
63     @UiThreadTest
testConstructor()64     public void testConstructor() {
65         new MediaController(mActivity, null);
66 
67         new MediaController(mActivity, true);
68 
69         new MediaController(mActivity);
70 
71         final XmlPullParser parser =
72                 mActivity.getResources().getXml(R.layout.mediacontroller_layout);
73         final AttributeSet attrs = Xml.asAttributeSet(parser);
74         new MediaController(mActivity, attrs);
75     }
76 
77     /**
78      * scenario description:
79      * 1. Show the MediaController.
80      *
81      */
82     @UiThreadTest
testMediaController()83     public void testMediaController() {
84         mMediaController = new MediaController(mActivity);
85         final MockMediaPlayerControl mediaPlayerControl = new MockMediaPlayerControl();
86         mMediaController.setMediaPlayer(mediaPlayerControl);
87 
88         assertFalse(mMediaController.isShowing());
89         mMediaController.show();
90         // setAnchorView() must be called before show(),
91         // otherwise MediaController never show.
92         assertFalse(mMediaController.isShowing());
93 
94         View videoview = mActivity.findViewById(R.id.mediacontroller_videoview);
95         mMediaController.setAnchorView(videoview);
96 
97         mMediaController.show();
98         assertTrue(mMediaController.isShowing());
99 
100         // ideally test would trigger pause/play/ff/rew here and test response, but no way
101         // to trigger those actions from MediaController
102 
103         mMediaController = new MediaController(mActivity, false);
104         mMediaController.setMediaPlayer(mediaPlayerControl);
105         videoview = mActivity.findViewById(R.id.mediacontroller_videoview);
106         mMediaController.setAnchorView(videoview);
107 
108         mMediaController.show();
109         assertTrue(mMediaController.isShowing());
110     }
111 
testShow()112     public void testShow() {
113         mActivity.runOnUiThread(new Runnable() {
114             public void run() {
115                 mMediaController = new MediaController(mActivity, true);
116             }
117         });
118         mInstrumentation.waitForIdleSync();
119         assertFalse(mMediaController.isShowing());
120 
121         final MockMediaPlayerControl mediaPlayerControl = new MockMediaPlayerControl();
122         mMediaController.setMediaPlayer(mediaPlayerControl);
123 
124         final VideoView videoView =
125                 (VideoView) mActivity.findViewById(R.id.mediacontroller_videoview);
126         mMediaController.setAnchorView(videoView);
127 
128         mActivity.runOnUiThread(new Runnable() {
129             public void run() {
130                 mMediaController.show();
131             }
132         });
133         mInstrumentation.waitForIdleSync();
134         assertTrue(mMediaController.isShowing());
135 
136         mActivity.runOnUiThread(new Runnable() {
137             public void run() {
138                 mMediaController.hide();
139             }
140         });
141         mInstrumentation.waitForIdleSync();
142         assertFalse(mMediaController.isShowing());
143 
144         final int timeout = 2000;
145         mActivity.runOnUiThread(new Runnable() {
146             public void run() {
147                 mMediaController.show(timeout);
148             }
149         });
150         mInstrumentation.waitForIdleSync();
151         assertTrue(mMediaController.isShowing());
152 
153         // isShowing() should return false, but MediaController still shows, this may be a bug.
154         new PollingCheck(timeout + 500) {
155             @Override
156             protected boolean check() {
157                 return mMediaController.isShowing();
158             }
159         }.run();
160     }
161 
prepareSampleVideo()162     private String prepareSampleVideo() {
163         InputStream source = null;
164         OutputStream target = null;
165         final String VIDEO_NAME   = "testvideo.3gp";
166 
167         try {
168             source = mActivity.getResources().openRawResource(R.raw.testvideo);
169             target = mActivity.openFileOutput(VIDEO_NAME, Context.MODE_PRIVATE);
170 
171             final byte[] buffer = new byte[1024];
172             for (int len = source.read(buffer); len > 0; len = source.read(buffer)) {
173                 target.write(buffer, 0, len);
174             }
175         } catch (final IOException e) {
176             fail(e.getMessage());
177         } finally {
178             try {
179                 if (source != null) {
180                     source.close();
181                 }
182                 if (target != null) {
183                     target.close();
184                 }
185             } catch (final IOException ignored) {
186                 // Ignore the IOException.
187             }
188         }
189 
190         return mActivity.getFileStreamPath(VIDEO_NAME).getAbsolutePath();
191     }
192 
testOnTrackballEvent()193     public void testOnTrackballEvent() {
194         mActivity.runOnUiThread(new Runnable() {
195             public void run() {
196                 mMediaController = new MediaController(mActivity);
197             }
198         });
199         mInstrumentation.waitForIdleSync();
200         final MockMediaPlayerControl mediaPlayerControl = new MockMediaPlayerControl();
201         mMediaController.setMediaPlayer(mediaPlayerControl);
202 
203         final VideoView videoView =
204                 (VideoView) mActivity.findViewById(R.id.mediacontroller_videoview);
205         videoView.setMediaController(mMediaController);
206         mActivity.runOnUiThread(new Runnable() {
207             public void run() {
208                 videoView.setVideoPath(prepareSampleVideo());
209                 videoView.requestFocus();
210             }
211         });
212         mInstrumentation.waitForIdleSync();
213 
214         final long curTime = System.currentTimeMillis();
215         // get the center of the VideoView.
216         final int[] xy = new int[2];
217         videoView.getLocationOnScreen(xy);
218 
219         final int viewWidth = videoView.getWidth();
220         final int viewHeight = videoView.getHeight();
221 
222         final float x = xy[0] + viewWidth / 2.0f;
223         final float y = xy[1] + viewHeight / 2.0f;
224         final MotionEvent event = MotionEvent.obtain(curTime, 100,
225                 MotionEvent.ACTION_DOWN, x, y, 0);
226         mInstrumentation.sendTrackballEventSync(event);
227         mInstrumentation.waitForIdleSync();
228     }
229 
230     @UiThreadTest
testSetEnabled()231     public void testSetEnabled() {
232         final View videoView = mActivity.findViewById(R.id.mediacontroller_videoview);
233         final MockMediaPlayerControl mediaPlayerControl = new MockMediaPlayerControl();
234 
235         mMediaController = new MediaController(mActivity);
236         mMediaController.setAnchorView(videoView);
237         mMediaController.setMediaPlayer(mediaPlayerControl);
238 
239         final MockOnClickListener next = new MockOnClickListener();
240         final MockOnClickListener prev = new MockOnClickListener();
241         mMediaController.setPrevNextListeners(next, prev);
242 
243         mMediaController.show();
244 
245         mMediaController.setEnabled(true);
246         assertTrue(mMediaController.isEnabled());
247 
248         mMediaController.setEnabled(false);
249         assertFalse(mMediaController.isEnabled());
250     }
251 
252     @UiThreadTest
testSetPrevNextListeners()253     public void testSetPrevNextListeners() {
254         final View videoView = mActivity.findViewById(R.id.mediacontroller_videoview);
255         final MockMediaPlayerControl mediaPlayerControl = new MockMediaPlayerControl();
256 
257         mMediaController = new MediaController(mActivity);
258         mMediaController.setAnchorView(videoView);
259         mMediaController.setMediaPlayer(mediaPlayerControl);
260 
261         final MockOnClickListener next = new MockOnClickListener();
262         final MockOnClickListener prev = new MockOnClickListener();
263         mMediaController.setPrevNextListeners(next, prev);
264     }
265 
266     private static class MockMediaPlayerControl implements MediaController.MediaPlayerControl {
267         private boolean mIsPlayingCalled = false;
268         private boolean mIsPlaying = false;
269         private int mPosition = 0;
270 
hasIsPlayingCalled()271         public boolean hasIsPlayingCalled() {
272             return mIsPlayingCalled;
273         }
274 
start()275         public void start() {
276             mIsPlaying = true;
277         }
278 
pause()279         public void pause() {
280             mIsPlaying = false;
281         }
282 
getDuration()283         public int getDuration() {
284             return 0;
285         }
286 
getCurrentPosition()287         public int getCurrentPosition() {
288             return mPosition;
289         }
290 
seekTo(int pos)291         public void seekTo(int pos) {
292             mPosition = pos;
293         }
294 
isPlaying()295         public boolean isPlaying() {
296             mIsPlayingCalled = true;
297             return mIsPlaying;
298         }
299 
getBufferPercentage()300         public int getBufferPercentage() {
301             return 0;
302         }
303 
canPause()304         public boolean canPause() {
305             return true;
306         }
307 
canSeekBackward()308         public boolean canSeekBackward() {
309             return true;
310         }
311 
canSeekForward()312         public boolean canSeekForward() {
313             return true;
314         }
315 
316         @Override
getAudioSessionId()317         public int getAudioSessionId() {
318             return 0;
319         }
320     }
321 
322     private static class MockOnClickListener implements OnClickListener {
323         private boolean mOnClickCalled = false;
324 
hasOnClickCalled()325         public boolean hasOnClickCalled() {
326             return mOnClickCalled;
327         }
328 
onClick(View v)329         public void onClick(View v) {
330             mOnClickCalled = true;
331         }
332     }
333 }
334