Fix activity monitoring test

Test: runtest -x
packages/services/Car/tests/carservice_test/src/com/android/car/test/SystemActivityMonitoringServiceTest.java

Change-Id: Id6ca97f14f9b46186b4d1f2d3e34849207615d05
Fix: b/62224020
diff --git a/tests/carservice_test/src/com/android/car/test/SystemActivityMonitoringServiceTest.java b/tests/carservice_test/src/com/android/car/test/SystemActivityMonitoringServiceTest.java
index 0b1e718..3345605 100644
--- a/tests/carservice_test/src/com/android/car/test/SystemActivityMonitoringServiceTest.java
+++ b/tests/carservice_test/src/com/android/car/test/SystemActivityMonitoringServiceTest.java
@@ -54,6 +54,11 @@
         // blocking activity.
         mDrivingStatusHandler.setDrivingStatusRestricted(drivingStatusRestricted);
 
+        // Due to asynchronous nature of Car Service initialization, if we won't wait we may inject
+        // an event while SensorHalService is not subscribed yet.
+        assertTrue(getMockedVehicleHal()
+                .waitForSubscriber(VehicleProperty.DRIVING_STATUS, TIMEOUT_MS));
+
         VehiclePropValue injectValue =
                 VehiclePropValueBuilder.newBuilder(VehicleProperty.DRIVING_STATUS)
                         .setTimestamp(SystemClock.elapsedRealtimeNanos())
diff --git a/vehicle-hal-support-lib/src/com/android/car/vehiclehal/test/MockedVehicleHal.java b/vehicle-hal-support-lib/src/com/android/car/vehiclehal/test/MockedVehicleHal.java
index 1a5b8c6..0da3565 100644
--- a/vehicle-hal-support-lib/src/com/android/car/vehiclehal/test/MockedVehicleHal.java
+++ b/vehicle-hal-support-lib/src/com/android/car/vehiclehal/test/MockedVehicleHal.java
@@ -21,8 +21,6 @@
 import static junit.framework.Assert.assertNotNull;
 import static junit.framework.Assert.fail;
 
-import com.google.android.collect.Lists;
-
 import android.hardware.automotive.vehicle.V2_0.IVehicle;
 import android.hardware.automotive.vehicle.V2_0.IVehicleCallback;
 import android.hardware.automotive.vehicle.V2_0.StatusCode;
@@ -31,6 +29,9 @@
 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
 import android.hardware.automotive.vehicle.V2_0.VehiclePropertyAccess;
 import android.os.RemoteException;
+import android.os.SystemClock;
+
+import com.google.android.collect.Lists;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -74,6 +75,23 @@
         addProperty(config, new StaticPropertyHandler(value));
     }
 
+    public boolean waitForSubscriber(int propId, long timeoutMillis) {
+        long startTime = SystemClock.elapsedRealtime();
+        try {
+            synchronized (this) {
+                while (mSubscribers.get(propId) == null) {
+                    long waitMillis = startTime - SystemClock.elapsedRealtime() + timeoutMillis;
+                    if (waitMillis < 0) break;
+                    wait(waitMillis);
+                }
+
+                return mSubscribers.get(propId) != null;
+            }
+        } catch (InterruptedException e) {
+            return false;
+        }
+    }
+
     public synchronized void injectEvent(VehiclePropValue value) {
         List<IVehicleCallback> callbacks = mSubscribers.get(value.prop);
         assertNotNull("Injecting event failed for property: " + value.prop
@@ -156,6 +174,7 @@
             if (subscribers == null) {
                 subscribers = new ArrayList<>();
                 mSubscribers.put(opt.propId, subscribers);
+                notifyAll();
             }
             subscribers.add(callback);
         }