1 /* 2 * Copyright (C) 2018 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.settings.widget; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.eq; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.graphics.SurfaceTexture; 30 import android.os.Bundle; 31 import android.view.LayoutInflater; 32 import android.view.TextureView; 33 import android.view.View; 34 import android.widget.ImageView; 35 36 import androidx.preference.PreferenceFragmentCompat; 37 import androidx.preference.PreferenceViewHolder; 38 39 import com.android.settings.R; 40 import com.android.settings.testutils.shadow.ShadowSettingsMediaPlayer; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.RuntimeEnvironment; 49 import org.robolectric.annotation.Config; 50 import org.robolectric.shadows.androidx.fragment.FragmentController; 51 52 @RunWith(RobolectricTestRunner.class) 53 @Config(shadows = ShadowSettingsMediaPlayer.class) 54 public class VideoPreferenceTest { 55 private static final int VIDEO_WIDTH = 100; 56 private static final int VIDEO_HEIGHT = 150; 57 58 private VideoPreference.AnimationController mAnimationController; 59 @Mock 60 private ImageView fakePreview; 61 @Mock 62 private ImageView fakePlayButton; 63 64 private Context mContext; 65 private VideoPreference mVideoPreference; 66 private PreferenceViewHolder mPreferenceViewHolder; 67 68 @Before setUp()69 public void setUp() { 70 MockitoAnnotations.initMocks(this); 71 72 mContext = RuntimeEnvironment.application; 73 mAnimationController = spy( 74 new MediaAnimationController(mContext, R.raw.sample_video)); 75 mVideoPreference = new VideoPreference(mContext, null /* attrs */); 76 mVideoPreference.mAnimationController = mAnimationController; 77 when(mAnimationController.getVideoWidth()).thenReturn(VIDEO_WIDTH); 78 when(mAnimationController.getVideoHeight()).thenReturn(VIDEO_HEIGHT); 79 80 mPreferenceViewHolder = PreferenceViewHolder.createInstanceForTests( 81 LayoutInflater.from(mContext).inflate(R.layout.video_preference, null)); 82 } 83 84 @Test onBindViewHolder_hasCorrectRatio()85 public void onBindViewHolder_hasCorrectRatio() { 86 mVideoPreference.mAnimationAvailable = true; 87 88 mVideoPreference.updateAspectRatio(); 89 mVideoPreference.onBindViewHolder(mPreferenceViewHolder); 90 91 final AspectRatioFrameLayout layout = 92 (AspectRatioFrameLayout) mPreferenceViewHolder.findViewById(R.id.video_container); 93 assertThat(layout.mAspectRatio).isWithin(0.01f).of(VIDEO_WIDTH / (float) VIDEO_HEIGHT); 94 } 95 96 @Test onSurfaceTextureUpdated_viewInvisible_shouldNotStartPlayingVideo()97 public void onSurfaceTextureUpdated_viewInvisible_shouldNotStartPlayingVideo() { 98 final TextureView video = 99 (TextureView) mPreferenceViewHolder.findViewById(R.id.video_texture_view); 100 mVideoPreference.mAnimationAvailable = true; 101 mVideoPreference.onBindViewHolder(mPreferenceViewHolder); 102 mAnimationController.attachView(video, fakePreview, fakePlayButton); 103 when(mAnimationController.isPlaying()).thenReturn(false); 104 final TextureView.SurfaceTextureListener listener = video.getSurfaceTextureListener(); 105 106 mVideoPreference.onViewInvisible(); 107 listener.onSurfaceTextureUpdated(mock(SurfaceTexture.class)); 108 109 verify(mAnimationController, never()).start(); 110 } 111 112 @Test onViewInvisible_shouldReleaseMediaplayer()113 public void onViewInvisible_shouldReleaseMediaplayer() { 114 mVideoPreference.onViewInvisible(); 115 116 verify(mAnimationController).release(); 117 } 118 119 @Test updateViewStates_paused_updatesViews()120 public void updateViewStates_paused_updatesViews() { 121 mAnimationController.start(); 122 123 mVideoPreference.mAnimationController.attachView(new TextureView(mContext), fakePreview, 124 fakePlayButton); 125 126 verify(fakePlayButton).setVisibility(eq(View.VISIBLE)); 127 verify(fakePreview).setVisibility(eq(View.VISIBLE)); 128 assertThat(mAnimationController.isPlaying()).isFalse(); 129 } 130 131 @Test updateViewStates_playing_updatesViews()132 public void updateViewStates_playing_updatesViews() { 133 mAnimationController.pause(); 134 135 mVideoPreference.mAnimationController.attachView(new TextureView(mContext), fakePreview, 136 fakePlayButton); 137 138 verify(fakePlayButton).setVisibility(eq(View.GONE)); 139 verify(fakePreview).setVisibility(eq(View.GONE)); 140 assertThat(mAnimationController.isPlaying()).isTrue(); 141 } 142 143 @Test 144 @Config(qualifiers = "mcc999") onViewVisible_createAnimationController()145 public void onViewVisible_createAnimationController() { 146 final PreferenceFragmentCompat fragment = FragmentController.of( 147 new VideoPreferenceTest.TestFragment(), 148 new Bundle()) 149 .create() 150 .start() 151 .resume() 152 .get(); 153 154 final VideoPreference vp1 = fragment.findPreference("video1"); 155 final VideoPreference vp2 = fragment.findPreference("video2"); 156 157 assertThat(vp1.mAnimationController instanceof MediaAnimationController).isTrue(); 158 assertThat(vp2.mAnimationController instanceof VectorAnimationController).isTrue(); 159 } 160 161 public static class TestFragment extends PreferenceFragmentCompat { 162 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)163 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 164 addPreferencesFromResource(R.xml.video_preference); 165 } 166 } 167 } 168