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