1 /*
2  * Copyright 2023 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 <Common.h>
18 #include <Enumerator.h>
19 
20 #include <random>
21 
22 namespace {
23 
24 using aidl::android::hardware::automotive::evs::CameraDesc;
25 using aidl::android::hardware::automotive::evs::IEvsCamera;
26 using aidl::android::hardware::automotive::evs::IEvsEnumerator;
27 using aidl::android::hardware::automotive::evs::Stream;
28 
29 constexpr int MIN_NUM_DEVICES = 1;
30 constexpr int MAX_NUM_DEVICES = 4;
31 
32 }  // namespace
33 
34 namespace aidl::android::automotive::evs::implementation {
35 
initializeMockEvsHal()36 std::shared_ptr<MockEvsHal> initializeMockEvsHal() {
37     // Initialize a mock EVS HAL with random number of cameras and displays.
38     std::random_device r;
39     std::default_random_engine engine(r());
40     std::uniform_int_distribution<int> uniform_dist(MIN_NUM_DEVICES, MAX_NUM_DEVICES);
41     std::shared_ptr<MockEvsHal> mockEvsHal =
42             std::make_shared<MockEvsHal>(/* numCameras= */ uniform_dist(engine),
43                                          /* numDisplays= */ uniform_dist(engine));
44     EXPECT_NE(mockEvsHal, nullptr);
45     mockEvsHal->initialize();
46 
47     return mockEvsHal;
48 }
49 
openFirstCamera(const std::shared_ptr<MockEvsHal> & handle)50 std::shared_ptr<IEvsCamera> openFirstCamera(const std::shared_ptr<MockEvsHal>& handle) {
51     EXPECT_NE(handle, nullptr);
52 
53     // Get a mock EVS camera.
54     std::shared_ptr<IEvsEnumerator> hwEnumerator = handle->getEnumerator();
55     EXPECT_NE(hwEnumerator, nullptr);
56 
57     std::vector<CameraDesc> cameras;
58     EXPECT_TRUE(hwEnumerator->getCameraList(&cameras).isOk());
59     EXPECT_GT(cameras.size(), 0);
60 
61     std::vector<Stream> configs;
62     EXPECT_TRUE(hwEnumerator->getStreamList(cameras[0], &configs).isOk());
63     EXPECT_GT(configs.size(), 0);
64 
65     std::shared_ptr<IEvsCamera> mockHwCamera;
66     EXPECT_TRUE(hwEnumerator->openCamera(cameras[0].id, configs[0], &mockHwCamera).isOk());
67     EXPECT_NE(mockHwCamera, nullptr);
68 
69     return mockHwCamera;
70 }
71 
72 }  // namespace aidl::android::automotive::evs::implementation
73