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