Fixed callback test to support slower update rate

Test: atest CarPropertyManagerTest
Fix: 237335605
Change-Id: I28d66afcfadaab566aa74bfdf49fda09cfb10ceb
diff --git a/tests/tests/car/src/android/car/cts/CarPropertyManagerTest.java b/tests/tests/car/src/android/car/cts/CarPropertyManagerTest.java
index d9cc66a..4d65259 100644
--- a/tests/tests/car/src/android/car/cts/CarPropertyManagerTest.java
+++ b/tests/tests/car/src/android/car/cts/CarPropertyManagerTest.java
@@ -1418,7 +1418,17 @@
 
         // Test for continuous properties
         int vehicleSpeed = VehiclePropertyIds.PERF_VEHICLE_SPEED;
-        CarPropertyEventCounter speedListenerUI = new CarPropertyEventCounter();
+        CarPropertyConfig<?> carPropertyConfig = mCarPropertyManager.getCarPropertyConfig(
+                VehiclePropertyIds.PERF_VEHICLE_SPEED);
+        float secondsToMillis = 1_000;
+        long bufferMillis = 1_000; // 1 second
+        // timeoutMillis is set to the maximum expected time needed to receive the required
+        // number of PERF_VEHICLE_SPEED events for test. If the test does not receive the
+        // required number of events before the timeout expires, it fails.
+        long timeoutMillis =
+                ((long) ((1.0f / carPropertyConfig.getMinSampleRate()) * secondsToMillis
+                        * UI_RATE_EVENT_COUNTER)) + bufferMillis;
+        CarPropertyEventCounter speedListenerUI = new CarPropertyEventCounter(timeoutMillis);
         CarPropertyEventCounter speedListenerFast = new CarPropertyEventCounter();
 
         assertThat(speedListenerUI.receivedEvent(vehicleSpeed)).isEqualTo(NO_EVENTS);
@@ -1428,22 +1438,22 @@
         assertThat(speedListenerFast.receivedError(vehicleSpeed)).isEqualTo(NO_EVENTS);
         assertThat(speedListenerFast.receivedErrorWithErrorCode(vehicleSpeed)).isEqualTo(NO_EVENTS);
 
+        speedListenerUI.resetCountDownLatch(UI_RATE_EVENT_COUNTER);
         mCarPropertyManager.registerCallback(speedListenerUI, vehicleSpeed,
                 CarPropertyManager.SENSOR_RATE_UI);
         mCarPropertyManager.registerCallback(speedListenerFast, vehicleSpeed,
                 CarPropertyManager.SENSOR_RATE_FASTEST);
-        speedListenerUI.resetCountDownLatch(UI_RATE_EVENT_COUNTER);
         speedListenerUI.assertOnChangeEventCalled();
+        mCarPropertyManager.unregisterCallback(speedListenerUI);
+        mCarPropertyManager.unregisterCallback(speedListenerFast);
+
         assertThat(speedListenerUI.receivedEvent(vehicleSpeed)).isGreaterThan(NO_EVENTS);
-        assertThat(speedListenerFast.receivedEvent(vehicleSpeed)).isGreaterThan(
+        assertThat(speedListenerFast.receivedEvent(vehicleSpeed)).isAtLeast(
                 speedListenerUI.receivedEvent(vehicleSpeed));
         // The test did not change property values, it should not get error with error codes.
         assertThat(speedListenerUI.receivedErrorWithErrorCode(vehicleSpeed)).isEqualTo(NO_EVENTS);
         assertThat(speedListenerFast.receivedErrorWithErrorCode(vehicleSpeed)).isEqualTo(NO_EVENTS);
 
-        mCarPropertyManager.unregisterCallback(speedListenerFast);
-        mCarPropertyManager.unregisterCallback(speedListenerUI);
-
         // Test for on_change properties
         int nightMode = VehiclePropertyIds.NIGHT_MODE;
         CarPropertyEventCounter nightModeListener = new CarPropertyEventCounter();
@@ -1452,7 +1462,6 @@
         nightModeListener.assertOnChangeEventCalled();
         assertThat(nightModeListener.receivedEvent(nightMode)).isEqualTo(1);
         mCarPropertyManager.unregisterCallback(nightModeListener);
-
     }
 
     @Test
@@ -1577,6 +1586,15 @@
         private final SparseArray<Integer> mErrorWithErrorCodeCounter = new SparseArray<>();
         private int mCounter = FAST_OR_FASTEST_EVENT_COUNTER;
         private CountDownLatch mCountDownLatch = new CountDownLatch(mCounter);
+        private final long mTimeoutMillis;
+
+        CarPropertyEventCounter(long timeoutMillis) {
+            mTimeoutMillis = timeoutMillis;
+        }
+
+        CarPropertyEventCounter() {
+            this(WAIT_CALLBACK);
+        }
 
         public int receivedEvent(int propId) {
             int val;
@@ -1633,18 +1651,19 @@
         }
 
         public void assertOnChangeEventCalled() throws InterruptedException {
-            if (!mCountDownLatch.await(WAIT_CALLBACK, TimeUnit.MILLISECONDS)) {
-                throw new IllegalStateException("Callback is not called:" + mCounter + "times in "
-                        + WAIT_CALLBACK + " ms.");
+            if (!mCountDownLatch.await(mTimeoutMillis, TimeUnit.MILLISECONDS)) {
+                throw new IllegalStateException(
+                        "Callback is not called " + mCounter + "times in " + mTimeoutMillis
+                                + " ms. It was only called " + (mCounter
+                                - mCountDownLatch.getCount()) + " times.");
             }
         }
 
         public void assertOnChangeEventNotCalled() throws InterruptedException {
             // Once get an event, fail the test.
             mCountDownLatch = new CountDownLatch(1);
-            if (mCountDownLatch.await(WAIT_CALLBACK, TimeUnit.MILLISECONDS)) {
-                throw new IllegalStateException("Callback is called in "
-                        + WAIT_CALLBACK + " ms.");
+            if (mCountDownLatch.await(mTimeoutMillis, TimeUnit.MILLISECONDS)) {
+                throw new IllegalStateException("Callback is called in " + mTimeoutMillis + " ms.");
             }
         }