1 /*
2 * Copyright (c) 2022-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 "distributed_hardware_service_test.h"
17
18 #include <chrono>
19 #include <thread>
20 #include <vector>
21
22 #include "constants.h"
23 #include "cJSON.h"
24 #include "dh_context.h"
25 #include "distributed_hardware_errno.h"
26 #include "distributed_hardware_log.h"
27 #include "distributed_hardware_service.h"
28 #include "distributed_hardware_manager.h"
29 #include "task_board.h"
30 #include "mock_publisher_listener.h"
31
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace DistributedHardware {
36 namespace {
37 const int32_t ASID = 4801;
38 const DHTopic TOPIC = DHTopic::TOPIC_START_DSCREEN;
39 sptr<IPublisherListener> g_listener(new MockPublisherListener());
40
41 const std::u16string ARGS_H = u"-h";
42 const std::u16string ARGS_L = u"-l";
43 const std::u16string ARGS_E = u"-e";
44 const std::u16string ARGS_T = u"-t";
45 const std::u16string ARGS_C = u"-c";
46 }
SetUpTestCase(void)47 void DistributedHardwareServiceTest::SetUpTestCase(void) {}
48
TearDownTestCase(void)49 void DistributedHardwareServiceTest::TearDownTestCase(void) {}
50
SetUp()51 void DistributedHardwareServiceTest::SetUp() {}
52
TearDown()53 void DistributedHardwareServiceTest::TearDown() {}
54
55 /**
56 * @tc.name: register_publisher_listener_001
57 * @tc.desc: Verify the RegisterPublisherListener function
58 * @tc.type: FUNC
59 * @tc.require: AR000GHSJM
60 */
61 HWTEST_F(DistributedHardwareServiceTest, register_publisher_listener_001, TestSize.Level0)
62 {
63 DistributedHardwareService service(ASID, true);
64 auto ret = service.RegisterPublisherListener(TOPIC, g_listener);
65
66 EXPECT_EQ(ret, DH_FWK_SUCCESS);
67 }
68
69 /**
70 * @tc.name: register_publisher_listener_002
71 * @tc.desc: Verify the RegisterPublisherListener function
72 * @tc.type: FUNC
73 * @tc.require: AR000GHSJM
74 */
75 HWTEST_F(DistributedHardwareServiceTest, register_publisher_listener_002, TestSize.Level0)
76 {
77 DistributedHardwareService service(ASID, true);
78 sptr<IPublisherListener> listener = nullptr;
79 auto ret = service.RegisterPublisherListener(TOPIC, listener);
80 EXPECT_EQ(ret, DH_FWK_SUCCESS);
81 }
82
83 /**
84 * @tc.name: unregister_publisher_listener_001
85 * @tc.desc: Verify the UnregisterPublisherListener function
86 * @tc.type: FUNC
87 * @tc.require: AR000GHSJM
88 */
89 HWTEST_F(DistributedHardwareServiceTest, unregister_publisher_listener_001, TestSize.Level0)
90 {
91 DistributedHardwareService service(ASID, true);
92 service.RegisterPublisherListener(TOPIC, g_listener);
93 auto ret = service.UnregisterPublisherListener(TOPIC, g_listener);
94
95 EXPECT_EQ(ret, DH_FWK_SUCCESS);
96 }
97
98 /**
99 * @tc.name: publish_message_001
100 * @tc.desc: Verify the PublishMessage function
101 * @tc.type: FUNC
102 * @tc.require: AR000GHSJM
103 */
104 HWTEST_F(DistributedHardwareServiceTest, publish_message_001, TestSize.Level0)
105 {
106 DistributedHardwareService service(ASID, true);
107 std::string msg;
108 service.RegisterPublisherListener(TOPIC, g_listener);
109 auto ret = service.PublishMessage(TOPIC, msg);
110
111 EXPECT_EQ(ret, DH_FWK_SUCCESS);
112 }
113
114 /**
115 * @tc.name: publish_message_002
116 * @tc.desc: Verify the PublishMessage function
117 * @tc.type: FUNC
118 * @tc.require: AR000GHSJM
119 */
120 HWTEST_F(DistributedHardwareServiceTest, publish_message_002, TestSize.Level0)
121 {
122 DistributedHardwareService service(ASID, true);
123 std::string msg = "msg";
124 service.RegisterPublisherListener(TOPIC, g_listener);
125 auto ret = service.PublishMessage(TOPIC, msg);
126
127 EXPECT_EQ(ret, DH_FWK_SUCCESS);
128 }
129
130 /**
131 * @tc.name: onStop_test_002
132 * @tc.desc: Verify the OnStop function
133 * @tc.type: FUNC
134 * @tc.require: AR000GHSJM
135 */
136 HWTEST_F(DistributedHardwareServiceTest, onStop_test_002, TestSize.Level0)
137 {
138 DistributedHardwareService service(ASID, true);
139 service.OnStop();
140
141 EXPECT_EQ(service.state_, ServiceRunningState::STATE_NOT_START);
142 }
143
144 /**
145 * @tc.name: dump_test_001
146 * @tc.desc: Verify the Dump function
147 * @tc.type: FUNC
148 * @tc.require: AR000GHSJM
149 */
150 HWTEST_F(DistributedHardwareServiceTest, dump_test_001, TestSize.Level0)
151 {
152 DistributedHardwareService service(ASID, true);
153 int32_t fd = 1;
154
155 auto ret = service.Dump(fd, std::vector<std::u16string> { ARGS_H });
156 EXPECT_EQ(ret, DH_FWK_SUCCESS);
157 ret = service.Dump(fd, std::vector<std::u16string> { ARGS_L });
158 EXPECT_EQ(ret, DH_FWK_SUCCESS);
159 ret = service.Dump(fd, std::vector<std::u16string> { ARGS_E });
160 EXPECT_EQ(ret, DH_FWK_SUCCESS);
161 ret = service.Dump(fd, std::vector<std::u16string> { ARGS_T });
162 EXPECT_EQ(ret, DH_FWK_SUCCESS);
163 ret = service.Dump(fd, std::vector<std::u16string> { ARGS_C });
164 EXPECT_EQ(ret, DH_FWK_SUCCESS);
165 }
166
167 /**
168 * @tc.name: OnStart_001
169 * @tc.desc: Verify the OnStart function
170 * @tc.type: FUNC
171 * @tc.require: AR000GHSJM
172 */
173 HWTEST_F(DistributedHardwareServiceTest, OnStart_001, TestSize.Level0)
174 {
175 DistributedHardwareService service(ASID, true);
176 service.state_ = ServiceRunningState::STATE_RUNNING;
177 service.OnStart();
178 service.OnStop();
179 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, service.state_);
180 }
181
182 /**
183 * @tc.name: QueryLocalSysSpec_001
184 * @tc.desc: Verify the QueryLocalSysSpec function
185 * @tc.type: FUNC
186 * @tc.require: AR000GHSJM
187 */
188 HWTEST_F(DistributedHardwareServiceTest, QueryLocalSysSpec_001, TestSize.Level0)
189 {
190 DistributedHardwareService service(ASID, true);
191 std::string ret = service.QueryLocalSysSpec(QueryLocalSysSpecType::HISTREAMER_AUDIO_ENCODER);
192 ret = service.QueryLocalSysSpec(QueryLocalSysSpecType::HISTREAMER_AUDIO_DECODER);
193 ret = service.QueryLocalSysSpec(QueryLocalSysSpecType::HISTREAMER_VIDEO_ENCODER);
194 ret = service.QueryLocalSysSpec(QueryLocalSysSpecType::HISTREAMER_VIDEO_DECODER);
195 ret = service.QueryLocalSysSpec(QueryLocalSysSpecType::MAX);
196 EXPECT_EQ(ret.empty(), true);
197 }
198
199 /**
200 * @tc.name: PauseDistributedHardware_001
201 * @tc.desc: Verify the PauseDistributedHardware function
202 * @tc.type: FUNC
203 * @tc.require: AR000GHSJM
204 */
205 HWTEST_F(DistributedHardwareServiceTest, PauseDistributedHardware_001, TestSize.Level0)
206 {
207 DistributedHardwareService service(ASID, true);
208 std::string networkId = "111";
209 auto ret = service.PauseDistributedHardware(DHType::UNKNOWN, networkId);
210 ret = service.PauseDistributedHardware(DHType::CAMERA, networkId);
211 ret = service.PauseDistributedHardware(DHType::MAX_DH, networkId);
212 EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID);
213 }
214
215 /**
216 * @tc.name: ResumeDistributedHardware_001
217 * @tc.desc: Verify the ResumeDistributedHardware function
218 * @tc.type: FUNC
219 * @tc.require: AR000GHSJM
220 */
221 HWTEST_F(DistributedHardwareServiceTest, ResumeDistributedHardware_001, TestSize.Level0)
222 {
223 DistributedHardwareService service(ASID, true);
224 std::string networkId = "111";
225 auto ret = service.PauseDistributedHardware(DHType::UNKNOWN, networkId);
226 ret = service.PauseDistributedHardware(DHType::AUDIO, networkId);
227 ret = service.PauseDistributedHardware(DHType::MAX_DH, networkId);
228 EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID);
229 }
230
231 /**
232 * @tc.name: StopDistributedHardware_001
233 * @tc.desc: Verify the StopDistributedHardware function
234 * @tc.type: FUNC
235 * @tc.require: AR000GHSJM
236 */
237 HWTEST_F(DistributedHardwareServiceTest, StopDistributedHardware_001, TestSize.Level0)
238 {
239 DistributedHardwareService service(ASID, true);
240 std::string networkId = "111";
241 auto ret = service.PauseDistributedHardware(DHType::UNKNOWN, networkId);
242 ret = service.PauseDistributedHardware(DHType::INPUT, networkId);
243 ret = service.PauseDistributedHardware(DHType::MAX_DH, networkId);
244 EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID);
245 }
246
247 HWTEST_F(DistributedHardwareServiceTest, ResumeDistributedHardware_002, TestSize.Level0)
248 {
249 DistributedHardwareService service(ASID, true);
250 std::string networkId = "111";
251 auto ret = service.ResumeDistributedHardware(DHType::UNKNOWN, networkId);
252 EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID);
253 }
254
255 HWTEST_F(DistributedHardwareServiceTest, StopDistributedHardware_002, TestSize.Level0)
256 {
257 DistributedHardwareService service(ASID, true);
258 std::string networkId = "111";
259 auto ret = service.StopDistributedHardware(DHType::UNKNOWN, networkId);
260 EXPECT_EQ(ret, ERR_DH_FWK_PARA_INVALID);
261 }
262
263 HWTEST_F(DistributedHardwareServiceTest, DoBusinessInit_001, TestSize.Level0)
264 {
265 DistributedHardwareService service(ASID, true);
266 auto ret = service.DoBusinessInit();
267 EXPECT_EQ(true, ret);
268 }
269
270 HWTEST_F(DistributedHardwareServiceTest, QueryDhSysSpec_001, TestSize.Level0)
271 {
272 std::string targetKey = "";
273 std::string attrs = "";
274 DistributedHardwareService service(ASID, true);
275 auto ret = service.QueryDhSysSpec(targetKey, attrs);
276 EXPECT_EQ(0, ret.length());
277 }
278
279 HWTEST_F(DistributedHardwareServiceTest, QueryDhSysSpec_002, TestSize.Level0)
280 {
281 std::string targetKey = "histmAudEnc";
282 int32_t target = 100;
283 cJSON *attrJson = cJSON_CreateObject();
284 if (attrJson == nullptr) {
285 return;
286 }
287 cJSON_AddNumberToObject(attrJson, targetKey.c_str(), target);
288 char* cjson = cJSON_PrintUnformatted(attrJson);
289 if (cjson == nullptr) {
290 cJSON_Delete(attrJson);
291 return;
292 }
293 std::string attrs(cjson);
294 DistributedHardwareService service(ASID, true);
295 auto ret = service.QueryDhSysSpec(targetKey, attrs);
296 EXPECT_EQ(0, ret.length());
297 cJSON_free(cjson);
298 cJSON_Delete(attrJson);
299
300 cJSON *attrJson1 = cJSON_CreateObject();
301 if (attrJson1 == nullptr) {
302 return;
303 }
304 std::string targetKeyValue = "targetKeyValue";
305 cJSON_AddStringToObject(attrJson1, targetKey.c_str(), targetKeyValue.c_str());
306 char* cjson1 = cJSON_PrintUnformatted(attrJson1);
307 if (cjson1 == nullptr) {
308 cJSON_Delete(attrJson1);
309 return;
310 }
311 std::string attrs1(cjson1);
312 ret = service.QueryDhSysSpec(targetKey, attrs1);
313 EXPECT_NE(0, ret.length());
314 cJSON_free(cjson1);
315 cJSON_Delete(attrJson1);
316 }
317 } // namespace DistributedHardware
318 } // namespace OHOS
319