Add null check for MediaPlayer
In IllustrationVideoView, in case creation of media player failed, check for null and throw WTF instead of crashing with null pointer. Test: ./gradlew test Bug: 74090736 Change-Id: Id63027067cc687516f585503b5218f4467845039
This commit is contained in:
parent
d1d889deee
commit
bf0415d6a8
|
@ -23,9 +23,11 @@ import android.graphics.SurfaceTexture;
|
|||
import android.graphics.drawable.Animatable;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.RawRes;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.Surface;
|
||||
import android.view.TextureView;
|
||||
import android.view.View;
|
||||
|
@ -51,8 +53,11 @@ public class IllustrationVideoView extends TextureView implements Animatable,
|
|||
MediaPlayer.OnSeekCompleteListener,
|
||||
MediaPlayer.OnInfoListener {
|
||||
|
||||
private static final String TAG = "IllustrationVideoView";
|
||||
|
||||
protected float mAspectRatio = 1.0f; // initial guess until we know
|
||||
|
||||
@Nullable // Can be null when media player fails to initialize
|
||||
protected MediaPlayer mMediaPlayer;
|
||||
|
||||
private @RawRes int mVideoResId = 0;
|
||||
|
@ -129,15 +134,20 @@ public class IllustrationVideoView extends TextureView implements Animatable,
|
|||
|
||||
mMediaPlayer = MediaPlayer.create(getContext(), mVideoResId);
|
||||
|
||||
mMediaPlayer.setSurface(mSurface);
|
||||
mMediaPlayer.setOnPreparedListener(this);
|
||||
mMediaPlayer.setOnSeekCompleteListener(this);
|
||||
mMediaPlayer.setOnInfoListener(this);
|
||||
if (mMediaPlayer != null) {
|
||||
mMediaPlayer.setSurface(mSurface);
|
||||
mMediaPlayer.setOnPreparedListener(this);
|
||||
mMediaPlayer.setOnSeekCompleteListener(this);
|
||||
mMediaPlayer.setOnInfoListener(this);
|
||||
|
||||
float aspectRatio = (float) mMediaPlayer.getVideoHeight() / mMediaPlayer.getVideoWidth();
|
||||
if (mAspectRatio != aspectRatio) {
|
||||
mAspectRatio = aspectRatio;
|
||||
requestLayout();
|
||||
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 (getWindowVisibility() == View.VISIBLE) {
|
||||
start();
|
||||
|
|
|
@ -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.util.Log;
|
||||
|
||||
import org.robolectric.annotation.Implementation;
|
||||
import org.robolectric.annotation.Implements;
|
||||
|
||||
@Implements(Log.class)
|
||||
public class ShadowLog extends org.robolectric.shadows.ShadowLog {
|
||||
|
||||
public static boolean sWtfIsFatal = true;
|
||||
|
||||
public static class TerribleFailure extends RuntimeException {
|
||||
|
||||
public TerribleFailure(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public static void wtf(String tag, String msg) {
|
||||
org.robolectric.shadows.ShadowLog.wtf(tag, msg);
|
||||
if (sWtfIsFatal) {
|
||||
throw new TerribleFailure(msg, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public static void wtf(String tag, String msg, Throwable throwable) {
|
||||
org.robolectric.shadows.ShadowLog.wtf(tag, msg, throwable);
|
||||
if (sWtfIsFatal) {
|
||||
throw new TerribleFailure(msg, throwable);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@ package com.android.setupwizardlib.view;
|
|||
|
||||
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;
|
||||
|
@ -33,6 +34,8 @@ import android.view.Surface;
|
|||
|
||||
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.view.IllustrationVideoViewTest.ShadowSurface;
|
||||
|
||||
|
@ -55,6 +58,7 @@ import org.robolectric.util.ReflectionHelpers;
|
|||
@Config(
|
||||
sdk = Config.NEWEST_SDK,
|
||||
shadows = {
|
||||
ShadowLog.class,
|
||||
ShadowMockMediaPlayer.class,
|
||||
ShadowSurface.class
|
||||
})
|
||||
|
@ -75,6 +79,17 @@ public class IllustrationVideoViewTest {
|
|||
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
|
||||
public void testPausedWhenWindowFocusLost() {
|
||||
createDefaultView();
|
||||
|
|
Loading…
Reference in a new issue