1 /* 2 * Copyright (C) 2018 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.hardware.cts.helpers.sensorverification; 18 19 import android.hardware.cts.helpers.SensorStats; 20 import android.hardware.cts.helpers.SensorTestWarningException; 21 import android.hardware.cts.helpers.TestSensorEnvironment; 22 import android.hardware.cts.helpers.TestSensorEvent; 23 24 import java.util.ArrayList; 25 import java.util.Collection; 26 import junit.framework.TestCase; 27 28 /** 29 * Tests for {@link OffsetVerification}. 30 */ 31 public class OffsetVerificationTest extends TestCase { 32 33 /** 34 * Test {@link OffsetVerification#verify(TestSensorEnvironment, SensorStats)}. 35 * The test verifies that the indices of the offsets within a sensor event are 36 * correctly used to determine if the offsets exceed the allowed limit. 37 * The format of the test array mirrors that of a {@link SensorEvent}: 38 * v[0] : value 1 39 * v[1] : value 2 40 * v[2] : value 3 41 * v[3] : value 1 offset 42 * v[4] : value 2 offset 43 * v[5] : value 3 offset 44 */ testSingleEventVerify()45 public void testSingleEventVerify() { 46 float[][] values = null; 47 48 // Verify that the norm is calculated correctly 49 values = new float[][]{ {10, 10, 10, 2, 2, 2} }; 50 runStats(2.0f /* threshold */, values, true /* pass */); 51 runStats(1.9f /* threshold */, values, false /* pass */); 52 53 // Verify that the first value from the offsets is used 54 values = new float[][]{ {10, 10, 10, 2, 0, 0} }; 55 runStats(2.0f /* threshold */, values, true /* pass */); 56 runStats(1.9f /* threshold */, values, false /* pass */); 57 58 // Verify that the last value from the offsets is used 59 values = new float[][]{ {10, 10, 10, 0, 0, 2} }; 60 runStats(2.0f /* threshold */, values, true /* pass */); 61 runStats(1.9f /* threshold */, values, false /* pass */); 62 63 // Verify that no values outside the offsets is used 64 values = new float[][]{ {10, 10, 10, 0, 0, 0, 1} }; 65 runStats(0.1f /* threshold */, values, true /* pass */); 66 } 67 68 /** 69 * Test {@link OffsetVerification#verify(TestSensorEnvironment, SensorStats)}. 70 * This test verifies that multiple sensor events are correctly recorded and 71 * verified. 72 */ testMultipleEventVerify()73 public void testMultipleEventVerify() { 74 float[][] values = null; 75 76 values = new float[][] { 77 {10, 10, 10, 2, 2, 2}, 78 {10, 10, 10, 2, 2, 2} 79 }; 80 runStats(2.0f /* threshold */, values, true /* pass */); 81 runStats(1.9f /* threshold */, values, false /* pass */); 82 83 // Verify when the first event exceeds the threshold and the second does not 84 values = new float[][] { 85 {0, 0, 0, 2, 2, 2}, 86 {0, 0, 0, 0, 0, 0} 87 }; 88 runStats(1.9f /* threshold */, values, false /* pass */); 89 90 // Verify when the second event exceeds the threshold and the first does not 91 values = new float[][] { 92 {0, 0, 0, 0, 0, 0}, 93 {0, 0, 0, 10, 10, 10} 94 }; 95 runStats(3.0f /* threshold */, values, false /* pass */); 96 } 97 runStats(float threshold, float[][] values, boolean pass)98 private void runStats(float threshold, float[][] values, boolean pass) { 99 SensorStats stats = new SensorStats(); 100 OffsetVerification verification = getVerification(threshold, values); 101 if (pass) { 102 verification.verify(stats); 103 } else { 104 try { 105 verification.verify(stats); 106 throw new Error("Expected a SensorTestWarningException"); 107 } catch (SensorTestWarningException e) { 108 // Expected; 109 } 110 } 111 assertEquals(pass, stats.getValue(OffsetVerification.PASSED_KEY)); 112 } 113 getVerification( float threshold, float[] ... values)114 private static OffsetVerification getVerification( 115 float threshold, float[] ... values) { 116 Collection<TestSensorEvent> events = new ArrayList<>(values.length); 117 for (float[] value : values) { 118 events.add(new TestSensorEvent(null /* sensor */, 0 /* timestamp */, 119 0 /* receivedTimestamp */, value)); 120 } 121 OffsetVerification verification = new OffsetVerification(threshold); 122 verification.addSensorEvents(events); 123 return verification; 124 } 125 } 126