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 <memory>
17 #include <utility>
18 #include <vector>
19
20 #include <unistd.h>
21
22 #include "accesstoken_kit.h"
23 #include <gtest/gtest.h>
24 #include "input_device.h"
25 #include "pointer_event.h"
26 #include "securec.h"
27
28 #include "devicestatus_define.h"
29 #include "devicestatus_errors.h"
30 #include "input_adapter.h"
31 #include "nativetoken_kit.h"
32 #include "token_setproc.h"
33
34 #undef LOG_TAG
35 #define LOG_TAG "InputAdapterTest"
36
37 namespace OHOS {
38 namespace Msdp {
39 namespace DeviceStatus {
40 using namespace testing::ext;
41 namespace {
42 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
43 const std::string SYSTEM_CORE { "system_core" };
44 uint64_t g_tokenID { 0 };
45 const char* g_cores[] = { "ohos.permission.INPUT_MONITORING" };
46 const char* g_coresInject[] = { "ohos.permission.INJECT_INPUT_EVENT" };
47 } // namespace
48
49 class InputAdapterTest : public testing::Test {
50 public:
51 void SetUp();
52 void TearDown();
53 static void SetUpTestCase();
54 static void SetPermission(const std::string &level, const char** perms, size_t permAmount);
55 static void RemovePermission();
56 };
57
SetPermission(const std::string & level,const char ** perms,size_t permAmount)58 void InputAdapterTest::SetPermission(const std::string &level, const char** perms, size_t permAmount)
59 {
60 CALL_DEBUG_ENTER;
61 if (perms == nullptr || permAmount == 0) {
62 FI_HILOGE("The perms is empty");
63 return;
64 }
65
66 NativeTokenInfoParams infoInstance = {
67 .dcapsNum = 0,
68 .permsNum = permAmount,
69 .aclsNum = 0,
70 .dcaps = nullptr,
71 .perms = perms,
72 .acls = nullptr,
73 .processName = "InputAdapterTest",
74 .aplStr = level.c_str(),
75 };
76 g_tokenID = GetAccessTokenId(&infoInstance);
77 SetSelfTokenID(g_tokenID);
78 OHOS::Security::AccessToken::AccessTokenKit::AccessTokenKit::ReloadNativeTokenInfo();
79 }
80
RemovePermission()81 void InputAdapterTest::RemovePermission()
82 {
83 CALL_DEBUG_ENTER;
84 int32_t ret = OHOS::Security::AccessToken::AccessTokenKit::DeleteToken(g_tokenID);
85 if (ret != RET_OK) {
86 FI_HILOGE("Failed to remove permission");
87 return;
88 }
89 }
90
SetUpTestCase()91 void InputAdapterTest::SetUpTestCase() {}
92
SetUp()93 void InputAdapterTest::SetUp() {}
94
TearDown()95 void InputAdapterTest::TearDown()
96 {
97 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
98 }
99
100 /**
101 * @tc.name: TestPointerAddMonitor
102 * @tc.desc: Test AddMonitor
103 * @tc.type: FUNC
104 * @tc.require:
105 */
106 HWTEST_F(InputAdapterTest, TestPointerAddMonitor, TestSize.Level1)
107 {
108 CALL_TEST_DEBUG;
109 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
110 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon65db283c0202(std::shared_ptr<OHOS::MMI::PointerEvent>) 111 auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
112 FI_HILOGI("OnEvent");
113 };
114 int32_t monitorId = inputAdapter->AddMonitor(callback);
115 ASSERT_FALSE(monitorId > 0);
116 inputAdapter->RemoveMonitor(monitorId);
117 RemovePermission();
118 }
119
120 /**
121 * @tc.name: TestPointerAddMonitor
122 * @tc.desc: Test AddMonitor
123 * @tc.type: FUNC
124 * @tc.require:
125 */
126 HWTEST_F(InputAdapterTest, TestKeyAddMonitor, TestSize.Level1)
127 {
128 CALL_TEST_DEBUG;
129 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
130 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon65db283c0302(std::shared_ptr<OHOS::MMI::KeyEvent>) 131 auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
132 FI_HILOGI("OnEvent");
133 };
134 int32_t monitorId = inputAdapter->AddMonitor(callback);
135 ASSERT_FALSE(monitorId > 0);
136 inputAdapter->RemoveMonitor(monitorId);
137 RemovePermission();
138 }
139
140 /**
141 * @tc.name: TestAddKeyEventInterceptor
142 * @tc.desc: Test AddKeyEventInterceptor
143 * @tc.type: FUNC
144 * @tc.require:
145 */
146 HWTEST_F(InputAdapterTest, AddKeyEventInterceptor, TestSize.Level1)
147 {
148 CALL_TEST_DEBUG;
149 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
150 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon65db283c0402(std::shared_ptr<OHOS::MMI::KeyEvent>) 151 auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
152 FI_HILOGI("OnEvent");
153 };
154 int32_t interceptorId = inputAdapter->AddInterceptor(callback);
155 ASSERT_FALSE(interceptorId > 0);
156 inputAdapter->RemoveInterceptor(interceptorId);
157 RemovePermission();
158 }
159
160 /**
161 * @tc.name: TestAddPointerEventInterceptor
162 * @tc.desc: Test AddPointerEventInterceptor
163 * @tc.type: FUNC
164 * @tc.require:
165 */
166 HWTEST_F(InputAdapterTest, AddPointerEventInterceptor, TestSize.Level1)
167 {
168 CALL_TEST_DEBUG;
169 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
170 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon65db283c0502(std::shared_ptr<OHOS::MMI::PointerEvent>) 171 auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
172 FI_HILOGI("OnEvent");
173 };
174 int32_t interceptorId = inputAdapter->AddInterceptor(callback);
175 ASSERT_FALSE(interceptorId > 0);
176 inputAdapter->RemoveInterceptor(interceptorId);
177 RemovePermission();
178 }
179
180 /**
181 * @tc.name: TestAddBothEventInterceptor
182 * @tc.desc: Test AddBothEventInterceptor
183 * @tc.type: FUNC
184 * @tc.require:
185 */
186 HWTEST_F(InputAdapterTest, AddBothEventInterceptor, TestSize.Level1)
187 {
188 CALL_TEST_DEBUG;
189 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
190 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon65db283c0602(std::shared_ptr<OHOS::MMI::PointerEvent>) 191 auto pointerCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
192 FI_HILOGI("OnEvent");
193 };
__anon65db283c0702(std::shared_ptr<OHOS::MMI::KeyEvent>) 194 auto keyCallback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
195 FI_HILOGI("OnEvent");
196 };
197 int32_t interceptorId = inputAdapter->AddInterceptor(pointerCallback, keyCallback);
198 ASSERT_FALSE(interceptorId > 0);
199 inputAdapter->RemoveInterceptor(interceptorId);
200 RemovePermission();
201 }
202
203 /**
204 * @tc.name: TestAddFilter
205 * @tc.desc: Test AddFilter
206 * @tc.type: FUNC
207 * @tc.require:
208 */
209 HWTEST_F(InputAdapterTest, AddFilter, TestSize.Level1)
210 {
211 CALL_TEST_DEBUG;
212 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
213 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon65db283c0802(std::shared_ptr<OHOS::MMI::PointerEvent>) 214 auto filterCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) -> bool {
215 FI_HILOGI("OnEvent");
216 return true;
217 };
218 int32_t filterId = inputAdapter->AddFilter(filterCallback);
219 ASSERT_FALSE(filterId > 0);
220 inputAdapter->RemoveFilter(filterId);
221 RemovePermission();
222 }
223
224 /**
225 * @tc.name: TestSetPointerVisibility
226 * @tc.desc: Test SetPointerVisibility
227 * @tc.type: FUNC
228 * @tc.require:
229 */
230 HWTEST_F(InputAdapterTest, TestSetPointerVisibility, TestSize.Level1)
231 {
232 CALL_TEST_DEBUG;
233 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
234 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
235 int32_t filterId = inputAdapter->SetPointerVisibility(true);
236 ASSERT_FALSE(filterId > 0);
237 RemovePermission();
238 }
239
240 /**
241 * @tc.name: TestSetPointerLocation
242 * @tc.desc: Test SetPointerLocation
243 * @tc.type: FUNC
244 * @tc.require:
245 */
246 HWTEST_F(InputAdapterTest, TestSetPointerLocation, TestSize.Level1)
247 {
248 CALL_TEST_DEBUG;
249 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
250 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
251 int32_t filterId = inputAdapter->SetPointerLocation(0, 0);
252 ASSERT_FALSE(filterId > 0);
253 RemovePermission();
254 }
255
256 /**
257 * @tc.name: TestEnableInputDevice
258 * @tc.desc: Test EnableInputDevice
259 * @tc.type: FUNC
260 * @tc.require:
261 */
262 HWTEST_F(InputAdapterTest, TestEnableInputDevice, TestSize.Level1)
263 {
264 CALL_TEST_DEBUG;
265 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
266 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
267 int32_t ret = inputAdapter->EnableInputDevice(true);
268 ASSERT_EQ(ret, RET_OK);
269 RemovePermission();
270 }
271
272 /**
273 * @tc.name: TestSimulateKeyEvent
274 * @tc.desc: Test SimulateKeyEvent
275 * @tc.type: FUNC
276 * @tc.require:
277 */
278 HWTEST_F(InputAdapterTest, TestSimulateKeyEvent, TestSize.Level1)
279 {
280 CALL_TEST_DEBUG;
281 SetPermission(SYSTEM_CORE, g_coresInject, sizeof(g_coresInject) / sizeof(g_coresInject[0]));
282 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
283 ASSERT_NO_FATAL_FAILURE(inputAdapter->SimulateInputEvent(MMI::KeyEvent::Create()));
284 RemovePermission();
285 }
286
287 /**
288 * @tc.name: TestSimulatePointerEvent
289 * @tc.desc: Test SimulatePointerEvent
290 * @tc.type: FUNC
291 * @tc.require:
292 */
293 HWTEST_F(InputAdapterTest, TestSimulatePointerEvent, TestSize.Level1)
294 {
295 CALL_TEST_DEBUG;
296 SetPermission(SYSTEM_CORE, g_coresInject, sizeof(g_coresInject) / sizeof(g_coresInject[0]));
297 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
298 ASSERT_NO_FATAL_FAILURE(inputAdapter->SimulateInputEvent(MMI::PointerEvent::Create()));
299 RemovePermission();
300 }
301
302 } // namespace DeviceStatus
303 } // namespace Msdp
304 } // namespace OHOS
305