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.debuggableapi31; 18 19 import static org.junit.Assert.fail; 20 21 import android.content.Context; 22 import android.hardware.HardwareBuffer; 23 import android.hardware.Sensor; 24 import android.hardware.SensorDirectChannel; 25 import android.hardware.SensorManager; 26 import android.hardware.cts.helpers.SensorRatePermissionEventConnectionTestHelper; 27 import android.hardware.cts.helpers.TestSensorEnvironment; 28 import android.hardware.cts.helpers.TestSensorEventListener; 29 30 import androidx.test.platform.app.InstrumentationRegistry; 31 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.concurrent.TimeUnit; 39 40 /** 41 * Test app that runs in debuggable mode, API 31, and requests >= 200 Hz sampling rate 42 * 43 * Expected behavior: 44 * - A Security Exception is thrown when trying to create a connection with the sensor service. 45 */ 46 @RunWith(Parameterized.class) 47 public class DebuggableAPI31Test { 48 private static SensorManager mSensorManager; 49 private static Context mContext; 50 51 private final int sensorType; 52 DebuggableAPI31Test(int sensorType)53 public DebuggableAPI31Test(int sensorType) { 54 this.sensorType = sensorType; 55 } 56 57 @Parameterized.Parameters cappedSensorTypeSet()58 public static Collection cappedSensorTypeSet() { 59 return SensorRatePermissionEventConnectionTestHelper.CAPPED_SENSOR_TYPE_SET; 60 } 61 62 @Before setUp()63 public void setUp() { 64 mContext = InstrumentationRegistry.getInstrumentation().getContext(); 65 mSensorManager = mContext.getSystemService(SensorManager.class); 66 } 67 68 @Test testRegisterListener()69 public void testRegisterListener() { 70 try { 71 Sensor sensor = mSensorManager.getDefaultSensor(sensorType); 72 if (sensor == null) { 73 return; 74 } 75 TestSensorEnvironment testSensorEnvironment = new TestSensorEnvironment( 76 mContext, 77 sensor, 78 SensorManager.SENSOR_DELAY_FASTEST, 79 (int) TimeUnit.SECONDS.toMicros(5)); 80 TestSensorEventListener listener = new TestSensorEventListener(testSensorEnvironment); 81 boolean res = mSensorManager.registerListener( 82 listener, 83 sensor, 84 testSensorEnvironment.getRequestedSamplingPeriodUs(), 85 testSensorEnvironment.getMaxReportLatencyUs()); 86 fail("Should have thrown a SecurityException!"); 87 } catch (Exception e) { 88 // Expected 89 } 90 } 91 92 @Test testDirectChannel()93 public void testDirectChannel() { 94 try { 95 Sensor s = mSensorManager.getDefaultSensor(sensorType); 96 if (s == null) { 97 return; 98 } 99 if (!s.isDirectChannelTypeSupported(SensorDirectChannel.TYPE_HARDWARE_BUFFER) 100 || s.getHighestDirectReportRateLevel() <= SensorDirectChannel.RATE_FAST) { 101 return; 102 } 103 int rateLevel = SensorDirectChannel.RATE_VERY_FAST; 104 HardwareBuffer hardwareBuffer = HardwareBuffer.create( 105 1000, 1, HardwareBuffer.BLOB, 1, 106 HardwareBuffer.USAGE_CPU_READ_OFTEN | HardwareBuffer.USAGE_GPU_DATA_BUFFER 107 | HardwareBuffer.USAGE_SENSOR_DIRECT_DATA); 108 SensorDirectChannel channel = mSensorManager.createDirectChannel(hardwareBuffer); 109 channel.configure(s, rateLevel); 110 hardwareBuffer.close(); 111 fail("Should have thrown a SecurityException"); 112 } catch (SecurityException e) { 113 // Expected 114 } 115 } 116 }