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 <gtest/gtest.h>
17
18 #include "dcamera_handler.h"
19 #define private public
20 #include "distributed_camera_sink_service.h"
21 #undef private
22 #include "dcamera_sink_callback.h"
23 #include "distributed_camera_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "idistributed_camera_sink.h"
26
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace DistributedHardware {
31 class DistributedCameraSinkServiceTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 };
38
39 const int32_t TEST_TWENTY_MS = 20000;
40 std::string g_dhId;
41 std::string g_networkId = "08647073e02e7a78f09473aa122ff57fc81c00";
42 std::string g_testParams = "TestParams";
43 std::string g_testCameraInfo = "";
44 std::string g_testChannelInfoContinue = R"({
45 "Type": "OPERATION",
46 "dhId": "camrea_0",
47 "Command": "CHANNEL_NEG",
48 "Value": {"SourceDevId": "TestDevId",
49 "Detail": [{"DataSessionFlag": "dataContinue", "StreamType": 0}]}
50 })";
51 std::string g_testOpenInfoService = R"({
52 "Type": "OPERATION",
53 "dhId": "camrea_0",
54 "Command": "OPEN_CHANNEL",
55 "Value": {"SourceDevId": "TestDevId"}
56 })";
57 std::shared_ptr<DistributedCameraSinkService> sinkService_;
58
SetUpTestCase(void)59 void DistributedCameraSinkServiceTest::SetUpTestCase(void)
60 {
61 DHLOGI("enter");
62 sinkService_ = std::make_shared<DistributedCameraSinkService>(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true);
63 DCameraHandler::GetInstance().Initialize();
64 g_dhId = DCameraHandler::GetInstance().GetCameras().front();
65 sptr<IDCameraSinkCallback> sinkCallback(new DCameraSinkCallback());
66 sinkService_->InitSink(g_testParams, sinkCallback);
67 }
68
TearDownTestCase(void)69 void DistributedCameraSinkServiceTest::TearDownTestCase(void)
70 {
71 DHLOGI("enter");
72 sinkService_->ReleaseSink();
73 usleep(TEST_TWENTY_MS);
74 sinkService_ = nullptr;
75 }
76
SetUp(void)77 void DistributedCameraSinkServiceTest::SetUp(void)
78 {
79 DHLOGI("enter");
80 }
81
TearDown(void)82 void DistributedCameraSinkServiceTest::TearDown(void)
83 {
84 DHLOGI("enter");
85 }
86
87 /**
88 * @tc.name: dcamera_sink_service_test_001
89 * @tc.desc: Verify the SubscribeLocalHardware function.
90 * @tc.type: FUNC
91 * @tc.require: AR000GK6MT
92 */
93 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_001, TestSize.Level1)
94 {
95 DHLOGI("dcamera_sink_service_test_001");
96 EXPECT_EQ(sinkService_ == nullptr, false);
97
98 int32_t ret = sinkService_->SubscribeLocalHardware(g_dhId, g_testParams);
99 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
100 }
101
102 /**
103 * @tc.name: dcamera_sink_service_test_002
104 * @tc.desc: Verify the UnSubscribeLocalHardware function.
105 * @tc.type: FUNC
106 * @tc.require: AR000GK6MT
107 */
108 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_002, TestSize.Level1)
109 {
110 DHLOGI("dcamera_sink_service_test_002");
111 EXPECT_EQ(sinkService_ == nullptr, false);
112
113 int32_t ret = sinkService_->UnsubscribeLocalHardware(g_dhId);
114 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
115 }
116
117 /**
118 * @tc.name: dcamera_sink_service_test_003
119 * @tc.desc: Verify the StopCapture function.
120 * @tc.type: FUNC
121 * @tc.require: AR000GK6N1
122 */
123 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_003, TestSize.Level1)
124 {
125 DHLOGI("dcamera_sink_service_test_003");
126 EXPECT_EQ(sinkService_ == nullptr, false);
127
128 int32_t ret = sinkService_->StopCapture(g_dhId);
129 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
130 }
131
132 /**
133 * @tc.name: dcamera_sink_service_test_004
134 * @tc.desc: Verify the ChannelNeg function.
135 * @tc.type: FUNC
136 * @tc.require: AR000GK6MT
137 */
138 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_004, TestSize.Level1)
139 {
140 DHLOGI("dcamera_sink_service_test_004");
141 EXPECT_EQ(sinkService_ == nullptr, false);
142
143 int32_t ret = sinkService_->ChannelNeg(g_dhId, g_testChannelInfoContinue);
144 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
145 }
146
147 /**
148 * @tc.name: dcamera_sink_service_test_005
149 * @tc.desc: Verify the GetCameraInfo function.
150 * @tc.type: FUNC
151 * @tc.require: AR000GK6MT
152 */
153 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_005, TestSize.Level1)
154 {
155 DHLOGI("dcamera_sink_service_test_005");
156 EXPECT_EQ(sinkService_ == nullptr, false);
157
158 int32_t ret = sinkService_->GetCameraInfo(g_dhId, g_testCameraInfo);
159 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
160 }
161
162 /**
163 * @tc.name: dcamera_sink_service_test_006
164 * @tc.desc: Verify the OpenChannel and CloseChannel function.
165 * @tc.type: FUNC
166 * @tc.require: AR000GK6N1
167 */
168 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_006, TestSize.Level1)
169 {
170 DHLOGI("dcamera_sink_service_test_006");
171 EXPECT_EQ(sinkService_ == nullptr, false);
172
173 int32_t ret = sinkService_->OpenChannel(g_dhId, g_testOpenInfoService);
174 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
175
176 ret = sinkService_->ChannelNeg(g_dhId, g_testChannelInfoContinue);
177 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
178
179 ret = sinkService_->CloseChannel(g_dhId);
180 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
181 }
182
183 /**
184 * @tc.name: dcamera_sink_service_test_007
185 * @tc.desc: Verify the Dump function.
186 * @tc.type: FUNC
187 * @tc.require: AR000GK6N1
188 */
189 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_007, TestSize.Level1)
190 {
191 DHLOGI("dcamera_sink_service_test_007");
192 EXPECT_EQ(sinkService_ == nullptr, false);
193
194 int32_t fd = 1;
195 std::vector<std::u16string> args;
196 std::u16string str(u"");
197 args.push_back(str);
198 int ret = sinkService_->Dump(fd, args);
199 EXPECT_EQ(DCAMERA_OK, ret);
200 }
201
202 /**
203 * @tc.name: dcamera_sink_service_test_008
204 * @tc.desc: Verify the SubscribeLocalHardware function can't find id.
205 * @tc.type: FUNC
206 * @tc.require: AR000GK6MT
207 */
208 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_008, TestSize.Level1)
209 {
210 DHLOGI("dcamera_sink_service_test_008");
211 EXPECT_EQ(sinkService_ == nullptr, false);
212
213 int32_t ret = sinkService_->SubscribeLocalHardware("", g_testParams);
214 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
215 }
216
217 /**
218 * @tc.name: dcamera_sink_service_test_009
219 * @tc.desc: Verify the UnsubscribeLocalHardware function can't find id.
220 * @tc.type: FUNC
221 * @tc.require: AR000GK6MT
222 */
223 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_009, TestSize.Level1)
224 {
225 DHLOGI("dcamera_sink_service_test_009");
226 EXPECT_EQ(sinkService_ == nullptr, false);
227
228 int32_t ret = sinkService_->UnsubscribeLocalHardware("");
229 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
230 }
231
232 /**
233 * @tc.name: dcamera_sink_service_test_010
234 * @tc.desc: Verify the ChannelNeg function can't find id.
235 * @tc.type: FUNC
236 */
237 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_010, TestSize.Level1)
238 {
239 DHLOGI("dcamera_sink_service_test_010");
240 EXPECT_EQ(sinkService_ == nullptr, false);
241
242 int32_t ret = sinkService_->ChannelNeg("", g_testChannelInfoContinue);
243 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
244 }
245
246 /**
247 * @tc.name: dcamera_sink_service_test_011
248 * @tc.desc: Verify the GetCameraInfo function can't find id.
249 * @tc.type: FUNC
250 */
251 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_011, TestSize.Level1)
252 {
253 DHLOGI("dcamera_sink_service_test_011");
254 EXPECT_EQ(sinkService_ == nullptr, false);
255
256 int32_t ret = sinkService_->GetCameraInfo("", g_testCameraInfo);
257 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
258 }
259
260 /**
261 * @tc.name: dcamera_sink_service_test_012
262 * @tc.desc: Verify the OpenChannel and CloseChannel function can't find id.
263 * @tc.type: FUNC
264 */
265 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_012, TestSize.Level1)
266 {
267 DHLOGI("dcamera_sink_service_test_012");
268 EXPECT_EQ(sinkService_ == nullptr, false);
269
270 int32_t ret = sinkService_->OpenChannel("", g_testOpenInfoService);
271 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
272
273 ret = sinkService_->CloseChannel("");
274 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
275 }
276
277 /**
278 * @tc.name: dcamera_sink_service_test_013
279 * @tc.desc: Verify the Dump function with large arg.
280 * @tc.type: FUNC
281 */
282 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_013, TestSize.Level1)
283 {
284 DHLOGI("dcamera_sink_service_test_013");
285 EXPECT_EQ(sinkService_ == nullptr, false);
286
287 int32_t fd = 1;
288 std::vector<std::u16string> args(sinkService_->DUMP_MAX_SIZE, u"");
289 std::u16string str(u"");
290 args.push_back(str);
291 int ret = sinkService_->Dump(fd, args);
292 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
293 }
294
295 /**
296 * @tc.name: dcamera_sink_service_test_014
297 * @tc.desc: Verify the StopCapture function can't find id.
298 * @tc.type: FUNC
299 */
300 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_014, TestSize.Level1)
301 {
302 DHLOGI("dcamera_sink_service_test_014");
303 EXPECT_EQ(sinkService_ == nullptr, false);
304
305 int32_t ret = sinkService_->StopCapture("");
306 EXPECT_EQ(DCAMERA_NOT_FOUND, ret);
307 }
308
309 /**
310 * @tc.name: dcamera_sink_service_test_015
311 * @tc.desc: Verify the PauseDistributedHardware function.
312 * @tc.type: FUNC
313 */
314 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_015, TestSize.Level1)
315 {
316 DHLOGI("dcamera_sink_service_test_015");
317 EXPECT_EQ(sinkService_ == nullptr, false);
318
319 int32_t ret = sinkService_->PauseDistributedHardware(g_networkId);
320 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
321 }
322
323 /**
324 * @tc.name: dcamera_sink_service_test_016
325 * @tc.desc: Verify the ResumeDistributedHardware function.
326 * @tc.type: FUNC
327 */
328 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_016, TestSize.Level1)
329 {
330 DHLOGI("dcamera_sink_service_test_016");
331 EXPECT_EQ(sinkService_ == nullptr, false);
332
333 int32_t ret = sinkService_->ResumeDistributedHardware(g_networkId);
334 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
335 }
336
337 /**
338 * @tc.name: dcamera_sink_service_test_017
339 * @tc.desc: Verify the StopDistributedHardware function.
340 * @tc.type: FUNC
341 */
342 HWTEST_F(DistributedCameraSinkServiceTest, dcamera_sink_service_test_017, TestSize.Level1)
343 {
344 DHLOGI("dcamera_sink_service_test_017");
345 EXPECT_EQ(sinkService_ == nullptr, false);
346
347 int32_t ret = sinkService_->StopDistributedHardware(g_networkId);
348 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
349 }
350 } // namespace DistributedHardware
351 } // namespace OHOS