• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 junit.framework.TestCase;
20 
21 import android.hardware.cts.helpers.SensorStats;
22 import android.hardware.cts.helpers.TestSensorEnvironment;
23 import android.hardware.cts.helpers.TestSensorEvent;
24 
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Random;
29 
30 /**
31  * Tests for {@link InitialValueVerification}.
32  */
33 public class InitialValueVerificationTest extends TestCase {
34     private static final long INITIAL_WINDOW_LENGTH = 2_000_000_000L; // 2s
35     private static final long TOTAL_WINDOW_LENGTH = 5_000_000_000L; // 5s
36     private static final long SENSOR_PERIOD = 500_000_000L; // 0.5s
37     private static final float MAX_ABSOLUTE_DELTA = 3f;
38     private static final Random random = new Random(123L);
39     private static final float NOISE_STD = 0.01f;
40 
41     /**
42      * Test {@link InitialValueVerification#verify(SensorStats)}.
43      */
testVerify()44     public void testVerify() {
45         float[] initialValues = new float[] {80.4f, 12.3f, -67f};
46         verifyStatsWithTwoWindows(initialValues, initialValues, true);
47 
48         // Only modify the first element in the array but close enough
49         float[] laterValues = new float[] {78.1f, 12.3f, -67f};
50         verifyStatsWithTwoWindows(initialValues, laterValues, true);
51         // Only modify the first element in the array but by more than the MAX_ABSOLUTE_DELTA
52         laterValues = new float[] {70.1f, 12.3f, -67f};
53         verifyStatsWithTwoWindows(initialValues, laterValues, false);
54 
55         // Only modify the second element in the array but close enough
56         laterValues = new float[] {80.4f, 11.3f, -67f};
57         verifyStatsWithTwoWindows(initialValues, laterValues, true);
58         // Only modify the second element in the array but by more than the MAX_ABSOLUTE_DELTA
59         laterValues = new float[] {80.4f, 7.3f, -67f};
60         verifyStatsWithTwoWindows(initialValues, laterValues, false);
61 
62         // Only modify the third element in the array but close enough
63         laterValues = new float[] {80.4f, 12.3f, -65f};
64         verifyStatsWithTwoWindows(initialValues, laterValues, true);
65         // Only modify the third element in the array but by more than the MAX_ABSOLUTE_DELTA
66         laterValues = new float[] {80.4f, 12.3f, 45f};
67         verifyStatsWithTwoWindows(initialValues, laterValues, false);
68     }
69 
getVerification(Collection<TestSensorEvent> events, float maxAbsoluteDelta, long initialWindowLength)70     private static InitialValueVerification getVerification(Collection<TestSensorEvent> events,
71             float maxAbsoluteDelta, long initialWindowLength) {
72         InitialValueVerification verification =
73                 new InitialValueVerification(maxAbsoluteDelta, initialWindowLength);
74         verification.addSensorEvents(events);
75         return verification;
76     }
77 
verifyStatsWithTwoWindows(float[] initialValues, float[] laterValues, boolean pass)78     private static void verifyStatsWithTwoWindows(float[] initialValues, float[] laterValues,
79             boolean pass) {
80         List<TestSensorEvent> events = new ArrayList<>();
81         // Initial window
82         for (long timestamp = 0L; timestamp <= INITIAL_WINDOW_LENGTH; timestamp += SENSOR_PERIOD) {
83             float[] initialValuesWithNoise = addNoise(initialValues);
84             events.add(new TestSensorEvent(null /* sensor */, timestamp, 0 /* accuracy */,
85                     initialValuesWithNoise));
86         }
87         // Later window
88         for (long timestamp = INITIAL_WINDOW_LENGTH
89                 + SENSOR_PERIOD; timestamp <= TOTAL_WINDOW_LENGTH; timestamp += SENSOR_PERIOD) {
90             float[] laterValuesWithNoise = addNoise(laterValues);
91             events.add(new TestSensorEvent(null /* sensor */, timestamp, 0 /* accuracy */,
92                     laterValuesWithNoise));
93         }
94         SensorStats stats = new SensorStats();
95         InitialValueVerification verification =
96                 getVerification(events, MAX_ABSOLUTE_DELTA, INITIAL_WINDOW_LENGTH);
97 
98         try {
99             verification.verify(stats);
100             assertTrue(pass);
101         } catch (AssertionError e) {
102             assertFalse(pass);
103         }
104         verifyStats(stats, pass, initialValues, laterValues);
105     }
106 
addNoise(float[] values)107     private static float[] addNoise(float[] values) {
108         float[] valuesWithNoise = new float[values.length];
109         for(int i = 0; i < values.length; i++) {
110             valuesWithNoise[i] = values[i] + random.nextFloat() * NOISE_STD;
111         }
112         return valuesWithNoise;
113     }
114 
verifyStats(SensorStats stats, boolean passed, float[] initialMeans, float[] laterMeans)115     private static void verifyStats(SensorStats stats, boolean passed, float[] initialMeans,
116             float[] laterMeans) {
117         assertEquals(passed, stats.getValue(InitialValueVerification.PASSED_KEY));
118         float[] actualInitialMeans = (float[]) stats.getValue(SensorStats.INITIAL_MEAN_KEY);
119         float[] actualLaterMeans = (float[]) stats.getValue(SensorStats.LATER_MEAN_KEY);
120         assertEquals(initialMeans.length, actualInitialMeans.length);
121         assertEquals(laterMeans.length, actualLaterMeans.length);
122         for (int i = 0; i < initialMeans.length; i++) {
123             assertEquals(initialMeans[i], actualInitialMeans[i], 0.1);
124             assertEquals(laterMeans[i], actualLaterMeans[i], 0.1);
125         }
126     }
127 }
128