Merge changes from topic "b/273689055" into udc-qpr-dev

* changes:
  Proximity sensor can be null, handling this case properly for DMS
  Revert "Revert "No sensors support in DDC rework""
This commit is contained in:
Oleg Petšjonkin
2023-06-21 08:52:51 +00:00
committed by Android (Google) Code Review
11 changed files with 349 additions and 59 deletions

View File

@@ -515,7 +515,9 @@ public class DisplayDeviceConfig {
private final SensorData mScreenOffBrightnessSensor = new SensorData();
// The details of the proximity sensor associated with this display.
private final SensorData mProximitySensor = new SensorData();
// Is null when no sensor should be used for that display
@Nullable
private SensorData mProximitySensor = new SensorData();
private final List<RefreshRateLimitation> mRefreshRateLimitations =
new ArrayList<>(2 /*initialCapacity*/);
@@ -1337,7 +1339,8 @@ public class DisplayDeviceConfig {
return mScreenOffBrightnessSensor;
}
SensorData getProximitySensor() {
@Nullable
public SensorData getProximitySensor() {
return mProximitySensor;
}
@@ -2563,46 +2566,51 @@ public class DisplayDeviceConfig {
private void loadAmbientLightSensorFromDdc(DisplayConfiguration config) {
final SensorDetails sensorDetails = config.getLightSensor();
if (sensorDetails != null) {
mAmbientLightSensor.type = sensorDetails.getType();
mAmbientLightSensor.name = sensorDetails.getName();
final RefreshRateRange rr = sensorDetails.getRefreshRate();
if (rr != null) {
mAmbientLightSensor.minRefreshRate = rr.getMinimum().floatValue();
mAmbientLightSensor.maxRefreshRate = rr.getMaximum().floatValue();
}
loadSensorData(sensorDetails, mAmbientLightSensor);
} else {
loadAmbientLightSensorFromConfigXml();
}
}
private void setProxSensorUnspecified() {
mProximitySensor.name = null;
mProximitySensor.type = null;
mProximitySensor = new SensorData();
}
private void loadScreenOffBrightnessSensorFromDdc(DisplayConfiguration config) {
final SensorDetails sensorDetails = config.getScreenOffBrightnessSensor();
if (sensorDetails != null) {
mScreenOffBrightnessSensor.type = sensorDetails.getType();
mScreenOffBrightnessSensor.name = sensorDetails.getName();
loadSensorData(sensorDetails, mScreenOffBrightnessSensor);
}
}
private void loadProxSensorFromDdc(DisplayConfiguration config) {
SensorDetails sensorDetails = config.getProxSensor();
if (sensorDetails != null) {
mProximitySensor.name = sensorDetails.getName();
mProximitySensor.type = sensorDetails.getType();
final RefreshRateRange rr = sensorDetails.getRefreshRate();
if (rr != null) {
mProximitySensor.minRefreshRate = rr.getMinimum().floatValue();
mProximitySensor.maxRefreshRate = rr.getMaximum().floatValue();
String name = sensorDetails.getName();
String type = sensorDetails.getType();
if ("".equals(name) && "".equals(type)) {
// <proxSensor> with empty values to the config means no sensor should be used
mProximitySensor = null;
} else {
mProximitySensor = new SensorData();
loadSensorData(sensorDetails, mProximitySensor);
}
} else {
setProxSensorUnspecified();
}
}
private void loadSensorData(@NonNull SensorDetails sensorDetails,
@NonNull SensorData sensorData) {
sensorData.name = sensorDetails.getName();
sensorData.type = sensorDetails.getType();
final RefreshRateRange rr = sensorDetails.getRefreshRate();
if (rr != null) {
sensorData.minRefreshRate = rr.getMinimum().floatValue();
sensorData.maxRefreshRate = rr.getMaximum().floatValue();
}
}
private void loadBrightnessChangeThresholdsFromXml() {
loadBrightnessChangeThresholds(/* config= */ null);
}

View File

@@ -4259,6 +4259,13 @@ public final class DisplayManagerService extends SystemService {
return !Float.isNaN(refreshRate) && (refreshRate > 0.0f);
}
@VisibleForTesting
void overrideSensorManager(SensorManager sensorManager) {
synchronized (mSyncRoot) {
mSensorManager = sensorManager;
}
}
@VisibleForTesting
final class LocalService extends DisplayManagerInternal {
@@ -4512,7 +4519,7 @@ public final class DisplayManagerService extends SystemService {
}
final DisplayDeviceConfig config = device.getDisplayDeviceConfig();
SensorData sensorData = config.getProximitySensor();
if (sensorData.matches(sensorName, sensorType)) {
if (sensorData != null && sensorData.matches(sensorName, sensorType)) {
return new RefreshRateRange(sensorData.minRefreshRate,
sensorData.maxRefreshRate);
}

View File

@@ -2303,29 +2303,23 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
}
private void loadAmbientLightSensor() {
DisplayDeviceConfig.SensorData lightSensor = mDisplayDeviceConfig.getAmbientLightSensor();
final int fallbackType = mDisplayId == Display.DEFAULT_DISPLAY
? Sensor.TYPE_LIGHT : SensorUtils.NO_FALLBACK;
mLightSensor = SensorUtils.findSensor(mSensorManager, lightSensor.type, lightSensor.name,
fallbackType);
mLightSensor = SensorUtils.findSensor(mSensorManager,
mDisplayDeviceConfig.getAmbientLightSensor(), fallbackType);
}
private void loadScreenOffBrightnessSensor() {
DisplayDeviceConfig.SensorData screenOffBrightnessSensor =
mDisplayDeviceConfig.getScreenOffBrightnessSensor();
mScreenOffBrightnessSensor = SensorUtils.findSensor(mSensorManager,
screenOffBrightnessSensor.type, screenOffBrightnessSensor.name,
SensorUtils.NO_FALLBACK);
mDisplayDeviceConfig.getScreenOffBrightnessSensor(), SensorUtils.NO_FALLBACK);
}
private void loadProximitySensor() {
if (DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT || mDisplayId != Display.DEFAULT_DISPLAY) {
return;
}
final DisplayDeviceConfig.SensorData proxSensor =
mDisplayDeviceConfig.getProximitySensor();
mProximitySensor = SensorUtils.findSensor(mSensorManager, proxSensor.type, proxSensor.name,
Sensor.TYPE_PROXIMITY);
mProximitySensor = SensorUtils.findSensor(mSensorManager,
mDisplayDeviceConfig.getProximitySensor(), Sensor.TYPE_PROXIMITY);
if (mProximitySensor != null) {
mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(),
TYPICAL_PROXIMITY_THRESHOLD);

View File

@@ -1950,19 +1950,15 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
}
private void loadAmbientLightSensor() {
DisplayDeviceConfig.SensorData lightSensor = mDisplayDeviceConfig.getAmbientLightSensor();
final int fallbackType = mDisplayId == Display.DEFAULT_DISPLAY
? Sensor.TYPE_LIGHT : SensorUtils.NO_FALLBACK;
mLightSensor = SensorUtils.findSensor(mSensorManager, lightSensor.type, lightSensor.name,
fallbackType);
mLightSensor = SensorUtils.findSensor(mSensorManager,
mDisplayDeviceConfig.getAmbientLightSensor(), fallbackType);
}
private void loadScreenOffBrightnessSensor() {
DisplayDeviceConfig.SensorData screenOffBrightnessSensor =
mDisplayDeviceConfig.getScreenOffBrightnessSensor();
mScreenOffBrightnessSensor = SensorUtils.findSensor(mSensorManager,
screenOffBrightnessSensor.type, screenOffBrightnessSensor.name,
SensorUtils.NO_FALLBACK);
mDisplayDeviceConfig.getScreenOffBrightnessSensor(), SensorUtils.NO_FALLBACK);
}
private float clampScreenBrightness(float value) {

View File

@@ -358,10 +358,8 @@ public final class DisplayPowerProximityStateController {
if (DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT || mDisplayId != Display.DEFAULT_DISPLAY) {
return;
}
final DisplayDeviceConfig.SensorData proxSensor =
mDisplayDeviceConfig.getProximitySensor();
mProximitySensor = SensorUtils.findSensor(mSensorManager, proxSensor.type, proxSensor.name,
Sensor.TYPE_PROXIMITY);
mProximitySensor = SensorUtils.findSensor(mSensorManager,
mDisplayDeviceConfig.getProximitySensor(), Sensor.TYPE_PROXIMITY);
if (mProximitySensor != null) {
mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(),
TYPICAL_PROXIMITY_THRESHOLD);

View File

@@ -16,10 +16,13 @@
package com.android.server.display.utils;
import android.annotation.Nullable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.text.TextUtils;
import com.android.server.display.DisplayDeviceConfig;
import java.util.List;
/**
@@ -28,18 +31,27 @@ import java.util.List;
public class SensorUtils {
public static final int NO_FALLBACK = 0;
/**
* Finds the specified sensor for SensorData from DisplayDeviceConfig.
*/
@Nullable
public static Sensor findSensor(@Nullable SensorManager sensorManager,
@Nullable DisplayDeviceConfig.SensorData sensorData, int fallbackType) {
if (sensorData == null) {
return null;
} else {
return findSensor(sensorManager, sensorData.type, sensorData.name, fallbackType);
}
}
/**
* Finds the specified sensor by type and name using SensorManager.
*/
public static Sensor findSensor(SensorManager sensorManager, String sensorType,
String sensorName, int fallbackType) {
@Nullable
public static Sensor findSensor(@Nullable SensorManager sensorManager,
@Nullable String sensorType, @Nullable String sensorName, int fallbackType) {
if (sensorManager == null) {
return null;
}
if ("".equals(sensorName) && "".equals(sensorType)) {
return null;
}
final boolean isNameSpecified = !TextUtils.isEmpty(sensorName);
final boolean isTypeSpecified = !TextUtils.isEmpty(sensorType);
if (isNameSpecified || isTypeSpecified) {

View File

@@ -34,8 +34,8 @@ import android.hardware.display.DisplayManagerInternal;
import android.os.test.TestLooper;
import android.view.Display;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.server.testutils.OffsettableClock;
@@ -92,7 +92,7 @@ public final class DisplayPowerProximityStateControllerTest {
};
mDisplayPowerProximityStateController = new DisplayPowerProximityStateController(
mWakelockController, mDisplayDeviceConfig, mTestLooper.getLooper(),
mNudgeUpdatePowerState, 0,
mNudgeUpdatePowerState, Display.DEFAULT_DISPLAY,
mSensorManager, injector);
mSensorEventListener = mDisplayPowerProximityStateController.getProximitySensorListener();
}
@@ -128,7 +128,7 @@ public final class DisplayPowerProximityStateControllerTest {
enableProximitySensor();
emitAndValidatePositiveProximityEvent();
mDisplayPowerProximityStateController.ignoreProximitySensorUntilChangedInternal();
advanceTime(1);
advanceTime();
assertTrue(mDisplayPowerProximityStateController.shouldIgnoreProximityUntilChanged());
verify(mNudgeUpdatePowerState, times(2)).run();
@@ -170,7 +170,7 @@ public final class DisplayPowerProximityStateControllerTest {
}
@Test
public void isProximitySensorAvailableReturnsFalseWhenNotAvailable() {
public void isProximitySensorAvailableReturnsFalseWhenNotAvailableAndNoDefault() {
when(mDisplayDeviceConfig.getProximitySensor()).thenReturn(
new DisplayDeviceConfig.SensorData() {
{
@@ -178,6 +178,44 @@ public final class DisplayPowerProximityStateControllerTest {
name = null;
}
});
mDisplayPowerProximityStateController = new DisplayPowerProximityStateController(
mWakelockController, mDisplayDeviceConfig, mTestLooper.getLooper(),
mNudgeUpdatePowerState, Display.DEFAULT_DISPLAY,
mSensorManager, null);
assertFalse(mDisplayPowerProximityStateController.isProximitySensorAvailable());
}
@Test
public void isProximitySensorAvailableReturnsTrueWhenNotAvailableAndHasDefault()
throws Exception {
when(mDisplayDeviceConfig.getProximitySensor()).thenReturn(
new DisplayDeviceConfig.SensorData() {
{
type = null;
name = null;
}
});
when(mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)).thenReturn(
TestUtils.createSensor(Sensor.TYPE_PROXIMITY, "proximity"));
mDisplayPowerProximityStateController = new DisplayPowerProximityStateController(
mWakelockController, mDisplayDeviceConfig, mTestLooper.getLooper(),
mNudgeUpdatePowerState, Display.DEFAULT_DISPLAY,
mSensorManager, null);
assertTrue(mDisplayPowerProximityStateController.isProximitySensorAvailable());
}
@Test
public void isProximitySensorAvailableReturnsFalseWhenNotAvailableHasDefaultNonDefaultDisplay()
throws Exception {
when(mDisplayDeviceConfig.getProximitySensor()).thenReturn(
new DisplayDeviceConfig.SensorData() {
{
type = null;
name = null;
}
});
when(mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)).thenReturn(
TestUtils.createSensor(Sensor.TYPE_PROXIMITY, "proximity"));
mDisplayPowerProximityStateController = new DisplayPowerProximityStateController(
mWakelockController, mDisplayDeviceConfig, mTestLooper.getLooper(),
mNudgeUpdatePowerState, 1,
@@ -185,6 +223,19 @@ public final class DisplayPowerProximityStateControllerTest {
assertFalse(mDisplayPowerProximityStateController.isProximitySensorAvailable());
}
@Test
public void isProximitySensorAvailableReturnsTrueWhenNoSensorConfigured() throws Exception {
when(mDisplayDeviceConfig.getProximitySensor()).thenReturn(null);
when(mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)).thenReturn(
TestUtils.createSensor(Sensor.TYPE_PROXIMITY, Sensor.STRING_TYPE_PROXIMITY));
mDisplayPowerProximityStateController = new DisplayPowerProximityStateController(
mWakelockController, mDisplayDeviceConfig, mTestLooper.getLooper(),
mNudgeUpdatePowerState, Display.DEFAULT_DISPLAY,
mSensorManager, null);
assertFalse(mDisplayPowerProximityStateController.isProximitySensorAvailable());
}
@Test
public void notifyDisplayDeviceChangedReloadsTheProximitySensor() throws Exception {
DisplayDeviceConfig updatedDisplayDeviceConfig = mock(DisplayDeviceConfig.class);
@@ -326,8 +377,8 @@ public final class DisplayPowerProximityStateControllerTest {
assertEquals(mDisplayPowerProximityStateController.getPendingProximityDebounceTime(), -1);
}
private void advanceTime(long timeMs) {
mClock.fastForward(timeMs);
private void advanceTime() {
mClock.fastForward(1);
mTestLooper.dispatchAll();
}

View File

@@ -33,8 +33,8 @@ import android.os.Temperature;
import android.util.SparseArray;
import android.view.SurfaceControl;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.R;
import com.android.server.display.config.ThermalStatus;

View File

@@ -32,6 +32,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -60,6 +61,8 @@ import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Insets;
import android.graphics.Rect;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.display.BrightnessConfiguration;
import android.hardware.display.Curve;
import android.hardware.display.DisplayManager;
@@ -109,8 +112,6 @@ import com.android.server.wm.WindowManagerInternal;
import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -125,6 +126,7 @@ import org.mockito.MockitoAnnotations;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
@@ -136,6 +138,9 @@ import java.util.stream.LongStream;
public class DisplayManagerServiceTest {
private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
private static final long SHORT_DEFAULT_DISPLAY_TIMEOUT_MILLIS = 10;
private static final float FLOAT_TOLERANCE = 0.01f;
private static final String VIRTUAL_DISPLAY_NAME = "Test Virtual Display";
private static final String PACKAGE_NAME = "com.android.frameworks.servicestests";
private static final long STANDARD_DISPLAY_EVENTS = DisplayManager.EVENT_FLAG_DISPLAY_ADDED
@@ -250,6 +255,10 @@ public class DisplayManagerServiceTest {
@Mock IBinder mMockDisplayToken;
@Mock SensorManagerInternal mMockSensorManagerInternal;
@Mock SensorManager mSensorManager;
@Mock DisplayDeviceConfig mMockDisplayDeviceConfig;
@Captor ArgumentCaptor<ContentRecordingSession> mContentRecordingSessionCaptor;
@Before
@@ -267,7 +276,7 @@ public class DisplayManagerServiceTest {
LocalServices.removeServiceForTest(VirtualDeviceManagerInternal.class);
LocalServices.addService(
VirtualDeviceManagerInternal.class, mMockVirtualDeviceManagerInternal);
// TODO: b/287945043
mContext = spy(new ContextWrapper(ApplicationProvider.getApplicationContext()));
VirtualDeviceManager vdm = new VirtualDeviceManager(mIVirtualDeviceManager, mContext);
@@ -396,7 +405,7 @@ public class DisplayManagerServiceTest {
final int size = displayIds.length;
assertTrue(size > 0);
Map<Integer, Integer> expectedDisplayTypeToViewPortTypeMapping = ImmutableMap.of(
Map<Integer, Integer> expectedDisplayTypeToViewPortTypeMapping = Map.of(
Display.TYPE_INTERNAL, DisplayViewport.VIEWPORT_INTERNAL,
Display.TYPE_EXTERNAL, DisplayViewport.VIEWPORT_EXTERNAL
);
@@ -1934,6 +1943,74 @@ public class DisplayManagerServiceTest {
assertEquals(mode, displayManager.getHdrConversionModeInternal());
}
@Test
public void testReturnsRefreshRateForDisplayAndSensor_proximitySensorSet() {
DisplayManagerService displayManager = new DisplayManagerService(mContext, mBasicInjector);
DisplayManagerInternal localService = displayManager.new LocalService();
DisplayManagerService.BinderService displayManagerBinderService =
displayManager.new BinderService();
displayManager.overrideSensorManager(mSensorManager);
FakeDisplayDevice displayDevice = createFakeDisplayDevice(displayManager, new float[]{60f});
displayDevice.mDisplayDeviceConfig = mMockDisplayDeviceConfig;
int displayId = getDisplayIdForDisplayDevice(displayManager, displayManagerBinderService,
displayDevice);
String testSensorName = "testName";
String testSensorType = "testType";
Sensor testSensor = TestUtils.createSensor(testSensorType, testSensorName);
DisplayDeviceConfig.SensorData sensorData = new DisplayDeviceConfig.SensorData();
sensorData.type = testSensorType;
sensorData.name = testSensorName;
sensorData.minRefreshRate = 10f;
sensorData.maxRefreshRate = 100f;
when(mMockDisplayDeviceConfig.getProximitySensor()).thenReturn(sensorData);
when(mSensorManager.getSensorList(Sensor.TYPE_ALL)).thenReturn(Collections.singletonList(
testSensor));
SurfaceControl.RefreshRateRange result = localService.getRefreshRateForDisplayAndSensor(
displayId, testSensorName, testSensorType);
assertNotNull(result);
assertEquals(result.min, sensorData.minRefreshRate, FLOAT_TOLERANCE);
assertEquals(result.max, sensorData.maxRefreshRate, FLOAT_TOLERANCE);
}
@Test
public void testReturnsRefreshRateForDisplayAndSensor_proximitySensorNotSet() {
DisplayManagerService displayManager = new DisplayManagerService(mContext, mBasicInjector);
DisplayManagerInternal localService = displayManager.new LocalService();
DisplayManagerService.BinderService displayManagerBinderService =
displayManager.new BinderService();
displayManager.overrideSensorManager(mSensorManager);
FakeDisplayDevice displayDevice = createFakeDisplayDevice(displayManager, new float[]{60f});
displayDevice.mDisplayDeviceConfig = mMockDisplayDeviceConfig;
int displayId = getDisplayIdForDisplayDevice(displayManager, displayManagerBinderService,
displayDevice);
String testSensorName = "testName";
String testSensorType = "testType";
Sensor testSensor = TestUtils.createSensor(testSensorType, testSensorName);
DisplayDeviceConfig.SensorData sensorData = new DisplayDeviceConfig.SensorData();
sensorData.type = testSensorType;
sensorData.name = testSensorName;
sensorData.minRefreshRate = 10f;
sensorData.maxRefreshRate = 100f;
when(mMockDisplayDeviceConfig.getProximitySensor()).thenReturn(null);
when(mSensorManager.getSensorList(Sensor.TYPE_ALL)).thenReturn(Collections.singletonList(
testSensor));
SurfaceControl.RefreshRateRange result = localService.getRefreshRateForDisplayAndSensor(
displayId, testSensorName, testSensorType);
assertNull(result);
}
private void testDisplayInfoFrameRateOverrideModeCompat(boolean compatChangeEnabled)
throws Exception {
DisplayManagerService displayManager =

View File

@@ -18,6 +18,7 @@ package com.android.server.display;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.input.InputSensorInfo;
import android.os.Parcel;
import android.os.SystemClock;
import android.view.DisplayAddress;
@@ -75,6 +76,12 @@ public final class TestUtils {
return sensor;
}
public static Sensor createSensor(String type, String name) {
return new Sensor(new InputSensorInfo(
name, "vendor", 0, 0, 0, 1f, 1f, 1, 1, 1, 1,
type, "", 0, 0, 0));
}
/**
* Create a custom {@link DisplayAddress} to ensure we're not relying on any specific
* display-address implementation in our code. Intentionally uses default object (reference)

View File

@@ -0,0 +1,140 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.display.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;
import android.annotation.Nullable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.input.InputSensorInfo;
import androidx.test.filters.SmallTest;
import com.android.internal.annotations.Keep;
import com.android.server.display.DisplayDeviceConfig.SensorData;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Collections;
import java.util.List;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
@SmallTest
@RunWith(JUnitParamsRunner.class)
public class SensorUtilsTest {
private static final String TEST_SENSOR_NAME = "test_sensor_name";
private static final String TEST_SENSOR_TYPE = "test_sensor_type";
private static final Sensor TEST_SENSOR = createSensor();
@Mock
private SensorManager mSensorManager;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testNoSensorData() {
Sensor result = SensorUtils.findSensor(mSensorManager, null, Sensor.TYPE_LIGHT);
assertNull(result);
}
@Test
public void testNoSensorManager() {
Sensor result = SensorUtils.findSensor(null, new SensorData(), Sensor.TYPE_LIGHT);
assertNull(result);
}
@Keep
private static Object[][] findSensorData() {
// sensorName, sensorType, fallbackType, allSensors, defaultSensor, expectedResult
return new Object[][]{
// no data, no default
{null, null, Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), null, null},
// matching name, matching type, no default
{TEST_SENSOR_NAME, TEST_SENSOR_TYPE, Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), null, TEST_SENSOR},
// matching name, no default
{TEST_SENSOR_NAME, null, Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), null, TEST_SENSOR},
// not matching name, no default
{"not_matching_name", null, Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), null, null},
// matching type, no default
{null, TEST_SENSOR_TYPE, Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), null, TEST_SENSOR},
// not matching type, no default
{null, "not_matching_type", Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), null, null},
// not matching type, matching name, no default
{TEST_SENSOR_NAME, "not_matching_type", Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), null, null},
// not matching name, matching type, no default
{"not_matching_name", TEST_SENSOR_TYPE, Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), null, null},
// not matching type, not matching name, no default
{"not_matching_name", "not_matching_type", Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), null, null},
// not matching type, not matching name, with default
{"not_matching_name", "not_matching_type", Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), TEST_SENSOR, TEST_SENSOR},
// no data, with default
{null, null, Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), TEST_SENSOR, TEST_SENSOR},
// empty data, with default
{"", "", Sensor.TYPE_LIGHT,
Collections.singletonList(TEST_SENSOR), TEST_SENSOR, TEST_SENSOR},
// empty data, with default, no fallback
{"", "", SensorUtils.NO_FALLBACK,
Collections.singletonList(TEST_SENSOR), TEST_SENSOR, null},
};
}
@Test
@Parameters(method = "findSensorData")
public void testFindSensor(@Nullable String sensorName, @Nullable String sensorType,
int fallbackType, List<Sensor> allSensors, @Nullable Sensor defaultSensor,
@Nullable Sensor expectedResult) {
when(mSensorManager.getSensorList(Sensor.TYPE_ALL)).thenReturn(allSensors);
when(mSensorManager.getDefaultSensor(fallbackType)).thenReturn(defaultSensor);
SensorData sensorData = new SensorData();
sensorData.name = sensorName;
sensorData.type = sensorType;
Sensor result = SensorUtils.findSensor(mSensorManager, sensorData, fallbackType);
assertEquals(expectedResult, result);
}
private static Sensor createSensor() {
return new Sensor(new InputSensorInfo(
TEST_SENSOR_NAME, "vendor", 0, 0, 0, 1f, 1f, 1, 1, 1, 1,
TEST_SENSOR_TYPE, "", 0, 0, 0));
}
}