1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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
16 #include <gtest/gtest.h>
17 #define private public
18 #include "dcamera_source_state.h"
19 #undef private
20
21 #include "dcamera_source_controller.h"
22 #include "dcamera_utils_tools.h"
23 #include "icamera_state_listener.h"
24 #include "dcamera_source_controller_channel_listener.h"
25 #include "distributed_camera_errno.h"
26 #include "mock_dcamera_source_dev.h"
27 #include "mock_dcamera_source_state_listener.h"
28
29 using namespace testing::ext;
30
31 namespace OHOS {
32 namespace DistributedHardware {
33 class DCameraSourceControllerTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39
40 std::shared_ptr<EventBus> eventBus_;
41 std::shared_ptr<DCameraSourceDev> camDev_;
42 std::shared_ptr<ICameraStateListener> stateListener_;
43 std::shared_ptr<DCameraSourceStateMachine> stateMachine_;
44 std::shared_ptr<ICameraController> controller_;
45 std::vector<DCameraIndex> indexs_;
46 };
47
48 namespace {
49 const std::string TEST_DEVICE_ID = "bb536a637105409e904d4da83790a4a7";
50 const std::string TEST_CAMERA_DH_ID_0 = "camera_0";
51 const int32_t TEST_WIDTH = 1920;
52 const int32_t TEST_HEIGTH = 1080;
53 const int32_t TEST_FORMAT = 4;
54 const int32_t TEST_DATASPACE = 8;
55 const int32_t TEST_ISCAPTURE = 0;
56 const int32_t TEST_SLEEP_SEC = 200000;
57 }
58
SetUpTestCase(void)59 void DCameraSourceControllerTest::SetUpTestCase(void)
60 {
61 }
62
TearDownTestCase(void)63 void DCameraSourceControllerTest::TearDownTestCase(void)
64 {
65 }
66
SetUp(void)67 void DCameraSourceControllerTest::SetUp(void)
68 {
69 eventBus_ = std::make_shared<EventBus>("SrcController");
70 stateListener_ = std::make_shared<MockDCameraSourceStateListener>();
71 camDev_ = std::make_shared<MockDCameraSourceDev>(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, stateListener_);
72 stateMachine_ = std::make_shared<DCameraSourceStateMachine>(camDev_);
73 controller_ = std::make_shared<DCameraSourceController>(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, stateMachine_,
74 eventBus_);
75 DCameraIndex index;
76 index.devId_ = TEST_DEVICE_ID;
77 index.dhId_ = TEST_CAMERA_DH_ID_0;
78 indexs_.push_back(index);
79 }
80
TearDown(void)81 void DCameraSourceControllerTest::TearDown(void)
82 {
83 usleep(TEST_SLEEP_SEC);
84 eventBus_ = nullptr;
85 stateMachine_ = nullptr;
86 camDev_ = nullptr;
87 stateListener_ = nullptr;
88 controller_ = nullptr;
89 }
90
91 /**
92 * @tc.name: dcamera_source_controller_test_001
93 * @tc.desc: Verify source controller Init.
94 * @tc.type: FUNC
95 * @tc.require: Issue Number
96 */
97 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_001, TestSize.Level1)
98 {
99 int32_t ret = controller_->Init(indexs_);
100 EXPECT_EQ(ret, DCAMERA_OK);
101 }
102 /**
103 * @tc.name: dcamera_source_controller_test_002
104 * @tc.desc: Verify source controller UnInit.
105 * @tc.type: FUNC
106 * @tc.require: Issue Number
107 */
108 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_002, TestSize.Level1)
109 {
110 int32_t ret = controller_->Init(indexs_);
111 EXPECT_EQ(ret, DCAMERA_OK);
112 ret = controller_->UnInit();
113 EXPECT_EQ(ret, DCAMERA_OK);
114 }
115 /**
116 * @tc.name: dcamera_source_controller_test_003
117 * @tc.desc: Verify source controller StartCapture.
118 * @tc.type: FUNC
119 * @tc.require: Issue Number
120 */
121 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_003, TestSize.Level1)
122 {
123 std::vector<std::shared_ptr<DCameraCaptureInfo>> captureInfos;
124 std::shared_ptr<DCameraCaptureInfo> capture = std::make_shared<DCameraCaptureInfo>();
125 capture->width_ = TEST_WIDTH;
126 capture->height_ = TEST_HEIGTH;
127 capture->format_ = TEST_FORMAT;
128 capture->dataspace_ = TEST_DATASPACE;
129 capture->isCapture_ = TEST_ISCAPTURE;
130 capture->encodeType_ = DCEncodeType::ENCODE_TYPE_H264;
131 capture->streamType_ = DCStreamType::SNAPSHOT_FRAME;
132 captureInfos.push_back(capture);
133
134 controller_->Init(indexs_);
135 int32_t ret = controller_->StartCapture(captureInfos);
136 controller_->UnInit();
137 EXPECT_EQ(ret, DCAMERA_BAD_OPERATE);
138 }
139 /**
140 * @tc.name: dcamera_source_controller_test_004
141 * @tc.desc: Verify source controller StartCapture and StopCapture.
142 * @tc.type: FUNC
143 * @tc.require: Issue Number
144 */
145 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_004, TestSize.Level1)
146 {
147 std::vector<std::shared_ptr<DCameraCaptureInfo>> captureInfos;
148 std::shared_ptr<DCameraCaptureInfo> capture = std::make_shared<DCameraCaptureInfo>();
149 capture->width_ = TEST_WIDTH;
150 capture->height_ = TEST_HEIGTH;
151 capture->format_ = TEST_FORMAT;
152 capture->dataspace_ = TEST_DATASPACE;
153 capture->isCapture_ = TEST_ISCAPTURE;
154 capture->encodeType_ = DCEncodeType::ENCODE_TYPE_H264;
155 capture->streamType_ = DCStreamType::SNAPSHOT_FRAME;
156 captureInfos.push_back(capture);
157 controller_->Init(indexs_);
158 int32_t ret = controller_->StartCapture(captureInfos);
159
160 ret = controller_->StopCapture();
161 controller_->UnInit();
162 EXPECT_EQ(ret, DCAMERA_BAD_OPERATE);
163 }
164
165 /**
166 * @tc.name: dcamera_source_controller_test_005
167 * @tc.desc: Verify source controller ChannelNeg.
168 * @tc.type: FUNC
169 * @tc.require: Issue Number
170 */
171 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_005, TestSize.Level1)
172 {
173 std::shared_ptr<DCameraChannelInfo> chanInfo = std::make_shared<DCameraChannelInfo>();
174 int32_t ret = GetLocalDeviceNetworkId(chanInfo->sourceDevId_);
175 DCameraChannelDetail continueChInfo(CONTINUE_SESSION_FLAG, CONTINUOUS_FRAME);
176 DCameraChannelDetail snapShotChInfo(SNAP_SHOT_SESSION_FLAG, SNAPSHOT_FRAME);
177 chanInfo->detail_.push_back(continueChInfo);
178 chanInfo->detail_.push_back(snapShotChInfo);
179 controller_->Init(indexs_);
180 ret = controller_->ChannelNeg(chanInfo);
181 controller_->UnInit();
182 EXPECT_EQ(ret, DCAMERA_BAD_OPERATE);
183 }
184
185 /**
186 * @tc.name: dcamera_source_controller_test_006
187 * @tc.desc: Verify source controller UpdateSettings.
188 * @tc.type: FUNC
189 * @tc.require: Issue Number
190 */
191 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_006, TestSize.Level1)
192 {
193 std::shared_ptr<DCameraChannelInfo> chanInfo = std::make_shared<DCameraChannelInfo>();
194 std::vector<std::shared_ptr<DCameraSettings>> settings;
195 std::shared_ptr<DCameraSettings> setting = std::make_shared<DCameraSettings>();
196 setting->type_ = DCSettingsType::DISABLE_METADATA;
197 setting->value_ = "UpdateSettingsTest";
198 settings.push_back(setting);
199
200 controller_->Init(indexs_);
201 int32_t ret = controller_->UpdateSettings(settings);
202 controller_->UnInit();
203 EXPECT_EQ(ret, DCAMERA_BAD_OPERATE);
204 }
205
206 /**
207 * @tc.name: dcamera_source_controller_test_007
208 * @tc.desc: Verify source controller GetCameraInfo.
209 * @tc.type: FUNC
210 * @tc.require: Issue Number
211 */
212 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_007, TestSize.Level1)
213 {
214 controller_->Init(indexs_);
215 std::shared_ptr<DCameraInfo> camInfo = std::make_shared<DCameraInfo>();
216 camInfo->state_ = 1;
217 int32_t ret = controller_->GetCameraInfo(camInfo);
218 controller_->UnInit();
219 EXPECT_EQ(ret, DCAMERA_BAD_OPERATE);
220 }
221
222 /**
223 * @tc.name: dcamera_source_controller_test_008
224 * @tc.desc: Verify source controller OpenChannel.
225 * @tc.type: FUNC
226 * @tc.require: Issue Number
227 */
228 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_008, TestSize.Level1)
229 {
230 controller_->Init(indexs_);
231 std::shared_ptr<DCameraOpenInfo> openInfo = std::make_shared<DCameraOpenInfo>();
232 int32_t ret = GetLocalDeviceNetworkId(openInfo->sourceDevId_);
233 ret = controller_->OpenChannel(openInfo);
234 controller_->UnInit();
235 EXPECT_EQ(ret, DCAMERA_BAD_OPERATE);
236 }
237
238 /**
239 * @tc.name: dcamera_source_controller_test_009
240 * @tc.desc: Verify source controller OpenChannel and CloseChannel.
241 * @tc.type: FUNC
242 * @tc.require: Issue Number
243 */
244 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_009, TestSize.Level1)
245 {
246 controller_->Init(indexs_);
247 std::shared_ptr<DCameraOpenInfo> openInfo = std::make_shared<DCameraOpenInfo>();
248 int32_t ret = GetLocalDeviceNetworkId(openInfo->sourceDevId_);
249 controller_->OpenChannel(openInfo);
250 ret = controller_->CloseChannel();
251 controller_->UnInit();
252 EXPECT_EQ(ret, DCAMERA_OK);
253 }
254
255 /**
256 * @tc.name: dcamera_source_controller_test_0010
257 * @tc.desc: Verify source controller OnDataReceived.
258 * @tc.type: FUNC
259 * @tc.require: Issue Number
260 */
261 HWTEST_F(DCameraSourceControllerTest, dcamera_source_controller_test_0010, TestSize.Level1)
262 {
263 int32_t ret = controller_->Init(indexs_);
264 std::shared_ptr<DCameraSourceController> controller = std::make_shared<DCameraSourceController>(TEST_DEVICE_ID,
265 TEST_CAMERA_DH_ID_0, stateMachine_, eventBus_);
266 std::shared_ptr<ICameraChannelListener> listener_ =
267 std::make_shared<DCameraSourceControllerChannelListener>(controller);
268 int32_t state = 0;
269 listener_->OnSessionState(state);
270 int32_t eventType = 1;
271 int32_t eventReason = 1;
272 std::string detail = "OnSessionErrorTest";
273 listener_->OnSessionError(eventType, eventReason, detail);
274 std::vector<std::shared_ptr<DataBuffer>> buffers;
275 listener_->OnDataReceived(buffers);
276 ret = controller_->UnInit();
277 EXPECT_EQ(ret, DCAMERA_OK);
278 }
279 }
280 }