1 /*
2  * Copyright (C) 2020 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 package com.android.car;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.car.Car;
21 import android.car.occupantawareness.OccupantAwarenessDetection;
22 import android.car.occupantawareness.OccupantAwarenessManager;
23 import android.car.occupantawareness.SystemStatusEvent;
24 import android.content.Context;
25 import android.test.suitebuilder.annotation.MediumTest;
26 
27 import androidx.test.core.app.ApplicationProvider;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.TimeUnit;
36 
37 /*
38  * IMPORTANT NOTE:
39  * The test assumes that mock hal for occupant awareness interface is running at the time of test.
40  * Depending on test target, mock hal for occupant awareness interface may need to be added to build
41  * targets. Instructions to add mock hal to build -
42  * PRODUCT_PACKAGES += android.hardware.automotive.occupant_awareness@1.0-service_mock
43  *
44  * Mock hal may need to be launched before running the test -
45  * /system/bin/android.hardware.automotive.occupant_awareness@1.0-service_mock
46  *
47  * The test will likely fail if mock hal is not running on the target.
48  */
49 @RunWith(AndroidJUnit4.class)
50 @MediumTest
51 public final class OccupantAwarenessServiceIntegrationTest {
52     private CompletableFuture<SystemStatusEvent> mFutureStatus;
53     private CompletableFuture<OccupantAwarenessDetection> mFutureDriverDetection;
54     private CompletableFuture<OccupantAwarenessDetection> mFuturePassengerDetection;
55 
56     private final Context mContext = ApplicationProvider.getApplicationContext();
57     private Car mCar = Car.createCar(mContext);
58     private final OccupantAwarenessManager mOasManager =
59             (OccupantAwarenessManager) mCar.getCarManager(Car.OCCUPANT_AWARENESS_SERVICE);
60 
61     @Before
setUp()62     public void setUp() {
63         mOasManager.registerChangeCallback(new CallbackHandler());
64     }
65 
66     @Test
testGetCapabilityForRole()67     public void testGetCapabilityForRole() throws Exception {
68         // Mock hal only supports driver monitoring and presence detection for only driver and
69         // front seat passengers.
70         // This test ensures that correct values are passed to clients of occupant awareness
71         // service.
72         assertThat(mOasManager.getCapabilityForRole(
73                 OccupantAwarenessDetection.VEHICLE_OCCUPANT_DRIVER))
74                 .isEqualTo(SystemStatusEvent.DETECTION_TYPE_PRESENCE
75                     | SystemStatusEvent.DETECTION_TYPE_DRIVER_MONITORING);
76 
77         assertThat(mOasManager.getCapabilityForRole(
78                 OccupantAwarenessDetection.VEHICLE_OCCUPANT_FRONT_PASSENGER))
79                 .isEqualTo(SystemStatusEvent.DETECTION_TYPE_PRESENCE);
80 
81         assertThat(mOasManager.getCapabilityForRole(
82                 OccupantAwarenessDetection.VEHICLE_OCCUPANT_ROW_2_PASSENGER_RIGHT))
83                 .isEqualTo(SystemStatusEvent.DETECTION_TYPE_NONE);
84 
85         assertThat(mOasManager.getCapabilityForRole(
86                 OccupantAwarenessDetection.VEHICLE_OCCUPANT_NONE))
87                 .isEqualTo(SystemStatusEvent.DETECTION_TYPE_NONE);
88     }
89 
90     @Test
testDetectionEvents()91     public void testDetectionEvents() throws Exception {
92         // Since the test assumes mock hal is the source of detections, the pattern of driver and
93         // passenger detection is pre-determined. The test verifies that detections from occupant
94         // awareness manager matches expected detections.
95         OccupantAwarenessDetection driverDetection =
96                 mFutureDriverDetection.get(1, TimeUnit.SECONDS);
97         OccupantAwarenessDetection passengerDetection =
98                 mFuturePassengerDetection.get(1, TimeUnit.SECONDS);
99 
100         assertThat(driverDetection.driverMonitoringDetection.isLookingOnRoad).isFalse();
101         assertThat(passengerDetection.isPresent).isTrue();
102     }
103 
104     @Test
testSystemTransitionsToReady()105     public void testSystemTransitionsToReady() throws Exception {
106         // Since the test assumes mock hal is the source of detections, it is expected that mock hal
107         // is ready to serve requests from occupant awareness service. The test verifies that
108         // occupant awareness manager notifies that the system transitions to ready state.
109         SystemStatusEvent status;
110         status = mFutureStatus.get(1, TimeUnit.SECONDS);
111         assertThat(status.systemStatus).isEqualTo(SystemStatusEvent.SYSTEM_STATUS_READY);
112     }
113 
114     private final class CallbackHandler extends OccupantAwarenessManager.ChangeCallback {
115         @Override
onDetectionEvent(OccupantAwarenessDetection event)116         public void onDetectionEvent(OccupantAwarenessDetection event) {
117             switch(event.role) {
118                 case OccupantAwarenessDetection.VEHICLE_OCCUPANT_DRIVER:
119                     mFutureDriverDetection.complete(event);
120                     break;
121                 case OccupantAwarenessDetection.VEHICLE_OCCUPANT_FRONT_PASSENGER:
122                     mFuturePassengerDetection.complete(event);
123                     break;
124             }
125         }
126 
127         @Override
onSystemStateChanged(SystemStatusEvent systemStatus)128         public void onSystemStateChanged(SystemStatusEvent systemStatus) {
129             mFutureStatus.complete(systemStatus);
130         }
131     }
132 }
133