• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "EvsCamera.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <cstdint>
23 #include <unordered_set>
24 #include <vector>
25 
26 namespace aidl::android::hardware::automotive::evs::implementation {
27 
28 class EvsCameraForTest : public EvsCamera {
29   private:
30     using Base = EvsCamera;
31 
32   public:
33     using EvsCamera::mStreamState;
34     using EvsCamera::shutdown;
35     using EvsCamera::StreamState;
36 
~EvsCameraForTest()37     ~EvsCameraForTest() override { shutdown(); }
38 
allocateOneFrame(buffer_handle_t * handle)39     ::android::status_t allocateOneFrame(buffer_handle_t* handle) override {
40         static std::intptr_t handle_cnt = 0;
41         *handle = reinterpret_cast<buffer_handle_t>(++handle_cnt);
42         return ::android::OK;
43     }
44 
freeOneFrame(const buffer_handle_t)45     void freeOneFrame(const buffer_handle_t /* handle */) override {
46         // Nothing to free because the handles are fake.
47     }
48 
preVideoStreamStart_locked(const std::shared_ptr<evs::IEvsCameraStream> & receiver,ndk::ScopedAStatus & status,std::unique_lock<std::mutex> & lck)49     bool preVideoStreamStart_locked(const std::shared_ptr<evs::IEvsCameraStream>& receiver,
50                                     ndk::ScopedAStatus& status,
51                                     std::unique_lock<std::mutex>& lck) override {
52         mPreStartCalled = true;
53         EXPECT_EQ(mStreamState, StreamState::STOPPED);
54         EXPECT_FALSE(mStreamStarted);
55         EXPECT_FALSE(mStreamStopped);
56         return Base::preVideoStreamStart_locked(receiver, status, lck);
57     }
58 
startVideoStreamImpl_locked(const std::shared_ptr<evs::IEvsCameraStream> &,ndk::ScopedAStatus &,std::unique_lock<std::mutex> &)59     bool startVideoStreamImpl_locked(const std::shared_ptr<evs::IEvsCameraStream>& /* receiver */,
60                                      ndk::ScopedAStatus& /* status */,
61                                      std::unique_lock<std::mutex>& /* lck */) override {
62         EXPECT_EQ(mStreamState, StreamState::RUNNING);
63         EXPECT_FALSE(mStreamStarted);
64         EXPECT_FALSE(mStreamStopped);
65         mStreamStarted = true;
66         return true;
67     }
68 
postVideoStreamStart_locked(const std::shared_ptr<evs::IEvsCameraStream> & receiver,ndk::ScopedAStatus & status,std::unique_lock<std::mutex> & lck)69     bool postVideoStreamStart_locked(const std::shared_ptr<evs::IEvsCameraStream>& receiver,
70                                      ndk::ScopedAStatus& status,
71                                      std::unique_lock<std::mutex>& lck) override {
72         mPostStartCalled = true;
73         EXPECT_EQ(mStreamState, StreamState::RUNNING);
74         EXPECT_TRUE(mStreamStarted);
75         EXPECT_FALSE(mStreamStopped);
76         return Base::postVideoStreamStart_locked(receiver, status, lck);
77     }
78 
preVideoStreamStop_locked(ndk::ScopedAStatus & status,std::unique_lock<std::mutex> & lck)79     bool preVideoStreamStop_locked(ndk::ScopedAStatus& status,
80                                    std::unique_lock<std::mutex>& lck) override {
81         // Skip the check if stop was called before.
82         if (!mPreStopCalled) {
83             mPreStopCalled = true;
84             EXPECT_EQ(mStreamState, StreamState::RUNNING);
85             EXPECT_TRUE(mStreamStarted);
86             EXPECT_FALSE(mStreamStopped);
87         }
88         return Base::preVideoStreamStop_locked(status, lck);
89     }
90 
stopVideoStreamImpl_locked(ndk::ScopedAStatus &,std::unique_lock<std::mutex> &)91     bool stopVideoStreamImpl_locked(ndk::ScopedAStatus& /* status */,
92                                     std::unique_lock<std::mutex>& /* lck */) override {
93         EXPECT_EQ(mStreamState, StreamState::STOPPING);
94         EXPECT_TRUE(mStreamStarted);
95         EXPECT_FALSE(mStreamStopped);
96         mStreamStopped = true;
97         return true;
98     }
99 
postVideoStreamStop_locked(ndk::ScopedAStatus & status,std::unique_lock<std::mutex> & lck)100     bool postVideoStreamStop_locked(ndk::ScopedAStatus& status,
101                                     std::unique_lock<std::mutex>& lck) override {
102         mPostStopCalled = true;
103         const auto ret = Base::postVideoStreamStop_locked(status, lck);
104         EXPECT_EQ(mStreamState, StreamState::STOPPED);
105         EXPECT_TRUE(mStreamStarted);
106         EXPECT_TRUE(mStreamStopped);
107         return ret;
108     }
109 
110     MOCK_METHOD(::ndk::ScopedAStatus, forcePrimaryClient,
111                 (const std::shared_ptr<::aidl::android::hardware::automotive::evs::IEvsDisplay>&
112                          in_display),
113                 (override));
114     MOCK_METHOD(::ndk::ScopedAStatus, getCameraInfo,
115                 (::aidl::android::hardware::automotive::evs::CameraDesc * _aidl_return),
116                 (override));
117     MOCK_METHOD(::ndk::ScopedAStatus, getExtendedInfo,
118                 (int32_t in_opaqueIdentifier, std::vector<uint8_t>* _aidl_return), (override));
119     MOCK_METHOD(::ndk::ScopedAStatus, getIntParameter,
120                 (::aidl::android::hardware::automotive::evs::CameraParam in_id,
121                  std::vector<int32_t>* _aidl_return),
122                 (override));
123     MOCK_METHOD(::ndk::ScopedAStatus, getIntParameterRange,
124                 (::aidl::android::hardware::automotive::evs::CameraParam in_id,
125                  ::aidl::android::hardware::automotive::evs::ParameterRange* _aidl_return),
126                 (override));
127     MOCK_METHOD(::ndk::ScopedAStatus, getParameterList,
128                 (std::vector<::aidl::android::hardware::automotive::evs::CameraParam> *
129                  _aidl_return),
130                 (override));
131     MOCK_METHOD(::ndk::ScopedAStatus, getPhysicalCameraInfo,
132                 (const std::string& in_deviceId,
133                  ::aidl::android::hardware::automotive::evs::CameraDesc* _aidl_return),
134                 (override));
135     MOCK_METHOD(::ndk::ScopedAStatus, setExtendedInfo,
136                 (int32_t in_opaqueIdentifier, const std::vector<uint8_t>& in_opaqueValue),
137                 (override));
138     MOCK_METHOD(::ndk::ScopedAStatus, setIntParameter,
139                 (::aidl::android::hardware::automotive::evs::CameraParam in_id, int32_t in_value,
140                  std::vector<int32_t>* _aidl_return),
141                 (override));
142     MOCK_METHOD(::ndk::ScopedAStatus, setPrimaryClient, (), (override));
143     MOCK_METHOD(::ndk::ScopedAStatus, unsetPrimaryClient, (), (override));
144 
145     bool mStreamStarted = false;
146     bool mStreamStopped = false;
147     bool mPreStartCalled = false;
148     bool mPostStartCalled = false;
149     bool mPreStopCalled = false;
150     bool mPostStopCalled = false;
151 };
152 
153 class MockEvsCameraStream : public evs::IEvsCameraStream {
154     MOCK_METHOD(::ndk::SpAIBinder, asBinder, (), (override));
155     MOCK_METHOD(bool, isRemote, (), (override));
156     MOCK_METHOD(
157             ::ndk::ScopedAStatus, deliverFrame,
158             (const std::vector<::aidl::android::hardware::automotive::evs::BufferDesc>& in_buffer),
159             (override));
160     MOCK_METHOD(::ndk::ScopedAStatus, notify,
161                 (const ::aidl::android::hardware::automotive::evs::EvsEventDesc& in_event),
162                 (override));
163     MOCK_METHOD(::ndk::ScopedAStatus, getInterfaceVersion, (int32_t * _aidl_return), (override));
164     MOCK_METHOD(::ndk::ScopedAStatus, getInterfaceHash, (std::string * _aidl_return), (override));
165 };
166 
167 using StreamState = EvsCameraForTest::StreamState;
168 
TEST(EvsCameraStateTest,StateChangeHooks)169 TEST(EvsCameraStateTest, StateChangeHooks) {
170     auto evsCam = ndk::SharedRefBase::make<EvsCameraForTest>();
171     auto mockStream = ndk::SharedRefBase::make<MockEvsCameraStream>();
172     EXPECT_FALSE(evsCam->mPreStartCalled);
173     EXPECT_FALSE(evsCam->mPostStartCalled);
174     EXPECT_FALSE(evsCam->mPreStopCalled);
175     EXPECT_FALSE(evsCam->mPostStopCalled);
176     EXPECT_FALSE(evsCam->mStreamStarted);
177     EXPECT_FALSE(evsCam->mStreamStopped);
178     EXPECT_EQ(evsCam->mStreamState, StreamState::STOPPED);
179     evsCam->startVideoStream(mockStream);
180 
181     EXPECT_TRUE(evsCam->mPreStartCalled);
182     EXPECT_TRUE(evsCam->mPostStartCalled);
183     EXPECT_FALSE(evsCam->mPreStopCalled);
184     EXPECT_FALSE(evsCam->mPostStopCalled);
185     EXPECT_TRUE(evsCam->mStreamStarted);
186     EXPECT_FALSE(evsCam->mStreamStopped);
187     EXPECT_EQ(evsCam->mStreamState, StreamState::RUNNING);
188     evsCam->stopVideoStream();
189 
190     EXPECT_TRUE(evsCam->mPreStartCalled);
191     EXPECT_TRUE(evsCam->mPostStartCalled);
192     EXPECT_TRUE(evsCam->mPreStopCalled);
193     EXPECT_TRUE(evsCam->mPostStopCalled);
194     EXPECT_TRUE(evsCam->mStreamStarted);
195     EXPECT_TRUE(evsCam->mStreamStopped);
196     EXPECT_EQ(evsCam->mStreamState, StreamState::STOPPED);
197 
198     evsCam->shutdown();
199     EXPECT_EQ(evsCam->mStreamState, StreamState::DEAD);
200 }
201 
202 }  // namespace aidl::android::hardware::automotive::evs::implementation
203