1 /* 2 * Copyright (C) 2014 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 29 /** 30 * Tests for {@link JitterVerification}. 31 */ 32 public class JitterVerificationTest extends TestCase { 33 34 public void testVerify() { 35 final int SAMPLE_SIZE = 100; 36 // for unit testing the verification, only the parameter 'sensorMightHaveMoreListeners' is 37 // required 38 TestSensorEnvironment environment = new TestSensorEnvironment( 39 null /* context */, 40 null /* sensor */, 41 false /* sensorMightHaveMoreListeners */, 42 0 /*samplingPeriodUs */, 43 0 /* maxReportLatencyUs */); 44 45 // 100 samples at 1000Hz 46 long[] timestamps = new long[SAMPLE_SIZE]; 47 for (int i = 0; i < SAMPLE_SIZE; i++) { 48 timestamps[i] = i * 100000; 49 } 50 SensorStats stats = new SensorStats(); 51 ISensorVerification verification = getVerification(1, timestamps); 52 verification.verify(environment, stats); 53 verifyStats(stats, true, 0.0); 54 55 // 90 samples at 1000Hz, 10 samples at 2000Hz 56 long timestamp = 0; 57 for (int i = 0; i < SAMPLE_SIZE; i++) { 58 timestamps[i] = timestamp; 59 timestamp += (i % 10 == 0) ? 500000 : 1000000; 60 } 61 stats = new SensorStats(); 62 verification = getVerification(1, timestamps); 63 try { 64 verification.verify(environment, stats); 65 throw new Error("Expected an AssertionError"); 66 } catch (AssertionError e) { 67 // Expected; 68 } 69 verifyStats(stats, false, 25); // 500 us range (250 us in single-sided sense) 70 // divide by 1ms requested sample time x 100% 71 } 72 73 public void testCalculateDelta() { 74 long[] timestamps = new long[]{0, 1, 2, 3, 4}; 75 JitterVerification verification = getVerification(1, timestamps); 76 List<Long> deltaValues = verification.getDeltaValues(); 77 assertEquals(4, deltaValues.size()); 78 assertEquals(1, deltaValues.get(0).longValue()); 79 assertEquals(1, deltaValues.get(1).longValue()); 80 assertEquals(1, deltaValues.get(2).longValue()); 81 assertEquals(1, deltaValues.get(3).longValue()); 82 83 timestamps = new long[]{0, 0, 2, 4, 4}; 84 verification = getVerification(1, timestamps); 85 deltaValues = verification.getDeltaValues(); 86 assertEquals(4, deltaValues.size()); 87 assertEquals(0, deltaValues.get(0).longValue()); 88 assertEquals(2, deltaValues.get(1).longValue()); 89 assertEquals(2, deltaValues.get(2).longValue()); 90 assertEquals(0, deltaValues.get(3).longValue()); 91 92 timestamps = new long[]{0, 1, 4, 9, 16}; 93 verification = getVerification(1, timestamps); 94 deltaValues = verification.getDeltaValues(); 95 assertEquals(4, deltaValues.size()); 96 assertEquals(1, deltaValues.get(0).longValue()); 97 assertEquals(3, deltaValues.get(1).longValue()); 98 assertEquals(5, deltaValues.get(2).longValue()); 99 assertEquals(7, deltaValues.get(3).longValue()); 100 } 101 102 private static JitterVerification getVerification(int marginPercent, long ... timestamps) { 103 Collection<TestSensorEvent> events = new ArrayList<>(timestamps.length); 104 for (long timestamp : timestamps) { 105 events.add(new TestSensorEvent(null, timestamp, 0, null)); 106 } 107 long samplePeriodNs = 1000*1000; //1000Hz 108 long jitterThresholdNs = 20*1000; // 2% of that 109 110 JitterVerification verification = 111 new JitterVerification(marginPercent/100.0f, jitterThresholdNs, samplePeriodNs); 112 verification.addSensorEvents(events); 113 return verification; 114 } 115 116 private void verifyStats(SensorStats stats, boolean passed, double percentageJitter) { 117 assertEquals(passed, stats.getValue(JitterVerification.PASSED_KEY)); 118 assertEquals( 119 percentageJitter, 120 (Double) stats.getValue(SensorStats.JITTER_95_PERCENTILE_PERCENT_KEY), 121 0.01); 122 } 123 } 124