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.directreportapi31;
18 
19 import android.content.Context;
20 import android.hardware.SensorDirectChannel;
21 import android.hardware.SensorPrivacyManager;
22 import android.hardware.cts.SensorDirectReportTest;
23 import android.hardware.cts.helpers.SensorRatePermissionDirectReportTestHelper;
24 import android.hardware.cts.helpers.SensorRatePermissionEventConnectionTestHelper;
25 import android.os.UserHandle;
26 
27 import androidx.test.platform.app.InstrumentationRegistry;
28 
29 import org.junit.After;
30 import org.junit.Assert;
31 import org.junit.Assume;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.Parameterized;
36 
37 import java.util.Collection;
38 import java.util.List;
39 
40 /**
41  * Test sampling rates obtained by direct connections when:
42  * - The mic toggle is on.
43  * - App targets API 31 (w/o having HIGH_SAMPLING_RATE_SENSORS permission)
44  *
45  * Expected behaviors:
46  * - Sampling rate is capped, regardless of the state of the toggle
47  */
48 @RunWith(Parameterized.class)
49 public class DirectReportAPI31Test {
50     private static SensorRatePermissionDirectReportTestHelper mDirectReportTestHelper;
51     private static SensorPrivacyManager mSensorPrivacyManager;
52     private static int mUserID;
53 
54     private final int sensorType;
55 
DirectReportAPI31Test(int sensorType)56     public DirectReportAPI31Test(int sensorType) {
57         this.sensorType = sensorType;
58     }
59 
60     @Parameterized.Parameters
cappedSensorTypeSet()61     public static Collection cappedSensorTypeSet() {
62         return SensorRatePermissionEventConnectionTestHelper.CAPPED_SENSOR_TYPE_SET;
63     }
64 
65     @Before
setUp()66     public void setUp() {
67         Context context = InstrumentationRegistry.getInstrumentation().getContext();
68         mDirectReportTestHelper = new SensorRatePermissionDirectReportTestHelper(context,
69                 sensorType);
70         Assume.assumeTrue("Failed to create mDirectReportTestHelper!",
71                 mDirectReportTestHelper != null);
72 
73         mSensorPrivacyManager = context.getSystemService(SensorPrivacyManager.class);
74         Assume.assumeTrue(mSensorPrivacyManager
75                 .supportsSensorToggle(SensorPrivacyManager.Sensors.MICROPHONE));
76         mUserID = UserHandle.myUserId();
77     }
78 
79     @After
tearDown()80     public void tearDown() throws InterruptedException {
81         if (mDirectReportTestHelper != null) {
82             mDirectReportTestHelper.flipAndAssertMicToggleOff(mUserID, mSensorPrivacyManager);
83         }
84     }
85 
86     @Test
testSamplingRateMicToggleOn()87     public void testSamplingRateMicToggleOn() throws InterruptedException {
88         mDirectReportTestHelper.flipAndAssertMicToggleOn(mUserID, mSensorPrivacyManager);
89         List<SensorDirectReportTest.DirectReportSensorEvent> events =
90                 mDirectReportTestHelper.getSensorEvents(SensorDirectChannel.RATE_VERY_FAST);
91 
92         double obtainedRate = SensorRatePermissionDirectReportTestHelper.computeAvgRate(events,
93                 Long.MIN_VALUE, Long.MAX_VALUE);
94 
95         Assert.assertTrue(mDirectReportTestHelper.errorWhenExceedCappedRate(),
96                 obtainedRate <= SensorRatePermissionDirectReportTestHelper.CAPPED_SAMPLE_RATE_HZ);
97     }
98 
99     @Test
testSamplingRateMicToggleOff()100     public void testSamplingRateMicToggleOff() throws InterruptedException {
101         mDirectReportTestHelper.flipAndAssertMicToggleOff(mUserID, mSensorPrivacyManager);
102         List<SensorDirectReportTest.DirectReportSensorEvent> events =
103                 mDirectReportTestHelper.getSensorEvents(SensorDirectChannel.RATE_VERY_FAST);
104 
105         double obtainedRate = SensorRatePermissionDirectReportTestHelper.computeAvgRate(events,
106                 Long.MIN_VALUE, Long.MAX_VALUE);
107 
108         Assert.assertTrue(mDirectReportTestHelper.errorWhenExceedCappedRate(),
109                 obtainedRate <= SensorRatePermissionDirectReportTestHelper.CAPPED_SAMPLE_RATE_HZ);
110     }
111 }
112