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