• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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_input.h"
19 #undef private
20 #include "distributed_camera_errno.h"
21 #include "mock_dcamera_source_state_listener.h"
22 #include "dcamera_source_input_channel_listener.h"
23 
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 class DCameraSourceInputTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34 
35     std::shared_ptr<DCameraSourceInput> testInput_;
36     std::shared_ptr<DCameraSourceDev> camDev_;
37     std::shared_ptr<ICameraStateListener> stateListener_;
38 private:
39     static void SetStreamInfos();
40     static void SetCaptureInfos();
41 };
42 
43 namespace {
44 const std::string TEST_DEVICE_ID = "bb536a637105409e904d4da83790a4a7";
45 const std::string TEST_CAMERA_DH_ID_0 = "camera_0";
46 const int32_t TEST_WIDTH = 1920;
47 const int32_t TEST_HEIGTH = 1080;
48 const int32_t TEST_STREAMID = 2;
49 const int32_t TEST_SLEEP_SEC = 200000;
50 std::vector<std::shared_ptr<DCStreamInfo>> g_streamInfos;
51 std::vector<std::shared_ptr<DCCaptureInfo>> g_captureInfos;
52 std::vector<std::shared_ptr<DCameraSettings>> g_cameraSettings;
53 std::vector<int> g_streamIds;
54 std::vector<DCameraIndex> g_camIndexs;
55 }
56 
SetUpTestCase(void)57 void DCameraSourceInputTest::SetUpTestCase(void)
58 {
59 }
60 
SetStreamInfos()61 void DCameraSourceInputTest::SetStreamInfos()
62 {
63     DCameraIndex camIndex1(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0);
64     g_camIndexs.push_back(camIndex1);
65     std::shared_ptr<DCStreamInfo> streamInfo1 = std::make_shared<DCStreamInfo>();
66     streamInfo1->streamId_ = 1;
67     streamInfo1->width_ = TEST_WIDTH;
68     streamInfo1->height_ = TEST_HEIGTH;
69     streamInfo1->stride_ = 1;
70     streamInfo1->format_ = 1;
71     streamInfo1->dataspace_ = 1;
72     streamInfo1->encodeType_ = ENCODE_TYPE_JPEG;
73     streamInfo1->type_ = SNAPSHOT_FRAME;
74 
75     std::shared_ptr<DCStreamInfo> streamInfo2 = std::make_shared<DCStreamInfo>();
76     streamInfo2->streamId_ = TEST_STREAMID;
77     streamInfo2->width_ = TEST_WIDTH;
78     streamInfo2->height_ = TEST_HEIGTH;
79     streamInfo2->stride_ = 1;
80     streamInfo2->format_ = 1;
81     streamInfo2->dataspace_ = 1;
82     streamInfo2->encodeType_ = ENCODE_TYPE_JPEG;
83     streamInfo2->type_ = CONTINUOUS_FRAME;
84     g_streamInfos.push_back(streamInfo1);
85     g_streamInfos.push_back(streamInfo2);
86 }
87 
SetCaptureInfos()88 void DCameraSourceInputTest::SetCaptureInfos()
89 {
90     std::shared_ptr<DCCaptureInfo> captureInfo1 = std::make_shared<DCCaptureInfo>();
91     captureInfo1->streamIds_.push_back(1);
92     captureInfo1->width_ = TEST_WIDTH;
93     captureInfo1->height_ = TEST_HEIGTH;
94     captureInfo1->stride_ = 1;
95     captureInfo1->format_ = 1;
96     captureInfo1->dataspace_ = 1;
97     captureInfo1->encodeType_ = ENCODE_TYPE_H265;
98     captureInfo1->type_ = CONTINUOUS_FRAME;
99 
100     std::shared_ptr<DCCaptureInfo> captureInfo2 = std::make_shared<DCCaptureInfo>();
101     captureInfo2->streamIds_.push_back(1);
102     captureInfo2->width_ = TEST_WIDTH;
103     captureInfo2->height_ = TEST_HEIGTH;
104     captureInfo2->stride_ = 1;
105     captureInfo2->format_ = 1;
106     captureInfo2->dataspace_ = 1;
107     captureInfo2->encodeType_ = ENCODE_TYPE_H265;
108     captureInfo2->type_ = CONTINUOUS_FRAME;
109     g_captureInfos.push_back(captureInfo1);
110     g_captureInfos.push_back(captureInfo2);
111 
112     std::shared_ptr<DCameraSettings> camSettings1 = std::make_shared<DCameraSettings>();
113     camSettings1->type_ = UPDATE_METADATA;
114     camSettings1->value_ = "SettingValue";
115 
116     std::shared_ptr<DCameraSettings> camSettings2 = std::make_shared<DCameraSettings>();
117     camSettings2->type_ = ENABLE_METADATA;
118     camSettings2->value_ = "SettingValue";
119     g_cameraSettings.push_back(camSettings1);
120     g_cameraSettings.push_back(camSettings2);
121 
122     g_streamIds.push_back(1);
123     g_streamIds.push_back(TEST_STREAMID);
124 }
125 
TearDownTestCase(void)126 void DCameraSourceInputTest::TearDownTestCase(void)
127 {
128 }
129 
SetUp(void)130 void DCameraSourceInputTest::SetUp(void)
131 {
132     g_streamInfos.clear();
133     g_captureInfos.clear();
134     g_cameraSettings.clear();
135     g_streamIds.clear();
136     g_camIndexs.clear();
137 
138     SetStreamInfos();
139     SetCaptureInfos();
140 
141     stateListener_ = std::make_shared<MockDCameraSourceStateListener>();
142     camDev_ = std::make_shared<DCameraSourceDev>(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, stateListener_);
143     testInput_ = std::make_shared<DCameraSourceInput>(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, camDev_);
144 }
145 
TearDown(void)146 void DCameraSourceInputTest::TearDown(void)
147 {
148     usleep(TEST_SLEEP_SEC);
149     camDev_ = nullptr;
150     stateListener_ = nullptr;
151     testInput_ = nullptr;
152 }
153 
154 /**
155  * @tc.name: dcamera_source_input_test_001
156  * @tc.desc: Verify source inptut Init.
157  * @tc.type: FUNC
158  * @tc.require: Issue Number
159  */
160 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_001, TestSize.Level1)
161 {
162     EXPECT_EQ(false, testInput_ == nullptr);
163 
164     int32_t rc = testInput_->Init();
165     EXPECT_EQ(rc, DCAMERA_OK);
166 }
167 
168 /**
169  * @tc.name: dcamera_source_input_test_002
170  * @tc.desc: Verify source inptut UnInit.
171  * @tc.type: FUNC
172  * @tc.require: Issue Number
173  */
174 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_002, TestSize.Level1)
175 {
176     EXPECT_EQ(false, testInput_ == nullptr);
177 
178     int32_t rc = testInput_->Init();
179     EXPECT_EQ(rc, DCAMERA_OK);
180 
181     rc = testInput_->UnInit();
182     EXPECT_EQ(rc, DCAMERA_OK);
183 }
184 
185 /**
186  * @tc.name: dcamera_source_input_test_003
187  * @tc.desc: Verify source inptut ConfigStreams.
188  * @tc.type: FUNC
189  * @tc.require: Issue Number
190  */
191 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_003, TestSize.Level1)
192 {
193     EXPECT_EQ(false, testInput_ == nullptr);
194 
195     int32_t rc = testInput_->Init();
196     EXPECT_EQ(rc, DCAMERA_OK);
197 
198     rc = testInput_->ConfigStreams(g_streamInfos);
199     EXPECT_EQ(rc, DCAMERA_OK);
200 
201     std::vector<std::shared_ptr<DCStreamInfo>> streamInfos;
202     rc = testInput_->ConfigStreams(streamInfos);
203     EXPECT_EQ(rc, DCAMERA_OK);
204 
205     rc = testInput_->UnInit();
206     EXPECT_EQ(rc, DCAMERA_OK);
207 }
208 
209 /**
210  * @tc.name: dcamera_source_input_test_004
211  * @tc.desc: Verify source inptut ReleaseStreams.
212  * @tc.type: FUNC
213  * @tc.require: Issue Number
214  */
215 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_004, TestSize.Level1)
216 {
217     EXPECT_EQ(false, testInput_ == nullptr);
218 
219     int32_t rc = testInput_->Init();
220     EXPECT_EQ(rc, DCAMERA_OK);
221 
222     rc = testInput_->ConfigStreams(g_streamInfos);
223     EXPECT_EQ(rc, DCAMERA_OK);
224 
225     bool isAllRelease = true;
226     rc = testInput_->ReleaseStreams(g_streamIds, isAllRelease);
227     EXPECT_EQ(rc, DCAMERA_OK);
228 
229     rc = testInput_->UnInit();
230     EXPECT_EQ(rc, DCAMERA_OK);
231 }
232 
233 /**
234  * @tc.name: dcamera_source_input_test_005
235  * @tc.desc: Verify source inptut ReleaseAllStreams.
236  * @tc.type: FUNC
237  * @tc.require: Issue Number
238  */
239 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_005, TestSize.Level1)
240 {
241     EXPECT_EQ(false, testInput_ == nullptr);
242 
243     int32_t rc = testInput_->Init();
244     EXPECT_EQ(rc, DCAMERA_OK);
245 
246     rc = testInput_->ConfigStreams(g_streamInfos);
247     EXPECT_EQ(rc, DCAMERA_OK);
248 
249     rc = testInput_->ReleaseAllStreams();
250     EXPECT_EQ(rc, DCAMERA_OK);
251 
252     rc = testInput_->UnInit();
253     EXPECT_EQ(rc, DCAMERA_OK);
254 }
255 
256 /**
257  * @tc.name: dcamera_source_input_test_006
258  * @tc.desc: Verify source inptut StartCapture.
259  * @tc.type: FUNC
260  * @tc.require: Issue Number
261  */
262 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_006, TestSize.Level1)
263 {
264     EXPECT_EQ(false, testInput_ == nullptr);
265 
266     int32_t rc = testInput_->Init();
267     EXPECT_EQ(rc, DCAMERA_OK);
268 
269     rc = testInput_->StartCapture(g_captureInfos);
270     EXPECT_EQ(rc, DCAMERA_OK);
271 
272     rc = testInput_->UnInit();
273     EXPECT_EQ(rc, DCAMERA_OK);
274 }
275 
276 /**
277  * @tc.name: dcamera_source_input_test_007
278  * @tc.desc: Verify source inptut StopCapture.
279  * @tc.type: FUNC
280  * @tc.require: Issue Number
281  */
282 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_007, TestSize.Level1)
283 {
284     EXPECT_EQ(false, testInput_ == nullptr);
285 
286     int32_t rc = testInput_->Init();
287     EXPECT_EQ(rc, DCAMERA_OK);
288 
289     rc = testInput_->StartCapture(g_captureInfos);
290     EXPECT_EQ(rc, DCAMERA_OK);
291 
292     bool isAllStop = true;
293     rc = testInput_->StopCapture(g_streamIds, isAllStop);
294     EXPECT_EQ(rc, DCAMERA_OK);
295 
296     rc = testInput_->UnInit();
297     EXPECT_EQ(rc, DCAMERA_OK);
298 }
299 
300 /**
301  * @tc.name: dcamera_source_input_test_008
302  * @tc.desc: Verify source inptut StopAllCapture.
303  * @tc.type: FUNC
304  * @tc.require: Issue Number
305  */
306 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_008, TestSize.Level1)
307 {
308     EXPECT_EQ(false, testInput_ == nullptr);
309 
310     int32_t rc = testInput_->Init();
311     EXPECT_EQ(rc, DCAMERA_OK);
312 
313     rc = testInput_->StartCapture(g_captureInfos);
314     EXPECT_EQ(rc, DCAMERA_OK);
315 
316     rc = testInput_->StopAllCapture();
317     EXPECT_EQ(rc, DCAMERA_OK);
318 
319     rc = testInput_->UnInit();
320     EXPECT_EQ(rc, DCAMERA_OK);
321 }
322 
323 /**
324  * @tc.name: dcamera_source_input_test_009
325  * @tc.desc: Verify source inptut OpenChannel.
326  * @tc.type: FUNC
327  * @tc.require: Issue Number
328  */
329 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_009, TestSize.Level1)
330 {
331     EXPECT_EQ(false, testInput_ == nullptr);
332 
333     int32_t rc = testInput_->Init();
334     EXPECT_EQ(rc, DCAMERA_OK);
335 
336     rc = camDev_->InitDCameraSourceDev();
337     EXPECT_EQ(rc, DCAMERA_OK);
338 
339     rc = testInput_->OpenChannel(g_camIndexs);
340     EXPECT_NE(rc, DCAMERA_OK);
341     rc = testInput_->UnInit();
342     EXPECT_EQ(rc, DCAMERA_OK);
343 }
344 
345 /**
346  * @tc.name: dcamera_source_input_test_010
347  * @tc.desc: Verify source inptut CloseChannel.
348  * @tc.type: FUNC
349  * @tc.require: Issue Number
350  */
351 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_010, TestSize.Level1)
352 {
353     EXPECT_EQ(false, testInput_ == nullptr);
354 
355     int32_t rc = testInput_->Init();
356     EXPECT_EQ(rc, DCAMERA_OK);
357 
358     rc = camDev_->InitDCameraSourceDev();
359     EXPECT_EQ(rc, DCAMERA_OK);
360 
361     rc = testInput_->OpenChannel(g_camIndexs);
362     EXPECT_NE(rc, DCAMERA_OK);
363     rc = testInput_->CloseChannel();
364     EXPECT_EQ(rc, DCAMERA_OK);
365 
366     rc = testInput_->UnInit();
367     EXPECT_EQ(rc, DCAMERA_OK);
368 }
369 
370 /**
371  * @tc.name: dcamera_source_input_test_011
372  * @tc.desc: Verify source inptut UpdateSettings.
373  * @tc.type: FUNC
374  * @tc.require: Issue Number
375  */
376 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_011, TestSize.Level1)
377 {
378     EXPECT_EQ(false, testInput_ == nullptr);
379 
380     int32_t rc = testInput_->Init();
381     EXPECT_EQ(rc, DCAMERA_OK);
382 
383     rc = testInput_->UpdateSettings(g_cameraSettings);
384     EXPECT_EQ(rc, DCAMERA_OK);
385 
386     rc = testInput_->UnInit();
387     EXPECT_EQ(rc, DCAMERA_OK);
388 }
389 
390 /**
391  * @tc.name: dcamera_source_input_test_012
392  * @tc.desc: Verify source inptut OnDataReceived.
393  * @tc.type: FUNC
394  * @tc.require: Issue Number
395  */
396 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_012, TestSize.Level1)
397 {
398     int32_t rc = testInput_->Init();
399     std::vector<std::shared_ptr<DataBuffer>> buffers;
400     size_t capacity = 1;
401     std::shared_ptr<DataBuffer> buffer = std::make_shared<DataBuffer>(capacity);
402     buffers.push_back(buffer);
403     testInput_->OnDataReceived(DCStreamType::SNAPSHOT_FRAME, buffers);
404     EXPECT_EQ(rc, DCAMERA_OK);
405 }
406 
407 /**
408  * @tc.name: dcamera_source_input_test_013
409  * @tc.desc: Verify source inptut EstablishSnapshotFrameSession.
410  * @tc.type: FUNC
411  * @tc.require: Issue Number
412  */
413 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_013, TestSize.Level1)
414 {
415     int32_t rc = testInput_->Init();
416     rc = camDev_->InitDCameraSourceDev();
417     EXPECT_EQ(rc, DCAMERA_OK);
418 
419     testInput_->FinshFrameAsyncTrace(DCStreamType::CONTINUOUS_FRAME);
420     testInput_->FinshFrameAsyncTrace(DCStreamType::SNAPSHOT_FRAME);
421     int32_t state = 0;
422     testInput_->OnSessionState(DCStreamType::CONTINUOUS_FRAME, state);
423     state = 1;
424     testInput_->OnSessionState(DCStreamType::CONTINUOUS_FRAME, state);
425     state = 2;
426     testInput_->OnSessionState(DCStreamType::CONTINUOUS_FRAME, state);
427     rc = testInput_->EstablishSnapshotFrameSession(g_camIndexs);
428     EXPECT_NE(rc, DCAMERA_OK);
429 
430     rc = testInput_->EstablishContinuousFrameSession(g_camIndexs);
431     EXPECT_NE(rc, DCAMERA_OK);
432 }
433 
434 /**
435  * @tc.name: dcamera_source_input_test_014
436  * @tc.desc: Verify source inptut EstablishSnapshotFrameSession.
437  * @tc.type: FUNC
438  * @tc.require: Issue Number
439  */
440 HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_014, TestSize.Level1)
441 {
442     auto testInputListener =
443         std::make_shared<DCameraSourceInputChannelListener>(testInput_, DCStreamType::CONTINUOUS_FRAME);
444     int32_t state = 0;
445     std::string networkId = "networkId";
446     testInputListener->OnSessionState(state, networkId);
447     int32_t eventType = 0;
448     int32_t eventReason = 1;
449     std::string detail = "detail";
450     testInputListener->OnSessionError(eventType, eventReason, detail);
451     std::vector<std::shared_ptr<DataBuffer>> buffers;
452     size_t capacity = 0;
453     std::shared_ptr<DataBuffer> dataBuffer = std::make_shared<DataBuffer>(capacity);
454     buffers.push_back(dataBuffer);
455     testInputListener->OnDataReceived(buffers);
456     EXPECT_EQ(true, capacity == 0);
457 }
458 } // namespace DistributedHardware
459 } // namespace OHOS
460