1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CPP_EVS_MANAGER_1_1_TEST_FUZZER_MOCKHWENUMERATOR_H_
16 #define CPP_EVS_MANAGER_1_1_TEST_FUZZER_MOCKHWENUMERATOR_H_
17 
18 #include "MockHWCamera.h"
19 #include "MockHWDisplay.h"
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 using ::android::hardware::automotive::evs::V1_0::DisplayState;
25 
26 namespace android::automotive::evs::V1_1::implementation {
27 
28 class MockHWEnumerator : public ::android::hardware::automotive::evs::V1_1::IEvsEnumerator {
29 public:
MockHWEnumerator()30     MockHWEnumerator() {
31         // four cameras
32         for (uint64_t i = startMockHWCameraId; i < endMockHWCameraId; i++) {
33             sp<MockHWCamera> camera = new MockHWCamera();
34             mHWCameras[i] = camera;
35         }
36 
37         // two displays
38         for (uint8_t i = static_cast<uint8_t>(startMockHWDisplayId);
39              i < static_cast<uint8_t>(endMockHWDisplayId); i++) {
40             sp<MockHWDisplay> display = new MockHWDisplay();
41             mHWDisplays[i] = display;
42             mDisplayPortList[i] = i;
43         }
44     }
45 
46     // Methods from ::android::hardware::automotive::evs::V1_0::IEvsEnumerator follow.
47     MOCK_METHOD(Return<void>, getCameraList, (getCameraList_cb _hidl_cb), (override));
openCamera(const hidl_string & cameraId)48     Return<sp<hardware::automotive::evs::V1_0::IEvsCamera>> openCamera(
49             const hidl_string& cameraId) override {
50         uint64_t idx = std::stoi(cameraId);
51         if (mHWCameras.find(idx) != mHWCameras.end()) {
52             return mHWCameras[idx];
53         }
54         return nullptr;
55     }
56     MOCK_METHOD(Return<void>, closeCamera,
57                 (const sp<hardware::automotive::evs::V1_0::IEvsCamera>& carCamera), (override));
openDisplay()58     Return<sp<IEvsDisplay_1_0>> openDisplay() override { return mHWDisplays.begin()->second; }
closeDisplay(const sp<IEvsDisplay_1_0> & display)59     Return<void> closeDisplay(const sp<IEvsDisplay_1_0>& display) override { return {}; }
60     MOCK_METHOD(Return<DisplayState>, getDisplayState, (), (override));
61 
62     // Methods from ::android::hardware::automotive::evs::V1_1::IEvsEnumerator follow.
getCameraList_1_1(getCameraList_1_1_cb _hidl_cb)63     Return<void> getCameraList_1_1(getCameraList_1_1_cb _hidl_cb) { return {}; }
openCamera_1_1(const hidl_string & cameraId,const Stream & streamCfg)64     Return<sp<IEvsCamera_1_1>> openCamera_1_1(const hidl_string& cameraId,
65                                               const Stream& streamCfg) override {
66         uint64_t idx = std::stoi(cameraId);
67         if (mHWCameras.find(idx) != mHWCameras.end()) {
68             return mHWCameras[idx];
69         }
70         return nullptr;
71     }
72     MOCK_METHOD(Return<bool>, isHardware, (), (override));
getDisplayIdList(getDisplayIdList_cb _list_cb)73     Return<void> getDisplayIdList(getDisplayIdList_cb _list_cb) override {
74         std::vector<uint8_t> ids;
75 
76         ids.resize(mDisplayPortList.size());
77         unsigned i = 0;
78         for (const auto& [port, id] : mDisplayPortList) {
79             ids[i++] = port;
80         }
81 
82         _list_cb(ids);
83         return {};
84     }
openDisplay_1_1(uint8_t port)85     Return<sp<IEvsDisplay_1_1>> openDisplay_1_1(uint8_t port) override {
86         auto iter = mDisplayPortList.find(port);
87         if (iter != mDisplayPortList.end()) {
88             uint64_t id = iter->second;
89             auto it = mHWDisplays.find(id);
90             if (it != mHWDisplays.end()) {
91                 return it->second;
92             }
93             return nullptr;
94         }
95         return nullptr;
96     }
97     MOCK_METHOD(Return<void>, getUltrasonicsArrayList, (getUltrasonicsArrayList_cb _hidl_cb),
98                 (override));
99     MOCK_METHOD(Return<sp<IEvsUltrasonicsArray>>, openUltrasonicsArray,
100                 (const hidl_string& ultrasonicsArrayId), (override));
101     MOCK_METHOD(Return<void>, closeUltrasonicsArray,
102                 (const sp<IEvsUltrasonicsArray>& evsUltrasonicsArray), (override));
103 
104 private:
105     std::map<uint64_t, sp<MockHWDisplay>> mHWDisplays;
106     std::map<uint64_t, sp<MockHWCamera>> mHWCameras;
107     std::map<uint8_t, uint64_t> mDisplayPortList;
108 };
109 
110 }  // namespace android::automotive::evs::V1_1::implementation
111 
112 #endif  // CPP_EVS_MANAGER_1_1_TEST_FUZZER_MOCKHWENUMERATOR_H_
113