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.settings.development; 18 19 import static com.android.settings.development.TransitionAnimationScalePreferenceController.DEFAULT_VALUE; 20 import static com.android.settings.development.TransitionAnimationScalePreferenceController.TRANSITION_ANIMATION_SCALE_SELECTOR; 21 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.content.res.Resources; 27 import android.os.RemoteException; 28 import android.view.IWindowManager; 29 30 import androidx.preference.ListPreference; 31 import androidx.preference.PreferenceScreen; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.util.ReflectionHelpers; 41 42 @RunWith(RobolectricTestRunner.class) 43 public class TransitionAnimationScalePreferenceControllerTest { 44 45 @Mock 46 private ListPreference mPreference; 47 @Mock 48 private PreferenceScreen mScreen; 49 @Mock 50 private IWindowManager mWindowManager; 51 52 /** 53 * 0: Animation off 54 * 1: Animation scale .5x 55 * 2: Animation scale 1x 56 * 3: Animation scale 1.5x 57 * 4: Animation scale 2x 58 * 5: Animation scale 5x 59 * 6: Animation scale 10x 60 */ 61 private String[] mListValues; 62 private String[] mListSummaries; 63 private Context mContext; 64 private TransitionAnimationScalePreferenceController mController; 65 66 @Before setup()67 public void setup() { 68 MockitoAnnotations.initMocks(this); 69 mContext = RuntimeEnvironment.application; 70 final Resources resources = mContext.getResources(); 71 mListValues = resources.getStringArray( 72 com.android.settingslib.R.array.transition_animation_scale_values); 73 mListSummaries = resources.getStringArray( 74 com.android.settingslib.R.array.transition_animation_scale_entries); 75 mController = new TransitionAnimationScalePreferenceController(mContext); 76 ReflectionHelpers.setField(mController, "mWindowManager", mWindowManager); 77 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 78 mController.displayPreference(mScreen); 79 } 80 81 @Test onPreferenceChange_noValueSet_shouldSetDefault()82 public void onPreferenceChange_noValueSet_shouldSetDefault() throws RemoteException { 83 mController.onPreferenceChange(mPreference, null /* new value */); 84 85 verify(mWindowManager) 86 .setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, DEFAULT_VALUE); 87 } 88 89 @Test onPreferenceChange_option5Selected_shouldSetOption5()90 public void onPreferenceChange_option5Selected_shouldSetOption5() throws RemoteException { 91 mController.onPreferenceChange(mPreference, mListValues[5]); 92 93 verify(mWindowManager) 94 .setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, Float.valueOf(mListValues[5])); 95 } 96 97 @Test updateState_option5Set_shouldUpdatePreferenceToOption5()98 public void updateState_option5Set_shouldUpdatePreferenceToOption5() throws RemoteException { 99 when(mWindowManager.getAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR)) 100 .thenReturn(Float.valueOf(mListValues[5])); 101 102 mController.updateState(mPreference); 103 104 verify(mPreference).setValue(mListValues[5]); 105 verify(mPreference).setSummary(mListSummaries[5]); 106 } 107 108 @Test updateState_option3Set_shouldUpdatePreferenceToOption3()109 public void updateState_option3Set_shouldUpdatePreferenceToOption3() throws RemoteException { 110 when(mWindowManager.getAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR)) 111 .thenReturn(Float.valueOf(mListValues[3])); 112 113 mController.updateState(mPreference); 114 115 verify(mPreference).setValue(mListValues[3]); 116 verify(mPreference).setSummary(mListSummaries[3]); 117 } 118 119 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()120 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() throws RemoteException { 121 mController.onDeveloperOptionsSwitchDisabled(); 122 123 verify(mWindowManager) 124 .setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, DEFAULT_VALUE); 125 verify(mPreference).setEnabled(false); 126 } 127 } 128