1 /*
2  * Copyright (C) 2020 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 android.sensorratepermission.cts.returnedrateinfo;
18 
19 import android.content.Context;
20 import android.hardware.Sensor;
21 import android.hardware.SensorManager;
22 import android.hardware.cts.helpers.SensorRatePermissionDirectReportTestHelper;
23 import android.hardware.cts.helpers.SensorRatePermissionEventConnectionTestHelper;
24 
25 import androidx.test.platform.app.InstrumentationRegistry;
26 
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.Parameterized;
32 
33 import java.util.Collection;
34 import java.util.List;
35 
36 /**
37  * Test output of the following methods when the app targets API level >= S.
38  * - getMinDelay()
39  * - getSensorList()
40  * - getHighestDirectReportRateLevel()
41  */
42 @RunWith(Parameterized.class)
43 public class ReturnedRateInfoTest {
44     private static SensorManager mSensorManager;
45 
46     private final int sensorType;
47 
ReturnedRateInfoTest(int sensorType)48     public ReturnedRateInfoTest(int sensorType) {
49         this.sensorType = sensorType;
50     }
51 
52     @Parameterized.Parameters
cappedSensorTypeSet()53     public static Collection cappedSensorTypeSet() {
54         return SensorRatePermissionEventConnectionTestHelper.CAPPED_SENSOR_TYPE_SET;
55     }
56 
57     @Before
setUp()58     public void setUp() {
59         Context context = InstrumentationRegistry.getInstrumentation().getContext();
60         mSensorManager = context.getSystemService(SensorManager.class);
61     }
62 
63     @Test
testGetMinDelayMethod()64     public void testGetMinDelayMethod() {
65         int cappedMinDelayUs = 1 * 1000 * 1000
66                 / SensorRatePermissionEventConnectionTestHelper.CAPPED_SAMPLE_RATE_HZ;
67 
68         Sensor s = mSensorManager.getDefaultSensor(sensorType);
69         if (s == null) {
70             return;
71         }
72         int minDelay = s.getMinDelay();
73 
74         Assert.assertTrue("Min delay cannot be smaller than " + cappedMinDelayUs + " (Us)!",
75                 minDelay >= cappedMinDelayUs);
76     }
77 
78     @Test
testGetSensorListMethod()79     public void testGetSensorListMethod() {
80         int cappedMinDelayUs = 1 * 1000 * 1000
81                 / SensorRatePermissionEventConnectionTestHelper.CAPPED_SAMPLE_RATE_HZ;
82 
83         List<Sensor> allSensorList = mSensorManager.getSensorList(sensorType);
84         if (allSensorList == null) {
85             return;
86         }
87         for (Sensor s : allSensorList) {
88             Assert.assertTrue("Min delay cannot be smaller than " + cappedMinDelayUs + " (Us)!",
89                     s.getMinDelay() >= cappedMinDelayUs);
90         }
91     }
92 
93     @Test
testGetHighestDirectReportRateLevelMethod()94     public void testGetHighestDirectReportRateLevelMethod() {
95         Sensor s = mSensorManager.getDefaultSensor(sensorType);
96         if (s == null) {
97             return;
98         }
99         int obtainedHighestRateLevel = s.getHighestDirectReportRateLevel();
100 
101         Assert.assertTrue("Highest direct report rate level cannot be larger than "
102                         + SensorRatePermissionDirectReportTestHelper.CAPPED_DIRECT_REPORT_RATE_LEVEL,
103                 obtainedHighestRateLevel
104                         <= SensorRatePermissionDirectReportTestHelper.CAPPED_DIRECT_REPORT_RATE_LEVEL);
105     }
106 }