• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.settings.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 
25 import android.content.Context;
26 import android.media.MediaPlayer;
27 import android.view.Surface;
28 import android.view.TextureView;
29 
30 import androidx.test.core.app.ApplicationProvider;
31 
32 import com.android.settings.accessibility.VideoPlayer.State;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 
41 /** Tests for {@link VideoPlayer}. */
42 @RunWith(RobolectricTestRunner.class)
43 public class VideoPlayerTest {
44 
45     @Mock
46     private MediaPlayer mMediaPlayer;
47 
48     @Mock
49     private TextureView mTextureView;
50 
51     @Mock
52     private Surface mSurface;
53 
54     private VideoPlayer mVideoPlayer;
55 
56     @Before
initVideoPlayer()57     public void initVideoPlayer() {
58         MockitoAnnotations.initMocks(this);
59 
60         final int videoRes = 0;
61         final Context context = ApplicationProvider.getApplicationContext();
62 
63         mVideoPlayer = spy(VideoPlayer.create(context, videoRes, mTextureView));
64         mVideoPlayer.mMediaPlayer = mMediaPlayer;
65         mVideoPlayer.mAnimationSurface = mSurface;
66     }
67 
68     @Test
setSurfaceTextureListener_success()69     public void setSurfaceTextureListener_success() {
70         verify(mTextureView).setSurfaceTextureListener(any());
71     }
72 
73     @Test
onPlayerPaused_startedState_pause()74     public void onPlayerPaused_startedState_pause() {
75         mVideoPlayer.mMediaPlayerState = State.STARTED;
76 
77         mVideoPlayer.pause();
78 
79         assertThat(mVideoPlayer.mMediaPlayerState).isEqualTo(State.PAUSED);
80         verify(mMediaPlayer).pause();
81     }
82 
83     @Test
onPlayerResumed_pausedState_start()84     public void onPlayerResumed_pausedState_start() {
85         mVideoPlayer.mMediaPlayerState = State.PAUSED;
86 
87         mVideoPlayer.resume();
88 
89         assertThat(mVideoPlayer.mMediaPlayerState).isEqualTo(State.STARTED);
90         verify(mMediaPlayer).start();
91     }
92 
93     @Test
onPlayerReleased_stoppedState_release()94     public void onPlayerReleased_stoppedState_release() {
95         mVideoPlayer.mMediaPlayerState = State.STOPPED;
96 
97         mVideoPlayer.release();
98 
99         assertThat(mVideoPlayer.mMediaPlayerState).isEqualTo(State.END);
100         verify(mMediaPlayer).release();
101         verify(mSurface).release();
102     }
103 
104     @Test
onSurfaceTextureDestroyed_preparedState_release()105     public void onSurfaceTextureDestroyed_preparedState_release() {
106         mVideoPlayer.mMediaPlayerState = State.PREPARED;
107 
108         mVideoPlayer.onSurfaceTextureDestroyed(any());
109 
110         assertThat(mVideoPlayer.mMediaPlayerState).isEqualTo(State.END);
111         verify(mMediaPlayer).release();
112         verify(mSurface).release();
113     }
114 }
115