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.WindowAnimationScalePreferenceController.DEFAULT_VALUE; 20 import static com.android.settings.development.WindowAnimationScalePreferenceController.WINDOW_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 WindowAnimationScalePreferenceControllerTest { 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 WindowAnimationScalePreferenceController 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.window_animation_scale_values); 72 mListSummaries = resources.getStringArray(R.array.window_animation_scale_entries); 73 mController = new WindowAnimationScalePreferenceController(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).setAnimationScale(WINDOW_ANIMATION_SCALE_SELECTOR, DEFAULT_VALUE); 84 } 85 86 @Test onPreferenceChange_option5Selected_shouldSetOption5()87 public void onPreferenceChange_option5Selected_shouldSetOption5() throws RemoteException { 88 mController.onPreferenceChange(mPreference, mListValues[5]); 89 90 verify(mWindowManager).setAnimationScale(WINDOW_ANIMATION_SCALE_SELECTOR, 91 Float.valueOf(mListValues[5])); 92 } 93 94 @Test updateState_option5Set_shouldUpdatePreferenceToOption5()95 public void updateState_option5Set_shouldUpdatePreferenceToOption5() throws RemoteException { 96 when(mWindowManager.getAnimationScale(WINDOW_ANIMATION_SCALE_SELECTOR)) 97 .thenReturn(Float.valueOf(mListValues[5])); 98 99 mController.updateState(mPreference); 100 101 verify(mPreference).setValue(mListValues[5]); 102 verify(mPreference).setSummary(mListSummaries[5]); 103 } 104 105 @Test updateState_option3Set_shouldUpdatePreferenceToOption3()106 public void updateState_option3Set_shouldUpdatePreferenceToOption3() throws RemoteException { 107 when(mWindowManager.getAnimationScale(WINDOW_ANIMATION_SCALE_SELECTOR)) 108 .thenReturn(Float.valueOf(mListValues[3])); 109 110 mController.updateState(mPreference); 111 112 verify(mPreference).setValue(mListValues[3]); 113 verify(mPreference).setSummary(mListSummaries[3]); 114 } 115 116 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()117 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() throws RemoteException { 118 mController.onDeveloperOptionsSwitchDisabled(); 119 120 verify(mWindowManager).setAnimationScale(WINDOW_ANIMATION_SCALE_SELECTOR, DEFAULT_VALUE); 121 verify(mPreference).setEnabled(false); 122 } 123 } 124