Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.display.whitebalance; |
| 18 | |
| 19 | import com.android.internal.R; |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 20 | import com.android.server.display.utils.AmbientFilter; |
| 21 | import com.android.server.display.utils.AmbientFilterStubber; |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.spy; |
| 27 | import static org.mockito.Mockito.when; |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 28 | import static org.mockito.Mockito.doAnswer; |
| 29 | import static org.mockito.Matchers.any; |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 30 | import static org.mockito.Matchers.anyLong; |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 31 | import static org.mockito.Matchers.eq; |
| 32 | import org.mockito.stubbing.Answer; |
| 33 | import org.mockito.invocation.InvocationOnMock; |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 34 | import org.mockito.Mock; |
| 35 | import org.mockito.MockitoAnnotations; |
| 36 | import org.mockito.Spy; |
| 37 | |
| 38 | import android.content.ContextWrapper; |
| 39 | import android.content.res.Resources; |
| 40 | import android.content.res.TypedArray; |
| 41 | import android.hardware.Sensor; |
| 42 | import android.hardware.SensorEvent; |
| 43 | import android.hardware.SensorEventListener; |
| 44 | import android.hardware.SensorManager; |
| 45 | import android.os.Handler; |
| 46 | import android.os.Looper; |
| 47 | import android.util.TypedValue; |
| 48 | |
| 49 | import androidx.test.InstrumentationRegistry; |
| 50 | |
| 51 | import org.junit.Before; |
| 52 | import org.junit.After; |
| 53 | import org.junit.Test; |
| 54 | import org.junit.runner.RunWith; |
| 55 | import org.junit.runners.JUnit4; |
| 56 | |
| 57 | import java.lang.reflect.Constructor; |
| 58 | import java.lang.reflect.Field; |
| 59 | import java.lang.reflect.Method; |
| 60 | import java.util.List; |
| 61 | |
| 62 | @RunWith(JUnit4.class) |
| 63 | public final class AmbientLuxTest { |
| 64 | private static final int AMBIENT_COLOR_TYPE = 20705; |
| 65 | private static final String AMBIENT_COLOR_TYPE_STR = "colorSensoryDensoryDoc"; |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 66 | private static final float LOW_LIGHT_AMBIENT_COLOR_TEMPERATURE = 5432.1f; |
| 67 | private static final float HIGH_LIGHT_AMBIENT_COLOR_TEMPERATURE = 3456.7f; |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 68 | |
| 69 | private Handler mHandler = new Handler(Looper.getMainLooper()); |
| 70 | private Sensor mLightSensor; |
| 71 | private Sensor mAmbientColorSensor; |
| 72 | private ContextWrapper mContextSpy; |
| 73 | private Resources mResourcesSpy; |
| 74 | |
| 75 | @Mock private SensorManager mSensorManagerMock; |
| 76 | |
| 77 | @Mock private TypedArray mBrightnesses; |
| 78 | @Mock private TypedArray mBiases; |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 79 | @Mock private TypedArray mHighLightBrightnesses; |
| 80 | @Mock private TypedArray mHighLightBiases; |
Long Ling | 69a91fe | 2019-10-01 11:56:37 -0700 | [diff] [blame] | 81 | @Mock private TypedArray mAmbientColorTemperatures; |
| 82 | @Mock private TypedArray mDisplayColorTemperatures; |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 83 | |
| 84 | @Before |
| 85 | public void setUp() throws Exception { |
| 86 | MockitoAnnotations.initMocks(this); |
| 87 | mLightSensor = createSensor(Sensor.TYPE_LIGHT, null); |
| 88 | mAmbientColorSensor = createSensor(AMBIENT_COLOR_TYPE, AMBIENT_COLOR_TYPE_STR); |
| 89 | mContextSpy = spy(new ContextWrapper(InstrumentationRegistry.getContext())); |
| 90 | mResourcesSpy = spy(mContextSpy.getResources()); |
| 91 | when(mContextSpy.getResources()).thenReturn(mResourcesSpy); |
| 92 | when(mSensorManagerMock.getDefaultSensor(Sensor.TYPE_LIGHT)).thenReturn(mLightSensor); |
| 93 | final List<Sensor> sensorList = ImmutableList.of(mLightSensor, mAmbientColorSensor); |
| 94 | when(mSensorManagerMock.getSensorList(Sensor.TYPE_ALL)).thenReturn(sensorList); |
| 95 | when(mResourcesSpy.getString( |
| 96 | R.string.config_displayWhiteBalanceColorTemperatureSensorName)) |
| 97 | .thenReturn(AMBIENT_COLOR_TYPE_STR); |
| 98 | when(mResourcesSpy.getInteger( |
| 99 | R.integer.config_displayWhiteBalanceDecreaseDebounce)) |
| 100 | .thenReturn(0); |
| 101 | when(mResourcesSpy.getInteger( |
| 102 | R.integer.config_displayWhiteBalanceIncreaseDebounce)) |
| 103 | .thenReturn(0); |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 104 | mockResourcesFloat(R.dimen.config_displayWhiteBalanceLowLightAmbientColorTemperature, |
| 105 | LOW_LIGHT_AMBIENT_COLOR_TEMPERATURE); |
| 106 | mockResourcesFloat(R.dimen.config_displayWhiteBalanceHighLightAmbientColorTemperature, |
| 107 | HIGH_LIGHT_AMBIENT_COLOR_TEMPERATURE); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 108 | when(mResourcesSpy.obtainTypedArray( |
| 109 | R.array.config_displayWhiteBalanceAmbientColorTemperatures)) |
Long Ling | 69a91fe | 2019-10-01 11:56:37 -0700 | [diff] [blame] | 110 | .thenReturn(mAmbientColorTemperatures); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 111 | when(mResourcesSpy.obtainTypedArray( |
| 112 | R.array.config_displayWhiteBalanceDisplayColorTemperatures)) |
Long Ling | 69a91fe | 2019-10-01 11:56:37 -0700 | [diff] [blame] | 113 | .thenReturn(mDisplayColorTemperatures); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 114 | |
| 115 | when(mResourcesSpy.obtainTypedArray( |
| 116 | R.array.config_displayWhiteBalanceLowLightAmbientBrightnesses)) |
| 117 | .thenReturn(mBrightnesses); |
| 118 | when(mResourcesSpy.obtainTypedArray( |
| 119 | R.array.config_displayWhiteBalanceLowLightAmbientBiases)) |
| 120 | .thenReturn(mBiases); |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 121 | when(mResourcesSpy.obtainTypedArray( |
| 122 | R.array.config_displayWhiteBalanceHighLightAmbientBrightnesses)) |
| 123 | .thenReturn(mHighLightBrightnesses); |
| 124 | when(mResourcesSpy.obtainTypedArray( |
| 125 | R.array.config_displayWhiteBalanceHighLightAmbientBiases)) |
| 126 | .thenReturn(mHighLightBiases); |
| 127 | mockThrottler(); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testNoSpline() throws Exception { |
| 132 | setBrightnesses(); |
| 133 | setBiases(); |
| 134 | |
| 135 | DisplayWhiteBalanceController controller = |
| 136 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 137 | final float ambientColorTemperature = 8000.0f; |
| 138 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 139 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 140 | |
| 141 | for (float luxOverride = 0.1f; luxOverride <= 10000; luxOverride *= 10) { |
| 142 | setEstimatedBrightnessAndUpdate(controller, luxOverride); |
| 143 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 144 | ambientColorTemperature, 0.001); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | public void testSpline_OneSegment() throws Exception { |
| 150 | final float lowerBrightness = 10.0f; |
| 151 | final float upperBrightness = 50.0f; |
| 152 | setBrightnesses(lowerBrightness, upperBrightness); |
| 153 | setBiases(0.0f, 1.0f); |
| 154 | |
| 155 | DisplayWhiteBalanceController controller = |
| 156 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 157 | final float ambientColorTemperature = 8000.0f; |
| 158 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 159 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 160 | |
| 161 | for (float t = 0.0f; t <= 1.0f; t += 0.1f) { |
| 162 | setEstimatedBrightnessAndUpdate(controller, |
| 163 | mix(lowerBrightness, upperBrightness, t)); |
| 164 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 165 | mix(LOW_LIGHT_AMBIENT_COLOR_TEMPERATURE, ambientColorTemperature, t), 0.001); |
| 166 | } |
| 167 | |
| 168 | setEstimatedBrightnessAndUpdate(controller, 0.0f); |
| 169 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 170 | LOW_LIGHT_AMBIENT_COLOR_TEMPERATURE, 0.001); |
| 171 | |
| 172 | setEstimatedBrightnessAndUpdate(controller, upperBrightness + 1.0f); |
| 173 | assertEquals(controller.mPendingAmbientColorTemperature, ambientColorTemperature, 0.001); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testSpline_TwoSegments() throws Exception { |
| 178 | final float brightness0 = 10.0f; |
| 179 | final float brightness1 = 50.0f; |
| 180 | final float brightness2 = 60.0f; |
| 181 | setBrightnesses(brightness0, brightness1, brightness2); |
| 182 | final float bias0 = 0.0f; |
| 183 | final float bias1 = 0.25f; |
| 184 | final float bias2 = 1.0f; |
| 185 | setBiases(bias0, bias1, bias2); |
| 186 | |
| 187 | DisplayWhiteBalanceController controller = |
| 188 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 189 | final float ambientColorTemperature = 8000.0f; |
| 190 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 191 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 192 | |
| 193 | for (float t = 0.0f; t <= 1.0f; t += 0.1f) { |
| 194 | float luxOverride = mix(brightness0, brightness1, t); |
| 195 | setEstimatedBrightnessAndUpdate(controller, luxOverride); |
| 196 | float bias = mix(bias0, bias1, t); |
| 197 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 198 | mix(LOW_LIGHT_AMBIENT_COLOR_TEMPERATURE, ambientColorTemperature, bias), 0.001); |
| 199 | } |
| 200 | |
| 201 | for (float t = 0.0f; t <= 1.0f; t += 0.1f) { |
| 202 | float luxOverride = mix(brightness1, brightness2, t); |
| 203 | setEstimatedBrightnessAndUpdate(controller, luxOverride); |
| 204 | float bias = mix(bias1, bias2, t); |
| 205 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 206 | mix(LOW_LIGHT_AMBIENT_COLOR_TEMPERATURE, ambientColorTemperature, bias), 0.001); |
| 207 | } |
| 208 | |
| 209 | setEstimatedBrightnessAndUpdate(controller, 0.0f); |
| 210 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 211 | LOW_LIGHT_AMBIENT_COLOR_TEMPERATURE, 0.001); |
| 212 | |
| 213 | setEstimatedBrightnessAndUpdate(controller, brightness2 + 1.0f); |
| 214 | assertEquals(controller.mPendingAmbientColorTemperature, ambientColorTemperature, 0.001); |
| 215 | } |
| 216 | |
| 217 | @Test |
| 218 | public void testSpline_VerticalSegment() throws Exception { |
| 219 | final float lowerBrightness = 10.0f; |
| 220 | final float upperBrightness = 10.0f; |
| 221 | setBrightnesses(lowerBrightness, upperBrightness); |
| 222 | setBiases(0.0f, 1.0f); |
| 223 | |
| 224 | DisplayWhiteBalanceController controller = |
| 225 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 226 | final float ambientColorTemperature = 8000.0f; |
| 227 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 228 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 229 | |
| 230 | setEstimatedBrightnessAndUpdate(controller, 0.0f); |
| 231 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 232 | LOW_LIGHT_AMBIENT_COLOR_TEMPERATURE, 0.001); |
| 233 | |
| 234 | setEstimatedBrightnessAndUpdate(controller, upperBrightness + 1.0f); |
| 235 | assertEquals(controller.mPendingAmbientColorTemperature, ambientColorTemperature, 0.001); |
| 236 | } |
| 237 | |
| 238 | @Test |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 239 | public void testSpline_InvalidEndBias() throws Exception { |
| 240 | setBrightnesses(10.0f, 1000.0f); |
| 241 | setBiases(0.0f, 2.0f); |
| 242 | |
| 243 | DisplayWhiteBalanceController controller = |
| 244 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 245 | final float ambientColorTemperature = 8000.0f; |
| 246 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 247 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 248 | |
| 249 | for (float luxOverride = 0.1f; luxOverride <= 10000; luxOverride *= 10) { |
| 250 | setEstimatedBrightnessAndUpdate(controller, luxOverride); |
| 251 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 252 | ambientColorTemperature, 0.001); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | @Test |
| 257 | public void testSpline_InvalidBeginBias() throws Exception { |
| 258 | setBrightnesses(10.0f, 1000.0f); |
| 259 | setBiases(0.1f, 1.0f); |
| 260 | |
| 261 | DisplayWhiteBalanceController controller = |
| 262 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 263 | final float ambientColorTemperature = 8000.0f; |
| 264 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 265 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 266 | |
| 267 | for (float luxOverride = 0.1f; luxOverride <= 10000; luxOverride *= 10) { |
| 268 | setEstimatedBrightnessAndUpdate(controller, luxOverride); |
| 269 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 270 | ambientColorTemperature, 0.001); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | @Test |
| 275 | public void testSpline_OneSegmentHighLight() throws Exception { |
| 276 | final float lowerBrightness = 10.0f; |
| 277 | final float upperBrightness = 50.0f; |
| 278 | setHighLightBrightnesses(lowerBrightness, upperBrightness); |
| 279 | setHighLightBiases(0.0f, 1.0f); |
| 280 | |
| 281 | DisplayWhiteBalanceController controller = |
| 282 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 283 | final float ambientColorTemperature = 8000.0f; |
| 284 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 285 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 286 | |
| 287 | for (float t = 0.0f; t <= 1.0f; t += 0.1f) { |
| 288 | setEstimatedBrightnessAndUpdate(controller, |
| 289 | mix(lowerBrightness, upperBrightness, t)); |
| 290 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 291 | mix(HIGH_LIGHT_AMBIENT_COLOR_TEMPERATURE, ambientColorTemperature, 1.0f - t), |
| 292 | 0.001); |
| 293 | } |
| 294 | |
| 295 | setEstimatedBrightnessAndUpdate(controller, upperBrightness + 1.0f); |
| 296 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 297 | HIGH_LIGHT_AMBIENT_COLOR_TEMPERATURE, 0.001); |
| 298 | |
| 299 | setEstimatedBrightnessAndUpdate(controller, 0.0f); |
| 300 | assertEquals(controller.mPendingAmbientColorTemperature, ambientColorTemperature, 0.001); |
| 301 | } |
| 302 | |
| 303 | @Test |
| 304 | public void testSpline_TwoSegmentsHighLight() throws Exception { |
| 305 | final float brightness0 = 10.0f; |
| 306 | final float brightness1 = 50.0f; |
| 307 | final float brightness2 = 60.0f; |
| 308 | setHighLightBrightnesses(brightness0, brightness1, brightness2); |
| 309 | final float bias0 = 0.0f; |
| 310 | final float bias1 = 0.25f; |
| 311 | final float bias2 = 1.0f; |
| 312 | setHighLightBiases(bias0, bias1, bias2); |
| 313 | |
| 314 | DisplayWhiteBalanceController controller = |
| 315 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 316 | final float ambientColorTemperature = 6000.0f; |
| 317 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 318 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 319 | |
| 320 | for (float t = 0.0f; t <= 1.0f; t += 0.1f) { |
| 321 | float luxOverride = mix(brightness0, brightness1, t); |
| 322 | setEstimatedBrightnessAndUpdate(controller, luxOverride); |
| 323 | float bias = mix(bias0, bias1, t); |
| 324 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 325 | mix(HIGH_LIGHT_AMBIENT_COLOR_TEMPERATURE, ambientColorTemperature, 1.0f - bias), |
| 326 | 0.01); |
| 327 | } |
| 328 | |
| 329 | for (float t = 0.0f; t <= 1.0f; t += 0.1f) { |
| 330 | float luxOverride = mix(brightness1, brightness2, t); |
| 331 | setEstimatedBrightnessAndUpdate(controller, luxOverride); |
| 332 | float bias = mix(bias1, bias2, t); |
| 333 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 334 | mix(HIGH_LIGHT_AMBIENT_COLOR_TEMPERATURE, ambientColorTemperature, 1.0f - bias), |
| 335 | 0.01); |
| 336 | } |
| 337 | |
| 338 | setEstimatedBrightnessAndUpdate(controller, brightness2 + 1.0f); |
| 339 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 340 | HIGH_LIGHT_AMBIENT_COLOR_TEMPERATURE, 0.001); |
| 341 | |
| 342 | setEstimatedBrightnessAndUpdate(controller, 0.0f); |
| 343 | assertEquals(controller.mPendingAmbientColorTemperature, ambientColorTemperature, 0.001); |
| 344 | } |
| 345 | |
| 346 | @Test |
| 347 | public void testSpline_InvalidCombinations() throws Exception { |
| 348 | setBrightnesses(100.0f, 200.0f); |
| 349 | setBiases(0.0f, 1.0f); |
| 350 | setHighLightBrightnesses(150.0f, 250.0f); |
| 351 | setHighLightBiases(0.0f, 1.0f); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 352 | |
| 353 | DisplayWhiteBalanceController controller = |
| 354 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 355 | final float ambientColorTemperature = 8000.0f; |
| 356 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 357 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 358 | |
| 359 | for (float luxOverride = 0.1f; luxOverride <= 10000; luxOverride *= 10) { |
| 360 | setEstimatedBrightnessAndUpdate(controller, luxOverride); |
| 361 | assertEquals(controller.mPendingAmbientColorTemperature, |
| 362 | ambientColorTemperature, 0.001); |
| 363 | } |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Anthony Han | 35df7e2 | 2019-06-27 17:27:24 -0700 | [diff] [blame] | 366 | @Test |
| 367 | public void testLowLight_DefaultAmbient() throws Exception { |
| 368 | final float lowerBrightness = 10.0f; |
| 369 | final float upperBrightness = 50.0f; |
| 370 | setBrightnesses(lowerBrightness, upperBrightness); |
| 371 | setBiases(0.0f, 1.0f); |
| 372 | |
| 373 | DisplayWhiteBalanceController controller = |
| 374 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 375 | final float ambientColorTemperature = -1.0f; |
| 376 | setEstimatedColorTemperature(controller, ambientColorTemperature); |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 377 | controller.mBrightnessFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 35df7e2 | 2019-06-27 17:27:24 -0700 | [diff] [blame] | 378 | |
| 379 | for (float t = 0.0f; t <= 1.0f; t += 0.1f) { |
| 380 | setEstimatedBrightnessAndUpdate(controller, |
| 381 | mix(lowerBrightness, upperBrightness, t)); |
| 382 | assertEquals(controller.mPendingAmbientColorTemperature, ambientColorTemperature, |
| 383 | 0.001); |
| 384 | } |
| 385 | |
| 386 | setEstimatedBrightnessAndUpdate(controller, 0.0f); |
| 387 | assertEquals(controller.mPendingAmbientColorTemperature, ambientColorTemperature, 0.001); |
| 388 | |
| 389 | setEstimatedBrightnessAndUpdate(controller, upperBrightness + 1.0f); |
| 390 | assertEquals(controller.mPendingAmbientColorTemperature, ambientColorTemperature, 0.001); |
| 391 | } |
| 392 | |
Long Ling | 69a91fe | 2019-10-01 11:56:37 -0700 | [diff] [blame] | 393 | @Test |
| 394 | public void testWhiteBalance_updateWithEmptyFilter() throws Exception { |
| 395 | setAmbientColorTemperatures(5300.0f, 6000.0f, 7000.0f, 8000.0f); |
| 396 | setDisplayColorTemperatures(6300.0f, 6400.0f, 6850.0f, 7450.0f); |
| 397 | DisplayWhiteBalanceController controller = |
| 398 | DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); |
| 399 | controller.updateAmbientColorTemperature(); |
| 400 | assertEquals(-1.0f, controller.mPendingAmbientColorTemperature, 0); |
| 401 | } |
| 402 | |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 403 | void mockThrottler() { |
| 404 | when(mResourcesSpy.getInteger( |
| 405 | R.integer.config_displayWhiteBalanceDecreaseDebounce)).thenReturn(0); |
| 406 | when(mResourcesSpy.getInteger( |
| 407 | R.integer.config_displayWhiteBalanceIncreaseDebounce)).thenReturn(0); |
| 408 | TypedArray base = mResourcesSpy.obtainTypedArray( |
| 409 | R.array.config_displayWhiteBalanceBaseThresholds); |
| 410 | TypedArray inc = mResourcesSpy.obtainTypedArray( |
| 411 | R.array.config_displayWhiteBalanceIncreaseThresholds); |
| 412 | TypedArray dec = mResourcesSpy.obtainTypedArray( |
| 413 | R.array.config_displayWhiteBalanceDecreaseThresholds); |
| 414 | base = spy(base); |
| 415 | inc = spy(inc); |
| 416 | dec = spy(dec); |
| 417 | when(mResourcesSpy.obtainTypedArray( |
| 418 | R.array.config_displayWhiteBalanceBaseThresholds)).thenReturn(base); |
| 419 | when(mResourcesSpy.obtainTypedArray( |
| 420 | R.array.config_displayWhiteBalanceIncreaseThresholds)).thenReturn(inc); |
| 421 | when(mResourcesSpy.obtainTypedArray( |
| 422 | R.array.config_displayWhiteBalanceDecreaseThresholds)).thenReturn(dec); |
| 423 | setFloatArrayResource(base, new float[]{0.0f}); |
| 424 | setFloatArrayResource(inc, new float[]{0.0f}); |
| 425 | setFloatArrayResource(dec, new float[]{0.0f}); |
| 426 | } |
| 427 | |
| 428 | private void mockResourcesFloat(int id, float floatValue) { |
| 429 | doAnswer(new Answer<Void>() { |
| 430 | public Void answer(InvocationOnMock invocation) { |
| 431 | TypedValue value = (TypedValue)invocation.getArgument(1); |
| 432 | value.type = TypedValue.TYPE_FLOAT; |
| 433 | value.data = Float.floatToIntBits(floatValue); |
| 434 | return null; |
| 435 | } |
| 436 | }).when(mResourcesSpy).getValue( |
| 437 | eq(id), |
| 438 | any(TypedValue.class), eq(true)); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | private void setEstimatedColorTemperature(DisplayWhiteBalanceController controller, |
| 442 | float ambientColorTemperature) { |
Long Ling | a21479a | 2019-07-22 15:49:02 -0700 | [diff] [blame] | 443 | AmbientFilter colorTemperatureFilter = spy(new AmbientFilterStubber()); |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 444 | controller.mColorTemperatureFilter = colorTemperatureFilter; |
| 445 | when(colorTemperatureFilter.getEstimate(anyLong())).thenReturn(ambientColorTemperature); |
| 446 | } |
| 447 | |
| 448 | private void setEstimatedBrightnessAndUpdate(DisplayWhiteBalanceController controller, |
| 449 | float brightness) { |
| 450 | when(controller.mBrightnessFilter.getEstimate(anyLong())).thenReturn(brightness); |
| 451 | controller.updateAmbientColorTemperature(); |
| 452 | } |
| 453 | |
| 454 | private void setBrightnesses(float... vals) { |
| 455 | setFloatArrayResource(mBrightnesses, vals); |
| 456 | } |
| 457 | |
| 458 | private void setBiases(float... vals) { |
| 459 | setFloatArrayResource(mBiases, vals); |
| 460 | } |
| 461 | |
Anthony Han | 0c8297c | 2019-06-18 16:56:11 -0700 | [diff] [blame] | 462 | private void setHighLightBrightnesses(float... vals) { |
| 463 | setFloatArrayResource(mHighLightBrightnesses, vals); |
| 464 | } |
| 465 | |
| 466 | private void setHighLightBiases(float... vals) { |
| 467 | setFloatArrayResource(mHighLightBiases, vals); |
| 468 | } |
| 469 | |
Long Ling | 69a91fe | 2019-10-01 11:56:37 -0700 | [diff] [blame] | 470 | private void setAmbientColorTemperatures(float... vals) { |
| 471 | setFloatArrayResource(mAmbientColorTemperatures, vals); |
| 472 | } |
| 473 | |
| 474 | private void setDisplayColorTemperatures(float... vals) { |
| 475 | setFloatArrayResource(mDisplayColorTemperatures, vals); |
| 476 | } |
| 477 | |
Anthony Han | 04c78a9 | 2019-05-03 13:34:57 -0700 | [diff] [blame] | 478 | private void setFloatArrayResource(TypedArray array, float[] vals) { |
| 479 | when(array.length()).thenReturn(vals.length); |
| 480 | for (int i = 0; i < vals.length; i++) { |
| 481 | when(array.getFloat(i, Float.NaN)).thenReturn(vals[i]); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | private void setSensorType(Sensor sensor, int type, String strType) throws Exception { |
| 486 | Method setter = Sensor.class.getDeclaredMethod("setType", Integer.TYPE); |
| 487 | setter.setAccessible(true); |
| 488 | setter.invoke(sensor, type); |
| 489 | if (strType != null) { |
| 490 | Field f = sensor.getClass().getDeclaredField("mStringType"); |
| 491 | f.setAccessible(true); |
| 492 | f.set(sensor, strType); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | private Sensor createSensor(int type, String strType) throws Exception { |
| 497 | Constructor<Sensor> constr = Sensor.class.getDeclaredConstructor(); |
| 498 | constr.setAccessible(true); |
| 499 | Sensor sensor = constr.newInstance(); |
| 500 | setSensorType(sensor, type, strType); |
| 501 | return sensor; |
| 502 | } |
| 503 | |
| 504 | private TypedArray createTypedArray() throws Exception { |
| 505 | TypedArray mockArray = mock(TypedArray.class); |
| 506 | return mockArray; |
| 507 | } |
| 508 | |
| 509 | private static float mix(float a, float b, float t) { |
| 510 | return (1.0f - t) * a + t * b; |
| 511 | } |
| 512 | } |