1 /* 2 * Copyright (C) 2017 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.setupwizardlib.view; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.reset; 23 import static org.mockito.Mockito.verify; 24 import static org.robolectric.RuntimeEnvironment.application; 25 26 import android.annotation.TargetApi; 27 import android.content.Context; 28 import android.graphics.SurfaceTexture; 29 import android.media.MediaPlayer; 30 import android.os.Build.VERSION_CODES; 31 import android.support.annotation.RawRes; 32 import android.view.Surface; 33 34 import com.android.setupwizardlib.BuildConfig; 35 import com.android.setupwizardlib.R; 36 import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; 37 import com.android.setupwizardlib.view.IllustrationVideoViewTest.ShadowMockMediaPlayer; 38 import com.android.setupwizardlib.view.IllustrationVideoViewTest.ShadowSurface; 39 40 import org.junit.After; 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.Robolectric; 47 import org.robolectric.annotation.Config; 48 import org.robolectric.annotation.Implementation; 49 import org.robolectric.annotation.Implements; 50 import org.robolectric.annotation.RealObject; 51 import org.robolectric.internal.Shadow; 52 import org.robolectric.shadows.ShadowMediaPlayer; 53 import org.robolectric.util.ReflectionHelpers; 54 55 @RunWith(SuwLibRobolectricTestRunner.class) 56 @Config( 57 constants = BuildConfig.class, 58 sdk = Config.NEWEST_SDK, 59 shadows = { 60 ShadowMockMediaPlayer.class, 61 ShadowSurface.class 62 }) 63 public class IllustrationVideoViewTest { 64 65 @Mock 66 private SurfaceTexture mSurfaceTexture; 67 68 private IllustrationVideoView mView; 69 70 @Before 71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 } 74 75 @After 76 public void tearDown() { 77 ShadowMockMediaPlayer.reset(); 78 } 79 80 @Test 81 public void testPausedWhenWindowFocusLost() { 82 createDefaultView(); 83 mView.start(); 84 85 assertNotNull(mView.mMediaPlayer); 86 assertNotNull(mView.mSurface); 87 88 mView.onWindowFocusChanged(false); 89 verify(ShadowMockMediaPlayer.getMock()).pause(); 90 } 91 92 @Test 93 public void testStartedWhenWindowFocusRegained() { 94 testPausedWhenWindowFocusLost(); 95 96 // Clear verifications for calls in the other test 97 reset(ShadowMockMediaPlayer.getMock()); 98 99 mView.onWindowFocusChanged(true); 100 verify(ShadowMockMediaPlayer.getMock()).start(); 101 } 102 103 @Test 104 public void testSurfaceReleasedWhenTextureDestroyed() { 105 createDefaultView(); 106 mView.start(); 107 108 assertNotNull(mView.mMediaPlayer); 109 assertNotNull(mView.mSurface); 110 111 mView.onSurfaceTextureDestroyed(mSurfaceTexture); 112 verify(ShadowMockMediaPlayer.getMock()).release(); 113 } 114 115 @Test 116 public void testXmlSetVideoResId() { 117 createDefaultView(); 118 assertEquals(android.R.color.white, ShadowMockMediaPlayer.sResId); 119 } 120 121 @Test 122 public void testSetVideoResId() { 123 createDefaultView(); 124 125 @RawRes int black = android.R.color.black; 126 mView.setVideoResource(black); 127 128 assertEquals(android.R.color.black, ShadowMockMediaPlayer.sResId); 129 } 130 131 private void createDefaultView() { 132 mView = new IllustrationVideoView( 133 application, 134 Robolectric.buildAttributeSet() 135 // Any resource attribute should work, since the media player is mocked 136 .addAttribute(R.attr.suwVideo, "@android:color/white") 137 .build()); 138 mView.onSurfaceTextureAvailable(mSurfaceTexture, 500, 500); 139 } 140 141 @Implements(MediaPlayer.class) 142 public static class ShadowMockMediaPlayer extends ShadowMediaPlayer { 143 144 private static MediaPlayer sMediaPlayer = mock(MediaPlayer.class); 145 private static int sResId; 146 147 public static void reset() { 148 sMediaPlayer = mock(MediaPlayer.class); 149 sResId = 0; 150 } 151 152 @Implementation 153 public static MediaPlayer create(Context context, int resId) { 154 sResId = resId; 155 return sMediaPlayer; 156 } 157 158 public static MediaPlayer getMock() { 159 return sMediaPlayer; 160 } 161 } 162 163 @Implements(Surface.class) 164 @TargetApi(VERSION_CODES.HONEYCOMB) 165 public static class ShadowSurface extends org.robolectric.shadows.ShadowSurface { 166 167 @RealObject 168 private Surface mRealSurface; 169 170 public void __constructor__(SurfaceTexture surfaceTexture) { 171 // Call the constructor on the real object, so that critical fields such as mLock is 172 // initialized properly. 173 Shadow.invokeConstructor(Surface.class, mRealSurface, 174 ReflectionHelpers.ClassParameter.from(SurfaceTexture.class, surfaceTexture)); 175 super.__constructor__(surfaceTexture); 176 } 177 } 178 } 179