1 /*
2 * Copyright (c) 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include <fstream>
20
21 #include "input_device_manager.h"
22 #include "libinput.h"
23
24 struct udev_device {
25 uint32_t tags;
26 };
27
28 struct libinput_device {
29 struct udev_device udevDev;
30 unsigned int busType;
31 unsigned int version;
32 unsigned int product;
33 unsigned int vendor;
34 char name[9];
35 };
36
37 extern "C" {
libinput_device_get_name(struct libinput_device * device)38 const char *libinput_device_get_name(struct libinput_device *device)
39 {
40 const char* pName = device->name;
41 return pName;
42 }
43 }
44
45 namespace OHOS {
46 namespace MMI {
47 namespace {
48 using namespace testing::ext;
49 using namespace testing;
50 } // namespace
51
52 class InputDeviceManagerTest : public testing::Test {
53 public:
SetUpTestCase(void)54 static void SetUpTestCase(void) {}
TearDownTestCase(void)55 static void TearDownTestCase(void) {}
56 };
57
58 class MockIDeviceObserver : public IDeviceObserver {
59 public:
60 MOCK_METHOD1(OnDeviceAdded, void(int32_t deviceId));
61 MOCK_METHOD1(OnDeviceRemoved, void(int32_t deviceId));
62 MOCK_METHOD3(UpdatePointerDevice, void(bool, bool, bool));
63 };
64
65 /**
66 * @tc.name: NotifyDevCallback_Test_001
67 * @tc.desc: Test the function NotifyDevCallback
68 * @tc.type: FUNC
69 * @tc.require:
70 */
71 HWTEST_F(InputDeviceManagerTest, NotifyDevCallback_Test_001, TestSize.Level1)
72 {
73 CALL_TEST_DEBUG;
74 InputDeviceManager inputDevice;
75 int32_t deviceId = 1;
76 InputDeviceManager::InputDeviceInfo inDevice;
77 inDevice.inputDeviceOrigin = nullptr;
78 inDevice.isTouchableDevice = true;
79 ASSERT_NO_FATAL_FAILURE(inputDevice.NotifyDevCallback(deviceId, inDevice));
80 }
81
82 /**
83 * @tc.name: NotifyDevCallback_Test_002
84 * @tc.desc: Test the function NotifyDevCallback
85 * @tc.type: FUNC
86 * @tc.require:
87 */
88 HWTEST_F(InputDeviceManagerTest, NotifyDevCallback_Test_002, TestSize.Level1)
89 {
90 CALL_TEST_DEBUG;
91 InputDeviceManager inputDevice;
92 int32_t deviceId = 1;
93 InputDeviceManager::InputDeviceInfo inDevice;
94 struct libinput_device libDev {
95 .udevDev { 2 },
96 .busType = 1,
97 .version = 1,
98 .product = 1,
99 .vendor = 1,
100 .name = "test",
101 };
102 inDevice.inputDeviceOrigin = &libDev;
103 inDevice.isTouchableDevice = true;
104 ASSERT_NO_FATAL_FAILURE(inputDevice.NotifyDevCallback(deviceId, inDevice));
105 }
106
107 /**
108 * @tc.name: NotifyDevRemoveCallback_Test_001
109 * @tc.desc: Test the function NotifyDevCallback
110 * @tc.type: FUNC
111 * @tc.require:
112 */
113 HWTEST_F(InputDeviceManagerTest, NotifyDevRemoveCallback_Test_001, TestSize.Level1)
114 {
115 CALL_TEST_DEBUG;
116 InputDeviceManager inputDevice;
117 using InputDeviceCallback = std::function<void(int, std::string, std::string, std::string)>;
118 InputDeviceCallback callback =
__anoned4722d40202(int status, std::string nodeName, const std::string& deviceName, const std::string& deviceId) 119 [] (int status, std::string nodeName, const std::string& deviceName, const std::string& deviceId) {};
120 inputDevice.SetInputStatusChangeCallback(callback);
121
122 int32_t deviceId = 1;
123 InputDeviceManager::InputDeviceInfo inDevice;
124 inDevice.inputDeviceOrigin = nullptr;
125 inDevice.sysUid = "test";
126 ASSERT_NO_FATAL_FAILURE(inputDevice.NotifyDevRemoveCallback(deviceId, inDevice));
127 }
128
129 /**
130 * @tc.name: NotifyDevRemoveCallback_Test_002
131 * @tc.desc: Test the function NotifyDevCallback
132 * @tc.type: FUNC
133 * @tc.require:
134 */
135 HWTEST_F(InputDeviceManagerTest, NotifyDevRemoveCallback_Test_002, TestSize.Level1)
136 {
137 CALL_TEST_DEBUG;
138 InputDeviceManager inputDevice;
139 using InputDeviceCallback = std::function<void(int, std::string, std::string, std::string)>;
140 InputDeviceCallback callback =
__anoned4722d40302(int status, std::string nodeName, const std::string& deviceName, const std::string& deviceId) 141 [] (int status, std::string nodeName, const std::string& deviceName, const std::string& deviceId) {};
142 inputDevice.SetInputStatusChangeCallback(callback);
143
144 int32_t deviceId = 1;
145 InputDeviceManager::InputDeviceInfo inDevice;
146 struct libinput_device libDev {
147 .udevDev { 2 },
148 .busType = 1,
149 .version = 1,
150 .product = 1,
151 .vendor = 1,
152 .name = "test",
153 };
154 inDevice.inputDeviceOrigin = &libDev;
155 inDevice.sysUid = "test";
156 ASSERT_NO_FATAL_FAILURE(inputDevice.NotifyDevRemoveCallback(deviceId, inDevice));
157 }
158
159 /**
160 * @tc.name: NotifyDeviceAdded_Test_001
161 * @tc.desc: Test the function NotifyDeviceAdded
162 * @tc.type: FUNC
163 * @tc.require:
164 */
165 HWTEST_F(InputDeviceManagerTest, NotifyDeviceAdded_Test_001, TestSize.Level1)
166 {
167 CALL_TEST_DEBUG;
168 InputDeviceManager inputDevice;
169 auto observer = std::make_shared<MockIDeviceObserver>();
170 inputDevice.Attach(observer);
171 std::shared_ptr<IDeviceObserver> observerNull = nullptr;
172 inputDevice.Attach(observerNull);
173
174 int32_t deviceId = 600;
175 EXPECT_CALL(*observer, OnDeviceAdded(_)).Times(1);
176 ASSERT_NO_FATAL_FAILURE(inputDevice.NotifyDeviceAdded(deviceId));
177
178 EXPECT_CALL(*observer, OnDeviceRemoved(_)).Times(1);
179 ASSERT_NO_FATAL_FAILURE(inputDevice.NotifyDeviceRemoved(deviceId));
180 }
181 } // namespace MMI
182 } // namespace OHOS