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 
18 #include "dcamera_source_data_process.h"
19 #include "distributed_camera_errno.h"
20 
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace DistributedHardware {
25 class DCameraSourceDataProcessTest : public testing::Test {
26 public:
27     static void SetUpTestCase(void);
28     static void TearDownTestCase(void);
29     void SetUp();
30     void TearDown();
31 
32     std::shared_ptr<ICameraSourceDataProcess> testSrcDataProcess_;
33 };
34 
35 namespace {
36 const std::string TEST_DEVICE_ID = "bb536a637105409e904d4da83790a4a7";
37 const std::string TEST_CAMERA_DH_ID_0 = "camera_0";
38 const int32_t TEST_WIDTH = 1920;
39 const int32_t TEST_HEIGTH = 1080;
40 const int32_t TEST_FORMAT = 4;
41 const int32_t TEST_DATASPACE = 8;
42 const int32_t TEST_STREAMID = 2;
43 std::vector<std::shared_ptr<DCStreamInfo>> g_streamInfos;
44 std::vector<int32_t> g_streamIds;
45 }
46 
SetUpTestCase(void)47 void DCameraSourceDataProcessTest::SetUpTestCase(void)
48 {
49     std::shared_ptr<DCStreamInfo> streamInfo1 = std::make_shared<DCStreamInfo>();
50     streamInfo1->streamId_ = 1;
51     streamInfo1->width_ = TEST_WIDTH;
52     streamInfo1->height_ = TEST_HEIGTH;
53     streamInfo1->stride_ = 1;
54     streamInfo1->format_ = TEST_FORMAT;
55     streamInfo1->dataspace_ = TEST_DATASPACE;
56     streamInfo1->encodeType_ = ENCODE_TYPE_JPEG;
57     streamInfo1->type_ = SNAPSHOT_FRAME;
58 
59     std::shared_ptr<DCStreamInfo> streamInfo2 = std::make_shared<DCStreamInfo>();
60     streamInfo2->streamId_ = TEST_STREAMID;
61     streamInfo2->width_ = TEST_WIDTH;
62     streamInfo2->height_ = TEST_HEIGTH;
63     streamInfo2->stride_ = 1;
64     streamInfo2->format_ = TEST_FORMAT;
65     streamInfo2->dataspace_ = TEST_DATASPACE;
66     streamInfo2->encodeType_ = ENCODE_TYPE_JPEG;
67     streamInfo2->type_ = SNAPSHOT_FRAME;
68     g_streamInfos.push_back(streamInfo1);
69     g_streamInfos.push_back(streamInfo2);
70     g_streamIds.push_back(1);
71     g_streamIds.push_back(TEST_STREAMID);
72 }
73 
TearDownTestCase(void)74 void DCameraSourceDataProcessTest::TearDownTestCase(void)
75 {
76 }
77 
SetUp(void)78 void DCameraSourceDataProcessTest::SetUp(void)
79 {
80     testSrcDataProcess_ = std::make_shared<DCameraSourceDataProcess>(
81         TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, SNAPSHOT_FRAME);
82 }
83 
TearDown(void)84 void DCameraSourceDataProcessTest::TearDown(void)
85 {
86     testSrcDataProcess_ = nullptr;
87 }
88 
89 /**
90  * @tc.name: dcamera_source_data_process_test_001
91  * @tc.desc: Verify source data process ConfigStreams.
92  * @tc.type: FUNC
93  * @tc.require: Issue Number
94  */
95 HWTEST_F(DCameraSourceDataProcessTest, dcamera_source_data_process_test_001, TestSize.Level1)
96 {
97     EXPECT_EQ(false, testSrcDataProcess_ == nullptr);
98 
99     int32_t rc = testSrcDataProcess_->ConfigStreams(g_streamInfos);
100     EXPECT_EQ(rc, DCAMERA_OK);
101 }
102 
103 /**
104  * @tc.name: dcamera_source_data_process_test_002
105  * @tc.desc: Verify source data process ReleaseStreams.
106  * @tc.type: FUNC
107  * @tc.require: Issue Number
108  */
109 HWTEST_F(DCameraSourceDataProcessTest, dcamera_source_data_process_test_002, TestSize.Level1)
110 {
111     EXPECT_EQ(false, testSrcDataProcess_ == nullptr);
112 
113     int32_t rc = testSrcDataProcess_->ConfigStreams(g_streamInfos);
114     EXPECT_EQ(rc, DCAMERA_OK);
115 
116     rc = testSrcDataProcess_->ReleaseStreams(g_streamIds);
117     EXPECT_EQ(rc, DCAMERA_OK);
118 }
119 
120 /**
121  * @tc.name: dcamera_source_data_process_test_003
122  * @tc.desc: Verify source data process StartCapture.
123  * @tc.type: FUNC
124  * @tc.require: Issue Number
125  */
126 HWTEST_F(DCameraSourceDataProcessTest, dcamera_source_data_process_test_003, TestSize.Level1)
127 {
128     EXPECT_EQ(false, testSrcDataProcess_ == nullptr);
129 
130     std::shared_ptr<DCCaptureInfo> captureInfo = std::make_shared<DCCaptureInfo>();
131     captureInfo->streamIds_.push_back(1);
132     captureInfo->width_ = TEST_WIDTH;
133     captureInfo->height_ = TEST_HEIGTH;
134     captureInfo->stride_ = 1;
135     captureInfo->format_ = 1;
136     captureInfo->dataspace_ = 1;
137     captureInfo->encodeType_ = ENCODE_TYPE_H265;
138     captureInfo->type_ = CONTINUOUS_FRAME;
139     int32_t rc = testSrcDataProcess_->StartCapture(captureInfo);
140     EXPECT_EQ(rc, DCAMERA_OK);
141     captureInfo->isCapture_ = true;
142     rc = testSrcDataProcess_->StartCapture(captureInfo);
143     EXPECT_EQ(rc, DCAMERA_OK);
144 }
145 
146 /**
147  * @tc.name: dcamera_source_data_process_test_004
148  * @tc.desc: Verify source data process StopCapture.
149  * @tc.type: FUNC
150  * @tc.require: Issue Number
151  */
152 HWTEST_F(DCameraSourceDataProcessTest, dcamera_source_data_process_test_004, TestSize.Level1)
153 {
154     EXPECT_EQ(false, testSrcDataProcess_ == nullptr);
155 
156     std::shared_ptr<DCCaptureInfo> captureInfo = std::make_shared<DCCaptureInfo>();
157     captureInfo->streamIds_.push_back(1);
158     captureInfo->width_ = TEST_WIDTH;
159     captureInfo->height_ = TEST_HEIGTH;
160     captureInfo->stride_ = 1;
161     captureInfo->format_ = 1;
162     captureInfo->dataspace_ = 1;
163     captureInfo->encodeType_ = ENCODE_TYPE_H265;
164     captureInfo->type_ = CONTINUOUS_FRAME;
165     int32_t rc = testSrcDataProcess_->StartCapture(captureInfo);
166     EXPECT_EQ(rc, DCAMERA_OK);
167 
168     std::vector<int32_t> streamIds;
169     streamIds.push_back(1);
170     rc = testSrcDataProcess_->StopCapture(streamIds);
171     EXPECT_EQ(rc, DCAMERA_OK);
172 }
173 
174 /**
175  * @tc.name: dcamera_source_data_process_test_005
176  * @tc.desc: Verify source data process FeedStream.
177  * @tc.type: FUNC
178  * @tc.require: Issue Number
179  */
180 HWTEST_F(DCameraSourceDataProcessTest, dcamera_source_data_process_test_005, TestSize.Level1)
181 {
182     EXPECT_EQ(false, testSrcDataProcess_ == nullptr);
183 
184     size_t capacity = 100;
185     std::vector<std::shared_ptr<DataBuffer>> buffers;
186     std::shared_ptr<DataBuffer> db = std::make_shared<DataBuffer>(capacity);
187     buffers.push_back(db);
188     int32_t rc = testSrcDataProcess_->ConfigStreams(g_streamInfos);
189     EXPECT_EQ(rc, DCAMERA_OK);
190 
191     rc = testSrcDataProcess_->FeedStream(buffers);
192     EXPECT_EQ(rc, DCAMERA_OK);
193 
194     rc = testSrcDataProcess_->ReleaseStreams(g_streamIds);
195     EXPECT_EQ(rc, DCAMERA_OK);
196 }
197 
198 /**
199  * @tc.name: dcamera_source_data_process_test_006
200  * @tc.desc: Verify source data process GetProducerSize.
201  * @tc.type: FUNC
202  * @tc.require: Issue Number
203  */
204 HWTEST_F(DCameraSourceDataProcessTest, dcamera_source_data_process_test_006, TestSize.Level1)
205 {
206     EXPECT_EQ(false, testSrcDataProcess_ == nullptr);
207 
208     int32_t rc = testSrcDataProcess_->ConfigStreams(g_streamInfos);
209     EXPECT_EQ(rc, DCAMERA_OK);
210 
211     rc = testSrcDataProcess_->GetProducerSize();
212     EXPECT_EQ(rc, DCAMERA_OK);
213 
214     rc = testSrcDataProcess_->ReleaseStreams(g_streamIds);
215     EXPECT_EQ(rc, DCAMERA_OK);
216 }
217 } // namespace DistributedHardware
218 } // namespace OHOS
219