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.display;
18 
19 import static android.content.Context.POWER_SERVICE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.reset;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.os.PowerManager;
33 import android.provider.Settings.System;
34 
35 import androidx.preference.Preference;
36 import androidx.preference.PreferenceScreen;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 import org.robolectric.shadow.api.Shadow;
46 import org.robolectric.shadows.ShadowApplication;
47 import org.robolectric.shadows.ShadowContentResolver;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class BrightnessLevelPreferenceControllerTest {
51 
52     @Mock
53     private PowerManager mPowerManager;
54     @Mock
55     private PreferenceScreen mScreen;
56     @Mock
57     private Preference mPreference;
58 
59     private Context mContext;
60 
61     private ContentResolver mContentResolver;
62 
63     private BrightnessLevelPreferenceController mController;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         mContext = RuntimeEnvironment.application;
69         mContentResolver = mContext.getContentResolver();
70         when(mPowerManager.getBrightnessConstraint(
71                 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM)).thenReturn(0.0f);
72         when(mPowerManager.getBrightnessConstraint(
73                 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM)).thenReturn(1.0f);
74         when(mPowerManager.getBrightnessConstraint(
75                 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM_VR)).thenReturn(0.0f);
76         when(mPowerManager.getBrightnessConstraint(
77                 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM_VR)).thenReturn(1.0f);
78         ShadowApplication.getInstance().setSystemService(POWER_SERVICE,
79                 mPowerManager);
80         when(mScreen.findPreference(anyString())).thenReturn(mPreference);
81         mController = spy(new BrightnessLevelPreferenceController(mContext, null));
82         doReturn(false).when(mController).isInVrMode();
83     }
84 
85     @Test
isAvailable_shouldAlwaysReturnTrue()86     public void isAvailable_shouldAlwaysReturnTrue() {
87         assertThat(mController.isAvailable()).isTrue();
88     }
89 
90     @Test
isInVrMode_noVrManager_shouldAlwaysReturnFalse()91     public void isInVrMode_noVrManager_shouldAlwaysReturnFalse() {
92         doReturn(null).when(mController).safeGetVrManager();
93         assertThat(mController.isInVrMode()).isFalse();
94     }
95 
96     @Test
onStart_shouldRegisterObserver()97     public void onStart_shouldRegisterObserver() {
98         BrightnessLevelPreferenceController controller =
99                 new BrightnessLevelPreferenceController(mContext, null);
100         ShadowContentResolver shadowContentResolver = Shadow.extract(mContentResolver);
101 
102         controller.onStart();
103 
104         assertThat(shadowContentResolver.getContentObservers(
105                 System.getUriFor(System.SCREEN_BRIGHTNESS_FLOAT))).isNotEmpty();
106         assertThat(shadowContentResolver.getContentObservers(
107                 System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isNotEmpty();
108         assertThat(shadowContentResolver.getContentObservers(
109                 System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isNotEmpty();
110     }
111 
112     @Test
onStop_shouldUnregisterObserver()113     public void onStop_shouldUnregisterObserver() {
114         BrightnessLevelPreferenceController controller =
115                 new BrightnessLevelPreferenceController(mContext, null);
116         ShadowContentResolver shadowContentResolver = Shadow.extract(mContext.getContentResolver());
117 
118         controller.displayPreference(mScreen);
119         controller.onStart();
120         controller.onStop();
121 
122         assertThat(shadowContentResolver.getContentObservers(
123                 System.getUriFor(System.SCREEN_BRIGHTNESS_FLOAT))).isEmpty();
124         assertThat(shadowContentResolver.getContentObservers(
125                 System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT))).isEmpty();
126         assertThat(shadowContentResolver.getContentObservers(
127                 System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ))).isEmpty();
128     }
129 
130     @Test
updateState_inVrMode_shouldSetSummaryToVrBrightness()131     public void updateState_inVrMode_shouldSetSummaryToVrBrightness() {
132         doReturn(true).when(mController).isInVrMode();
133         System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, 0.6f);
134 
135         mController.updateState(mPreference);
136 
137         verify(mPreference).setSummary("91%");
138     }
139 
140     @Test
updateState_autoBrightness_shouldSetSummaryToAutoBrightness()141     public void updateState_autoBrightness_shouldSetSummaryToAutoBrightness() {
142         doReturn(false).when(mController).isInVrMode();
143         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
144                 System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
145 
146         System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, 0.1f);
147 
148         mController.updateState(mPreference);
149 
150         verify(mPreference).setSummary("54%");
151     }
152 
153     @Test
updateState_manualBrightness_shouldSetSummaryToScreenBrightness()154     public void updateState_manualBrightness_shouldSetSummaryToScreenBrightness() {
155         doReturn(false).when(mController).isInVrMode();
156         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
157                 System.SCREEN_BRIGHTNESS_MODE_MANUAL);
158 
159         System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, 0.5f);
160 
161         mController.updateState(mPreference);
162 
163         verify(mPreference).setSummary("87%");
164     }
165 
166     @Test
updateState_brightnessOutOfRange_shouldSetSummaryInRange()167     public void updateState_brightnessOutOfRange_shouldSetSummaryInRange() {
168         // VR mode
169         doReturn(true).when(mController).isInVrMode();
170 
171         System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, 1.05f);
172         mController.updateState(mPreference);
173         verify(mPreference).setSummary("100%");
174 
175         System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, -20f);
176         mController.updateState(mPreference);
177         verify(mPreference).setSummary("0%");
178 
179         // Auto mode
180         doReturn(false).when(mController).isInVrMode();
181         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
182                 System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
183 
184         reset(mPreference);
185         System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, 1.15f);
186         mController.updateState(mPreference);
187         verify(mPreference).setSummary("100%");
188 
189         System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, -10f);
190         mController.updateState(mPreference);
191         verify(mPreference).setSummary("0%");
192 
193         // Manual mode
194         System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
195                 System.SCREEN_BRIGHTNESS_MODE_MANUAL);
196 
197         reset(mPreference);
198         System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, 1.15f);
199         mController.updateState(mPreference);
200         verify(mPreference).setSummary("100%");
201 
202         System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, -10f);
203         mController.updateState(mPreference);
204         verify(mPreference).setSummary("0%");
205     }
206 }
207