• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     dataProcess_->pipeline_ = nullptr;
130     int32_t ret = dataProcess_->StartCapture(g_testCaptureInfoContinuousNotEncode);
131     EXPECT_EQ(DCAMERA_OK, ret);
132 }
133 
134 /**
135  * @tc.name: dcamera_sink_data_process_test_002
136  * @tc.desc: Verify the StartCapture function.
137  * @tc.type: FUNC
138  * @tc.require: AR000GK6MU
139  */
140 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_002, TestSize.Level1)
141 {
142     dataProcess_->pipeline_ = nullptr;
143     int32_t ret = dataProcess_->StartCapture(g_testCaptureInfoContinuousNeedEncode);
144     EXPECT_EQ(DCAMERA_OK, ret);
145 }
146 
147 /**
148  * @tc.name: dcamera_sink_data_process_test_003
149  * @tc.desc: Verify the StartCapture function.
150  * @tc.type: FUNC
151  * @tc.require: AR000GK6MU
152  */
153 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_003, TestSize.Level1)
154 {
155     dataProcess_->pipeline_ = nullptr;
156     int32_t ret = dataProcess_->StartCapture(g_testCaptureInfoSnapshot);
157     EXPECT_EQ(DCAMERA_OK, ret);
158 }
159 
160 /**
161  * @tc.name: dcamera_sink_data_process_test_004
162  * @tc.desc: Verify the StopCapture function.
163  * @tc.type: FUNC
164  * @tc.require: AR000GK6N1
165  */
166 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_004, TestSize.Level1)
167 {
168     int32_t ret = dataProcess_->StopCapture();
169     EXPECT_EQ(DCAMERA_OK, ret);
170 }
171 
172 /**
173  * @tc.name: dcamera_sink_data_process_test_005
174  * @tc.desc: Verify the FeedStream function.
175  * @tc.type: FUNC
176  * @tc.require: AR000GK6MV
177  */
178 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_005, TestSize.Level1)
179 {
180     dataProcess_->captureInfo_ = g_testCaptureInfoContinuousNotEncode;
181     int32_t ret = dataProcess_->FeedStream(g_testDataBuffer);
182     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
183     EXPECT_EQ(DCAMERA_OK, ret);
184 }
185 
186 /**
187  * @tc.name: dcamera_sink_data_process_test_006
188  * @tc.desc: Verify the FeedStream function.
189  * @tc.type: FUNC
190  * @tc.require: AR000GK6MV
191  */
192 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_006, TestSize.Level1)
193 {
194     dataProcess_->captureInfo_ = g_testCaptureInfoContinuousNeedEncode;
195     int32_t ret = dataProcess_->FeedStream(g_testDataBuffer);
196     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
197     EXPECT_EQ(DCAMERA_OK, ret);
198 }
199 
200 /**
201  * @tc.name: dcamera_sink_data_process_test_007
202  * @tc.desc: Verify the FeedStream function.
203  * @tc.type: FUNC
204  * @tc.require: AR000GK6MV
205  */
206 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_007, TestSize.Level1)
207 {
208     dataProcess_->captureInfo_ = g_testCaptureInfoSnapshot;
209     int32_t ret = dataProcess_->FeedStream(g_testDataBuffer);
210     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
211     EXPECT_EQ(DCAMERA_OK, ret);
212 }
213 
214 /**
215  * @tc.name: dcamera_sink_data_process_test_008
216  * @tc.desc: Verify the GetPipelineCodecType GetPipelineFormat function.
217  * @tc.type: FUNC
218  * @tc.require: AR000GK6MV
219  */
220 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_008, TestSize.Level1)
221 {
222     EXPECT_EQ(VideoCodecType::CODEC_H264, dataProcess_->GetPipelineCodecType(DCEncodeType::ENCODE_TYPE_H264));
223     EXPECT_EQ(VideoCodecType::CODEC_H265, dataProcess_->GetPipelineCodecType(DCEncodeType::ENCODE_TYPE_H265));
224     EXPECT_EQ(VideoCodecType::CODEC_MPEG4_ES, dataProcess_->GetPipelineCodecType(DCEncodeType::ENCODE_TYPE_MPEG4_ES));
225     EXPECT_EQ(VideoCodecType::NO_CODEC, dataProcess_->GetPipelineCodecType(DCEncodeType::ENCODE_TYPE_NULL));
226     EXPECT_EQ(Videoformat::RGBA_8888, dataProcess_->GetPipelineFormat(1));
227     EXPECT_EQ(Videoformat::NV21, dataProcess_->GetPipelineFormat(0));
228 }
229 
230 /**
231  * @tc.name: dcamera_sink_data_process_test_009
232  * @tc.desc: Verify the StartCapture function.
233  * @tc.type: FUNC
234  * @tc.require: AR000GK6MU
235  */
236 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_009, TestSize.Level1)
237 {
238     int32_t ret = dataProcess_->StartCapture(g_testCaptureInfoSnapshot);
239     EXPECT_EQ(DCAMERA_OK, ret);
240 }
241 
242 /**
243  * @tc.name: dcamera_sink_data_process_test_010
244  * @tc.desc: Verify the StartCapture function.
245  * @tc.type: FUNC
246  * @tc.require: AR000GK6MU
247  */
248 HWTEST_F(DCameraSinkDataProcessTest, dcamera_sink_data_process_test_010, TestSize.Level1)
249 {
250     std::string propertyName = "test010";
251     PropertyCarrier propertyCarrier;
252     int32_t ret = dataProcess_->GetProperty(propertyName, propertyCarrier);
253     EXPECT_EQ(DCAMERA_OK, ret);
254 }
255 } // namespace DistributedHardware
256 } // namespace OHOS