Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [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 static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.ArgumentMatchers.anyInt; |
| 22 | import static org.mockito.ArgumentMatchers.eq; |
| 23 | import static org.mockito.ArgumentMatchers.isA; |
| 24 | import static org.mockito.Mockito.spy; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.content.ContextWrapper; |
| 29 | import android.content.res.Resources; |
| 30 | import android.hardware.Sensor; |
Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [diff] [blame] | 31 | import android.hardware.SensorEventListener; |
| 32 | import android.hardware.SensorManager; |
| 33 | import android.os.Handler; |
| 34 | import android.os.Looper; |
Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [diff] [blame] | 35 | |
| 36 | import androidx.test.InstrumentationRegistry; |
| 37 | |
Santos Cordon | 64a8627 | 2019-11-28 11:24:21 +0000 | [diff] [blame] | 38 | import com.android.server.display.TestUtils; |
Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [diff] [blame] | 39 | import com.android.server.display.whitebalance.AmbientSensor.AmbientBrightnessSensor; |
| 40 | import com.android.server.display.whitebalance.AmbientSensor.AmbientColorTemperatureSensor; |
| 41 | |
| 42 | import com.google.common.collect.ImmutableList; |
| 43 | |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.junit.runners.JUnit4; |
| 48 | import org.mockito.ArgumentCaptor; |
| 49 | import org.mockito.Mock; |
| 50 | import org.mockito.MockitoAnnotations; |
| 51 | |
Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [diff] [blame] | 52 | import java.util.List; |
| 53 | import java.util.concurrent.CountDownLatch; |
| 54 | import java.util.concurrent.TimeUnit; |
| 55 | |
| 56 | @RunWith(JUnit4.class) |
| 57 | public final class AmbientSensorTest { |
| 58 | private static final int AMBIENT_COLOR_TYPE = 20705; |
| 59 | private static final String AMBIENT_COLOR_TYPE_STR = "colorSensoryDensoryDoc"; |
| 60 | |
| 61 | private Handler mHandler = new Handler(Looper.getMainLooper()); |
| 62 | private Sensor mLightSensor; |
| 63 | private Sensor mAmbientColorSensor; |
| 64 | private ContextWrapper mContextSpy; |
| 65 | private Resources mResourcesSpy; |
| 66 | |
| 67 | @Mock private SensorManager mSensorManagerMock; |
| 68 | |
| 69 | @Before |
| 70 | public void setUp() throws Exception { |
| 71 | MockitoAnnotations.initMocks(this); |
Santos Cordon | 64a8627 | 2019-11-28 11:24:21 +0000 | [diff] [blame] | 72 | mLightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, null); |
| 73 | mAmbientColorSensor = TestUtils.createSensor(AMBIENT_COLOR_TYPE, AMBIENT_COLOR_TYPE_STR); |
Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [diff] [blame] | 74 | mContextSpy = spy(new ContextWrapper(InstrumentationRegistry.getContext())); |
| 75 | mResourcesSpy = spy(mContextSpy.getResources()); |
| 76 | when(mContextSpy.getResources()).thenReturn(mResourcesSpy); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testAmbientBrightnessSensorCallback_NoCallbacks() throws Exception { |
| 81 | when(mSensorManagerMock.getDefaultSensor(Sensor.TYPE_LIGHT)).thenReturn(mLightSensor); |
| 82 | AmbientBrightnessSensor abs = DisplayWhiteBalanceFactory.createBrightnessSensor( |
| 83 | mHandler, mSensorManagerMock, InstrumentationRegistry.getContext().getResources()); |
| 84 | |
| 85 | abs.setCallbacks(null); |
| 86 | abs.setEnabled(true); |
| 87 | ArgumentCaptor<SensorEventListener> captor = |
| 88 | ArgumentCaptor.forClass(SensorEventListener.class); |
| 89 | verify(mSensorManagerMock).registerListener(captor.capture(), isA(Sensor.class), anyInt(), |
| 90 | isA(Handler.class)); |
| 91 | |
| 92 | // There should be no issues when we callback the listener, even if there is no callback |
| 93 | // set. |
| 94 | SensorEventListener listener = captor.getValue(); |
Santos Cordon | 64a8627 | 2019-11-28 11:24:21 +0000 | [diff] [blame] | 95 | listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 100)); |
Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testAmbientBrightnessSensorCallback_CallbacksCalled() throws Exception { |
| 100 | final int luxValue = 83; |
| 101 | when(mSensorManagerMock.getDefaultSensor(Sensor.TYPE_LIGHT)).thenReturn(mLightSensor); |
| 102 | AmbientBrightnessSensor abs = DisplayWhiteBalanceFactory.createBrightnessSensor( |
| 103 | mHandler, mSensorManagerMock, InstrumentationRegistry.getContext().getResources()); |
| 104 | |
| 105 | final int[] luxReturned = new int[] { -1 }; |
| 106 | final CountDownLatch changeSignal = new CountDownLatch(1); |
| 107 | abs.setCallbacks(new AmbientBrightnessSensor.Callbacks() { |
| 108 | @Override |
| 109 | public void onAmbientBrightnessChanged(float value) { |
| 110 | luxReturned[0] = (int) value; |
| 111 | changeSignal.countDown(); |
| 112 | } |
| 113 | }); |
| 114 | |
| 115 | abs.setEnabled(true); |
| 116 | ArgumentCaptor<SensorEventListener> captor = |
| 117 | ArgumentCaptor.forClass(SensorEventListener.class); |
| 118 | verify(mSensorManagerMock).registerListener(captor.capture(), eq(mLightSensor), |
| 119 | anyInt(), eq(mHandler)); |
| 120 | SensorEventListener listener = captor.getValue(); |
Santos Cordon | 64a8627 | 2019-11-28 11:24:21 +0000 | [diff] [blame] | 121 | listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, luxValue)); |
Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [diff] [blame] | 122 | assertTrue(changeSignal.await(5, TimeUnit.SECONDS)); |
| 123 | assertEquals(luxValue, luxReturned[0]); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testAmbientColorTemperatureSensorCallback_CallbacksCalled() throws Exception { |
| 128 | final int colorTempValue = 79; |
| 129 | final List<Sensor> sensorList = ImmutableList.of(mLightSensor, mAmbientColorSensor); |
| 130 | when(mSensorManagerMock.getSensorList(Sensor.TYPE_ALL)).thenReturn(sensorList); |
| 131 | when(mResourcesSpy.getString( |
| 132 | com.android.internal.R.string.config_displayWhiteBalanceColorTemperatureSensorName)) |
| 133 | .thenReturn(AMBIENT_COLOR_TYPE_STR); |
| 134 | |
| 135 | AmbientColorTemperatureSensor abs = DisplayWhiteBalanceFactory.createColorTemperatureSensor( |
| 136 | mHandler, mSensorManagerMock, mResourcesSpy); |
| 137 | |
| 138 | final int[] colorTempReturned = new int[] { -1 }; |
| 139 | final CountDownLatch changeSignal = new CountDownLatch(1); |
| 140 | abs.setCallbacks(new AmbientColorTemperatureSensor.Callbacks() { |
| 141 | @Override |
| 142 | public void onAmbientColorTemperatureChanged(float value) { |
| 143 | colorTempReturned[0] = (int) value; |
| 144 | changeSignal.countDown(); |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | abs.setEnabled(true); |
| 149 | ArgumentCaptor<SensorEventListener> captor = |
| 150 | ArgumentCaptor.forClass(SensorEventListener.class); |
| 151 | verify(mSensorManagerMock).registerListener(captor.capture(), eq(mAmbientColorSensor), |
| 152 | anyInt(), eq(mHandler)); |
| 153 | SensorEventListener listener = captor.getValue(); |
Santos Cordon | 64a8627 | 2019-11-28 11:24:21 +0000 | [diff] [blame] | 154 | listener.onSensorChanged(TestUtils.createSensorEvent(mAmbientColorSensor, colorTempValue)); |
Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [diff] [blame] | 155 | assertTrue(changeSignal.await(5, TimeUnit.SECONDS)); |
| 156 | assertEquals(colorTempValue, colorTempReturned[0]); |
| 157 | } |
Santos Cordon | ceb0ae1 | 2019-04-29 18:06:09 +0100 | [diff] [blame] | 158 | } |