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 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.res.Resources; 26 import android.os.RemoteException; 27 import android.support.v7.preference.ListPreference; 28 import android.support.v7.preference.PreferenceScreen; 29 import android.view.IWindowManager; 30 31 import com.android.settings.R; 32 import com.android.settings.testutils.SettingsRobolectricTestRunner; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.util.ReflectionHelpers; 41 42 @RunWith(SettingsRobolectricTestRunner.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(R.array.transition_animation_scale_values); 72 mListSummaries = resources.getStringArray(R.array.transition_animation_scale_entries); 73 mController = new TransitionAnimationScalePreferenceController(mContext); 74 ReflectionHelpers.setField(mController, "mWindowManager", mWindowManager); 75 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 76 mController.displayPreference(mScreen); 77 } 78 79 @Test onPreferenceChange_noValueSet_shouldSetDefault()80 public void onPreferenceChange_noValueSet_shouldSetDefault() throws RemoteException { 81 mController.onPreferenceChange(mPreference, null /* new value */); 82 83 verify(mWindowManager) 84 .setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, DEFAULT_VALUE); 85 } 86 87 @Test onPreferenceChange_option5Selected_shouldSetOption5()88 public void onPreferenceChange_option5Selected_shouldSetOption5() throws RemoteException { 89 mController.onPreferenceChange(mPreference, mListValues[5]); 90 91 verify(mWindowManager) 92 .setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, Float.valueOf(mListValues[5])); 93 } 94 95 @Test updateState_option5Set_shouldUpdatePreferenceToOption5()96 public void updateState_option5Set_shouldUpdatePreferenceToOption5() throws RemoteException { 97 when(mWindowManager.getAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR)) 98 .thenReturn(Float.valueOf(mListValues[5])); 99 100 mController.updateState(mPreference); 101 102 verify(mPreference).setValue(mListValues[5]); 103 verify(mPreference).setSummary(mListSummaries[5]); 104 } 105 106 @Test updateState_option3Set_shouldUpdatePreferenceToOption3()107 public void updateState_option3Set_shouldUpdatePreferenceToOption3() throws RemoteException { 108 when(mWindowManager.getAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR)) 109 .thenReturn(Float.valueOf(mListValues[3])); 110 111 mController.updateState(mPreference); 112 113 verify(mPreference).setValue(mListValues[3]); 114 verify(mPreference).setSummary(mListSummaries[3]); 115 } 116 117 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()118 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() throws RemoteException { 119 mController.onDeveloperOptionsSwitchDisabled(); 120 121 verify(mWindowManager) 122 .setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, DEFAULT_VALUE); 123 verify(mPreference).setEnabled(false); 124 } 125 } 126