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