Relieve Sensor Event Gap Test Criterion

* Change the tolerance percentage from 1% to 2%. The maximum is increase
  proportionally as well.
* Fix the tolerance calculation, avoid rounding error.
* Minor change of code FifoLengthVerification and SensorBatching
  FifoTest. Code logic kept unchanged.

Bug: b/24680993

Change-Id: I376bc5a8351c95e8230a61f2749288efc50257f2
diff --git a/tests/tests/hardware/src/android/hardware/cts/SensorBatchingFifoTest.java b/tests/tests/hardware/src/android/hardware/cts/SensorBatchingFifoTest.java
index 4c6362a..328b5cd 100644
--- a/tests/tests/hardware/src/android/hardware/cts/SensorBatchingFifoTest.java
+++ b/tests/tests/hardware/src/android/hardware/cts/SensorBatchingFifoTest.java
@@ -114,7 +114,7 @@
                 sensor,
                 false,
                 sensor.getMinDelay(),
-                Integer.MAX_VALUE);
+                Integer.MAX_VALUE /*maxReportLatencyUs*/);
 
         TestSensorOperation op = TestSensorOperation.createOperation(environment,
                 sensor.getFifoReservedEventCount() * 2);
diff --git a/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/EventGapVerification.java b/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/EventGapVerification.java
index 76f1594..c64810b 100644
--- a/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/EventGapVerification.java
+++ b/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/EventGapVerification.java
@@ -28,15 +28,16 @@
     // Number of events to truncate (discard) from the initial events received
     private static final int TRUNCATE_EVENTS_COUNT = 1;
 
-    // Number of event gaps to tolerate is the minimum of 1% of total events received or 10.
-    private static final int EVENT_GAP_TOLERANCE_COUNT = 10;
-    private static final double EVENT_GAP_TOLERANCE_PERCENT = 0.01;
+    // Number of event gaps to tolerate is 2% of total number of events received rounded up to next
+    // integer or 20, whichever is smaller.
+    private static final int EVENT_GAP_THRESHOLD_MAX = 20;
+    private static final double EVENT_GAP_TOLERANCE = 0.02;
 
     private final int mExpectedDelayUs;
 
     private final List<IndexedEventPair> mEventGaps = new LinkedList<IndexedEventPair>();
     private TestSensorEvent mPreviousEvent = null;
-    private int mIndex = 0;
+    private int mEventCount = 0;
 
     /**
      * Construct a {@link EventGapVerification}
@@ -72,14 +73,16 @@
         }
 
         final int count = mEventGaps.size();
-        // Ensure that eventGapTolerance is at least 1.
-        int eventGapTolerance = (int)Math.max(1, Math.min(EVENT_GAP_TOLERANCE_COUNT,
-                    EVENT_GAP_TOLERANCE_PERCENT * mIndex));
-        stats.addValue(PASSED_KEY, count <= eventGapTolerance);
+        // Ensure the threshold is rounded up.
+        double eventGapThreshold =
+                Math.ceil(Math.min(EVENT_GAP_THRESHOLD_MAX, mEventCount * EVENT_GAP_TOLERANCE));
+        boolean pass = count <= eventGapThreshold;
+
+        stats.addValue(PASSED_KEY, pass);
         stats.addValue(SensorStats.EVENT_GAP_COUNT_KEY, count);
         stats.addValue(SensorStats.EVENT_GAP_POSITIONS_KEY, getIndexArray(mEventGaps));
 
-        if (count > eventGapTolerance) {
+        if (!pass) {
             StringBuilder sb = new StringBuilder();
             sb.append(count).append(" events gaps: ");
             for (int i = 0; i < Math.min(count, TRUNCATE_MESSAGE_LENGTH); i++) {
@@ -109,16 +112,16 @@
      */
     @Override
     protected void addSensorEventInternal(TestSensorEvent event) {
-        if (mIndex >= TRUNCATE_EVENTS_COUNT) {
+        if (mEventCount >= TRUNCATE_EVENTS_COUNT) {
             if (mPreviousEvent != null) {
                 long deltaNs = event.timestamp - mPreviousEvent.timestamp;
                 long deltaUs = TimeUnit.MICROSECONDS.convert(deltaNs, TimeUnit.NANOSECONDS);
                 if (deltaUs > mExpectedDelayUs * THRESHOLD) {
-                    mEventGaps.add(new IndexedEventPair(mIndex, event, mPreviousEvent));
+                    mEventGaps.add(new IndexedEventPair(mEventCount, event, mPreviousEvent));
                 }
             }
             mPreviousEvent = event;
         }
-        mIndex++;
+        mEventCount++;
     }
 }
diff --git a/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/FifoLengthVerification.java b/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/FifoLengthVerification.java
index 09bbdc8..51811a7 100644
--- a/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/FifoLengthVerification.java
+++ b/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/FifoLengthVerification.java
@@ -105,7 +105,7 @@
             // Any event that arrives within before 0.5*expectedReportLatency is considered
             // to be in the same batch of events, else it is considered as the beginning of a new
             // batch.
-            if (timestampDiff < 0.5 * mExpectedReportLatencyUs/1000) {
+            if (timestampDiff < mExpectedReportLatencyUs/1000/2) {
                 batchCount++;
             } else {
                 endofbatch = true;