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.occupantawareness.OccupantAwarenessDetection;
21 import android.car.occupantawareness.SystemStatusEvent;
22 import android.hardware.automotive.occupant_awareness.IOccupantAwareness;
23 import android.hardware.automotive.occupant_awareness.OccupantAwarenessStatus;
24 import android.test.suitebuilder.annotation.MediumTest;
25 
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import junit.framework.TestCase;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @RunWith(AndroidJUnit4.class)
34 @MediumTest
35 public class OccupantAwarenessUtilsTest extends TestCase {
36 
37     @Test
test_convertToStatusEvent()38     public void test_convertToStatusEvent() {
39         SystemStatusEvent event;
40 
41         event =
42                 OccupantAwarenessUtils.convertToStatusEvent(
43                         IOccupantAwareness.CAP_NONE, OccupantAwarenessStatus.READY);
44         assertThat(event.systemStatus).isEqualTo(SystemStatusEvent.SYSTEM_STATUS_READY);
45         assertThat(event.detectionType).isEqualTo(SystemStatusEvent.DETECTION_TYPE_NONE);
46 
47         event =
48                 OccupantAwarenessUtils.convertToStatusEvent(
49                         IOccupantAwareness.CAP_PRESENCE_DETECTION,
50                         OccupantAwarenessStatus.NOT_SUPPORTED);
51         assertThat(event.systemStatus).isEqualTo(SystemStatusEvent.SYSTEM_STATUS_NOT_SUPPORTED);
52         assertThat(event.detectionType).isEqualTo(SystemStatusEvent.DETECTION_TYPE_PRESENCE);
53 
54         event =
55                 OccupantAwarenessUtils.convertToStatusEvent(
56                         IOccupantAwareness.CAP_GAZE_DETECTION,
57                         OccupantAwarenessStatus.NOT_INITIALIZED);
58         assertThat(event.systemStatus).isEqualTo(SystemStatusEvent.SYSTEM_STATUS_NOT_READY);
59         assertThat(event.detectionType).isEqualTo(SystemStatusEvent.DETECTION_TYPE_GAZE);
60 
61         event =
62                 OccupantAwarenessUtils.convertToStatusEvent(
63                         IOccupantAwareness.CAP_DRIVER_MONITORING_DETECTION,
64                         OccupantAwarenessStatus.FAILURE);
65         assertThat(event.systemStatus).isEqualTo(SystemStatusEvent.SYSTEM_STATUS_SYSTEM_FAILURE);
66         assertThat(event.detectionType)
67                 .isEqualTo(SystemStatusEvent.DETECTION_TYPE_DRIVER_MONITORING);
68     }
69 
70     @Test
test_convertToConfidenceScore()71     public void test_convertToConfidenceScore() {
72         assertThat(
73                         OccupantAwarenessUtils.convertToConfidenceScore(
74                                 android.hardware.automotive.occupant_awareness.ConfidenceLevel.MAX))
75                 .isEqualTo(OccupantAwarenessDetection.CONFIDENCE_LEVEL_MAX);
76 
77         assertThat(
78                         OccupantAwarenessUtils.convertToConfidenceScore(
79                                 android.hardware.automotive.occupant_awareness.ConfidenceLevel
80                                         .HIGH))
81                 .isEqualTo(OccupantAwarenessDetection.CONFIDENCE_LEVEL_HIGH);
82 
83         assertThat(
84                         OccupantAwarenessUtils.convertToConfidenceScore(
85                                 android.hardware.automotive.occupant_awareness.ConfidenceLevel.LOW))
86                 .isEqualTo(OccupantAwarenessDetection.CONFIDENCE_LEVEL_LOW);
87 
88         assertThat(
89                         OccupantAwarenessUtils.convertToConfidenceScore(
90                                 android.hardware.automotive.occupant_awareness.ConfidenceLevel
91                                         .NONE))
92                 .isEqualTo(OccupantAwarenessDetection.CONFIDENCE_LEVEL_NONE);
93     }
94 
95     @Test
test_convertToPoint3D()96     public void test_convertToPoint3D() {
97         assertThat(OccupantAwarenessUtils.convertToPoint3D(null)).isNull();
98         assertThat(OccupantAwarenessUtils.convertToPoint3D(new double[0])).isNull();
99         assertThat(OccupantAwarenessUtils.convertToPoint3D(new double[2])).isNull();
100         assertThat(OccupantAwarenessUtils.convertToPoint3D(new double[] {1, 2, 3})).isNotNull();
101     }
102 
103     @Test
test_convertToRole()104     public void test_convertToRole() {
105         assertThat(
106                         OccupantAwarenessUtils.convertToRole(
107                                 android.hardware.automotive.occupant_awareness.Role.INVALID))
108                 .isEqualTo(OccupantAwarenessDetection.VEHICLE_OCCUPANT_NONE);
109 
110         assertThat(
111                         OccupantAwarenessUtils.convertToRole(
112                                 android.hardware.automotive.occupant_awareness.Role.UNKNOWN))
113                 .isEqualTo(OccupantAwarenessDetection.VEHICLE_OCCUPANT_NONE);
114 
115         assertThat(
116                         OccupantAwarenessUtils.convertToRole(
117                                 android.hardware.automotive.occupant_awareness.Role.DRIVER
118                                         | android.hardware.automotive.occupant_awareness.Role
119                                                 .FRONT_PASSENGER
120                                         | android.hardware.automotive.occupant_awareness.Role
121                                                 .ROW_2_PASSENGER_CENTER))
122                 .isEqualTo(
123                         OccupantAwarenessDetection.VEHICLE_OCCUPANT_DRIVER
124                                 | OccupantAwarenessDetection.VEHICLE_OCCUPANT_FRONT_PASSENGER
125                                 | OccupantAwarenessDetection
126                                         .VEHICLE_OCCUPANT_ROW_2_PASSENGER_CENTER);
127 
128         assertThat(
129                         OccupantAwarenessUtils.convertToRole(
130                                 android.hardware.automotive.occupant_awareness.Role.ALL_OCCUPANTS))
131                 .isEqualTo(OccupantAwarenessDetection.VEHICLE_OCCUPANT_ALL_OCCUPANTS);
132     }
133 }
134