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 com.android.cts.verifier.sensors;
18 
19 import com.android.cts.verifier.R;
20 import com.android.cts.verifier.sensors.base.SensorCtsVerifierTestActivity;
21 
22 import android.content.Context;
23 import android.hardware.Sensor;
24 import android.hardware.SensorManager;
25 import android.hardware.cts.helpers.TestSensorEnvironment;
26 import android.hardware.cts.helpers.sensoroperations.TestSensorOperation;
27 import android.hardware.cts.helpers.sensorverification.HingeAngleVerification;
28 
29 public class HingeAngleTestActivity extends SensorCtsVerifierTestActivity {
30     private SensorManager mSensorManager;
31 
HingeAngleTestActivity()32     public HingeAngleTestActivity() {
33         super(HingeAngleTestActivity.class, true /* enableRetry */);
34     }
35 
36     @Override
activitySetUp()37     protected void activitySetUp() throws InterruptedException {
38         mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
39 
40         getTestLogger().logInstructions(R.string.snsr_hinge_angle_test_instructions);
41         waitForUserToContinue();
42     }
43 
testExerciseHinge()44     public String testExerciseHinge() throws Throwable {
45         return verifyMeasurements(R.string.snsr_hinge_angle_test_exercise_hinge);
46     }
47 
48     /**
49      * Helper method that verifies hinge sensor measurements meet the following criteria:
50      *  - Maximum range of the sensor isn't exceeded.
51      *  - Sensor resolution is respected
52      *  - Duplicate values are not seen back-to-back.
53      */
verifyMeasurements(int instructionsResId)54     private String verifyMeasurements(int instructionsResId) throws Throwable {
55         Sensor wakeUpSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HINGE_ANGLE);
56         TestSensorEnvironment environment = new TestSensorEnvironment(
57                 getApplicationContext(),
58                 wakeUpSensor,
59                 0, /* samplingPeriod */
60                 SensorManager.SENSOR_DELAY_FASTEST);
61 
62         TestSensorOperation sensorOperation = TestSensorOperation
63                 .createOperation(environment, () -> waitForUser(instructionsResId));
64 
65         HingeAngleVerification verification = new HingeAngleVerification();
66         sensorOperation.addVerification(verification);
67         sensorOperation.execute(getCurrentTestNode());
68 
69         return null;
70     }
71 }