1 /*
2  * Copyright (C) 2023 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.utils;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 import static org.mockito.Mockito.when;
22 
23 import android.annotation.Nullable;
24 import android.hardware.Sensor;
25 import android.hardware.SensorManager;
26 import android.hardware.input.InputSensorInfo;
27 
28 import androidx.test.filters.SmallTest;
29 
30 import com.android.internal.annotations.Keep;
31 import com.android.server.display.DisplayDeviceConfig.SensorData;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 
39 import java.util.Collections;
40 import java.util.List;
41 
42 import junitparams.JUnitParamsRunner;
43 import junitparams.Parameters;
44 
45 @SmallTest
46 @RunWith(JUnitParamsRunner.class)
47 public class SensorUtilsTest {
48 
49     private static final String TEST_SENSOR_NAME = "test_sensor_name";
50     private static final String TEST_SENSOR_TYPE = "test_sensor_type";
51     private static final Sensor TEST_SENSOR = createSensor();
52     @Mock
53     private SensorManager mSensorManager;
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58     }
59 
60     @Test
testNoSensorData()61     public void testNoSensorData() {
62         Sensor result = SensorUtils.findSensor(mSensorManager, null, Sensor.TYPE_LIGHT);
63         assertNull(result);
64     }
65 
66     @Test
testNoSensorManager()67     public void testNoSensorManager() {
68         Sensor result = SensorUtils.findSensor(null, new SensorData(), Sensor.TYPE_LIGHT);
69         assertNull(result);
70     }
71 
72     @Keep
findSensorData()73     private static Object[][] findSensorData() {
74         // sensorName, sensorType, fallbackType, allSensors, defaultSensor, expectedResult
75         return new Object[][]{
76                 // no data, no default
77                 {null, null, Sensor.TYPE_LIGHT,
78                         Collections.singletonList(TEST_SENSOR), null, null},
79                 // matching name, matching type, no default
80                 {TEST_SENSOR_NAME, TEST_SENSOR_TYPE, Sensor.TYPE_LIGHT,
81                         Collections.singletonList(TEST_SENSOR), null, TEST_SENSOR},
82                 // matching name, no default
83                 {TEST_SENSOR_NAME, null, Sensor.TYPE_LIGHT,
84                         Collections.singletonList(TEST_SENSOR), null, TEST_SENSOR},
85                 // not matching name, no default
86                 {"not_matching_name", null, Sensor.TYPE_LIGHT,
87                         Collections.singletonList(TEST_SENSOR), null, null},
88                 // matching type, no default
89                 {null, TEST_SENSOR_TYPE, Sensor.TYPE_LIGHT,
90                         Collections.singletonList(TEST_SENSOR), null, TEST_SENSOR},
91                 // not matching type, no default
92                 {null, "not_matching_type", Sensor.TYPE_LIGHT,
93                         Collections.singletonList(TEST_SENSOR), null, null},
94                 // not matching type, matching name, no default
95                 {TEST_SENSOR_NAME, "not_matching_type", Sensor.TYPE_LIGHT,
96                         Collections.singletonList(TEST_SENSOR), null, null},
97                 // not matching name, matching type, no default
98                 {"not_matching_name", TEST_SENSOR_TYPE, Sensor.TYPE_LIGHT,
99                         Collections.singletonList(TEST_SENSOR), null, null},
100                 // not matching type, not matching name, no default
101                 {"not_matching_name", "not_matching_type", Sensor.TYPE_LIGHT,
102                         Collections.singletonList(TEST_SENSOR), null, null},
103                 // not matching type, not matching name, with default
104                 {"not_matching_name", "not_matching_type", Sensor.TYPE_LIGHT,
105                         Collections.singletonList(TEST_SENSOR), TEST_SENSOR, TEST_SENSOR},
106                 // no data, with default
107                 {null, null, Sensor.TYPE_LIGHT,
108                         Collections.singletonList(TEST_SENSOR), TEST_SENSOR, TEST_SENSOR},
109                 // empty data, with default
110                 {"", "", Sensor.TYPE_LIGHT,
111                         Collections.singletonList(TEST_SENSOR), TEST_SENSOR, TEST_SENSOR},
112                 // empty data, with default, no fallback
113                 {"", "", SensorUtils.NO_FALLBACK,
114                         Collections.singletonList(TEST_SENSOR), TEST_SENSOR, null},
115         };
116     }
117 
118     @Test
119     @Parameters(method = "findSensorData")
testFindSensor(@ullable String sensorName, @Nullable String sensorType, int fallbackType, List<Sensor> allSensors, @Nullable Sensor defaultSensor, @Nullable Sensor expectedResult)120     public void testFindSensor(@Nullable String sensorName, @Nullable String sensorType,
121             int fallbackType, List<Sensor> allSensors, @Nullable Sensor defaultSensor,
122             @Nullable Sensor expectedResult) {
123         when(mSensorManager.getSensorList(Sensor.TYPE_ALL)).thenReturn(allSensors);
124         when(mSensorManager.getDefaultSensor(fallbackType)).thenReturn(defaultSensor);
125 
126         SensorData sensorData = new SensorData();
127         sensorData.name = sensorName;
128         sensorData.type = sensorType;
129 
130         Sensor result = SensorUtils.findSensor(mSensorManager, sensorData, fallbackType);
131 
132         assertEquals(expectedResult, result);
133     }
134 
createSensor()135     private static Sensor createSensor() {
136         return new Sensor(new InputSensorInfo(
137                 TEST_SENSOR_NAME, "vendor", 0, 0, 0, 1f, 1f, 1, 1, 1, 1,
138                 TEST_SENSOR_TYPE, "", 0, 0, 0));
139     }
140 }
141