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
20         .DEFAULT_VALUE;
21 import static com.android.settings.development.TransitionAnimationScalePreferenceController
22         .TRANSITION_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 TransitionAnimationScalePreferenceControllerTest {
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 TransitionAnimationScalePreferenceController 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.transition_animation_scale_values);
76         mListSummaries = resources.getStringArray(R.array.transition_animation_scale_entries);
77         mController = new TransitionAnimationScalePreferenceController(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)
88             .setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, DEFAULT_VALUE);
89     }
90 
91     @Test
onPreferenceChange_option5Selected_shouldSetOption5()92     public void onPreferenceChange_option5Selected_shouldSetOption5() throws RemoteException {
93         mController.onPreferenceChange(mPreference, mListValues[5]);
94 
95         verify(mWindowManager)
96             .setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, Float.valueOf(mListValues[5]));
97     }
98 
99     @Test
updateState_option5Set_shouldUpdatePreferenceToOption5()100     public void updateState_option5Set_shouldUpdatePreferenceToOption5() throws RemoteException {
101         when(mWindowManager.getAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR))
102             .thenReturn(Float.valueOf(mListValues[5]));
103 
104         mController.updateState(mPreference);
105 
106         verify(mPreference).setValue(mListValues[5]);
107         verify(mPreference).setSummary(mListSummaries[5]);
108     }
109 
110     @Test
updateState_option3Set_shouldUpdatePreferenceToOption3()111     public void updateState_option3Set_shouldUpdatePreferenceToOption3() throws RemoteException {
112         when(mWindowManager.getAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR))
113             .thenReturn(Float.valueOf(mListValues[3]));
114 
115         mController.updateState(mPreference);
116 
117         verify(mPreference).setValue(mListValues[3]);
118         verify(mPreference).setSummary(mListSummaries[3]);
119     }
120 
121     @Test
onDeveloperOptionsSwitchDisabled_shouldDisablePreference()122     public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() throws RemoteException {
123         mController.onDeveloperOptionsSwitchDisabled();
124 
125         verify(mWindowManager)
126             .setAnimationScale(TRANSITION_ANIMATION_SCALE_SELECTOR, DEFAULT_VALUE);
127         verify(mPreference).setEnabled(false);
128     }
129 }
130