1 /*
2  * Copyright (c) 2021-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 <chrono>
17 #include <fstream>
18 #include <memory>
19 #include <thread>
20 
21 #define private public
22 #include "dcamera_sink_data_process.h"
23 #undef private
24 
25 #include <gtest/gtest.h>
26 #include <securec.h>
27 
28 #include "dcamera_handler.h"
29 #include "distributed_camera_errno.h"
30 #include "mock_camera_channel.h"
31 #include "mock_data_process_pipeline.h"
32 
33 using namespace testing::ext;
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 class DCameraSinkDataProcessTest : public testing::Test {
38 public:
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     void SetUp();
42     void TearDown();
43 
44     std::shared_ptr<DCameraSinkDataProcess> dataProcess_;
45     std::shared_ptr<ICameraChannel> channel_;
46 };
47 
48 const int32_t SLEEP_TIME_MS = 500;
49 const int32_t TEST_WIDTH = 1080;
50 const int32_t TEST_HEIGHT = 1920;
51 const std::string TEST_STRING = "test_string";
52 const int32_t TEST_TWENTY_MS = 20000;
53 
54 std::shared_ptr<DCameraCaptureInfo> g_testCaptureInfoContinuousNotEncode;
55 std::shared_ptr<DCameraCaptureInfo> g_testCaptureInfoContinuousNeedEncode;
56 std::shared_ptr<DCameraCaptureInfo> g_testCaptureInfoSnapshot;
57 
58 std::shared_ptr<DataBuffer> g_testDataBuffer;
59 
SetUpTestCase(void)60 void DCameraSinkDataProcessTest::SetUpTestCase(void)
61 {
62     std::shared_ptr<DCameraSettings> cameraSetting = std::make_shared<DCameraSettings>();
63     cameraSetting->type_ = UPDATE_METADATA;
64     cameraSetting->value_ = "";
65 
66     g_testCaptureInfoContinuousNotEncode = std::make_shared<DCameraCaptureInfo>();
67     g_testCaptureInfoContinuousNotEncode->width_ = TEST_WIDTH;
68     g_testCaptureInfoContinuousNotEncode->height_ = TEST_HEIGHT;
69     g_testCaptureInfoContinuousNotEncode->format_  = ENCODE_TYPE_H264;
70     g_testCaptureInfoContinuousNotEncode->dataspace_ = 0;
71     g_testCaptureInfoContinuousNotEncode->encodeType_ = ENCODE_TYPE_H264;
72     g_testCaptureInfoContinuousNotEncode->streamType_ = CONTINUOUS_FRAME;
73     g_testCaptureInfoContinuousNotEncode->captureSettings_.push_back(cameraSetting);
74 
75     g_testCaptureInfoContinuousNeedEncode = std::make_shared<DCameraCaptureInfo>();
76     g_testCaptureInfoContinuousNeedEncode->width_ = TEST_WIDTH;
77     g_testCaptureInfoContinuousNeedEncode->height_ = TEST_HEIGHT;
78     g_testCaptureInfoContinuousNeedEncode->format_  = ENCODE_TYPE_H264;
79     g_testCaptureInfoContinuousNeedEncode->dataspace_ = 0;
80     g_testCaptureInfoContinuousNeedEncode->encodeType_ = ENCODE_TYPE_H265;
81     g_testCaptureInfoContinuousNeedEncode->streamType_ = CONTINUOUS_FRAME;
82     g_testCaptureInfoContinuousNeedEncode->captureSettings_.push_back(cameraSetting);
83 
84     g_testCaptureInfoSnapshot = std::make_shared<DCameraCaptureInfo>();
85     g_testCaptureInfoSnapshot->width_ = TEST_WIDTH;
86     g_testCaptureInfoSnapshot->height_ = TEST_HEIGHT;
87     g_testCaptureInfoSnapshot->format_  = ENCODE_TYPE_JPEG;
88     g_testCaptureInfoSnapshot->dataspace_ = 0;
89     g_testCaptureInfoSnapshot->encodeType_ = ENCODE_TYPE_JPEG;
90     g_testCaptureInfoSnapshot->streamType_ = SNAPSHOT_FRAME;
91     g_testCaptureInfoSnapshot->captureSettings_.push_back(cameraSetting);
92 
93     g_testDataBuffer = std::make_shared<DataBuffer>(TEST_STRING.length() + 1);
94     memcpy_s(g_testDataBuffer->Data(), g_testDataBuffer->Capacity(),
95         (uint8_t *)TEST_STRING.c_str(), TEST_STRING.length());
96 }
97 
TearDownTestCase(void)98 void DCameraSinkDataProcessTest::TearDownTestCase(void)
99 {
100 }
101 
SetUp(void)102 void DCameraSinkDataProcessTest::SetUp(void)
103 {
104     channel_ = std::make_shared<MockCameraChannel>();
105     DCameraHandler::GetInstance().Initialize();
106     std::vector<std::string> cameras = DCameraHandler::GetInstance().GetCameras();
107     dataProcess_ = std::make_shared<DCameraSinkDataProcess>(cameras[0], channel_);
108     dataProcess_->Init();
109 
110     dataProcess_->pipeline_ = std::make_shared<MockDataProcessPipeline>();
111     dataProcess_->captureInfo_ = g_testCaptureInfoContinuousNeedEncode;
112 }
113 
TearDown(void)114 void DCameraSinkDataProcessTest::TearDown(void)
115 {
116     usleep(TEST_TWENTY_MS);
117     channel_ = nullptr;
118     dataProcess_ = nullptr;
119 }
120 
121 /**
122  * @tc.name: dcamera_sink_data_process_test_001
123  * @tc.desc: Verify the StartCapture function.
124  * @tc.type: FUNC
125  * @tc.require: AR000GK6MU
126  */
127 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_001, TestSize.Level1)
128 {
129     int32_t ret = dataProcess_->StartCapture(g_testCaptureInfoContinuousNotEncode);
130     EXPECT_EQ(DCAMERA_OK, ret);
131 }
132 
133 /**
134  * @tc.name: dcamera_sink_data_process_test_002
135  * @tc.desc: Verify the StartCapture function.
136  * @tc.type: FUNC
137  * @tc.require: AR000GK6MU
138  */
139 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_002, TestSize.Level1)
140 {
141     int32_t ret = dataProcess_->StartCapture(g_testCaptureInfoContinuousNeedEncode);
142     EXPECT_EQ(DCAMERA_OK, ret);
143 }
144 
145 /**
146  * @tc.name: dcamera_sink_data_process_test_003
147  * @tc.desc: Verify the StartCapture function.
148  * @tc.type: FUNC
149  * @tc.require: AR000GK6MU
150  */
151 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_003, TestSize.Level1)
152 {
153     int32_t ret = dataProcess_->StartCapture(g_testCaptureInfoSnapshot);
154     EXPECT_EQ(DCAMERA_OK, ret);
155 }
156 
157 /**
158  * @tc.name: dcamera_sink_data_process_test_004
159  * @tc.desc: Verify the StopCapture function.
160  * @tc.type: FUNC
161  * @tc.require: AR000GK6N1
162  */
163 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_004, TestSize.Level1)
164 {
165     int32_t ret = dataProcess_->StopCapture();
166     EXPECT_EQ(DCAMERA_OK, ret);
167 }
168 
169 /**
170  * @tc.name: dcamera_sink_data_process_test_005
171  * @tc.desc: Verify the FeedStream function.
172  * @tc.type: FUNC
173  * @tc.require: AR000GK6MV
174  */
175 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_005, TestSize.Level1)
176 {
177     dataProcess_->captureInfo_ = g_testCaptureInfoContinuousNotEncode;
178     int32_t ret = dataProcess_->FeedStream(g_testDataBuffer);
179     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
180     EXPECT_EQ(DCAMERA_OK, ret);
181 }
182 
183 /**
184  * @tc.name: dcamera_sink_data_process_test_006
185  * @tc.desc: Verify the FeedStream function.
186  * @tc.type: FUNC
187  * @tc.require: AR000GK6MV
188  */
189 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_006, TestSize.Level1)
190 {
191     dataProcess_->captureInfo_ = g_testCaptureInfoContinuousNeedEncode;
192     int32_t ret = dataProcess_->FeedStream(g_testDataBuffer);
193     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
194     EXPECT_EQ(DCAMERA_OK, ret);
195 }
196 
197 /**
198  * @tc.name: dcamera_sink_data_process_test_007
199  * @tc.desc: Verify the FeedStream function.
200  * @tc.type: FUNC
201  * @tc.require: AR000GK6MV
202  */
203 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_007, TestSize.Level1)
204 {
205     dataProcess_->captureInfo_ = g_testCaptureInfoSnapshot;
206     int32_t ret = dataProcess_->FeedStream(g_testDataBuffer);
207     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
208     EXPECT_EQ(DCAMERA_OK, ret);
209 }
210 
211 /**
212  * @tc.name: dcamera_sink_data_process_test_008
213  * @tc.desc: Verify the GetPipelineCodecType GetPipelineFormat function.
214  * @tc.type: FUNC
215  * @tc.require: AR000GK6MV
216  */
217 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_008, TestSize.Level1)
218 {
219     EXPECT_EQ(VideoCodecType::CODEC_H264, dataProcess_->GetPipelineCodecType(DCEncodeType::ENCODE_TYPE_H264));
220     EXPECT_EQ(VideoCodecType::CODEC_H265, dataProcess_->GetPipelineCodecType(DCEncodeType::ENCODE_TYPE_H265));
221     EXPECT_EQ(VideoCodecType::CODEC_MPEG4_ES, dataProcess_->GetPipelineCodecType(DCEncodeType::ENCODE_TYPE_MPEG4_ES));
222     EXPECT_EQ(VideoCodecType::NO_CODEC, dataProcess_->GetPipelineCodecType(DCEncodeType::ENCODE_TYPE_NULL));
223     EXPECT_EQ(Videoformat::RGBA_8888, dataProcess_->GetPipelineFormat(1));
224     EXPECT_EQ(Videoformat::NV21, dataProcess_->GetPipelineFormat(0));
225 }
226 } // namespace DistributedHardware
227 } // namespace OHOS