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_handler.h"
29 #include "dcamera_utils_tools.h"
30 #include "distributed_camera_errno.h"
31 #include "mock_dcamera_sink_controller.h"
32
33
34 using namespace testing::ext;
35
36 namespace OHOS {
37 namespace DistributedHardware {
38 class DCameraSinkDevTest : 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<DCameraSinkDev> dev_;
46 };
47
48 const int32_t TEST_TWENTY_MS = 20000;
49 const std::string TEST_PARAMETER = "";
50 std::string g_testCameraInfo = "";
51
52 std::string g_testChannelInfoDevContinue = R"({
53 "Type": "OPERATION",
54 "dhId": "camrea_0",
55 "Command": "CHANNEL_NEG",
56 "Value": {"SourceDevId": "TestDevId",
57 "Detail": [{"DataSessionFlag": "dataContinue", "StreamType": 0}]}
58 })";
59
60 std::string g_testOpenInfoDev = R"({
61 "Type": "OPERATION",
62 "dhId": "camrea_0",
63 "Command": "OPEN_CHANNEL",
64 "Value": {"SourceDevId": "TestDevId"}
65 })";
66
67 std::string g_testChannelInfoDevEmpty = "";
68 std::string g_testOpenInfoDevEmpty = "";
69
SetUpTestCase(void)70 void DCameraSinkDevTest::SetUpTestCase(void)
71 {
72 }
73
TearDownTestCase(void)74 void DCameraSinkDevTest::TearDownTestCase(void)
75 {
76 }
77
SetUp(void)78 void DCameraSinkDevTest::SetUp(void)
79 {
80 DCameraHandler::GetInstance().Initialize();
81 std::vector<std::string> cameras = DCameraHandler::GetInstance().GetCameras();
82 dev_ = std::make_shared<DCameraSinkDev>(cameras[0]);
83
84 dev_->accessControl_ = std::make_shared<DCameraSinkAccessControl>();
85 dev_->controller_ = std::make_shared<MockDCameraSinkController>(dev_->accessControl_);
86 }
87
TearDown(void)88 void DCameraSinkDevTest::TearDown(void)
89 {
90 usleep(TEST_TWENTY_MS);
91 dev_ = nullptr;
92 }
93
94 /**
95 * @tc.name: dcamera_sink_dev_test_001
96 * @tc.desc: Verify the Init and UnInit function.
97 * @tc.type: FUNC
98 * @tc.require: AR000GK6MV
99 */
100 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_001, TestSize.Level1)
101 {
102 int32_t ret = dev_->Init();
103 EXPECT_EQ(DCAMERA_OK, ret);
104 EXPECT_EQ(true, dev_->isInit_);
105
106 ret = dev_->UnInit();
107 EXPECT_EQ(DCAMERA_OK, ret);
108 EXPECT_EQ(false, dev_->isInit_);
109 }
110
111 /**
112 * @tc.name: dcamera_sink_dev_test_002
113 * @tc.desc: Verify the SubscribeLocalHardware function.
114 * @tc.type: FUNC
115 * @tc.require: AR000GK6MT
116 */
117 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_002, TestSize.Level1)
118 {
119 int32_t ret = dev_->SubscribeLocalHardware(TEST_PARAMETER);
120 EXPECT_EQ(DCAMERA_OK, ret);
121 }
122
123 /**
124 * @tc.name: dcamera_sink_dev_test_003
125 * @tc.desc: Verify the UnsubscribeLocalHardware function.
126 * @tc.type: FUNC
127 * @tc.require: AR000GK6MT
128 */
129 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_003, TestSize.Level1)
130 {
131 int32_t ret = dev_->UnsubscribeLocalHardware();
132 EXPECT_EQ(DCAMERA_OK, ret);
133 }
134
135 /**
136 * @tc.name: dcamera_sink_dev_test_004
137 * @tc.desc: Verify the GetCameraInfo function.
138 * @tc.type: FUNC
139 * @tc.require: AR000GK6MT
140 */
141 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_004, TestSize.Level1)
142 {
143 int32_t ret = dev_->GetCameraInfo(g_testCameraInfo);
144 EXPECT_EQ(DCAMERA_OK, ret);
145 DHLOGI("DCameraSinkDevTest::GetCameraInfo cameraInfo is %s", GetAnonyString(g_testCameraInfo).c_str());
146 }
147
148 /**
149 * @tc.name: dcamera_sink_dev_test_005
150 * @tc.desc: Verify the ChannelNeg function.
151 * @tc.type: FUNC
152 * @tc.require: AR000GK6MT
153 */
154 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_005, TestSize.Level1)
155 {
156 int32_t ret = dev_->ChannelNeg(g_testChannelInfoDevEmpty);
157 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
158 }
159
160 /**
161 * @tc.name: dcamera_sink_dev_test_006
162 * @tc.desc: Verify the ChannelNeg function.
163 * @tc.type: FUNC
164 * @tc.require: AR000GK6MT
165 */
166 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_006, TestSize.Level1)
167 {
168 int32_t ret = dev_->ChannelNeg(g_testChannelInfoDevContinue);
169 EXPECT_EQ(DCAMERA_OK, ret);
170 }
171
172 /**
173 * @tc.name: dcamera_sink_dev_test_007
174 * @tc.desc: Verify the StopCapture function.
175 * @tc.type: FUNC
176 * @tc.require: AR000GK6N1
177 */
178 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_007, TestSize.Level1)
179 {
180 int32_t ret = dev_->StopCapture();
181 EXPECT_EQ(DCAMERA_OK, ret);
182 }
183
184 /**
185 * @tc.name: dcamera_sink_dev_test_008
186 * @tc.desc: Verify the OpenChannel function.
187 * @tc.type: FUNC
188 * @tc.require: AR000GK6N1
189 */
190 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_008, TestSize.Level1)
191 {
192 int32_t ret = dev_->OpenChannel(g_testOpenInfoDevEmpty);
193 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
194 }
195
196 /**
197 * @tc.name: dcamera_sink_dev_test_009
198 * @tc.desc: Verify the OpenChannel function.
199 * @tc.type: FUNC
200 * @tc.require: AR000GK6N1
201 */
202 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_009, TestSize.Level1)
203 {
204 int32_t ret = dev_->OpenChannel(g_testOpenInfoDev);
205 EXPECT_EQ(DCAMERA_OK, ret);
206 }
207
208 /**
209 * @tc.name: dcamera_sink_dev_test_010
210 * @tc.desc: Verify the CloseChannel function.
211 * @tc.type: FUNC
212 * @tc.require: AR000GK6N1
213 */
214 HWTEST_F(DCameraSinkDevTest, dcamera_sink_dev_test_010, TestSize.Level1)
215 {
216 int32_t ret = dev_->CloseChannel();
217 EXPECT_EQ(DCAMERA_OK, ret);
218 }
219 } // namespace DistributedHardware
220 } // namespace OHOS