blob: f6c4d3aa5f5f6c1037c876a0ef2196041a3cf3c7 [file] [log] [blame]
Santos Cordon64a86272019-11-28 11:24:21 +00001/*
2 * Copyright (C) 2019 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
17package com.android.server.display;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.ArgumentMatchers.eq;
21import static org.mockito.Mockito.any;
22import static org.mockito.Mockito.anyFloat;
23import static org.mockito.Mockito.anyInt;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.when;
26
27import android.content.Context;
28import android.content.pm.PackageManager;
29import android.hardware.Sensor;
30import android.hardware.SensorEventListener;
31import android.hardware.SensorManager;
32import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest;
33import android.os.Handler;
34
35import androidx.test.InstrumentationRegistry;
36import androidx.test.filters.SmallTest;
37import androidx.test.runner.AndroidJUnit4;
38
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.ArgumentCaptor;
43import org.mockito.Mock;
44import org.mockito.MockitoAnnotations;
45
46@SmallTest
47@RunWith(AndroidJUnit4.class)
48public class AutomaticBrightnessControllerTest {
49
50 private static final int BRIGHTNESS_MIN = 1;
51 private static final int BRIGHTNESS_MAX = 255;
52 private static final int LIGHT_SENSOR_RATE = 20;
53 private static final int INITIAL_LIGHT_SENSOR_RATE = 20;
54 private static final int BRIGHTENING_LIGHT_DEBOUNCE_CONFIG = 0;
55 private static final int DARKENING_LIGHT_DEBOUNCE_CONFIG = 0;
56 private static final int SHORT_TERM_MODEL_TIMEOUT = 0;
57 private static final float DOZE_SCALE_FACTOR = 0.0f;
58 private static final boolean RESET_AMBIENT_LUX_AFTER_WARMUP_CONFIG = false;
59
60 private Context mContext;
61 @Mock SensorManager mSensorManager;
62 @Mock BrightnessMappingStrategy mBrightnessMappingStrategy;
63 @Mock HysteresisLevels mAmbientBrightnessThresholds;
64 @Mock HysteresisLevels mScreenBrightnessThresholds;
65 @Mock PackageManager mPackageManager;
66 @Mock Handler mNoopHandler;
67
68 private static final int LIGHT_SENSOR_WARMUP_TIME = 0;
69 @Before
70 public void setUp() {
71 MockitoAnnotations.initMocks(this);
72
73 mContext = InstrumentationRegistry.getContext();
74 }
75
76 private AutomaticBrightnessController setupController(Sensor lightSensor) {
77 AutomaticBrightnessController controller = new AutomaticBrightnessController(
78 new AutomaticBrightnessController.Injector() {
79 @Override
80 public Handler getBackgroundThreadHandler() {
81 return mNoopHandler;
82 }
83 },
84 () -> { }, mContext.getMainLooper(), mSensorManager, lightSensor,
85 mBrightnessMappingStrategy, LIGHT_SENSOR_WARMUP_TIME, BRIGHTNESS_MIN,
86 BRIGHTNESS_MAX, DOZE_SCALE_FACTOR, LIGHT_SENSOR_RATE, INITIAL_LIGHT_SENSOR_RATE,
87 BRIGHTENING_LIGHT_DEBOUNCE_CONFIG, DARKENING_LIGHT_DEBOUNCE_CONFIG,
88 RESET_AMBIENT_LUX_AFTER_WARMUP_CONFIG, mAmbientBrightnessThresholds,
89 mScreenBrightnessThresholds, SHORT_TERM_MODEL_TIMEOUT, mPackageManager);
90 controller.setLoggingEnabled(true);
91
92 // Configure the brightness controller and grab an instance of the sensor listener,
93 // through which we can deliver fake (for test) sensor values.
94 controller.configure(true /* enable */, null /* configuration */,
95 0 /* brightness */, false /* userChangedBrightness */, 0 /* adjustment */,
96 false /* userChanged */, DisplayPowerRequest.POLICY_BRIGHT);
97
98 return controller;
99 }
100
101 @Test
102 public void testNoHysteresisAtMinBrightness() throws Exception {
103 Sensor lightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, "Light Sensor");
104 AutomaticBrightnessController controller = setupController(lightSensor);
105
106 ArgumentCaptor<SensorEventListener> listenerCaptor =
107 ArgumentCaptor.forClass(SensorEventListener.class);
108 verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(lightSensor),
109 eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
110 SensorEventListener listener = listenerCaptor.getValue();
111
112 // Set up system to return 5 as a brightness value
113 float lux1 = 100.0f;
114 float normalizedBrightness1 = 0.02f;
115 when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux1))
116 .thenReturn(lux1);
117 when(mAmbientBrightnessThresholds.getDarkeningThreshold(lux1))
118 .thenReturn(lux1);
119 when(mBrightnessMappingStrategy.getBrightness(eq(lux1), eq(null), anyInt()))
120 .thenReturn(normalizedBrightness1);
121
122 // This is the important bit: When the new brightness is set, make sure the new
123 // brightening threshold is beyond the maximum brightness value...so that we can test that
124 // our threshold clamping works.
125 when(mScreenBrightnessThresholds.getBrighteningThreshold(5)).thenReturn(1.0f);
126
127 // Send new sensor value and verify
128 listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux1));
129 assertEquals(5, controller.getAutomaticScreenBrightness());
130
131
132 // Set up system to return 255 as a brightness value
133 float lux2 = 10.0f;
134 float normalizedBrightness2 = 0.0f;
135 when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux2))
136 .thenReturn(lux2);
137 when(mAmbientBrightnessThresholds.getDarkeningThreshold(lux2))
138 .thenReturn(lux2);
139 when(mBrightnessMappingStrategy.getBrightness(anyFloat(), eq(null), anyInt()))
140 .thenReturn(normalizedBrightness2);
141
142 // Send new sensor value and verify
143 listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2));
144 assertEquals(1, controller.getAutomaticScreenBrightness());
145 }
146
147 @Test
148 public void testNoHysteresisAtMaxBrightness() throws Exception {
149 Sensor lightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, "Light Sensor");
150 AutomaticBrightnessController controller = setupController(lightSensor);
151
152 ArgumentCaptor<SensorEventListener> listenerCaptor =
153 ArgumentCaptor.forClass(SensorEventListener.class);
154 verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(lightSensor),
155 eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
156 SensorEventListener listener = listenerCaptor.getValue();
157
158 // Set up system to return 250 as a brightness value
159 float lux1 = 100.0f;
160 float normalizedBrightness1 = 0.98f;
161 when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux1))
162 .thenReturn(lux1);
163 when(mAmbientBrightnessThresholds.getDarkeningThreshold(lux1))
164 .thenReturn(lux1);
165 when(mBrightnessMappingStrategy.getBrightness(eq(lux1), eq(null), anyInt()))
166 .thenReturn(normalizedBrightness1);
167
168 // This is the important bit: When the new brightness is set, make sure the new
169 // brightening threshold is beyond the maximum brightness value...so that we can test that
170 // our threshold clamping works.
171 when(mScreenBrightnessThresholds.getBrighteningThreshold(250)).thenReturn(260.0f);
172
173 // Send new sensor value and verify
174 listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux1));
175 assertEquals(250, controller.getAutomaticScreenBrightness());
176
177
178 // Set up system to return 255 as a brightness value
179 float lux2 = 110.0f;
180 float normalizedBrightness2 = 1.0f;
181 when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux2))
182 .thenReturn(lux2);
183 when(mAmbientBrightnessThresholds.getDarkeningThreshold(lux2))
184 .thenReturn(lux2);
185 when(mBrightnessMappingStrategy.getBrightness(anyFloat(), eq(null), anyInt()))
186 .thenReturn(normalizedBrightness2);
187
188 // Send new sensor value and verify
189 listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2));
190 assertEquals(255, controller.getAutomaticScreenBrightness());
191 }
192}