Merge "Use prepareAsync for media player"
This commit is contained in:
commit
f61eb6b329
|
@ -22,11 +22,17 @@ import android.content.res.TypedArray;
|
||||||
import android.graphics.SurfaceTexture;
|
import android.graphics.SurfaceTexture;
|
||||||
import android.graphics.drawable.Animatable;
|
import android.graphics.drawable.Animatable;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
|
import android.media.MediaPlayer.OnErrorListener;
|
||||||
|
import android.media.MediaPlayer.OnInfoListener;
|
||||||
|
import android.media.MediaPlayer.OnPreparedListener;
|
||||||
|
import android.media.MediaPlayer.OnSeekCompleteListener;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Build.VERSION_CODES;
|
import android.os.Build.VERSION_CODES;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Surface;
|
import android.view.Surface;
|
||||||
import android.view.TextureView;
|
import android.view.TextureView;
|
||||||
|
import android.view.TextureView.SurfaceTextureListener;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
@ -35,6 +41,8 @@ import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
import com.android.setupwizardlib.R;
|
import com.android.setupwizardlib.R;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A view for displaying videos in a continuous loop (without audio). This is typically used for
|
* A view for displaying videos in a continuous loop (without audio). This is typically used for
|
||||||
* animated illustrations.
|
* animated illustrations.
|
||||||
|
@ -49,10 +57,11 @@ import com.android.setupwizardlib.R;
|
||||||
*/
|
*/
|
||||||
@TargetApi(VERSION_CODES.ICE_CREAM_SANDWICH)
|
@TargetApi(VERSION_CODES.ICE_CREAM_SANDWICH)
|
||||||
public class IllustrationVideoView extends TextureView implements Animatable,
|
public class IllustrationVideoView extends TextureView implements Animatable,
|
||||||
TextureView.SurfaceTextureListener,
|
SurfaceTextureListener,
|
||||||
MediaPlayer.OnPreparedListener,
|
OnPreparedListener,
|
||||||
MediaPlayer.OnSeekCompleteListener,
|
OnSeekCompleteListener,
|
||||||
MediaPlayer.OnInfoListener {
|
OnInfoListener,
|
||||||
|
OnErrorListener {
|
||||||
|
|
||||||
private static final String TAG = "IllustrationVideoView";
|
private static final String TAG = "IllustrationVideoView";
|
||||||
|
|
||||||
|
@ -65,7 +74,7 @@ public class IllustrationVideoView extends TextureView implements Animatable,
|
||||||
|
|
||||||
@VisibleForTesting Surface mSurface;
|
@VisibleForTesting Surface mSurface;
|
||||||
|
|
||||||
protected int mWindowVisibility;
|
private boolean mPrepared;
|
||||||
|
|
||||||
public IllustrationVideoView(Context context, AttributeSet attrs) {
|
public IllustrationVideoView(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
|
@ -135,25 +144,25 @@ public class IllustrationVideoView extends TextureView implements Animatable,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mMediaPlayer = MediaPlayer.create(getContext(), mVideoResId);
|
mMediaPlayer = new MediaPlayer();
|
||||||
|
|
||||||
if (mMediaPlayer != null) {
|
mMediaPlayer.setSurface(mSurface);
|
||||||
mMediaPlayer.setSurface(mSurface);
|
mMediaPlayer.setOnPreparedListener(this);
|
||||||
mMediaPlayer.setOnPreparedListener(this);
|
mMediaPlayer.setOnSeekCompleteListener(this);
|
||||||
mMediaPlayer.setOnSeekCompleteListener(this);
|
mMediaPlayer.setOnInfoListener(this);
|
||||||
mMediaPlayer.setOnInfoListener(this);
|
mMediaPlayer.setOnErrorListener(this);
|
||||||
|
|
||||||
float aspectRatio =
|
setVideoResourceInternal(mVideoResId);
|
||||||
(float) mMediaPlayer.getVideoHeight() / mMediaPlayer.getVideoWidth();
|
}
|
||||||
if (mAspectRatio != aspectRatio) {
|
|
||||||
mAspectRatio = aspectRatio;
|
private void setVideoResourceInternal(@RawRes int videoRes) {
|
||||||
requestLayout();
|
Uri uri =
|
||||||
}
|
Uri.parse("android.resource://" + getContext().getPackageName() + "/" + videoRes);
|
||||||
} else {
|
try {
|
||||||
Log.wtf(TAG, "Unable to initialize media player for video view");
|
mMediaPlayer.setDataSource(getContext(), uri);
|
||||||
}
|
mMediaPlayer.prepareAsync();
|
||||||
if (mWindowVisibility == View.VISIBLE) {
|
} catch (IOException e) {
|
||||||
start();
|
Log.wtf(TAG, "Unable to set data source", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,7 +182,6 @@ public class IllustrationVideoView extends TextureView implements Animatable,
|
||||||
@Override
|
@Override
|
||||||
protected void onWindowVisibilityChanged(int visibility) {
|
protected void onWindowVisibilityChanged(int visibility) {
|
||||||
super.onWindowVisibilityChanged(visibility);
|
super.onWindowVisibilityChanged(visibility);
|
||||||
mWindowVisibility = visibility;
|
|
||||||
if (visibility == View.VISIBLE) {
|
if (visibility == View.VISIBLE) {
|
||||||
reattach();
|
reattach();
|
||||||
} else {
|
} else {
|
||||||
|
@ -195,9 +203,9 @@ public class IllustrationVideoView extends TextureView implements Animatable,
|
||||||
*/
|
*/
|
||||||
public void release() {
|
public void release() {
|
||||||
if (mMediaPlayer != null) {
|
if (mMediaPlayer != null) {
|
||||||
mMediaPlayer.stop();
|
|
||||||
mMediaPlayer.release();
|
mMediaPlayer.release();
|
||||||
mMediaPlayer = null;
|
mMediaPlayer = null;
|
||||||
|
mPrepared = false;
|
||||||
}
|
}
|
||||||
if (mSurface != null) {
|
if (mSurface != null) {
|
||||||
mSurface.release();
|
mSurface.release();
|
||||||
|
@ -212,14 +220,14 @@ public class IllustrationVideoView extends TextureView implements Animatable,
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initVideo() {
|
private void initVideo() {
|
||||||
if (mWindowVisibility != View.VISIBLE) {
|
if (getWindowVisibility() != View.VISIBLE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
createSurface();
|
createSurface();
|
||||||
if (mSurface != null) {
|
if (mSurface != null) {
|
||||||
createMediaPlayer();
|
createMediaPlayer();
|
||||||
} else {
|
} else {
|
||||||
Log.w("IllustrationVideoView", "Surface creation failed");
|
Log.w(TAG, "Surface creation failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,14 +261,14 @@ public class IllustrationVideoView extends TextureView implements Animatable,
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
|
if (mPrepared && mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
|
||||||
mMediaPlayer.start();
|
mMediaPlayer.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
if (mMediaPlayer != null) {
|
if (mPrepared && mMediaPlayer != null) {
|
||||||
mMediaPlayer.pause();
|
mMediaPlayer.pause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -284,15 +292,39 @@ public class IllustrationVideoView extends TextureView implements Animatable,
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPrepared(MediaPlayer mp) {
|
public void onPrepared(MediaPlayer mp) {
|
||||||
|
mPrepared = true;
|
||||||
mp.setLooping(shouldLoop());
|
mp.setLooping(shouldLoop());
|
||||||
|
float aspectRatio =
|
||||||
|
(float) mp.getVideoHeight() / mp.getVideoWidth();
|
||||||
|
if (Float.compare(mAspectRatio, aspectRatio) == 0) {
|
||||||
|
mAspectRatio = aspectRatio;
|
||||||
|
requestLayout();
|
||||||
|
}
|
||||||
|
if (getWindowVisibility() == View.VISIBLE) {
|
||||||
|
start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSeekComplete(MediaPlayer mp) {
|
public void onSeekComplete(MediaPlayer mp) {
|
||||||
mp.start();
|
if (isPrepared()) {
|
||||||
|
mp.start();
|
||||||
|
} else {
|
||||||
|
Log.wtf(TAG, "Seek complete but media player not prepared");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCurrentPosition() {
|
public int getCurrentPosition() {
|
||||||
return mMediaPlayer == null ? 0 : mMediaPlayer.getCurrentPosition();
|
return mMediaPlayer == null ? 0 : mMediaPlayer.getCurrentPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean isPrepared() {
|
||||||
|
return mPrepared;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
|
||||||
|
Log.w(TAG, "MediaPlayer error. what=" + what + " extra=" + extra);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2018 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.setupwizardlib.shadow;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.media.MediaPlayer;
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.robolectric.annotation.Implementation;
|
||||||
|
import org.robolectric.annotation.Implements;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.HttpCookie;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Implements(MediaPlayer.class)
|
||||||
|
public class ShadowMediaPlayer extends org.robolectric.shadows.ShadowMediaPlayer {
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
public void setDataSource(
|
||||||
|
@NonNull Context context,
|
||||||
|
@NonNull Uri uri,
|
||||||
|
@Nullable Map<String, String> headers,
|
||||||
|
@Nullable List<HttpCookie> cookies)
|
||||||
|
throws IOException {
|
||||||
|
setDataSource(context, uri, headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
public void seekTo(long msec, int mode) {
|
||||||
|
seekTo((int) msec);
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,18 +18,14 @@ package com.android.setupwizardlib.view;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.reset;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.robolectric.RuntimeEnvironment.application;
|
import static org.robolectric.RuntimeEnvironment.application;
|
||||||
|
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.content.Context;
|
import android.app.Activity;
|
||||||
import android.graphics.SurfaceTexture;
|
import android.graphics.SurfaceTexture;
|
||||||
import android.media.MediaPlayer;
|
import android.net.Uri;
|
||||||
import android.os.Build.VERSION_CODES;
|
import android.os.Build.VERSION_CODES;
|
||||||
import android.view.Surface;
|
import android.view.Surface;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
@ -39,31 +35,33 @@ import androidx.annotation.RawRes;
|
||||||
import com.android.setupwizardlib.R;
|
import com.android.setupwizardlib.R;
|
||||||
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
|
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
|
||||||
import com.android.setupwizardlib.shadow.ShadowLog;
|
import com.android.setupwizardlib.shadow.ShadowLog;
|
||||||
import com.android.setupwizardlib.shadow.ShadowLog.TerribleFailure;
|
import com.android.setupwizardlib.shadow.ShadowMediaPlayer;
|
||||||
import com.android.setupwizardlib.view.IllustrationVideoViewTest.ShadowMockMediaPlayer;
|
|
||||||
import com.android.setupwizardlib.view.IllustrationVideoViewTest.ShadowSurface;
|
import com.android.setupwizardlib.view.IllustrationVideoViewTest.ShadowSurface;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.Robolectric;
|
import org.robolectric.Robolectric;
|
||||||
|
import org.robolectric.Shadows;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
import org.robolectric.annotation.Implementation;
|
|
||||||
import org.robolectric.annotation.Implements;
|
import org.robolectric.annotation.Implements;
|
||||||
import org.robolectric.annotation.RealObject;
|
import org.robolectric.annotation.RealObject;
|
||||||
import org.robolectric.shadow.api.Shadow;
|
import org.robolectric.shadow.api.Shadow;
|
||||||
import org.robolectric.shadows.ShadowMediaPlayer;
|
import org.robolectric.shadows.ShadowMediaPlayer.InvalidStateBehavior;
|
||||||
|
import org.robolectric.shadows.ShadowMediaPlayer.MediaInfo;
|
||||||
|
import org.robolectric.shadows.ShadowMediaPlayer.State;
|
||||||
|
import org.robolectric.shadows.util.DataSource;
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
import org.robolectric.util.ReflectionHelpers.ClassParameter;
|
||||||
|
|
||||||
@RunWith(SuwLibRobolectricTestRunner.class)
|
@RunWith(SuwLibRobolectricTestRunner.class)
|
||||||
@Config(
|
@Config(
|
||||||
sdk = Config.NEWEST_SDK,
|
sdk = Config.NEWEST_SDK,
|
||||||
shadows = {
|
shadows = {
|
||||||
ShadowLog.class,
|
ShadowLog.class,
|
||||||
ShadowMockMediaPlayer.class,
|
ShadowMediaPlayer.class,
|
||||||
ShadowSurface.class
|
ShadowSurface.class
|
||||||
})
|
})
|
||||||
public class IllustrationVideoViewTest {
|
public class IllustrationVideoViewTest {
|
||||||
|
@ -73,33 +71,21 @@ public class IllustrationVideoViewTest {
|
||||||
|
|
||||||
private IllustrationVideoView mView;
|
private IllustrationVideoView mView;
|
||||||
|
|
||||||
|
private ShadowMediaPlayer mShadowMediaPlayer;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
}
|
addMediaInfo(android.R.color.white);
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
ShadowMockMediaPlayer.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void nullMediaPlayer_shouldThrowWtf() {
|
|
||||||
ShadowMockMediaPlayer.sMediaPlayer = null;
|
|
||||||
try {
|
|
||||||
createDefaultView();
|
|
||||||
fail("WTF should be thrown for null media player");
|
|
||||||
} catch (TerribleFailure e) {
|
|
||||||
// pass
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onVisibilityChanged_notVisible_shouldRelease() {
|
public void onVisibilityChanged_notVisible_shouldRelease() {
|
||||||
|
|
||||||
createDefaultView();
|
createDefaultView();
|
||||||
mView.onWindowVisibilityChanged(View.GONE);
|
mView.onWindowVisibilityChanged(View.GONE);
|
||||||
|
|
||||||
verify(ShadowMockMediaPlayer.sMediaPlayer).release();
|
assertThat(mShadowMediaPlayer.getState()).isEqualTo(State.END);
|
||||||
assertThat(mView.mSurface).isNull();
|
assertThat(mView.mSurface).isNull();
|
||||||
assertThat(mView.mMediaPlayer).isNull();
|
assertThat(mView.mMediaPlayer).isNull();
|
||||||
}
|
}
|
||||||
|
@ -121,24 +107,22 @@ public class IllustrationVideoViewTest {
|
||||||
@Test
|
@Test
|
||||||
public void testPausedWhenWindowFocusLost() {
|
public void testPausedWhenWindowFocusLost() {
|
||||||
createDefaultView();
|
createDefaultView();
|
||||||
|
Robolectric.flushForegroundThreadScheduler();
|
||||||
mView.start();
|
mView.start();
|
||||||
|
|
||||||
assertNotNull(mView.mMediaPlayer);
|
assertNotNull(mView.mMediaPlayer);
|
||||||
assertNotNull(mView.mSurface);
|
assertNotNull(mView.mSurface);
|
||||||
|
|
||||||
mView.onWindowFocusChanged(false);
|
mView.onWindowFocusChanged(false);
|
||||||
verify(ShadowMockMediaPlayer.getMock()).pause();
|
assertThat(mShadowMediaPlayer.getState()).isEqualTo(State.PAUSED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStartedWhenWindowFocusRegained() {
|
public void testStartedWhenWindowFocusRegained() {
|
||||||
testPausedWhenWindowFocusLost();
|
testPausedWhenWindowFocusLost();
|
||||||
|
|
||||||
// Clear verifications for calls in the other test
|
|
||||||
reset(ShadowMockMediaPlayer.getMock());
|
|
||||||
|
|
||||||
mView.onWindowFocusChanged(true);
|
mView.onWindowFocusChanged(true);
|
||||||
verify(ShadowMockMediaPlayer.getMock()).start();
|
assertThat(mShadowMediaPlayer.getState()).isEqualTo(State.STARTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -150,56 +134,68 @@ public class IllustrationVideoViewTest {
|
||||||
assertNotNull(mView.mSurface);
|
assertNotNull(mView.mSurface);
|
||||||
|
|
||||||
mView.onSurfaceTextureDestroyed(mSurfaceTexture);
|
mView.onSurfaceTextureDestroyed(mSurfaceTexture);
|
||||||
verify(ShadowMockMediaPlayer.getMock()).release();
|
assertThat(mShadowMediaPlayer.getState()).isEqualTo(State.END);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testXmlSetVideoResId() {
|
public void testXmlSetVideoResId() {
|
||||||
createDefaultView();
|
createDefaultView();
|
||||||
assertEquals(android.R.color.white, ShadowMockMediaPlayer.sResId);
|
assertThat(mShadowMediaPlayer.getSourceUri().toString())
|
||||||
|
.isEqualTo("android.resource://com.android.setupwizardlib/"
|
||||||
|
+ android.R.color.white);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetVideoResId() {
|
public void testSetVideoResId() {
|
||||||
|
addMediaInfo(android.R.color.black);
|
||||||
|
|
||||||
createDefaultView();
|
createDefaultView();
|
||||||
|
|
||||||
@RawRes int black = android.R.color.black;
|
@RawRes int black = android.R.color.black;
|
||||||
mView.setVideoResource(black);
|
mView.setVideoResource(black);
|
||||||
|
|
||||||
assertEquals(android.R.color.black, ShadowMockMediaPlayer.sResId);
|
mShadowMediaPlayer = (ShadowMediaPlayer) Shadows.shadowOf(mView.mMediaPlayer);
|
||||||
|
|
||||||
|
assertThat(mShadowMediaPlayer.getSourceUri().toString())
|
||||||
|
.isEqualTo("android.resource://com.android.setupwizardlib/"
|
||||||
|
+ android.R.color.black);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createDefaultView() {
|
private void createDefaultView() {
|
||||||
mView = new IllustrationVideoView(
|
mView = new IllustrationVideoView(
|
||||||
application,
|
application,
|
||||||
Robolectric.buildAttributeSet()
|
Robolectric.buildAttributeSet()
|
||||||
// Any resource attribute should work, since the media player is mocked
|
// Any resource attribute should work, since the DataSource is fake
|
||||||
.addAttribute(R.attr.suwVideo, "@android:color/white")
|
.addAttribute(R.attr.suwVideo, "@android:color/white")
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
|
Activity activity = Robolectric.setupActivity(Activity.class);
|
||||||
|
activity.setContentView(mView);
|
||||||
|
setWindowVisible();
|
||||||
|
|
||||||
mView.setSurfaceTexture(mock(SurfaceTexture.class));
|
mView.setSurfaceTexture(mock(SurfaceTexture.class));
|
||||||
mView.onSurfaceTextureAvailable(mSurfaceTexture, 500, 500);
|
mView.onSurfaceTextureAvailable(mSurfaceTexture, 500, 500);
|
||||||
|
mShadowMediaPlayer = (ShadowMediaPlayer) Shadows.shadowOf(mView.mMediaPlayer);
|
||||||
|
mShadowMediaPlayer.setInvalidStateBehavior(InvalidStateBehavior.EMULATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Implements(MediaPlayer.class)
|
private void setWindowVisible() {
|
||||||
public static class ShadowMockMediaPlayer extends ShadowMediaPlayer {
|
Object viewRootImpl = ReflectionHelpers.callInstanceMethod(mView, "getViewRootImpl");
|
||||||
|
ReflectionHelpers.callInstanceMethod(
|
||||||
|
viewRootImpl,
|
||||||
|
"handleAppVisibility",
|
||||||
|
ClassParameter.from(boolean.class, true));
|
||||||
|
assertThat(mView.isAttachedToWindow()).isTrue();
|
||||||
|
assertThat(mView.getWindowVisibility()).isEqualTo(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
private static MediaPlayer sMediaPlayer = mock(MediaPlayer.class);
|
private void addMediaInfo(@RawRes int res) {
|
||||||
private static int sResId;
|
ShadowMediaPlayer.addMediaInfo(
|
||||||
|
DataSource.toDataSource(
|
||||||
public static void reset() {
|
application,
|
||||||
sMediaPlayer = mock(MediaPlayer.class);
|
Uri.parse("android.resource://com.android.setupwizardlib/" + res),
|
||||||
sResId = 0;
|
null),
|
||||||
}
|
new MediaInfo(5000, 1));
|
||||||
|
|
||||||
@Implementation
|
|
||||||
public static MediaPlayer create(Context context, int resId) {
|
|
||||||
sResId = resId;
|
|
||||||
return sMediaPlayer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MediaPlayer getMock() {
|
|
||||||
return sMediaPlayer;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Implements(Surface.class)
|
@Implements(Surface.class)
|
||||||
|
|
Loading…
Reference in a new issue