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