1 /*
2  * Copyright (C) 2022 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 #include "EnumeratorProxy.h"
18 #include "IEnumeratorManager.h"
19 #include "MockEnumeratorManager.h"
20 #include "MockEvsDisplay.h"
21 #include "MockEvsEnumerator.h"
22 #include "MockUltrasonicsArray.h"
23 
24 #include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h>
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27 
28 // EnumeratorProxy is temporarily taking an Enumerator instance and converting
29 // the interface piecemeal. When this is complete, the constructor will be
30 // flipped back and these tests will be restored.
31 #ifdef TEMPORARILY_DISABLE_SEE_B_206829268
32 using ::android::sp;
33 using ::android::automotive::evs::V1_1::implementation::EnumeratorProxy;
34 using ::android::automotive::evs::V1_1::implementation::MockEvsDisplay_1_0;
35 using ::android::automotive::evs::V1_1::implementation::MockEvsEnumerator;
36 using ::android::automotive::evs::V1_1::implementation::NiceMockEnumeratorManager;
37 using ::android::automotive::evs::V1_1::implementation::NiceMockEvsEnumerator;
38 using ::android::automotive::evs::V1_1::implementation::NiceMockUltrasonicsArray;
39 using ::android::hardware::hidl_string;
40 using ::android::hardware::hidl_vec;
41 using ::testing::_;
42 using ::testing::ByMove;
43 using ::testing::Eq;
44 
45 using CameraDesc_1_0 = ::android::hardware::automotive::evs::V1_0::CameraDesc;
46 using CameraDesc_1_1 = ::android::hardware::automotive::evs::V1_1::CameraDesc;
47 using DisplayState_1_0 = ::android::hardware::automotive::evs::V1_0::DisplayState;
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 // These tests don't exercise any functional effects, instead they ensure that
51 // proxying from HIDL focused types proxy properly to x86 host compatible types.
52 ////////////////////////////////////////////////////////////////////////////////
53 
TEST(EnumeratorProxy,Constructs)54 TEST(EnumeratorProxy, Constructs) {
55     EnumeratorProxy enumeratorProxy{std::make_unique<NiceMockEnumeratorManager>()};
56 }
57 
TEST(EnumeratorProxy,GetsCameraList_1_0)58 TEST(EnumeratorProxy, GetsCameraList_1_0) {
59     std::vector<CameraDesc_1_0> expected_value{CameraDesc_1_0{.cameraId = "cam_123",
60                                                               .vendorFlags = 123},
61                                                CameraDesc_1_0{.cameraId = "cam_456",
62                                                               .vendorFlags = 456}};
63 
64     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
65     ON_CALL(*mockEnumeratorManager, getCameraList).WillByDefault(::testing::Return(expected_value));
66 
67     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
68     std::vector<CameraDesc_1_0> result;
69     enumeratorProxy.getCameraList(std::function<void(const hidl_vec<CameraDesc_1_0>&)>{
70             [&](const hidl_vec<CameraDesc_1_0>& cameras) {
71                 result = static_cast<std::vector<CameraDesc_1_0>>(cameras);
72             }});
73 
74     EXPECT_EQ(result, expected_value);
75 }
76 
TEST(EnumeratorProxy,OpensCamera)77 TEST(EnumeratorProxy, OpensCamera) {
78     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
79     EXPECT_CALL(*mockEnumeratorManager, openCamera("cam_123"));
80 
81     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
82     enumeratorProxy.openCamera(hidl_string{"cam_123"});
83 }
84 
TEST(EnumeratorProxy,ClosesCamera)85 TEST(EnumeratorProxy, ClosesCamera) {
86     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
87     EXPECT_CALL(*mockEnumeratorManager, closeCamera);
88 
89     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
90     enumeratorProxy.closeCamera({});
91 }
92 
TEST(EnumeratorProxy,OpensDisplay)93 TEST(EnumeratorProxy, OpensDisplay) {
94     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
95     EXPECT_CALL(*mockEnumeratorManager, openDisplay);
96 
97     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
98     enumeratorProxy.openDisplay();
99 }
100 
TEST(EnumeratorProxy,ClosesDisplay)101 TEST(EnumeratorProxy, ClosesDisplay) {
102     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
103     auto* mockEvsDisplay = new MockEvsDisplay_1_0();
104 
105     EXPECT_CALL(*mockEnumeratorManager, closeDisplay(Eq(mockEvsDisplay)));
106 
107     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
108     enumeratorProxy.closeDisplay(
109             sp<::android::hardware::automotive::evs::V1_0::IEvsDisplay>{mockEvsDisplay});
110 }
111 
TEST(EnumeratorProxy,GetsDisplayState)112 TEST(EnumeratorProxy, GetsDisplayState) {
113     DisplayState_1_0 state{DisplayState_1_0::VISIBLE_ON_NEXT_FRAME};
114 
115     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
116     ON_CALL(*mockEnumeratorManager, getDisplayState).WillByDefault(::testing::Return(state));
117     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
118     EXPECT_EQ(enumeratorProxy.getDisplayState(), DisplayState_1_0::VISIBLE_ON_NEXT_FRAME);
119 }
120 
TEST(EnumeratorProxy,GetsCameraList_1_1)121 TEST(EnumeratorProxy, GetsCameraList_1_1) {
122     std::vector<CameraDesc_1_1> expected_value{CameraDesc_1_1{
123                                                        .v1 = {CameraDesc_1_0{.cameraId = "cam_123",
124                                                                              .vendorFlags = 123}}},
125                                                CameraDesc_1_1{
126                                                        .v1 = {CameraDesc_1_0{.cameraId = "cam_456",
127                                                                              .vendorFlags = 456}}}};
128 
129     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
130     ON_CALL(*mockEnumeratorManager, getCameraList_1_1)
131             .WillByDefault(::testing::Return(expected_value));
132 
133     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
134     std::vector<CameraDesc_1_1> result;
135     enumeratorProxy.getCameraList_1_1(std::function<void(const hidl_vec<CameraDesc_1_1>&)>{
136             [&](const hidl_vec<CameraDesc_1_1>& cameras) {
137                 result = static_cast<std::vector<CameraDesc_1_1>>(cameras);
138             }});
139 
140     EXPECT_EQ(result, expected_value);
141 }
142 
TEST(EnumeratorProxy,OpensCamera_1_1)143 TEST(EnumeratorProxy, OpensCamera_1_1) {
144     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
145     std::unique_ptr<::android::hardware::automotive::evs::V1_1::IEvsCamera> camera;
146 
147     EXPECT_CALL(*mockEnumeratorManager, openCamera_1_1("cam_123", _))
148             .WillOnce(::testing::Return(ByMove(std::move(camera))));
149 
150     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
151     enumeratorProxy.openCamera_1_1(hidl_string{"cam_123"},
152                                    ::android::hardware::camera::device::V3_2::Stream{});
153 }
154 
TEST(EnumeratorProxy,CallsIsHardware)155 TEST(EnumeratorProxy, CallsIsHardware) {
156     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
157     ON_CALL(*mockEnumeratorManager, isHardware).WillByDefault(::testing::Return(false));
158 
159     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
160     EXPECT_EQ(false, enumeratorProxy.isHardware());
161 }
162 
TEST(EnumeratorProxy,GetsDisplayIdList)163 TEST(EnumeratorProxy, GetsDisplayIdList) {
164     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
165     ON_CALL(*mockEnumeratorManager, getDisplayIdList)
166             .WillByDefault(::testing::Return(std::vector<uint8_t>{1, 2, 3, 4}));
167 
168     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
169     std::vector<uint8_t> result;
170     enumeratorProxy.getDisplayIdList(
171             std::function<void(const hidl_vec<uint8_t>&)>{[&](const hidl_vec<std::uint8_t>& ids) {
172                 result = static_cast<std::vector<std::uint8_t>>(ids);
173             }});
174 
175     EXPECT_EQ(result, (std::vector<uint8_t>{1, 2, 3, 4}));
176 }
177 
TEST(EnumeratorProxy,OpensDisplay_1_1)178 TEST(EnumeratorProxy, OpensDisplay_1_1) {
179     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
180     EXPECT_CALL(*mockEnumeratorManager, openDisplay_1_1(123));
181 
182     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
183     enumeratorProxy.openDisplay_1_1(123);
184 }
185 
TEST(EnumeratorProxy,GetsUltrasonicsArrayList)186 TEST(EnumeratorProxy, GetsUltrasonicsArrayList) {
187     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
188     std::vector<::android::hardware::automotive::evs::V1_1::UltrasonicsArrayDesc>
189             ultrasonicsArrayDescriptions;
190     ON_CALL(*mockEnumeratorManager, getUltrasonicsArrayList)
191             .WillByDefault(::testing::Return(ultrasonicsArrayDescriptions));
192     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
193 
194     std::vector<::android::hardware::automotive::evs::V1_1::UltrasonicsArrayDesc> result;
195     enumeratorProxy.getUltrasonicsArrayList(
196             std::function<void(const ::android::hardware::hidl_vec<
197                                ::android::hardware::automotive::evs::V1_1::UltrasonicsArrayDesc>&)>(
198                     [](const ::android::hardware::hidl_vec<
199                             ::android::hardware::automotive::evs::V1_1::UltrasonicsArrayDesc>&) {
200                     }));
201 
202     EXPECT_EQ(result, ultrasonicsArrayDescriptions);
203 }
204 
TEST(EnumeratorProxy,OpensUltrasonicsArrayList)205 TEST(EnumeratorProxy, OpensUltrasonicsArrayList) {
206     auto mockEnumeratorManager = std::make_unique<NiceMockEnumeratorManager>();
207 
208     std::unique_ptr<::android::hardware::automotive::evs::V1_1::IEvsUltrasonicsArray>
209             ultrasonicsArray{std::make_unique<NiceMockUltrasonicsArray>()};
210     EXPECT_CALL(*mockEnumeratorManager, openUltrasonicsArray)
211             .WillOnce(::testing::Return(ByMove(std::move(ultrasonicsArray))));
212 
213     EnumeratorProxy enumeratorProxy{std::move(mockEnumeratorManager)};
214     enumeratorProxy.openUltrasonicsArray("ultrasonics_id");
215 }
216 #endif  // TEMPORARILY_DISABLE_SEE_B_206829268
217