1 /*
2  * Copyright (C) 2020 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.display;
18 
19 import static android.hardware.display.DisplayManager.DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED;
20 
21 import static com.android.internal.display.RefreshRateSettingsUtils.DEFAULT_REFRESH_RATE;
22 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
23 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.Mockito.when;
28 
29 import android.hardware.display.DisplayManager;
30 import android.platform.test.annotations.RequiresFlagsDisabled;
31 import android.platform.test.annotations.RequiresFlagsEnabled;
32 import android.platform.test.flag.junit.CheckFlagsRule;
33 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
34 import android.provider.Settings;
35 import android.testing.TestableContext;
36 import android.view.Display;
37 
38 import androidx.preference.SwitchPreference;
39 import androidx.test.platform.app.InstrumentationRegistry;
40 
41 import com.android.server.display.feature.flags.Flags;
42 
43 import org.junit.Before;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.robolectric.RobolectricTestRunner;
50 import org.robolectric.RuntimeEnvironment;
51 import org.robolectric.annotation.Config;
52 
53 @RunWith(RobolectricTestRunner.class)
54 public class PeakRefreshRatePreferenceControllerTest {
55 
56     private PeakRefreshRatePreferenceController mController;
57     private SwitchPreference mPreference;
58 
59     @Mock
60     private PeakRefreshRatePreferenceController.DeviceConfigDisplaySettings
61             mDeviceConfigDisplaySettings;
62     @Mock
63     private DisplayManager mDisplayManagerMock;
64     @Mock
65     private Display mDisplayMock;
66     @Mock
67     private Display mDisplayMock2;
68 
69     @Rule
70     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
71     @Rule
72     public final TestableContext mContext = new TestableContext(
73             InstrumentationRegistry.getInstrumentation().getContext());
74 
75     @Before
setUp()76     public void setUp() {
77         MockitoAnnotations.initMocks(this);
78         mContext.addMockSystemService(DisplayManager.class, mDisplayManagerMock);
79 
80         Display.Mode[] modes = new Display.Mode[]{
81                 new Display.Mode(/* modeId= */ 0, /* width= */ 800, /* height= */ 600,
82                         /* refreshRate= */ 60),
83                 new Display.Mode(/* modeId= */ 0, /* width= */ 800, /* height= */ 600,
84                         /* refreshRate= */ 120),
85                 new Display.Mode(/* modeId= */ 0, /* width= */ 800, /* height= */ 600,
86                         /* refreshRate= */ 90)
87         };
88         when(mDisplayManagerMock.getDisplay(Display.DEFAULT_DISPLAY)).thenReturn(mDisplayMock);
89         when(mDisplayMock.getSupportedModes()).thenReturn(modes);
90 
91         Display.Mode[] modes2 = new Display.Mode[]{
92                 new Display.Mode(/* modeId= */ 0, /* width= */ 800, /* height= */ 600,
93                         /* refreshRate= */ 70),
94                 new Display.Mode(/* modeId= */ 0, /* width= */ 800, /* height= */ 600,
95                         /* refreshRate= */ 130),
96                 new Display.Mode(/* modeId= */ 0, /* width= */ 800, /* height= */ 600,
97                         /* refreshRate= */ 80)
98         };
99         when(mDisplayManagerMock.getDisplay(Display.DEFAULT_DISPLAY + 1)).thenReturn(mDisplayMock2);
100         when(mDisplayMock2.getSupportedModes()).thenReturn(modes2);
101 
102         when(mDisplayManagerMock.getDisplays(DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED))
103                 .thenReturn(new Display[]{ mDisplayMock, mDisplayMock2 });
104 
105         mController = new PeakRefreshRatePreferenceController(mContext, "key");
106         mController.injectDeviceConfigDisplaySettings(mDeviceConfigDisplaySettings);
107         mPreference = new SwitchPreference(RuntimeEnvironment.application);
108     }
109 
110     @Test
111     @Config(qualifiers = "mcc999")
getAvailabilityStatus_withConfigNoShow_returnUnsupported()112     public void getAvailabilityStatus_withConfigNoShow_returnUnsupported() {
113         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
114     }
115 
116     @Test
getAvailabilityStatus_refreshRateLargerThanDefault_returnAvailable()117     public void getAvailabilityStatus_refreshRateLargerThanDefault_returnAvailable() {
118         mController.mPeakRefreshRate = DEFAULT_REFRESH_RATE + 1;
119 
120         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
121     }
122 
123     @Test
getAvailabilityStatus_refreshRateEqualToDefault_returnUnsupported()124     public void getAvailabilityStatus_refreshRateEqualToDefault_returnUnsupported() {
125         mController.mPeakRefreshRate = DEFAULT_REFRESH_RATE;
126 
127         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
128     }
129 
130     @Test
131     @RequiresFlagsDisabled(Flags.FLAG_BACK_UP_SMOOTH_DISPLAY_AND_FORCE_PEAK_REFRESH_RATE)
setChecked_enableSmoothDisplay_featureFlagOff()132     public void setChecked_enableSmoothDisplay_featureFlagOff() {
133         mController.mPeakRefreshRate = 88f;
134         mController.setChecked(true);
135 
136         assertThat(Settings.System.getFloat(mContext.getContentResolver(),
137                 Settings.System.PEAK_REFRESH_RATE, DEFAULT_REFRESH_RATE))
138                 .isEqualTo(88f);
139     }
140 
141     @Test
142     @RequiresFlagsEnabled(Flags.FLAG_BACK_UP_SMOOTH_DISPLAY_AND_FORCE_PEAK_REFRESH_RATE)
setChecked_enableSmoothDisplay_featureFlagOn()143     public void setChecked_enableSmoothDisplay_featureFlagOn() {
144         mController.mPeakRefreshRate = 88f;
145         mController.setChecked(true);
146 
147         assertThat(Settings.System.getFloat(mContext.getContentResolver(),
148                 Settings.System.PEAK_REFRESH_RATE, DEFAULT_REFRESH_RATE))
149                 .isPositiveInfinity();
150     }
151 
152     @Test
setChecked_disableSmoothDisplay_setDefaultRefreshRate()153     public void setChecked_disableSmoothDisplay_setDefaultRefreshRate() {
154         mController.mPeakRefreshRate = 88f;
155         mController.setChecked(false);
156 
157         assertThat(Settings.System.getFloat(mContext.getContentResolver(),
158                 Settings.System.PEAK_REFRESH_RATE, DEFAULT_REFRESH_RATE))
159                 .isEqualTo(DEFAULT_REFRESH_RATE);
160     }
161 
162     @Test
isChecked_enableSmoothDisplay_returnTrue()163     public void isChecked_enableSmoothDisplay_returnTrue() {
164         mController.mPeakRefreshRate = 88f;
165         mController.setChecked(true);
166 
167         assertThat(mController.isChecked()).isTrue();
168     }
169 
170     @Test
isChecked_disableSmoothDisplay_returnFalse()171     public void isChecked_disableSmoothDisplay_returnFalse() {
172         mController.mPeakRefreshRate = 88f;
173         mController.setChecked(false);
174 
175         assertThat(mController.isChecked()).isFalse();
176     }
177 
178     @Test
isChecked_default_returnTrue()179     public void isChecked_default_returnTrue() {
180         mController.mPeakRefreshRate = 88f;
181         when(mDeviceConfigDisplaySettings.getDefaultPeakRefreshRate())
182                 .thenReturn(mController.mPeakRefreshRate);
183 
184         assertThat(mController.isChecked()).isTrue();
185     }
186 
187     @Test
isChecked_default_returnFalse()188     public void isChecked_default_returnFalse() {
189         mController.mPeakRefreshRate = 88f;
190         when(mDeviceConfigDisplaySettings.getDefaultPeakRefreshRate()).thenReturn(60f);
191 
192         assertThat(mController.isChecked()).isFalse();
193     }
194 
195     @Test
196     @RequiresFlagsDisabled(Flags.FLAG_BACK_UP_SMOOTH_DISPLAY_AND_FORCE_PEAK_REFRESH_RATE)
peakRefreshRate_highestOfDefaultDisplay_featureFlagOff()197     public void peakRefreshRate_highestOfDefaultDisplay_featureFlagOff() {
198         assertThat(mController.mPeakRefreshRate).isEqualTo(120);
199     }
200 
201     @Test
202     @RequiresFlagsEnabled(Flags.FLAG_BACK_UP_SMOOTH_DISPLAY_AND_FORCE_PEAK_REFRESH_RATE)
peakRefreshRate_highestOfAllDisplays_featureFlagOn()203     public void peakRefreshRate_highestOfAllDisplays_featureFlagOn() {
204         assertThat(mController.mPeakRefreshRate).isEqualTo(130);
205     }
206 }
207