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 "distributed_input_inner_test.h"
17 
18 #include "system_ability_definition.h"
19 #include "dinput_context.h"
20 #include "dinput_errcode.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::DistributedHardware::DistributedInput;
24 using namespace std;
25 using namespace OHOS;
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace DistributedInput {
SetUp()30 void DistributedInputInnerTest::SetUp()
31 {
32 }
33 
TearDown()34 void DistributedInputInnerTest::TearDown()
35 {
36 }
37 
SetUpTestCase()38 void DistributedInputInnerTest::SetUpTestCase()
39 {
40 }
41 
TearDownTestCase()42 void DistributedInputInnerTest::TearDownTestCase()
43 {
44 }
45 
OnResult(const std::string & deviceId,const int32_t & status)46 void DistributedInputInnerTest::TestPrepareDInputCallback::OnResult(
47     const std::string &deviceId, const int32_t &status)
48 {
49     (void)deviceId;
50     (void)status;
51     return;
52 }
53 
OnResult(const std::string & deviceId,const int32_t & status)54 void DistributedInputInnerTest::TestUnprepareDInputCallback::OnResult(
55     const std::string &deviceId, const int32_t &status)
56 {
57     (void)deviceId;
58     (void)status;
59     return;
60 }
61 
OnResult(const std::string & deviceId,const uint32_t & inputTypes,const int32_t & status)62 void DistributedInputInnerTest::TestStartDInputCallback::OnResult(
63     const std::string &deviceId, const uint32_t &inputTypes, const int32_t &status)
64 {
65     (void)deviceId;
66     (void)inputTypes;
67     (void)status;
68     return;
69 }
70 
OnResult(const std::string & deviceId,const uint32_t & inputTypes,const int32_t & status)71 void DistributedInputInnerTest::TestStopDInputCallback::OnResult(
72     const std::string &deviceId, const uint32_t &inputTypes, const int32_t &status)
73 {
74     (void)deviceId;
75     (void)inputTypes;
76     (void)status;
77     return;
78 }
79 
OnResultDhids(const std::string & devId,const int32_t & status)80 void DistributedInputInnerTest::TestStartStopDInputCallback::OnResultDhids(
81     const std::string &devId, const int32_t &status)
82 {
83     (void)devId;
84     (void)status;
85     return;
86 }
87 
OnNodeOnLine(const std::string & srcDevId,const std::string & sinkDevId,const std::string & sinkNodeId,const std::string & sinkNodeDesc)88 void DistributedInputInnerTest::TestInputNodeListener::OnNodeOnLine(const std::string &srcDevId,
89     const std::string &sinkDevId, const std::string &sinkNodeId, const std::string &sinkNodeDesc)
90 {
91     (void)srcDevId;
92     (void)sinkDevId;
93     (void)sinkNodeId;
94     (void)sinkNodeDesc;
95     return;
96 }
97 
OnNodeOffLine(const std::string & srcDevId,const std::string & sinkDevId,const std::string & sinkNodeId)98 void DistributedInputInnerTest::TestInputNodeListener::OnNodeOffLine(const std::string &srcDevId,
99     const std::string &sinkDevId, const std::string &sinkNodeId)
100 {
101     (void)srcDevId;
102     (void)sinkDevId;
103     (void)sinkNodeId;
104     return;
105 }
106 
OnSimulationEvent(uint32_t type,uint32_t code,int32_t value)107 int32_t DistributedInputInnerTest::TestSimulationEventListenerStub::OnSimulationEvent(
108     uint32_t type, uint32_t code, int32_t value)
109 {
110     (void)type;
111     (void)code;
112     (void)value;
113     return DH_SUCCESS;
114 }
115 
OnResult(const std::string & devId,const uint32_t status)116 void DistributedInputInnerTest::TestRegisterSessionStateCb::OnResult(const std::string &devId, const uint32_t status)
117 {
118     (void)devId;
119     (void)status;
120     return;
121 }
122 
CheckSourceProxy() const123 int DistributedInputInnerTest::CheckSourceProxy() const
124 {
125     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
126         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
127     if (!systemAbilityManager) {
128         return DH_SUCCESS;
129     }
130 
131     OHOS::sptr<OHOS::IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(
132         DISTRIBUTED_HARDWARE_INPUT_SOURCE_SA_ID);
133     if (!remoteObject) {
134         return DH_SUCCESS;
135     }
136 
137     OHOS::sptr<IDistributedSourceInput> proxyTest;
138 
139     proxyTest = OHOS::iface_cast<IDistributedSourceInput>(remoteObject);
140     if ((!proxyTest) || (!proxyTest->AsObject())) {
141         return DH_SUCCESS;
142     }
143 
144     return DH_SUCCESS;
145 }
146 
CheckSinkProxy() const147 int DistributedInputInnerTest::CheckSinkProxy() const
148 {
149     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
150         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
151     if (!systemAbilityManager) {
152         return DH_SUCCESS;
153     }
154 
155     OHOS::sptr<OHOS::IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(
156         DISTRIBUTED_HARDWARE_INPUT_SINK_SA_ID);
157     if (!remoteObject) {
158         return DH_SUCCESS;
159     }
160 
161     OHOS::sptr<IDistributedSinkInput> proxyTest;
162 
163     proxyTest = OHOS::iface_cast<IDistributedSinkInput>(remoteObject);
164     if ((!proxyTest) || (!proxyTest->AsObject())) {
165         return DH_SUCCESS;
166     }
167 
168     return DH_SUCCESS;
169 }
170 
171 HWTEST_F(DistributedInputInnerTest, PrepareRemoteInput01, testing::ext::TestSize.Level0)
172 {
173     string deviceId = "PrepareRemoteInput01";
174     sptr<TestPrepareDInputCallback> callback(new TestPrepareDInputCallback());
175     int32_t ret = DistributedInputKit::PrepareRemoteInput(deviceId, callback);
176     EXPECT_EQ(DH_SUCCESS, ret);
177 }
178 
179 HWTEST_F(DistributedInputInnerTest, PrepareRemoteInput02, testing::ext::TestSize.Level0)
180 {
181     string deviceId = "";
182     sptr<TestPrepareDInputCallback> callback = nullptr;
183     int32_t ret = DistributedInputKit::PrepareRemoteInput(deviceId, callback);
184     EXPECT_EQ(DH_SUCCESS, ret);
185 }
186 
187 HWTEST_F(DistributedInputInnerTest, PrepareRemoteInput03, testing::ext::TestSize.Level0)
188 {
189     std::string srcId = "PrepareRemoteInput_test";
190     std::string sinkId = "PrepareRemoteInput_test";
191     sptr<IPrepareDInputCallback> callback(new TestPrepareDInputCallback());
192     int32_t ret = DistributedInputKit::PrepareRemoteInput(srcId, sinkId, callback);
193     EXPECT_EQ(DH_SUCCESS, ret);
194 }
195 
196 HWTEST_F(DistributedInputInnerTest, PrepareRemoteInput04, testing::ext::TestSize.Level0)
197 {
198     std::string srcId = "";
199     std::string sinkId = "";
200     sptr<TestPrepareDInputCallback> callback = nullptr;
201     int32_t ret = DistributedInputKit::PrepareRemoteInput(srcId, sinkId, callback);
202     EXPECT_EQ(DH_SUCCESS, ret);
203 }
204 
205 HWTEST_F(DistributedInputInnerTest, UnprepareRemoteInput01, testing::ext::TestSize.Level0)
206 {
207     string deviceId = "UnprepareRemoteInput01";
208     sptr<TestUnprepareDInputCallback> callback(new TestUnprepareDInputCallback());
209     int32_t ret = DistributedInputKit::UnprepareRemoteInput(deviceId, callback);
210     EXPECT_EQ(DH_SUCCESS, ret);
211 }
212 
213 HWTEST_F(DistributedInputInnerTest, UnprepareRemoteInput02, testing::ext::TestSize.Level0)
214 {
215     string deviceId = "";
216     sptr<TestUnprepareDInputCallback> callback = nullptr;
217     int32_t ret = DistributedInputKit::UnprepareRemoteInput(deviceId, callback);
218     EXPECT_EQ(DH_SUCCESS, ret);
219 }
220 
221 HWTEST_F(DistributedInputInnerTest, UnprepareRemoteInput03, testing::ext::TestSize.Level0)
222 {
223     std::string srcId = "PrepareRemoteInput_src";
224     std::string sinkId = "PrepareRemoteInput_sink";
225     sptr<TestUnprepareDInputCallback> callback(new TestUnprepareDInputCallback());
226     int32_t ret = DistributedInputKit::UnprepareRemoteInput(srcId, sinkId, callback);
227     EXPECT_EQ(DH_SUCCESS, ret);
228 }
229 
230 HWTEST_F(DistributedInputInnerTest, UnprepareRemoteInput04, testing::ext::TestSize.Level0)
231 {
232     std::string srcId = "";
233     std::string sinkId = "";
234     sptr<TestUnprepareDInputCallback> callback = nullptr;
235     int32_t ret = DistributedInputKit::UnprepareRemoteInput(srcId, sinkId, callback);
236     EXPECT_EQ(DH_SUCCESS, ret);
237 }
238 
239 HWTEST_F(DistributedInputInnerTest, StartRemoteInput01, testing::ext::TestSize.Level0)
240 {
241     string deviceId = "StartRemoteInput01";
242     sptr<TestStartDInputCallback> callback(new TestStartDInputCallback());
243     int32_t ret =
244         DistributedInputKit::StartRemoteInput(deviceId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
245     EXPECT_EQ(DH_SUCCESS, ret);
246 }
247 
248 HWTEST_F(DistributedInputInnerTest, StartRemoteInput02, testing::ext::TestSize.Level0)
249 {
250     string deviceId = "";
251     sptr<TestStartDInputCallback> callback = nullptr;
252     int32_t ret =
253         DistributedInputKit::StartRemoteInput(deviceId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
254     EXPECT_EQ(DH_SUCCESS, ret);
255 }
256 
257 HWTEST_F(DistributedInputInnerTest, StartRemoteInput03, testing::ext::TestSize.Level0)
258 {
259     std::string sinkId = "StartRemoteInput_sink";
260     std::vector<std::string> dhIds = {"dhIds_test"};
261     sptr<TestStartStopDInputCallback> callback(new TestStartStopDInputCallback());
262     int32_t ret = DistributedInputKit::StartRemoteInput(sinkId, dhIds, callback);
263     EXPECT_EQ(DH_SUCCESS, ret);
264 }
265 
266 HWTEST_F(DistributedInputInnerTest, StartRemoteInput04, testing::ext::TestSize.Level0)
267 {
268     std::string sinkId = "";
269     std::vector<std::string> dhIds;
270     sptr<TestStartStopDInputCallback> callback = nullptr;
271     int32_t ret = DistributedInputKit::StartRemoteInput(sinkId, dhIds, callback);
272     EXPECT_EQ(DH_SUCCESS, ret);
273 }
274 
275 HWTEST_F(DistributedInputInnerTest, StartRemoteInput05, testing::ext::TestSize.Level0)
276 {
277     string srcId = "StartRemoteInput01-src";
278     string sinkId = "StartRemoteInput01-sink";
279     sptr<TestStartDInputCallback> callback(new TestStartDInputCallback());
280     int32_t ret =
281         DistributedInputKit::StartRemoteInput(srcId, sinkId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
282     EXPECT_EQ(DH_SUCCESS, ret);
283 }
284 
285 HWTEST_F(DistributedInputInnerTest, StartRemoteInput06, testing::ext::TestSize.Level0)
286 {
287     string srcId = "";
288     string sinkId = "";
289     sptr<TestStartDInputCallback> callback = nullptr;
290     int32_t ret =
291         DistributedInputKit::StartRemoteInput(srcId, sinkId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
292     EXPECT_EQ(DH_SUCCESS, ret);
293 }
294 
295 HWTEST_F(DistributedInputInnerTest, StartRemoteInput07, testing::ext::TestSize.Level0)
296 {
297     string srcId = "StartRemoteInput01-src";
298     string sinkId = "StartRemoteInput01-sink";
299     std::vector<std::string> dhIds = {"dhIds_test"};
300     sptr<TestStartStopDInputCallback> callback(new TestStartStopDInputCallback());
301     int32_t ret = DistributedInputKit::StartRemoteInput(srcId, sinkId, dhIds, callback);
302     EXPECT_EQ(DH_SUCCESS, ret);
303 }
304 
305 HWTEST_F(DistributedInputInnerTest, StartRemoteInput08, testing::ext::TestSize.Level0)
306 {
307     string srcId = "";
308     string sinkId = "";
309     std::vector<std::string> dhIds;
310     sptr<TestStartStopDInputCallback> callback = nullptr;
311     int32_t ret = DistributedInputKit::StartRemoteInput(srcId, sinkId, dhIds, callback);
312     EXPECT_EQ(DH_SUCCESS, ret);
313 }
314 
315 HWTEST_F(DistributedInputInnerTest, StopRemoteInput01, testing::ext::TestSize.Level0)
316 {
317     string deviceId = "StopRemoteInput01";
318     sptr<TestStopDInputCallback> callback(new TestStopDInputCallback());
319     int32_t ret =
320         DistributedInputKit::StopRemoteInput(deviceId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
321     EXPECT_EQ(DH_SUCCESS, ret);
322 }
323 
324 HWTEST_F(DistributedInputInnerTest, StopRemoteInput02, testing::ext::TestSize.Level0)
325 {
326     string deviceId = "";
327     sptr<TestStopDInputCallback> callback = nullptr;
328     int32_t ret =
329         DistributedInputKit::StopRemoteInput(deviceId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
330     EXPECT_EQ(DH_SUCCESS, ret);
331 }
332 
333 HWTEST_F(DistributedInputInnerTest, StopRemoteInput03, testing::ext::TestSize.Level0)
334 {
335     std::string sinkId = "StartRemoteInput_test";
336     std::vector<std::string> dhIds = {"dhIds_test"};
337     sptr<TestStartStopDInputCallback> callback(new TestStartStopDInputCallback());
338     int32_t ret = DistributedInputKit::StopRemoteInput(sinkId, dhIds, callback);
339     EXPECT_EQ(DH_SUCCESS, ret);
340 }
341 
342 HWTEST_F(DistributedInputInnerTest, StopRemoteInput04, testing::ext::TestSize.Level0)
343 {
344     std::string sinkId = "";
345     std::vector<std::string> dhIds;
346     sptr<TestStartStopDInputCallback> callback = nullptr;
347     int32_t ret = DistributedInputKit::StopRemoteInput(sinkId, dhIds, callback);
348     EXPECT_EQ(DH_SUCCESS, ret);
349 }
350 
351 HWTEST_F(DistributedInputInnerTest, StopRemoteInput05, testing::ext::TestSize.Level0)
352 {
353     string srcId = "StopRemoteInput03-src";
354     string sinkId = "StopRemoteInput03-sink";
355     sptr<TestStopDInputCallback> callback(new TestStopDInputCallback());
356     int32_t ret =
357         DistributedInputKit::StopRemoteInput(srcId, sinkId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
358     EXPECT_EQ(DH_SUCCESS, ret);
359 }
360 
361 HWTEST_F(DistributedInputInnerTest, StopRemoteInput06, testing::ext::TestSize.Level0)
362 {
363     string srcId = "";
364     string sinkId = "";
365     sptr<TestStopDInputCallback> callback = nullptr;
366     int32_t ret =
367         DistributedInputKit::StopRemoteInput(srcId, sinkId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
368     EXPECT_EQ(DH_SUCCESS, ret);
369 }
370 
371 HWTEST_F(DistributedInputInnerTest, StopRemoteInput07, testing::ext::TestSize.Level0)
372 {
373     string srcId = "StartRemoteInput01-src";
374     string sinkId = "StartRemoteInput01-sink";
375     std::vector<std::string> dhIds = {"dhIds_test"};
376     sptr<TestStartStopDInputCallback> callback(new TestStartStopDInputCallback());
377     int32_t ret = DistributedInputKit::StopRemoteInput(srcId, sinkId, dhIds, callback);
378     EXPECT_EQ(DH_SUCCESS, ret);
379 }
380 
381 HWTEST_F(DistributedInputInnerTest, StopRemoteInput08, testing::ext::TestSize.Level0)
382 {
383     string srcId = "";
384     string sinkId = "";
385     std::vector<std::string> dhIds;
386     sptr<TestStartStopDInputCallback> callback = nullptr;
387     int32_t ret = DistributedInputKit::StopRemoteInput(srcId, sinkId, dhIds, callback);
388     EXPECT_EQ(DH_SUCCESS, ret);
389 }
390 
391 HWTEST_F(DistributedInputInnerTest, IsNeedFilterOut01, testing::ext::TestSize.Level0)
392 {
393     string deviceId = "IsNeedFilterOut01";
394     BusinessEvent event;
395     event.pressedKeys.push_back(29);
396     event.pressedKeys.push_back(56);
397     event.keyCode = 111;
398     event.keyAction = 108;
399     bool ret = DistributedInputKit::IsNeedFilterOut(deviceId, event);
400     EXPECT_EQ(true, ret);
401 }
402 
403 HWTEST_F(DistributedInputInnerTest, IsNeedFilterOut02, testing::ext::TestSize.Level0)
404 {
405     string deviceId;
406     BusinessEvent event;
407     bool ret = DistributedInputKit::IsNeedFilterOut(deviceId, event);
408     EXPECT_EQ(true, ret);
409 }
410 
411 /**
412  * @tc.name: IsTouchEventNeedFilterOut01
413  * @tc.desc: verify the function of filtering events on the touchscreen.
414  * @tc.type: FUNC
415  * @tc.require: SR000GNECO
416  */
417 HWTEST_F(DistributedInputInnerTest, IsTouchEventNeedFilterOut01, testing::ext::TestSize.Level0)
418 {
419     std::string sourceWinId = "123";
420     SinkScreenInfo sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(sourceWinId);
421     const TransformInfo trans{10, 10, 100, 100, 1.0, 1.0};
422     sinkScreenInfo.transformInfo = trans;
423     DInputContext::GetInstance().UpdateSinkScreenInfo(sourceWinId, sinkScreenInfo);
424 
425     sourceWinId = "456";
426     sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(sourceWinId);
427     const TransformInfo trans1{120, 130, 50, 50, 1.0, 1.0};
428     sinkScreenInfo.transformInfo = trans1;
429     DInputContext::GetInstance().UpdateSinkScreenInfo(sourceWinId, sinkScreenInfo);
430 
431     TouchScreenEvent event;
432     event.absX = 100;
433     event.absY = 100;
434     bool ret = DistributedInputKit::IsTouchEventNeedFilterOut(event);
435     EXPECT_EQ(true, ret);
436 
437     event.absX = 140;
438     event.absY = 150;
439     ret = DistributedInputKit::IsTouchEventNeedFilterOut(event);
440     EXPECT_EQ(true, ret);
441 
442     event.absX = 150;
443     event.absY = 20;
444     ret = DistributedInputKit::IsTouchEventNeedFilterOut(event);
445     EXPECT_EQ(false, ret);
446 }
447 
448 HWTEST_F(DistributedInputInnerTest, IsStartDistributedInput01, testing::ext::TestSize.Level0)
449 {
450     uint32_t flag = 1;
451     DInputServerType retFlag = DistributedInputKit::IsStartDistributedInput(flag);
452     EXPECT_EQ(DInputServerType::NULL_SERVER_TYPE, retFlag);
453 }
454 
455 HWTEST_F(DistributedInputInnerTest, IsStartDistributedInput02, testing::ext::TestSize.Level0)
456 {
457     std::string dhId = "IsStartDistributedInput02";
458     bool ret = DistributedInputKit::IsStartDistributedInput(dhId);
459     EXPECT_EQ(true, ret);
460 }
461 
462 HWTEST_F(DistributedInputInnerTest, IsStartDistributedInput03, testing::ext::TestSize.Level0)
463 {
464     std::string dhId = "";
465     bool ret = DistributedInputKit::IsStartDistributedInput(dhId);
466     EXPECT_EQ(true, ret);
467 }
468 
469 HWTEST_F(DistributedInputInnerTest, RegisterSimulationEventListener01, testing::ext::TestSize.Level0)
470 {
471     sptr<TestSimulationEventListenerStub> listener(new TestSimulationEventListenerStub());
472     int32_t ret = DistributedInputKit::RegisterSimulationEventListener(listener);
473     EXPECT_EQ(DH_SUCCESS, ret);
474 }
475 
476 HWTEST_F(DistributedInputInnerTest, RegisterSimulationEventListener02, testing::ext::TestSize.Level0)
477 {
478     sptr<TestSimulationEventListenerStub> listener = nullptr;
479     int32_t ret = DistributedInputKit::RegisterSimulationEventListener(listener);
480     EXPECT_EQ(DH_SUCCESS, ret);
481 }
482 
483 HWTEST_F(DistributedInputInnerTest, UnregisterSimulationEventListener01, testing::ext::TestSize.Level0)
484 {
485     sptr<TestSimulationEventListenerStub> listener(new TestSimulationEventListenerStub());
486     int32_t ret = DistributedInputKit::UnregisterSimulationEventListener(listener);
487     EXPECT_EQ(DH_SUCCESS, ret);
488 }
489 
490 HWTEST_F(DistributedInputInnerTest, UnregisterSimulationEventListener02, testing::ext::TestSize.Level0)
491 {
492     sptr<TestSimulationEventListenerStub> listener = nullptr;
493     int32_t ret = DistributedInputKit::UnregisterSimulationEventListener(listener);
494     EXPECT_EQ(DH_SUCCESS, ret);
495 }
496 
497 HWTEST_F(DistributedInputInnerTest, RegisterSessionStateCb01, testing::ext::TestSize.Level0)
498 {
499     sptr<TestRegisterSessionStateCb> listener(new TestRegisterSessionStateCb());
500     int32_t ret = DistributedInputKit::RegisterSessionStateCb(listener);
501     EXPECT_EQ(DH_SUCCESS, ret);
502 }
503 
504 HWTEST_F(DistributedInputInnerTest, UnregisterSessionStateCb01, testing::ext::TestSize.Level0)
505 {
506     int32_t ret = DistributedInputKit::UnregisterSessionStateCb();
507     EXPECT_EQ(DH_SUCCESS, ret);
508 }
509 } // namespace DistributedInput
510 } // namespace DistributedHardware
511 } // namespace OHOS