diff options
author | Maurice Lam <yukl@google.com> | 2018-05-21 13:01:58 -0700 |
---|---|---|
committer | Maurice Lam <yukl@google.com> | 2018-05-24 17:05:03 -0700 |
commit | 51e01202a497650c13df3404044012294e3f5116 (patch) | |
tree | 109da8b40476473d56b58c024e11c6fe77323886 /library/test/robotest | |
parent | 6ffb792f4620dcab24f71b24536c1c2d90996a0e (diff) | |
download | setupwizard-51e01202a497650c13df3404044012294e3f5116.tar.gz |
Use prepareAsync for media player
To avoid blocking the main thread for too long.
Test: ./gradlew test
Bug: 78360819
Change-Id: Idfbfcc86e9fec61858ee1e623518aadb5f06deaf
Diffstat (limited to 'library/test/robotest')
-rw-r--r-- | library/test/robotest/src/com/android/setupwizardlib/shadow/ShadowMediaPlayer.java | 51 | ||||
-rw-r--r-- | library/test/robotest/src/com/android/setupwizardlib/view/IllustrationVideoViewTest.java | 110 |
2 files changed, 104 insertions, 57 deletions
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<String, String> headers, + @Nullable List<HttpCookie> 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 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; - } + 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); + } - 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) |