1 /*
2  * Copyright (c) 2022 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_collector_test.h"
17 #include "event_handler.h"
18 #include "dinput_errcode.h"
19 
20 using namespace testing::ext;
21 using namespace OHOS::DistributedHardware::DistributedInput;
22 using namespace std;
23 namespace OHOS {
24 namespace DistributedHardware {
25 namespace DistributedInput {
SetUp()26 void DistributedInputCollectorTest::SetUp()
27 {
28 }
29 
TearDown()30 void DistributedInputCollectorTest::TearDown()
31 {
32     DistributedInputCollector::GetInstance().StopCollectionThread();
33 }
34 
SetUpTestCase()35 void DistributedInputCollectorTest::SetUpTestCase()
36 {
37 }
38 
TearDownTestCase()39 void DistributedInputCollectorTest::TearDownTestCase()
40 {
41 }
42 
DInputSinkCollectorEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)43 DistributedInputCollectorTest::DInputSinkCollectorEventHandler::DInputSinkCollectorEventHandler(
44     const std::shared_ptr<AppExecFwk::EventRunner> &runner) : AppExecFwk::EventHandler(runner)
45 {
46 }
47 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)48 void DistributedInputCollectorTest::DInputSinkCollectorEventHandler::ProcessEvent(
49     const AppExecFwk::InnerEvent::Pointer &event)
50 {
51     (void)event;
52 }
53 
54 HWTEST_F(DistributedInputCollectorTest, Init01, testing::ext::TestSize.Level1)
55 {
56     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
57     int32_t ret = DistributedInputCollector::GetInstance().StartCollectionThread(eventHandler_);
58     EXPECT_EQ(ERR_DH_INPUT_SERVER_SINK_COLLECTOR_INIT_FAIL, ret);
59 }
60 
61 HWTEST_F(DistributedInputCollectorTest, Init02, testing::ext::TestSize.Level1)
62 {
63     std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
64     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ =
65         std::make_shared<DistributedInputCollectorTest::DInputSinkCollectorEventHandler>(runner);
66 
67     int32_t ret = DistributedInputCollector::GetInstance().StartCollectionThread(eventHandler_);
68     EXPECT_EQ(DH_SUCCESS, ret);
69 }
70 
71 HWTEST_F(DistributedInputCollectorTest, IsAllDevicesStoped01, testing::ext::TestSize.Level1)
72 {
73     uint32_t inputTypes = 1;
74     std::map<int32_t, std::string> deviceInfo;
75     std::vector<std::string> dhIds;
76     std::string mouseNodePath = "mouseNodePath_test";
77     std::string dhid = "dhid_test";
78     DistributedInputCollector::GetInstance().GetDeviceInfoByType(inputTypes, deviceInfo);
79     DistributedInputCollector::GetInstance().GetMouseNodePath(dhIds, mouseNodePath, dhid);
80     bool isStop = DistributedInputCollector::GetInstance().IsAllDevicesStoped();
81     EXPECT_EQ(true, isStop);
82 }
83 
84 HWTEST_F(DistributedInputCollectorTest, IsAllDevicesStoped02, testing::ext::TestSize.Level1)
85 {
86     DistributedInputCollector::GetInstance().inputHub_ = nullptr;
87     uint32_t inputTypes = 1;
88     std::map<int32_t, std::string> deviceInfo;
89     std::vector<std::string> dhIds;
90     std::string mouseNodePath = "mouseNodePath_test";
91     std::string dhid = "dhid_test";
92     DistributedInputCollector::GetInstance().GetDeviceInfoByType(inputTypes, deviceInfo);
93     DistributedInputCollector::GetInstance().GetMouseNodePath(dhIds, mouseNodePath, dhid);
94     bool isStop = DistributedInputCollector::GetInstance().IsAllDevicesStoped();
95     EXPECT_EQ(false, isStop);
96 }
97 
98 HWTEST_F(DistributedInputCollectorTest, SetSharingTypes01, testing::ext::TestSize.Level1)
99 {
100     DistributedInputCollector::GetInstance().inputHub_ = std::make_unique<InputHub>(false);
101     bool enabled = true;
102     uint32_t inputType = static_cast<uint32_t>(DInputDeviceType::ALL);
103 
104     AffectDhIds ret = DistributedInputCollector::GetInstance().SetSharingTypes(enabled, inputType);
105     EXPECT_EQ(0, ret.sharingDhIds.size());
106     std::vector<std::string> sharingDhIds;
107     sharingDhIds.push_back("sharingDhId_test");
108     std::vector<std::string> noSharingDhIds;
109     noSharingDhIds.push_back("noSharingDhId_test");
110     AffectDhIds affectDhIds {sharingDhIds, noSharingDhIds};
111     DistributedInputCollector::GetInstance().ReportDhIdSharingState(affectDhIds);
112     DistributedInputCollector::GetInstance().sharingDhIdListeners_.clear();
113     DistributedInputCollector::GetInstance().ReportDhIdSharingState(affectDhIds);
114 }
115 } // namespace DistributedInput
116 } // namespace DistributedHardware
117 } // namespace OHOS