From 51e01202a497650c13df3404044012294e3f5116 Mon Sep 17 00:00:00 2001 From: Maurice Lam Date: Mon, 21 May 2018 13:01:58 -0700 Subject: [PATCH] Use prepareAsync for media player To avoid blocking the main thread for too long. Test: ./gradlew test Bug: 78360819 Change-Id: Idfbfcc86e9fec61858ee1e623518aadb5f06deaf --- .../view/IllustrationVideoView.java | 90 +++++++++----- .../shadow/ShadowMediaPlayer.java | 51 ++++++++ .../view/IllustrationVideoViewTest.java | 110 +++++++++--------- 3 files changed, 165 insertions(+), 86 deletions(-) create mode 100644 library/test/robotest/src/com/android/setupwizardlib/shadow/ShadowMediaPlayer.java diff --git a/library/main/src/com/android/setupwizardlib/view/IllustrationVideoView.java b/library/main/src/com/android/setupwizardlib/view/IllustrationVideoView.java index 53149ea..e5688b3 100644 --- a/library/main/src/com/android/setupwizardlib/view/IllustrationVideoView.java +++ b/library/main/src/com/android/setupwizardlib/view/IllustrationVideoView.java @@ -22,11 +22,17 @@ import android.content.res.TypedArray; import android.graphics.SurfaceTexture; import android.graphics.drawable.Animatable; 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.util.AttributeSet; import android.util.Log; import android.view.Surface; import android.view.TextureView; +import android.view.TextureView.SurfaceTextureListener; import android.view.View; import androidx.annotation.Nullable; @@ -35,6 +41,8 @@ import androidx.annotation.VisibleForTesting; 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 * animated illustrations. @@ -49,10 +57,11 @@ import com.android.setupwizardlib.R; */ @TargetApi(VERSION_CODES.ICE_CREAM_SANDWICH) public class IllustrationVideoView extends TextureView implements Animatable, - TextureView.SurfaceTextureListener, - MediaPlayer.OnPreparedListener, - MediaPlayer.OnSeekCompleteListener, - MediaPlayer.OnInfoListener { + SurfaceTextureListener, + OnPreparedListener, + OnSeekCompleteListener, + OnInfoListener, + OnErrorListener { private static final String TAG = "IllustrationVideoView"; @@ -65,7 +74,7 @@ public class IllustrationVideoView extends TextureView implements Animatable, @VisibleForTesting Surface mSurface; - protected int mWindowVisibility; + private boolean mPrepared; public IllustrationVideoView(Context context, AttributeSet attrs) { super(context, attrs); @@ -135,25 +144,25 @@ public class IllustrationVideoView extends TextureView implements Animatable, return; } - mMediaPlayer = MediaPlayer.create(getContext(), mVideoResId); + mMediaPlayer = new MediaPlayer(); - if (mMediaPlayer != null) { - mMediaPlayer.setSurface(mSurface); - mMediaPlayer.setOnPreparedListener(this); - mMediaPlayer.setOnSeekCompleteListener(this); - mMediaPlayer.setOnInfoListener(this); + mMediaPlayer.setSurface(mSurface); + mMediaPlayer.setOnPreparedListener(this); + mMediaPlayer.setOnSeekCompleteListener(this); + mMediaPlayer.setOnInfoListener(this); + mMediaPlayer.setOnErrorListener(this); - float aspectRatio = - (float) mMediaPlayer.getVideoHeight() / mMediaPlayer.getVideoWidth(); - if (mAspectRatio != aspectRatio) { - mAspectRatio = aspectRatio; - requestLayout(); - } - } else { - Log.wtf(TAG, "Unable to initialize media player for video view"); - } - if (mWindowVisibility == View.VISIBLE) { - start(); + setVideoResourceInternal(mVideoResId); + } + + private void setVideoResourceInternal(@RawRes int videoRes) { + Uri uri = + Uri.parse("android.resource://" + getContext().getPackageName() + "/" + videoRes); + try { + mMediaPlayer.setDataSource(getContext(), uri); + mMediaPlayer.prepareAsync(); + } catch (IOException e) { + Log.wtf(TAG, "Unable to set data source", e); } } @@ -173,7 +182,6 @@ public class IllustrationVideoView extends TextureView implements Animatable, @Override protected void onWindowVisibilityChanged(int visibility) { super.onWindowVisibilityChanged(visibility); - mWindowVisibility = visibility; if (visibility == View.VISIBLE) { reattach(); } else { @@ -195,9 +203,9 @@ public class IllustrationVideoView extends TextureView implements Animatable, */ public void release() { if (mMediaPlayer != null) { - mMediaPlayer.stop(); mMediaPlayer.release(); mMediaPlayer = null; + mPrepared = false; } if (mSurface != null) { mSurface.release(); @@ -212,14 +220,14 @@ public class IllustrationVideoView extends TextureView implements Animatable, } private void initVideo() { - if (mWindowVisibility != View.VISIBLE) { + if (getWindowVisibility() != View.VISIBLE) { return; } createSurface(); if (mSurface != null) { createMediaPlayer(); } 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 public void start() { - if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) { + if (mPrepared && mMediaPlayer != null && !mMediaPlayer.isPlaying()) { mMediaPlayer.start(); } } @Override public void stop() { - if (mMediaPlayer != null) { + if (mPrepared && mMediaPlayer != null) { mMediaPlayer.pause(); } } @@ -284,15 +292,39 @@ public class IllustrationVideoView extends TextureView implements Animatable, @Override public void onPrepared(MediaPlayer mp) { + mPrepared = true; 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 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() { 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; + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/shadow/ShadowMediaPlayer.java b/library/test/robotest/src/com/android/setupwizardlib/shadow/ShadowMediaPlayer.java new file mode 100644 index 0000000..2ef2b7d --- /dev/null +++ b/library/test/robotest/src/com/android/setupwizardlib/shadow/ShadowMediaPlayer.java @@ -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 headers, + @Nullable List cookies) + throws IOException { + setDataSource(context, uri, headers); + } + + @Implementation + public void seekTo(long msec, int mode) { + seekTo((int) msec); + } +} diff --git a/library/test/robotest/src/com/android/setupwizardlib/view/IllustrationVideoViewTest.java b/library/test/robotest/src/com/android/setupwizardlib/view/IllustrationVideoViewTest.java index 0e0e99c..4d06707 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/view/IllustrationVideoViewTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/view/IllustrationVideoViewTest.java @@ -18,18 +18,14 @@ package com.android.setupwizardlib.view; 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.fail; 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 android.annotation.TargetApi; -import android.content.Context; +import android.app.Activity; import android.graphics.SurfaceTexture; -import android.media.MediaPlayer; +import android.net.Uri; import android.os.Build.VERSION_CODES; import android.view.Surface; import android.view.View; @@ -39,31 +35,33 @@ import androidx.annotation.RawRes; import com.android.setupwizardlib.R; import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; import com.android.setupwizardlib.shadow.ShadowLog; -import com.android.setupwizardlib.shadow.ShadowLog.TerribleFailure; -import com.android.setupwizardlib.view.IllustrationVideoViewTest.ShadowMockMediaPlayer; +import com.android.setupwizardlib.shadow.ShadowMediaPlayer; import com.android.setupwizardlib.view.IllustrationVideoViewTest.ShadowSurface; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.Robolectric; +import org.robolectric.Shadows; import org.robolectric.annotation.Config; -import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; import org.robolectric.annotation.RealObject; 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.ClassParameter; @RunWith(SuwLibRobolectricTestRunner.class) @Config( sdk = Config.NEWEST_SDK, shadows = { ShadowLog.class, - ShadowMockMediaPlayer.class, + ShadowMediaPlayer.class, ShadowSurface.class }) public class IllustrationVideoViewTest { @@ -73,33 +71,21 @@ public class IllustrationVideoViewTest { private IllustrationVideoView mView; + private ShadowMediaPlayer mShadowMediaPlayer; + @Before public void setUp() { MockitoAnnotations.initMocks(this); - } - - @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 - } + addMediaInfo(android.R.color.white); } @Test public void onVisibilityChanged_notVisible_shouldRelease() { + createDefaultView(); mView.onWindowVisibilityChanged(View.GONE); - verify(ShadowMockMediaPlayer.sMediaPlayer).release(); + assertThat(mShadowMediaPlayer.getState()).isEqualTo(State.END); assertThat(mView.mSurface).isNull(); assertThat(mView.mMediaPlayer).isNull(); } @@ -121,24 +107,22 @@ public class IllustrationVideoViewTest { @Test public void testPausedWhenWindowFocusLost() { createDefaultView(); + Robolectric.flushForegroundThreadScheduler(); mView.start(); assertNotNull(mView.mMediaPlayer); assertNotNull(mView.mSurface); mView.onWindowFocusChanged(false); - verify(ShadowMockMediaPlayer.getMock()).pause(); + assertThat(mShadowMediaPlayer.getState()).isEqualTo(State.PAUSED); } @Test public void testStartedWhenWindowFocusRegained() { testPausedWhenWindowFocusLost(); - // Clear verifications for calls in the other test - reset(ShadowMockMediaPlayer.getMock()); - mView.onWindowFocusChanged(true); - verify(ShadowMockMediaPlayer.getMock()).start(); + assertThat(mShadowMediaPlayer.getState()).isEqualTo(State.STARTED); } @Test @@ -150,56 +134,68 @@ public class IllustrationVideoViewTest { assertNotNull(mView.mSurface); mView.onSurfaceTextureDestroyed(mSurfaceTexture); - verify(ShadowMockMediaPlayer.getMock()).release(); + assertThat(mShadowMediaPlayer.getState()).isEqualTo(State.END); } @Test public void testXmlSetVideoResId() { createDefaultView(); - assertEquals(android.R.color.white, ShadowMockMediaPlayer.sResId); + assertThat(mShadowMediaPlayer.getSourceUri().toString()) + .isEqualTo("android.resource://com.android.setupwizardlib/" + + android.R.color.white); } @Test public void testSetVideoResId() { + addMediaInfo(android.R.color.black); + createDefaultView(); @RawRes int black = android.R.color.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() { mView = new IllustrationVideoView( application, 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") .build()); + + Activity activity = Robolectric.setupActivity(Activity.class); + activity.setContentView(mView); + setWindowVisible(); + mView.setSurfaceTexture(mock(SurfaceTexture.class)); mView.onSurfaceTextureAvailable(mSurfaceTexture, 500, 500); + mShadowMediaPlayer = (ShadowMediaPlayer) Shadows.shadowOf(mView.mMediaPlayer); + mShadowMediaPlayer.setInvalidStateBehavior(InvalidStateBehavior.EMULATE); } - @Implements(MediaPlayer.class) - public static class ShadowMockMediaPlayer extends ShadowMediaPlayer { + private void setWindowVisible() { + 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 static int sResId; - - public static void reset() { - sMediaPlayer = mock(MediaPlayer.class); - sResId = 0; - } - - @Implementation - public static MediaPlayer create(Context context, int resId) { - sResId = resId; - return sMediaPlayer; - } - - public static MediaPlayer getMock() { - return sMediaPlayer; - } + private void addMediaInfo(@RawRes int res) { + ShadowMediaPlayer.addMediaInfo( + DataSource.toDataSource( + application, + Uri.parse("android.resource://com.android.setupwizardlib/" + res), + null), + new MediaInfo(5000, 1)); } @Implements(Surface.class)