1 /*
2  * Copyright (c) 2022-2024 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 
18 #include "dcamera_pipeline_sink.h"
19 #include "fps_controller_process.h"
20 #include "distributed_camera_constants.h"
21 #include "distributed_camera_errno.h"
22 #include "mock_dcamera_data_process_listener.h"
23 
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 class DCameraPipelineSinkTest : 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<IDataProcessPipeline> testSinkPipeline_;
36     std::shared_ptr<DCameraPipelineSink> testPipelineSink_;
37 };
38 
39 namespace {
40 const int32_t TEST_WIDTH = 1920;
41 const int32_t TEST_HEIGTH = 1080;
42 const int32_t SLEEP_TIME = 200000;
43 }
44 
SetUpTestCase(void)45 void DCameraPipelineSinkTest::SetUpTestCase(void)
46 {
47 }
48 
TearDownTestCase(void)49 void DCameraPipelineSinkTest::TearDownTestCase(void)
50 {
51 }
52 
SetUp(void)53 void DCameraPipelineSinkTest::SetUp(void)
54 {
55     testSinkPipeline_ = std::make_shared<DCameraPipelineSink>();
56     testPipelineSink_ = std::make_shared<DCameraPipelineSink>();
57 }
58 
TearDown(void)59 void DCameraPipelineSinkTest::TearDown(void)
60 {
61     testSinkPipeline_ = nullptr;
62     testPipelineSink_ = nullptr;
63 }
64 
65 /**
66  * @tc.name: dcamera_pipeline_sink_test_001
67  * @tc.desc: Verify pipeline sink CreateDataProcessPipeline normal.
68  * @tc.type: FUNC
69  * @tc.require: Issue Number
70  */
71 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_001, TestSize.Level1)
72 {
73     EXPECT_EQ(false, testSinkPipeline_ == nullptr);
74 
75     std::shared_ptr<DataProcessListener> listener = std::make_shared<MockDCameraDataProcessListener>();
76     VideoConfigParams srcParams(VideoCodecType::CODEC_H264,
77                                 Videoformat::NV21,
78                                 DCAMERA_PRODUCER_FPS_DEFAULT,
79                                 TEST_WIDTH,
80                                 TEST_HEIGTH);
81     VideoConfigParams destParams(VideoCodecType::CODEC_H264,
82                                  Videoformat::NV21,
83                                  DCAMERA_PRODUCER_FPS_DEFAULT,
84                                  TEST_WIDTH,
85                                  TEST_HEIGTH);
86     int32_t rc = testSinkPipeline_->CreateDataProcessPipeline(PipelineType::VIDEO, srcParams, destParams, listener);
87     EXPECT_EQ(rc, DCAMERA_OK);
88     usleep(SLEEP_TIME);
89 }
90 
91 /**
92  * @tc.name: dcamera_pipeline_sink_test_002
93  * @tc.desc: Verify pipeline sink ProcessData normal.
94  * @tc.type: FUNC
95  * @tc.require: Issue Number
96  */
97 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_002, TestSize.Level1)
98 {
99     EXPECT_EQ(false, testSinkPipeline_ == nullptr);
100 
101     std::shared_ptr<DataProcessListener> listener = std::make_shared<MockDCameraDataProcessListener>();
102     VideoConfigParams srcParams(VideoCodecType::CODEC_H264,
103                                 Videoformat::NV21,
104                                 DCAMERA_PRODUCER_FPS_DEFAULT,
105                                 TEST_WIDTH,
106                                 TEST_HEIGTH);
107     VideoConfigParams destParams(VideoCodecType::CODEC_H264,
108                                  Videoformat::NV21,
109                                  DCAMERA_PRODUCER_FPS_DEFAULT,
110                                  TEST_WIDTH,
111                                  TEST_HEIGTH);
112     int32_t rc = testSinkPipeline_->CreateDataProcessPipeline(PipelineType::VIDEO, srcParams, destParams, listener);
113     EXPECT_EQ(rc, DCAMERA_OK);
114 
115     size_t capacity = 100;
116     std::vector<std::shared_ptr<DataBuffer>> buffers;
117     std::shared_ptr<DataBuffer> db = std::make_shared<DataBuffer>(capacity);
118     buffers.push_back(db);
119     rc = testSinkPipeline_->ProcessData(buffers);
120     EXPECT_EQ(rc, DCAMERA_OK);
121 
122     usleep(SLEEP_TIME);
123 }
124 
125 /**
126  * @tc.name: dcamera_pipeline_sink_test_003
127  * @tc.desc: Verify pipeline sink CreateDataProcessPipeline abnormal.
128  * @tc.type: FUNC
129  * @tc.require: Issue Number
130  */
131 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_003, TestSize.Level1)
132 {
133     EXPECT_EQ(false, testSinkPipeline_ == nullptr);
134 
135     std::shared_ptr<DataProcessListener> listener = std::make_shared<MockDCameraDataProcessListener>();
136     VideoConfigParams srcParams(VideoCodecType::NO_CODEC,
137                                 Videoformat::NV21,
138                                 DCAMERA_PRODUCER_FPS_DEFAULT,
139                                 TEST_WIDTH,
140                                 TEST_HEIGTH);
141     VideoConfigParams destParams(VideoCodecType::CODEC_H264,
142                                  Videoformat::NV21,
143                                  DCAMERA_PRODUCER_FPS_DEFAULT,
144                                  TEST_WIDTH,
145                                  TEST_HEIGTH);
146     int32_t rc = testSinkPipeline_->CreateDataProcessPipeline(
147         PipelineType::PHOTO_JPEG, srcParams, destParams, listener);
148     EXPECT_EQ(rc, DCAMERA_NOT_FOUND);
149     usleep(SLEEP_TIME);
150 }
151 
152 /**
153  * @tc.name: dcamera_pipeline_sink_test_004
154  * @tc.desc: Verify pipeline sink ProcessData abnormal.
155  * @tc.type: FUNC
156  * @tc.require: Issue Number
157  */
158 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_004, TestSize.Level1)
159 {
160     EXPECT_EQ(false, testSinkPipeline_ == nullptr);
161 
162     std::shared_ptr<DataProcessListener> listener = std::make_shared<MockDCameraDataProcessListener>();
163     VideoConfigParams srcParams(VideoCodecType::CODEC_H264,
164                                 Videoformat::NV21,
165                                 DCAMERA_PRODUCER_FPS_DEFAULT,
166                                 TEST_WIDTH,
167                                 TEST_HEIGTH);
168     VideoConfigParams destParams(VideoCodecType::CODEC_H264,
169                                  Videoformat::NV21,
170                                  DCAMERA_PRODUCER_FPS_DEFAULT,
171                                  TEST_WIDTH,
172                                  TEST_HEIGTH);
173     int32_t rc = testSinkPipeline_->CreateDataProcessPipeline(PipelineType::VIDEO, srcParams, destParams, listener);
174     EXPECT_EQ(rc, DCAMERA_OK);
175 
176     std::vector<std::shared_ptr<DataBuffer>> buffers;
177     rc = testSinkPipeline_->ProcessData(buffers);
178     EXPECT_NE(rc, DCAMERA_OK);
179     usleep(SLEEP_TIME);
180 }
181 
182 /**
183  * @tc.name: dcamera_pipeline_sink_test_005
184  * @tc.desc: Verify pipeline sink ProcessData abnormal.
185  * @tc.type: FUNC
186  * @tc.require: Issue Number
187  */
188 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_005, TestSize.Level1)
189 {
190     EXPECT_EQ(false, testSinkPipeline_ == nullptr);
191 
192     size_t capacity = 100;
193     std::vector<std::shared_ptr<DataBuffer>> buffers;
194     std::shared_ptr<DataBuffer> db = std::make_shared<DataBuffer>(capacity);
195     buffers.push_back(db);
196     int32_t rc = testSinkPipeline_->ProcessData(buffers);
197     EXPECT_EQ(rc, DCAMERA_INIT_ERR);
198     usleep(SLEEP_TIME);
199 }
200 
201 /**
202  * @tc.name: dcamera_pipeline_sink_test_006
203  * @tc.desc: Verify pipeline sink IsInRange abnormal.
204  * @tc.type: FUNC
205  * @tc.require: I9NPV9
206  */
207 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_006, TestSize.Level1)
208 {
209     EXPECT_EQ(false, testPipelineSink_ == nullptr);
210 
211     VideoConfigParams vcParams(VideoCodecType::NO_CODEC, Videoformat::NV21, DCAMERA_PRODUCER_FPS_DEFAULT,
212         TEST_WIDTH, TEST_HEIGTH);
213     bool vc = testPipelineSink_->IsInRange(vcParams);
214     EXPECT_EQ(true, vc);
215 
216     VideoConfigParams vcParams1(VideoCodecType::NO_CODEC, Videoformat::NV21, -1,
217         TEST_WIDTH, TEST_HEIGTH);
218     bool vc1 = testPipelineSink_->IsInRange(vcParams1);
219     EXPECT_EQ(true, vc1);
220 
221     VideoConfigParams vcParams2(VideoCodecType::NO_CODEC, Videoformat::NV21, 31,
222         TEST_WIDTH, TEST_HEIGTH);
223     bool vc2 = testPipelineSink_->IsInRange(vcParams2);
224     EXPECT_EQ(true, vc2);
225 
226     VideoConfigParams vcParams3(VideoCodecType::NO_CODEC, Videoformat::NV21, DCAMERA_PRODUCER_FPS_DEFAULT,
227         300, TEST_HEIGTH);
228     bool vc3 = testPipelineSink_->IsInRange(vcParams3);
229     EXPECT_EQ(true, vc3);
230 
231     VideoConfigParams vcParams4(VideoCodecType::NO_CODEC, Videoformat::NV21, DCAMERA_PRODUCER_FPS_DEFAULT,
232         2000, TEST_HEIGTH);
233     bool vc4 = testPipelineSink_->IsInRange(vcParams4);
234     EXPECT_EQ(true, vc4);
235 
236     VideoConfigParams vcParams5(VideoCodecType::NO_CODEC, Videoformat::NV21, DCAMERA_PRODUCER_FPS_DEFAULT,
237         TEST_WIDTH, 200);
238     bool vc5 = testPipelineSink_->IsInRange(vcParams5);
239     EXPECT_EQ(true, vc5);
240 
241     VideoConfigParams vcParams6(VideoCodecType::NO_CODEC, Videoformat::NV21, DCAMERA_PRODUCER_FPS_DEFAULT,
242         TEST_WIDTH, 1100);
243     bool vc6 = testPipelineSink_->IsInRange(vcParams6);
244     EXPECT_EQ(true, vc6);
245 }
246 
247 /**
248  * @tc.name: dcamera_pipeline_sink_test_007
249  * @tc.desc: Verify pipeline sink GetProperty abnormal.
250  * @tc.type: FUNC
251  * @tc.require: I9NPV9
252  */
253 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_007, TestSize.Level1)
254 {
255     EXPECT_EQ(false, testPipelineSink_ == nullptr);
256 
257     std::string propertyName = "propertyName";
258     PropertyCarrier propertyCarrier;
259     int32_t rc1 = testPipelineSink_->GetProperty(propertyName, propertyCarrier);
260     EXPECT_EQ(DCAMERA_BAD_VALUE, rc1);
261 
262     std::shared_ptr<DCameraPipelineSource> sourcePipeline = std::make_shared<DCameraPipelineSource>();
263     std::weak_ptr<DCameraPipelineSource> callbackPipelineSource(sourcePipeline);
264     testPipelineSink_->pipelineHead_ = std::make_shared<FpsControllerProcess>(callbackPipelineSource);
265     int32_t rc2 = testPipelineSink_->GetProperty(propertyName, propertyCarrier);
266     EXPECT_EQ(DCAMERA_OK, rc2);
267 
268     testPipelineSink_->pipelineHead_ = std::make_shared<FpsControllerProcess>(callbackPipelineSource);
269     int32_t rc3 = testPipelineSink_->GetProperty(propertyName, propertyCarrier);
270     EXPECT_EQ(DCAMERA_OK, rc3);
271 }
272 
273 /**
274  * @tc.name: dcamera_pipeline_sink_test_008
275  * @tc.desc: Verify pipeline sink OnProcessedVideoBuffer abnormal.
276  * @tc.type: FUNC
277  * @tc.require: I9NPV9
278  */
279 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_008, TestSize.Level1)
280 {
281     EXPECT_EQ(false, testPipelineSink_ == nullptr);
282 
283     size_t i = 1;
284     std::shared_ptr<DataBuffer> videoResult = std::make_shared<DataBuffer>(i);
285     testPipelineSink_->OnProcessedVideoBuffer(videoResult);
286     EXPECT_TRUE(true);
287 
288     testPipelineSink_->processListener_ = std::make_shared<MockDCameraDataProcessListener>();
289     testPipelineSink_->OnProcessedVideoBuffer(videoResult);
290     EXPECT_TRUE(true);
291 }
292 
293 /**
294  * @tc.name: dcamera_pipeline_sink_test_009
295  * @tc.desc: Verify pipeline sink OnError abnormal.
296  * @tc.type: FUNC
297  * @tc.require: I9NPV9
298  */
299 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_009, TestSize.Level1)
300 {
301     EXPECT_EQ(false, testPipelineSink_ == nullptr);
302 
303     DataProcessErrorType errorType = DataProcessErrorType::ERROR_PIPELINE_ENCODER;
304     testPipelineSink_->OnError(errorType);
305     EXPECT_TRUE(true);
306 
307     testPipelineSink_->processListener_ = std::make_shared<MockDCameraDataProcessListener>();
308     testPipelineSink_->OnError(errorType);
309     EXPECT_TRUE(true);
310 }
311 
312 /**
313  * @tc.name: dcamera_pipeline_sink_test_010
314  * @tc.desc: Verify pipeline sink DestroyDataProcessPipeline abnormal.
315  * @tc.type: FUNC
316  * @tc.require: I9NPV9
317  */
318 HWTEST_F(DCameraPipelineSinkTest, dcamera_pipeline_sink_test_010, TestSize.Level1)
319 {
320     EXPECT_EQ(false, testPipelineSink_ == nullptr);
321 
322     std::shared_ptr<DCameraPipelineSource> sourcePipeline = std::make_shared<DCameraPipelineSource>();
323     std::weak_ptr<DCameraPipelineSource> callbackPipelineSource(sourcePipeline);
324     testPipelineSink_->pipelineHead_ = std::make_shared<FpsControllerProcess>(callbackPipelineSource);
325     testPipelineSink_->piplineType_ = PipelineType::PHOTO_JPEG;
326     testPipelineSink_->DestroyDataProcessPipeline();
327     EXPECT_EQ(PipelineType::VIDEO, testPipelineSink_->piplineType_);
328 
329     testPipelineSink_->pipelineHead_ = nullptr;
330     testPipelineSink_->piplineType_ = PipelineType::PHOTO_JPEG;
331     testPipelineSink_->DestroyDataProcessPipeline();
332     EXPECT_EQ(PipelineType::VIDEO, testPipelineSink_->piplineType_);
333 }
334 } // namespace DistributedHardware
335 } // namespace OHOS
336