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.Mockito.when; 22 23 import android.content.Context; 24 import android.media.MediaPlayer; 25 import android.support.v7.preference.PreferenceViewHolder; 26 import android.view.LayoutInflater; 27 28 import com.android.settings.R; 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RuntimeEnvironment; 37 38 @RunWith(SettingsRobolectricTestRunner.class) 39 public class VideoPreferenceTest { 40 private static final int VIDEO_WIDTH = 100; 41 private static final int VIDEO_HEIGHT = 150; 42 @Mock 43 private MediaPlayer mMediaPlayer; 44 private Context mContext; 45 private VideoPreference mVideoPreference; 46 private PreferenceViewHolder mPreferenceViewHolder; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 52 mContext = RuntimeEnvironment.application; 53 mVideoPreference = new VideoPreference(mContext, null /* attrs */); 54 mVideoPreference.mMediaPlayer = mMediaPlayer; 55 when(mMediaPlayer.getVideoWidth()).thenReturn(VIDEO_WIDTH); 56 when(mMediaPlayer.getVideoHeight()).thenReturn(VIDEO_HEIGHT); 57 58 mPreferenceViewHolder = PreferenceViewHolder.createInstanceForTests( 59 LayoutInflater.from(mContext).inflate(R.layout.video_preference, null)); 60 } 61 62 @Test onBindViewHolder_hasCorrectRatio()63 public void onBindViewHolder_hasCorrectRatio() { 64 mVideoPreference.mAnimationAvailable = true; 65 66 mVideoPreference.updateAspectRatio(); 67 mVideoPreference.onBindViewHolder(mPreferenceViewHolder); 68 69 final AspectRatioFrameLayout layout = 70 (AspectRatioFrameLayout) mPreferenceViewHolder.findViewById(R.id.video_container); 71 assertThat(layout.mAspectRatio).isWithin(0.01f).of(VIDEO_WIDTH / (float) VIDEO_HEIGHT); 72 } 73 } 74