1 /*
2 * Copyright (C) 2019 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
17 #define LOG_TAG "**** HAL log ****"
18
19 #include <aidl/Gtest.h>
20 #include <aidl/Vintf.h>
21 #include <android-base/logging.h>
22 #include <android/hardware/automotive/occupant_awareness/BnOccupantAwarenessClientCallback.h>
23 #include <android/hardware/automotive/occupant_awareness/IOccupantAwareness.h>
24 #include <binder/IServiceManager.h>
25 #include <binder/ProcessState.h>
26 #include <gtest/gtest.h>
27
28 #include <chrono>
29 #include <future>
30 #include <vector>
31
32 using namespace android::hardware::automotive::occupant_awareness;
33 using android::hardware::automotive::occupant_awareness::IOccupantAwareness;
34
35 using android::ProcessState;
36 using android::sp;
37 using android::String16;
38 using android::binder::Status;
39
40 constexpr auto kTimeout = std::chrono::seconds(3);
41
42 #define EXPECT_OK(ret) ASSERT_TRUE((ret).isOk())
43
44 class OccupantAwarenessCallback : public BnOccupantAwarenessClientCallback {
45 public:
OccupantAwarenessCallback(const std::function<void (int,OccupantAwarenessStatus)> & callback)46 OccupantAwarenessCallback(const std::function<void(int, OccupantAwarenessStatus)>& callback)
47 : mCallback(callback) {}
onSystemStatusChanged(int detectionFlags,OccupantAwarenessStatus status)48 Status onSystemStatusChanged(int detectionFlags, OccupantAwarenessStatus status) override {
49 mCallback(detectionFlags, status);
50 return Status::ok();
51 }
52
onDetectionEvent(const OccupantDetections & detections)53 Status onDetectionEvent(const OccupantDetections& detections) override {
54 (void)detections;
55 return Status::ok();
56 }
57
58 private:
59 std::function<void(int, OccupantAwarenessStatus)> mCallback;
60 };
61
62 class OccupantAwarenessAidl : public testing::TestWithParam<std::string> {
63 public:
SetUp()64 virtual void SetUp() override {
65 mOccupantAwarenessService =
66 android::waitForDeclaredService<IOccupantAwareness>(String16(GetParam().c_str()));
67 ASSERT_NE(mOccupantAwarenessService, nullptr);
68 }
69
70 sp<IOccupantAwareness> mOccupantAwarenessService;
71 };
72
73 // Test that startDetection() returns within the timeout.
TEST_P(OccupantAwarenessAidl,StartDetectionTest)74 TEST_P(OccupantAwarenessAidl, StartDetectionTest) {
75 auto start = std::chrono::system_clock::now();
76 OccupantAwarenessStatus occupantAwarenessStatus;
77 Status status = mOccupantAwarenessService->startDetection(&occupantAwarenessStatus);
78 auto elapsed = std::chrono::system_clock::now() - start;
79 EXPECT_OK(status);
80 ASSERT_LE(elapsed, kTimeout);
81
82 EXPECT_OK(mOccupantAwarenessService->stopDetection(&occupantAwarenessStatus));
83 }
84
85 // Test that getCapabilityForRole() returns supported capabilities for the role. The test only
86 // verifies that the IPC call returns successfully and does not verify the supported capabilities.
TEST_P(OccupantAwarenessAidl,GetCapabilityTest)87 TEST_P(OccupantAwarenessAidl, GetCapabilityTest) {
88 std::vector<Role> rolesToTest = {Role::FRONT_PASSENGER, Role::DRIVER,
89 Role::ROW_2_PASSENGER_LEFT, Role::ROW_2_PASSENGER_CENTER,
90 Role::ROW_2_PASSENGER_RIGHT, Role::ROW_3_PASSENGER_LEFT,
91 Role::ROW_3_PASSENGER_CENTER, Role::ROW_3_PASSENGER_RIGHT,
92 Role::FRONT_OCCUPANTS, Role::ROW_2_OCCUPANTS,
93 Role::ROW_3_OCCUPANTS, Role::ALL_OCCUPANTS};
94
95 for (auto role : rolesToTest) {
96 int32_t capabilities;
97 EXPECT_OK(mOccupantAwarenessService->getCapabilityForRole(role, &capabilities));
98 }
99 }
100
101 // Test that getCapabilityForRole() returns failure when arguments are invalid.
TEST_P(OccupantAwarenessAidl,GetCapabilityFailureTest)102 TEST_P(OccupantAwarenessAidl, GetCapabilityFailureTest) {
103 int32_t capabilities;
104 EXPECT_FALSE(
105 mOccupantAwarenessService->getCapabilityForRole(Role::INVALID, &capabilities).isOk());
106
107 Role invalidRole = static_cast<Role>(static_cast<int>(Role::ALL_OCCUPANTS) + 1);
108 EXPECT_FALSE(
109 mOccupantAwarenessService->getCapabilityForRole(invalidRole, &capabilities).isOk());
110 }
111
112 // Test that getState() returns within the timeout. The test do not attempt to verify the state, but
113 // only checks that the IPC call returns successfully.
TEST_P(OccupantAwarenessAidl,GetStateTest)114 TEST_P(OccupantAwarenessAidl, GetStateTest) {
115 std::vector<Role> rolesToTest = {Role::FRONT_PASSENGER, Role::DRIVER,
116 Role::ROW_2_PASSENGER_LEFT, Role::ROW_2_PASSENGER_CENTER,
117 Role::ROW_2_PASSENGER_RIGHT, Role::ROW_3_PASSENGER_LEFT,
118 Role::ROW_3_PASSENGER_CENTER, Role::ROW_3_PASSENGER_RIGHT,
119 Role::FRONT_OCCUPANTS, Role::ROW_2_OCCUPANTS,
120 Role::ROW_3_OCCUPANTS, Role::ALL_OCCUPANTS};
121
122 std::vector<int> detectionCapabilities = {IOccupantAwareness::CAP_PRESENCE_DETECTION,
123 IOccupantAwareness::CAP_GAZE_DETECTION,
124 IOccupantAwareness::CAP_DRIVER_MONITORING_DETECTION};
125
126 for (auto role : rolesToTest) {
127 for (auto detectionCapability : detectionCapabilities) {
128 OccupantAwarenessStatus oasStatus;
129 EXPECT_OK(mOccupantAwarenessService->getState(role, detectionCapability, &oasStatus));
130 }
131 }
132 }
133
134 // Test that getState() returns failure with invalid args.
TEST_P(OccupantAwarenessAidl,GetStateFailureTest)135 TEST_P(OccupantAwarenessAidl, GetStateFailureTest) {
136 // Verify that getState() returns error when role is invalid (0).
137 OccupantAwarenessStatus oasStatus;
138 EXPECT_FALSE(mOccupantAwarenessService
139 ->getState(Role::INVALID, IOccupantAwareness::CAP_PRESENCE_DETECTION,
140 &oasStatus)
141 .isOk());
142
143 // Verify that getState() returns error when role is invalid (invalid flag).
144 int invalidRole = static_cast<int>(Role::ALL_OCCUPANTS) + 1;
145 EXPECT_FALSE(mOccupantAwarenessService
146 ->getState(static_cast<Role>(invalidRole),
147 IOccupantAwareness::CAP_PRESENCE_DETECTION, &oasStatus)
148 .isOk());
149
150 // Verify that getState() returns error when capability is invalid (none).
151 EXPECT_FALSE(mOccupantAwarenessService
152 ->getState(Role::FRONT_PASSENGER, IOccupantAwareness::CAP_NONE, &oasStatus)
153 .isOk());
154
155 // Verify that getState() returns error when capability is invalid (invalid flag).
156 int invalidDetectionFlags = 0x10;
157 EXPECT_FALSE(mOccupantAwarenessService
158 ->getState(Role::FRONT_PASSENGER, invalidDetectionFlags, &oasStatus)
159 .isOk());
160 }
161
162 // Test that setCallback() returns within the timeout.
TEST_P(OccupantAwarenessAidl,SetCallbackTest)163 TEST_P(OccupantAwarenessAidl, SetCallbackTest) {
164 sp<OccupantAwarenessCallback> callback =
165 new OccupantAwarenessCallback([](int detectionFlags, OccupantAwarenessStatus status) {
166 (void)detectionFlags;
167 (void)status;
168 });
169 auto start = std::chrono::system_clock::now();
170 Status status = mOccupantAwarenessService->setCallback(callback);
171 auto elapsed = std::chrono::system_clock::now() - start;
172 EXPECT_OK(status);
173 ASSERT_LE(elapsed, kTimeout);
174 }
175
176 // Test that setCallback() returns failure with invalid args.
TEST_P(OccupantAwarenessAidl,SetCallbackFailureTest)177 TEST_P(OccupantAwarenessAidl, SetCallbackFailureTest) {
178 sp<OccupantAwarenessCallback> callback = nullptr;
179 Status status = mOccupantAwarenessService->setCallback(callback);
180 EXPECT_FALSE(status.isOk());
181 }
182
183 // Test that getLatestDetection() returns within the timeout.
TEST_P(OccupantAwarenessAidl,GetLatestDetectionTest)184 TEST_P(OccupantAwarenessAidl, GetLatestDetectionTest) {
185 auto start = std::chrono::system_clock::now();
186 OccupantDetections detections;
187 // Do not check status here, since error status is returned when no detection is present.
188 (void)mOccupantAwarenessService->getLatestDetection(&detections);
189 auto elapsed = std::chrono::system_clock::now() - start;
190 ASSERT_LE(elapsed, kTimeout);
191 }
192
193 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(OccupantAwarenessAidl);
194 INSTANTIATE_TEST_SUITE_P(
195 InstantiationName, OccupantAwarenessAidl,
196 testing::ValuesIn(android::getAidlHalInstanceNames(IOccupantAwareness::descriptor)),
197 android::PrintInstanceNameToString);
198
main(int argc,char ** argv)199 int main(int argc, char** argv) {
200 ::testing::InitGoogleTest(&argc, argv);
201 ProcessState::self()->setThreadPoolMaxThreadCount(1);
202 ProcessState::self()->startThreadPool();
203 return RUN_ALL_TESTS();
204 }
205