• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <memory>
18 #define private public
19 #include "dcamera_sink_output.h"
20 #undef private
21 
22 #include "mock_camera_channel.h"
23 #include "mock_camera_operator.h"
24 #include "mock_dcamera_sink_data_process.h"
25 
26 #include "dcamera_handler.h"
27 #include "dcamera_sink_access_control.h"
28 #include "dcamera_sink_controller.h"
29 #include "dcamera_sink_data_process.h"
30 #include "dcamera_sink_dev.h"
31 #include "dcamera_utils_tools.h"
32 #include "distributed_camera_errno.h"
33 
34 using namespace testing::ext;
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 class DCameraSinkOutputTest : public testing::Test {
39 public:
40     static void SetUpTestCase(void);
41     static void TearDownTestCase(void);
42     void SetUp();
43     void TearDown();
44 
45     std::shared_ptr<DCameraSinkOutput> output_;
46     std::shared_ptr<ICameraOperator> operator_;
47     std::shared_ptr<ICameraChannel> channel_;
48     std::shared_ptr<ICameraSinkDataProcess> dataProcess_;
49 };
50 
51 const std::string SESSION_FLAG_CONTINUE = "dataContinue";
52 const std::string SESSION_FLAG_SNAPSHOT = "dataSnapshot";
53 const int32_t TEST_WIDTH = 1080;
54 const int32_t TEST_HEIGHT = 1920;
55 const int32_t TEST_TWENTY_MS = 20000;
56 
57 std::string g_testDeviceIdOutput;
58 DCameraChannelDetail g_testDetailOutput;
59 
60 std::shared_ptr<DCameraCaptureInfo> g_testCaptureInfoOutputContinuousNotEncode;
61 std::shared_ptr<DCameraCaptureInfo> g_testCaptureInfoOutputContinuousNeedEncode;
62 std::shared_ptr<DCameraCaptureInfo> g_testCaptureInfoOutputSnapshot;
63 
64 std::shared_ptr<DCameraChannelInfo> g_testChannelInfo;
65 
66 std::vector<std::shared_ptr<DCameraCaptureInfo>> g_testCaptureInfosOutput;
67 
SetUpTestCase(void)68 void DCameraSinkOutputTest::SetUpTestCase(void)
69 {
70     GetLocalDeviceNetworkId(g_testDeviceIdOutput);
71 
72     std::shared_ptr<DCameraSettings> cameraSetting = std::make_shared<DCameraSettings>();
73     cameraSetting->type_ = UPDATE_METADATA;
74     cameraSetting->value_ = "";
75 
76     g_testCaptureInfoOutputContinuousNotEncode = std::make_shared<DCameraCaptureInfo>();
77     g_testCaptureInfoOutputContinuousNotEncode->width_ = TEST_WIDTH;
78     g_testCaptureInfoOutputContinuousNotEncode->height_ = TEST_HEIGHT;
79     g_testCaptureInfoOutputContinuousNotEncode->format_  = ENCODE_TYPE_H264;
80     g_testCaptureInfoOutputContinuousNotEncode->dataspace_ = 0;
81     g_testCaptureInfoOutputContinuousNotEncode->encodeType_ = ENCODE_TYPE_H264;
82     g_testCaptureInfoOutputContinuousNotEncode->streamType_ = CONTINUOUS_FRAME;
83     g_testCaptureInfoOutputContinuousNotEncode->captureSettings_.push_back(cameraSetting);
84 
85     g_testCaptureInfoOutputContinuousNeedEncode = std::make_shared<DCameraCaptureInfo>();
86     g_testCaptureInfoOutputContinuousNeedEncode->width_ = TEST_WIDTH;
87     g_testCaptureInfoOutputContinuousNeedEncode->height_ = TEST_HEIGHT;
88     g_testCaptureInfoOutputContinuousNeedEncode->format_  = ENCODE_TYPE_H264;
89     g_testCaptureInfoOutputContinuousNeedEncode->dataspace_ = 0;
90     g_testCaptureInfoOutputContinuousNeedEncode->encodeType_ = ENCODE_TYPE_H265;
91     g_testCaptureInfoOutputContinuousNeedEncode->streamType_ = CONTINUOUS_FRAME;
92     g_testCaptureInfoOutputContinuousNeedEncode->captureSettings_.push_back(cameraSetting);
93 
94     g_testCaptureInfoOutputSnapshot = std::make_shared<DCameraCaptureInfo>();
95     g_testCaptureInfoOutputSnapshot->width_ = TEST_WIDTH;
96     g_testCaptureInfoOutputSnapshot->height_ = TEST_HEIGHT;
97     g_testCaptureInfoOutputSnapshot->format_  = ENCODE_TYPE_JPEG;
98     g_testCaptureInfoOutputSnapshot->dataspace_ = 0;
99     g_testCaptureInfoOutputSnapshot->encodeType_ = ENCODE_TYPE_JPEG;
100     g_testCaptureInfoOutputSnapshot->streamType_ = SNAPSHOT_FRAME;
101     g_testCaptureInfoOutputSnapshot->captureSettings_.push_back(cameraSetting);
102 
103     g_testDetailOutput.dataSessionFlag_ = SESSION_FLAG_CONTINUE;
104     g_testDetailOutput.streamType_ = CONTINUOUS_FRAME;
105 
106     g_testChannelInfo = std::make_shared<DCameraChannelInfo>();
107     g_testChannelInfo->sourceDevId_ = g_testDeviceIdOutput;
108     std::vector<DCameraChannelDetail> detail0;
109     detail0.push_back(g_testDetailOutput);
110     g_testChannelInfo->detail_ = detail0;
111 }
112 
TearDownTestCase(void)113 void DCameraSinkOutputTest::TearDownTestCase(void)
114 {
115 }
116 
SetUp(void)117 void DCameraSinkOutputTest::SetUp(void)
118 {
119     DCameraHandler::GetInstance().Initialize();
120     std::vector<std::string> cameras = DCameraHandler::GetInstance().GetCameras();
121     operator_ = std::make_shared<MockCameraOperator>();
122     output_ = std::make_shared<DCameraSinkOutput>(cameras[0], operator_);
123 }
124 
TearDown(void)125 void DCameraSinkOutputTest::TearDown(void)
126 {
127     output_->channels_.clear();
128     output_->dataProcesses_.clear();
129 
130     operator_ = nullptr;
131     output_ = nullptr;
132 }
133 
134 /**
135  * @tc.name: dcamera_sink_output_test_001
136  * @tc.desc: Verify the UnInit function.
137  * @tc.type: FUNC
138  * @tc.require: I5N1JI
139  */
140 HWTEST_F(DCameraSinkOutputTest, dcamera_sink_output_test_001, TestSize.Level1)
141 {
142     int32_t ret = output_->Init();
143     EXPECT_EQ(DCAMERA_OK, ret);
144     EXPECT_EQ(true, output_->isInit_);
145     usleep(TEST_TWENTY_MS);
146     ret = output_->UnInit();
147     EXPECT_EQ(DCAMERA_OK, ret);
148     EXPECT_EQ(false, output_->isInit_);
149 }
150 
151 /**
152  * @tc.name: dcamera_sink_output_test_002
153  * @tc.desc: Verify the StartCapture function.
154  * @tc.type: FUNC
155  * @tc.require: I5N1JI
156  */
157 HWTEST_F(DCameraSinkOutputTest, dcamera_sink_output_test_002, TestSize.Level1)
158 {
159     channel_ = std::make_shared<MockCameraChannel>();
160     dataProcess_ = std::make_shared<MockDCameraSinkDataProcess>(channel_);
161     output_->channels_.emplace(SNAPSHOT_FRAME, channel_);
162     output_->channels_.emplace(CONTINUOUS_FRAME, channel_);
163     output_->dataProcesses_.emplace(SNAPSHOT_FRAME, dataProcess_);
164     output_->dataProcesses_.emplace(CONTINUOUS_FRAME, dataProcess_);
165     g_testCaptureInfosOutput.push_back(g_testCaptureInfoOutputContinuousNotEncode);
166     int32_t ret = output_->StartCapture(g_testCaptureInfosOutput);
167     EXPECT_EQ(DCAMERA_OK, ret);
168 
169     g_testCaptureInfosOutput.clear();
170 }
171 
172 /**
173  * @tc.name: dcamera_sink_output_test_003
174  * @tc.desc: Verify the StartCapture function.
175  * @tc.type: FUNC
176  * @tc.require: I5N1JI
177  */
178 HWTEST_F(DCameraSinkOutputTest, dcamera_sink_output_test_003, TestSize.Level1)
179 {
180     channel_ = std::make_shared<MockCameraChannel>();
181     dataProcess_ = std::make_shared<MockDCameraSinkDataProcess>(channel_);
182     output_->channels_.emplace(SNAPSHOT_FRAME, channel_);
183     output_->channels_.emplace(CONTINUOUS_FRAME, channel_);
184     output_->dataProcesses_.emplace(SNAPSHOT_FRAME, dataProcess_);
185     output_->dataProcesses_.emplace(CONTINUOUS_FRAME, dataProcess_);
186     g_testCaptureInfosOutput.push_back(g_testCaptureInfoOutputContinuousNeedEncode);
187     int32_t ret = output_->StartCapture(g_testCaptureInfosOutput);
188     EXPECT_EQ(DCAMERA_OK, ret);
189 
190     g_testCaptureInfosOutput.clear();
191 }
192 
193 /**
194  * @tc.name: dcamera_sink_output_test_004
195  * @tc.desc: Verify the StartCapture function.
196  * @tc.type: FUNC
197  * @tc.require: I5N1JI
198  */
199 HWTEST_F(DCameraSinkOutputTest, dcamera_sink_output_test_004, TestSize.Level1)
200 {
201     channel_ = std::make_shared<MockCameraChannel>();
202     dataProcess_ = std::make_shared<MockDCameraSinkDataProcess>(channel_);
203     output_->channels_.emplace(SNAPSHOT_FRAME, channel_);
204     output_->channels_.emplace(CONTINUOUS_FRAME, channel_);
205     output_->dataProcesses_.emplace(SNAPSHOT_FRAME, dataProcess_);
206     output_->dataProcesses_.emplace(CONTINUOUS_FRAME, dataProcess_);
207     g_testCaptureInfosOutput.push_back(g_testCaptureInfoOutputSnapshot);
208     int32_t ret = output_->StartCapture(g_testCaptureInfosOutput);
209     EXPECT_EQ(DCAMERA_OK, ret);
210 
211     g_testCaptureInfosOutput.clear();
212 }
213 
214 /**
215  * @tc.name: dcamera_sink_output_test_005
216  * @tc.desc: Verify the StopCapture function.
217  * @tc.type: FUNC
218  * @tc.require: I5N1JI
219  */
220 HWTEST_F(DCameraSinkOutputTest, dcamera_sink_output_test_005, TestSize.Level1)
221 {
222     channel_ = std::make_shared<MockCameraChannel>();
223     dataProcess_ = std::make_shared<MockDCameraSinkDataProcess>(channel_);
224     output_->channels_.emplace(SNAPSHOT_FRAME, channel_);
225     output_->channels_.emplace(CONTINUOUS_FRAME, channel_);
226     output_->dataProcesses_.emplace(SNAPSHOT_FRAME, dataProcess_);
227     output_->dataProcesses_.emplace(CONTINUOUS_FRAME, dataProcess_);
228     g_testCaptureInfosOutput.push_back(g_testCaptureInfoOutputContinuousNotEncode);
229     int32_t ret = output_->StartCapture(g_testCaptureInfosOutput);
230     EXPECT_EQ(DCAMERA_OK, ret);
231 
232     ret = output_->StopCapture();
233     EXPECT_EQ(DCAMERA_OK, ret);
234 
235     g_testCaptureInfosOutput.clear();
236 }
237 
238 /**
239  * @tc.name: dcamera_sink_output_test_006
240  * @tc.desc: Verify the OpenChannel function.
241  * @tc.type: FUNC
242  * @tc.require: I5N1JI
243  */
244 HWTEST_F(DCameraSinkOutputTest, dcamera_sink_output_test_006, TestSize.Level1)
245 {
246     channel_ = std::make_shared<MockCameraChannel>();
247     output_->channels_.emplace(SNAPSHOT_FRAME, channel_);
248     output_->channels_.emplace(CONTINUOUS_FRAME, channel_);
249     int32_t ret = output_->OpenChannel(g_testChannelInfo);
250     EXPECT_EQ(DCAMERA_OK, ret);
251 }
252 
253 /**
254  * @tc.name: dcamera_sink_output_test_007
255  * @tc.desc: Verify the CloseChannel function.
256  * @tc.type: FUNC
257  * @tc.require: I5N1JI
258  */
259 HWTEST_F(DCameraSinkOutputTest, dcamera_sink_output_test_007, TestSize.Level1)
260 {
261     channel_ = std::make_shared<MockCameraChannel>();
262     output_->channels_.emplace(SNAPSHOT_FRAME, channel_);
263     output_->channels_.emplace(CONTINUOUS_FRAME, channel_);
264     int32_t ret = output_->CloseChannel();
265     EXPECT_EQ(DCAMERA_OK, ret);
266 }
267 } // namespace DistributedHardware
268 } // namespace OHOS