1 2 package android.hardware.cts.helpers.sensorverification; 3 4 import java.util.LinkedList; 5 import java.util.List; 6 import java.util.concurrent.TimeUnit; 7 8 import android.hardware.Sensor; 9 import android.hardware.cts.helpers.SensorStats; 10 import android.hardware.cts.helpers.TestSensorEnvironment; 11 import android.hardware.cts.helpers.TestSensorEvent; 12 import android.hardware.cts.helpers.sensorverification.AbstractSensorVerification.IndexedEventPair; 13 import android.os.SystemClock; 14 import android.provider.Settings.System; 15 16 import junit.framework.Assert; 17 18 /** 19 * A {@link ISensorVerification} which verifies that there are no missing events. This is done by 20 * checking the last received sensor timestamp and checking that it is within 1.8 * the expected 21 * period. 22 */ 23 public class BatchArrivalVerification extends AbstractSensorVerification { 24 public static final String PASSED_KEY = "missing_event_passed"; 25 26 // Batch arrival tolerance is 5 seconds. 27 private static final int BATCH_ARRIVAL_TOLERANCE_US = 5000000; 28 29 // Number of indices to print in assert message before truncating 30 private static final int TRUNCATE_MESSAGE_LENGTH = 3; 31 32 // Number of events to truncate (discard) from the initial events received 33 private static final int TRUNCATE_EVENTS_COUNT = 100; 34 35 private final long mExpectedBatchArrivalTimeUs; 36 37 private final List<IndexedEventPair> mFailures = new LinkedList<IndexedEventPair>(); 38 private TestSensorEvent mFirstEvent = null; 39 private int mIndex = 0; 40 private final long mEstimatedTestStartTimeMs; 41 42 /** 43 * Construct a {@link EventGapVerification} 44 * 45 * @param expectedDelayUs the expected period in us. 46 */ 47 public BatchArrivalVerification(long expectedBatchArrivalTimeUs) { 48 mExpectedBatchArrivalTimeUs = expectedBatchArrivalTimeUs; 49 mEstimatedTestStartTimeMs = SystemClock.elapsedRealtime(); 50 } 51 52 /** 53 * Get the default {@link EventGapVerification}. 54 * 55 * @param environment the test environment 56 * @return the verification or null if the verification is not a continuous mode sensor. 57 */ 58 public static BatchArrivalVerification getDefault(TestSensorEnvironment environment) { 59 if (environment.getSensor().getReportingMode() != Sensor.REPORTING_MODE_CONTINUOUS) { 60 return null; 61 } 62 long fifoMaxEventCount = environment.getSensor().getFifoMaxEventCount(); 63 int maximumExpectedSamplingPeriodUs = environment.getMaximumExpectedSamplingPeriodUs(); 64 long reportLatencyUs = environment.getMaxReportLatencyUs(); 65 if (fifoMaxEventCount > 0 && maximumExpectedSamplingPeriodUs != Integer.MAX_VALUE) { 66 long fifoBasedReportLatencyUs = 67 fifoMaxEventCount * maximumExpectedSamplingPeriodUs; 68 // If the device goes into suspend mode during the test and the sensor under test is 69 // a non wake-up sensor, the FIFO will keep overwriting itself and the reportLatency 70 // of each event will be equal to the time it takes to fill up the FIFO. 71 if (environment.isDeviceSuspendTest() && !environment.getSensor().isWakeUpSensor()) { 72 reportLatencyUs = fifoBasedReportLatencyUs; 73 } else { 74 // In this case the sensor under test is either a wake-up sensor OR it 75 // is a non wake-up sensor but the device does not go into suspend. 76 // So the expected delay of a sensor_event is the minimum of the 77 // fifoBasedReportLatencyUs and the requested latency by the application. 78 reportLatencyUs = Math.min(reportLatencyUs, fifoBasedReportLatencyUs); 79 } 80 } 81 long expectedBatchArrivalTimeUs = reportLatencyUs + SystemClock.elapsedRealtime() * 1000; 82 return new BatchArrivalVerification(expectedBatchArrivalTimeUs); 83 } 84 85 /** 86 * {@inheritDoc} 87 */ 88 @Override 89 public void verify(TestSensorEnvironment environment, SensorStats stats) { 90 final int count = mFailures.size(); 91 stats.addValue(PASSED_KEY, count == 0); 92 stats.addValue(SensorStats.DELAYED_BATCH_DELIVERY, count); 93 94 if (count > 0) { 95 StringBuilder sb = new StringBuilder(); 96 sb.append(count).append(" BatchArrivalDelayed: "); 97 for (int i = 0; i < Math.min(count, TRUNCATE_MESSAGE_LENGTH); i++) { 98 IndexedEventPair info = mFailures.get(i); 99 sb.append(String.format("expectedBatchArrival=%dms actualBatchArrivalTime=%dms "+ 100 "estimedTestStartTime=%dms diff=%dms tolerance=%dms", 101 (mExpectedBatchArrivalTimeUs)/1000, 102 info.event.receivedTimestamp/(1000 * 1000), 103 mEstimatedTestStartTimeMs, 104 (mExpectedBatchArrivalTimeUs - 105 info.event.receivedTimestamp/1000)/1000, 106 BATCH_ARRIVAL_TOLERANCE_US/1000) 107 108 ); 109 110 } 111 if (count > TRUNCATE_MESSAGE_LENGTH) { 112 sb.append(count - TRUNCATE_MESSAGE_LENGTH).append(" more; "); 113 } 114 Assert.fail(sb.toString()); 115 } 116 } 117 118 /** 119 * {@inheritDoc} 120 */ 121 @Override 122 public BatchArrivalVerification clone() { 123 return new BatchArrivalVerification(mExpectedBatchArrivalTimeUs); 124 } 125 126 /** 127 * {@inheritDoc} 128 */ 129 @Override 130 protected void addSensorEventInternal(TestSensorEvent event) { 131 if (mFirstEvent == null) { 132 mFirstEvent = event; 133 } 134 if (mIndex == 1) { 135 if (Math.abs(mFirstEvent.receivedTimestamp/1000 - mExpectedBatchArrivalTimeUs) > 136 BATCH_ARRIVAL_TOLERANCE_US) { 137 mFailures.add(new IndexedEventPair(1, mFirstEvent, null)); 138 } 139 } 140 ++mIndex; 141 } 142 } 143