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 
19 #include "anonymous_string.h"
20 #include "distributed_hardware_log.h"
21 
22 #define private public
23 #include "dcamera_sink_access_control.h"
24 #include "dcamera_sink_controller.h"
25 #include "dcamera_sink_dev.h"
26 #undef private
27 
28 #include "dcamera_sink_callback.h"
29 #include "dcamera_handler.h"
30 #include "dcamera_utils_tools.h"
31 #include "distributed_camera_errno.h"
32 #include "mock_dcamera_sink_controller.h"
33 
34 
35 using namespace testing::ext;
36 
37 namespace OHOS {
38 namespace DistributedHardware {
39 class DCameraSinkDevTest : public testing::Test {
40 public:
41     static void SetUpTestCase(void);
42     static void TearDownTestCase(void);
43     void SetUp();
44     void TearDown();
45 
46     std::shared_ptr<DCameraSinkDev> dev_;
47 };
48 
49 const int32_t TEST_TWENTY_MS = 20000;
50 const std::string TEST_PARAMETER = "";
51 std::string g_testCameraInfo = "";
52 
53 std::string g_testChannelInfoDevContinue = R"({
54     "Type": "OPERATION",
55     "dhId": "camrea_0",
56     "Command": "CHANNEL_NEG",
57     "Value": {"SourceDevId": "TestDevId",
58     "Detail": [{"DataSessionFlag": "dataContinue", "StreamType": 0}]}
59 })";
60 
61 std::string g_testOpenInfoDev = R"({
62     "Type": "OPERATION",
63     "dhId": "camrea_0",
64     "Command": "OPEN_CHANNEL",
65     "Value": {"SourceDevId": "TestDevId"}
66 })";
67 
68 std::string g_testChannelInfoDevEmpty = "";
69 std::string g_testOpenInfoDevEmpty = "";
70 std::string g_sinkCtrlStr = "";
71 
SetUpTestCase(void)72 void DCameraSinkDevTest::SetUpTestCase(void)
73 {
74 }
75 
TearDownTestCase(void)76 void DCameraSinkDevTest::TearDownTestCase(void)
77 {
78 }
79 
SetUp(void)80 void DCameraSinkDevTest::SetUp(void)
81 {
82     DCameraHandler::GetInstance().Initialize();
83     std::vector<std::string> cameras = DCameraHandler::GetInstance().GetCameras();
84     sptr<IDCameraSinkCallback> sinkCallback(new DCameraSinkCallback());
85     dev_ = std::make_shared<DCameraSinkDev>(cameras[0], sinkCallback);
86 
87     dev_->accessControl_ = std::make_shared<DCameraSinkAccessControl>();
88     dev_->controller_ = std::make_shared<MockDCameraSinkController>(dev_->accessControl_);
89 }
90 
TearDown(void)91 void DCameraSinkDevTest::TearDown(void)
92 {
93     usleep(TEST_TWENTY_MS);
94     dev_ = nullptr;
95 }
96 
97 /**
98  * @tc.name: dcamera_sink_dev_test_002
99  * @tc.desc: Verify the SubscribeLocalHardware function.
100  * @tc.type: FUNC
101  * @tc.require: AR000GK6MT
102  */
103 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_002, TestSize.Level1)
104 {
105     int32_t ret = dev_->SubscribeLocalHardware(TEST_PARAMETER);
106     EXPECT_EQ(DCAMERA_OK, ret);
107 }
108 
109 /**
110  * @tc.name: dcamera_sink_dev_test_003
111  * @tc.desc: Verify the UnsubscribeLocalHardware function.
112  * @tc.type: FUNC
113  * @tc.require: AR000GK6MT
114  */
115 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_003, TestSize.Level1)
116 {
117     int32_t ret = dev_->UnsubscribeLocalHardware();
118     EXPECT_EQ(DCAMERA_OK, ret);
119 }
120 
121 /**
122  * @tc.name: dcamera_sink_dev_test_004
123  * @tc.desc: Verify the GetCameraInfo function.
124  * @tc.type: FUNC
125  * @tc.require: AR000GK6MT
126  */
127 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_004, TestSize.Level1)
128 {
129     int32_t ret = dev_->GetCameraInfo(g_testCameraInfo);
130     EXPECT_EQ(DCAMERA_OK, ret);
131     DHLOGI("DCameraSinkDevTest::GetCameraInfo cameraInfo is %{public}s", GetAnonyString(g_testCameraInfo).c_str());
132 
133     g_sinkCtrlStr = "test_004";
134     ret = dev_->GetCameraInfo(g_testCameraInfo);
135     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
136 }
137 
138 /**
139  * @tc.name: dcamera_sink_dev_test_005
140  * @tc.desc: Verify the ChannelNeg function.
141  * @tc.type: FUNC
142  * @tc.require: AR000GK6MT
143  */
144 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_005, TestSize.Level1)
145 {
146     int32_t ret = dev_->ChannelNeg(g_testChannelInfoDevEmpty);
147     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
148 }
149 
150 /**
151  * @tc.name: dcamera_sink_dev_test_006
152  * @tc.desc: Verify the ChannelNeg function.
153  * @tc.type: FUNC
154  * @tc.require: AR000GK6MT
155  */
156 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_006, TestSize.Level1)
157 {
158     int32_t ret = dev_->ChannelNeg(g_testChannelInfoDevContinue);
159     EXPECT_EQ(DCAMERA_OK, ret);
160     g_sinkCtrlStr = "test_006";
161     ret = dev_->ChannelNeg(g_testChannelInfoDevContinue);
162     EXPECT_NE(DCAMERA_OK, ret);
163     g_sinkCtrlStr = "";
164 }
165 
166 /**
167  * @tc.name: dcamera_sink_dev_test_007
168  * @tc.desc: Verify the StopCapture function.
169  * @tc.type: FUNC
170  * @tc.require: AR000GK6N1
171  */
172 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_007, TestSize.Level1)
173 {
174     int32_t ret = dev_->StopCapture();
175     EXPECT_EQ(DCAMERA_OK, ret);
176 }
177 
178 /**
179  * @tc.name: dcamera_sink_dev_test_008
180  * @tc.desc: Verify the OpenChannel function.
181  * @tc.type: FUNC
182  * @tc.require: AR000GK6N1
183  */
184 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_008, TestSize.Level1)
185 {
186     int32_t ret = dev_->OpenChannel(g_testOpenInfoDevEmpty);
187     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
188 }
189 
190 /**
191  * @tc.name: dcamera_sink_dev_test_009
192  * @tc.desc: Verify the OpenChannel function.
193  * @tc.type: FUNC
194  * @tc.require: AR000GK6N1
195  */
196 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_009, TestSize.Level1)
197 {
198     int32_t ret = dev_->OpenChannel(g_testOpenInfoDev);
199     EXPECT_EQ(DCAMERA_OK, ret);
200 }
201 
202 /**
203  * @tc.name: dcamera_sink_dev_test_010
204  * @tc.desc: Verify the CloseChannel function.
205  * @tc.type: FUNC
206  * @tc.require: AR000GK6N1
207  */
208 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_010, TestSize.Level1)
209 {
210     int32_t ret = dev_->CloseChannel();
211     EXPECT_EQ(DCAMERA_OK, ret);
212 }
213 
214 /**
215  * @tc.name: dcamera_sink_dev_test_011
216  * @tc.desc: Verify the GetDhid function.
217  * @tc.type: FUNC
218  * @tc.require: AR000GK6N1
219  */
220 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_011, TestSize.Level1)
221 {
222     dev_->dhId_ = "1";
223     std::string ret = dev_->GetDhid();
224     EXPECT_NE("", ret);
225 }
226 
227 /**
228  * @tc.name: dcamera_sink_dev_test_012
229  * @tc.desc: Verify the PauseDistributedHardware function.
230  * @tc.type: FUNC
231  * @tc.require: AR000GK6N1
232  */
233 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_012, TestSize.Level1)
234 {
235     std::string devId = "";
236     int32_t ret = dev_->PauseDistributedHardware(devId);
237     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
238 
239     devId = "devId";
240     ret = dev_->PauseDistributedHardware(devId);
241     EXPECT_EQ(DCAMERA_OK, ret);
242 
243     dev_->controller_ = nullptr;
244     ret = dev_->PauseDistributedHardware(devId);
245     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
246 }
247 
248 /**
249  * @tc.name: dcamera_sink_dev_test_013
250  * @tc.desc: Verify the ResumeDistributedHardware function.
251  * @tc.type: FUNC
252  * @tc.require: AR000GK6N1
253  */
254 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_013, TestSize.Level1)
255 {
256     std::string devId = "";
257     int32_t ret = dev_->ResumeDistributedHardware(devId);
258     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
259 
260     devId = "devId";
261     ret = dev_->ResumeDistributedHardware(devId);
262     EXPECT_EQ(DCAMERA_OK, ret);
263 
264     dev_->controller_ = nullptr;
265     ret = dev_->ResumeDistributedHardware(devId);
266     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
267 }
268 
269 /**
270  * @tc.name: dcamera_sink_dev_test_014
271  * @tc.desc: Verify the StopDistributedHardware function.
272  * @tc.type: FUNC
273  * @tc.require: AR000GK6N1
274  */
275 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_014, TestSize.Level1)
276 {
277     std::string devId = "";
278     int32_t ret = dev_->StopDistributedHardware(devId);
279     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
280 
281     devId = "devId";
282     ret = dev_->StopDistributedHardware(devId);
283     EXPECT_EQ(DCAMERA_OK, ret);
284 
285     dev_->controller_ = nullptr;
286     ret = dev_->StopDistributedHardware(devId);
287     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
288 }
289 } // namespace DistributedHardware
290 } // namespace OHOS