1 /*
2  * Copyright (C) 2016 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 com.android.androidbvt;
18 
19 import android.media.MediaPlayer;
20 import android.os.Looper;
21 import android.test.ActivityInstrumentationTestCase2;
22 import android.test.suitebuilder.annotation.LargeTest;
23 import android.util.Log;
24 
25 import com.android.androidbvt.app.MediaPlaybackTestApp;
26 
27 /**
28  * Basic tests for video playback
29  */
30 public class MediaPlaybackTests extends ActivityInstrumentationTestCase2<MediaPlaybackTestApp> {
31 
32     private static final String TAG = "MediaPlaybackTest";
33     private static final int LOOP_START_BUFFER_MS = 10000;
34     private static final int PLAY_BUFFER_MS = 2000;
35     private final Object mCompletionLock = new Object();
36     private final Object mLooperLock = new Object();
37     private boolean mPlaybackSucceeded = false;
38     private boolean mPlaybackError = false;
39     private Looper mLooper;
40     private MediaPlayer mPlayer;
41 
MediaPlaybackTests()42     public MediaPlaybackTests() {
43         super(MediaPlaybackTestApp.class);
44     }
45 
46     @Override
setUp()47     public void setUp() throws Exception {
48         // start activity
49         getActivity();
50     }
51 
52     @LargeTest
testVideoPlayback()53     public void testVideoPlayback() {
54         // start the MediaPlayer on a Looper thread, so it does not deadlock itself
55         new Thread() {
56             @Override
57             public void run() {
58                 Looper.prepare();
59                 mLooper = Looper.myLooper();
60                 mPlayer = MediaPlayer.create(getInstrumentation().getContext(), R.raw.bbb);
61                 mPlayer.setDisplay(getActivity().getSurfaceHolder());
62                 synchronized (mLooperLock) {
63                     mLooperLock.notify();
64                 }
65                 Looper.loop();
66             }
67         }.start();
68         // make sure the looper is really started before we proceed
69         synchronized (mLooperLock) {
70             try {
71                 mLooperLock.wait(LOOP_START_BUFFER_MS);
72             } catch (InterruptedException e) {
73                 fail("Loop thread start was interrupted");
74             }
75         }
76         mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
77             @Override
78             public boolean onError(MediaPlayer mp, int what, int extra) {
79                 mPlaybackError = true;
80                 mp.reset();
81                 return true;
82             }
83         });
84         mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
85             @Override
86             public void onCompletion(MediaPlayer mp) {
87                 synchronized (mCompletionLock) {
88                     Log.w(TAG, "Hit onCompletion!");
89                     mPlaybackSucceeded = true;
90                     mCompletionLock.notifyAll();
91                 }
92             }
93         });
94         mPlayer.start();
95         int duration = mPlayer.getDuration();
96         int currentPosition = mPlayer.getCurrentPosition();
97         synchronized (mCompletionLock) {
98             try {
99                 mCompletionLock.wait(duration - currentPosition + PLAY_BUFFER_MS);
100             } catch (InterruptedException e) {
101                 fail("Wait for playback was interrupted");
102             }
103         }
104         mLooper.quit();
105         mPlayer.release();
106         assertFalse(mPlaybackError);
107         assertTrue(mPlaybackSucceeded);
108     }
109 }
110