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>();
__anona32c6e0f0202(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_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
116 RemovePermission();
117 }
118
119 /**
120 * @tc.name: TestPointerAddMonitor
121 * @tc.desc: Test AddMonitor
122 * @tc.type: FUNC
123 * @tc.require:
124 */
125 HWTEST_F(InputAdapterTest, TestKeyAddMonitor, TestSize.Level1)
126 {
127 CALL_TEST_DEBUG;
128 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
129 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anona32c6e0f0302(std::shared_ptr<OHOS::MMI::KeyEvent>) 130 auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
131 FI_HILOGI("OnEvent");
132 };
133 int32_t monitorId = inputAdapter->AddMonitor(callback);
134 ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
135 RemovePermission();
136 }
137
138 /**
139 * @tc.name: TestAddKeyEventInterceptor
140 * @tc.desc: Test AddKeyEventInterceptor
141 * @tc.type: FUNC
142 * @tc.require:
143 */
144 HWTEST_F(InputAdapterTest, AddKeyEventInterceptor, TestSize.Level1)
145 {
146 CALL_TEST_DEBUG;
147 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
148 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anona32c6e0f0402(std::shared_ptr<OHOS::MMI::KeyEvent>) 149 auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
150 FI_HILOGI("OnEvent");
151 };
152 int32_t interceptorId = inputAdapter->AddInterceptor(callback);
153 ASSERT_FALSE(interceptorId > 0);
154 inputAdapter->RemoveInterceptor(interceptorId);
155 RemovePermission();
156 }
157
158 /**
159 * @tc.name: TestAddPointerEventInterceptor
160 * @tc.desc: Test AddPointerEventInterceptor
161 * @tc.type: FUNC
162 * @tc.require:
163 */
164 HWTEST_F(InputAdapterTest, AddPointerEventInterceptor, TestSize.Level1)
165 {
166 CALL_TEST_DEBUG;
167 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
168 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anona32c6e0f0502(std::shared_ptr<OHOS::MMI::PointerEvent>) 169 auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
170 FI_HILOGI("OnEvent");
171 };
172 int32_t interceptorId = inputAdapter->AddInterceptor(callback);
173 ASSERT_FALSE(interceptorId > 0);
174 inputAdapter->RemoveInterceptor(interceptorId);
175 RemovePermission();
176 }
177
178 /**
179 * @tc.name: TestAddBothEventInterceptor
180 * @tc.desc: Test AddBothEventInterceptor
181 * @tc.type: FUNC
182 * @tc.require:
183 */
184 HWTEST_F(InputAdapterTest, AddBothEventInterceptor, TestSize.Level1)
185 {
186 CALL_TEST_DEBUG;
187 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
188 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anona32c6e0f0602(std::shared_ptr<OHOS::MMI::PointerEvent>) 189 auto pointerCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
190 FI_HILOGI("OnEvent");
191 };
__anona32c6e0f0702(std::shared_ptr<OHOS::MMI::KeyEvent>) 192 auto keyCallback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
193 FI_HILOGI("OnEvent");
194 };
195 int32_t interceptorId = inputAdapter->AddInterceptor(pointerCallback, keyCallback);
196 ASSERT_FALSE(interceptorId > 0);
197 inputAdapter->RemoveInterceptor(interceptorId);
198 RemovePermission();
199 }
200
201 /**
202 * @tc.name: TestAddFilter
203 * @tc.desc: Test AddFilter
204 * @tc.type: FUNC
205 * @tc.require:
206 */
207 HWTEST_F(InputAdapterTest, AddFilter, TestSize.Level1)
208 {
209 CALL_TEST_DEBUG;
210 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
211 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anona32c6e0f0802(std::shared_ptr<OHOS::MMI::PointerEvent>) 212 auto filterCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) -> bool {
213 FI_HILOGI("OnEvent");
214 return true;
215 };
216 int32_t filterId = inputAdapter->AddFilter(filterCallback);
217 ASSERT_FALSE(filterId > 0);
218 inputAdapter->RemoveFilter(filterId);
219 RemovePermission();
220 }
221
222 /**
223 * @tc.name: TestSetPointerVisibility
224 * @tc.desc: Test SetPointerVisibility
225 * @tc.type: FUNC
226 * @tc.require:
227 */
228 HWTEST_F(InputAdapterTest, TestSetPointerVisibility, TestSize.Level1)
229 {
230 CALL_TEST_DEBUG;
231 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
232 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
233 int32_t filterId = inputAdapter->SetPointerVisibility(true);
234 ASSERT_FALSE(filterId > 0);
235 RemovePermission();
236 }
237
238 /**
239 * @tc.name: TestSetPointerLocation
240 * @tc.desc: Test SetPointerLocation
241 * @tc.type: FUNC
242 * @tc.require:
243 */
244 HWTEST_F(InputAdapterTest, TestSetPointerLocation, TestSize.Level1)
245 {
246 CALL_TEST_DEBUG;
247 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
248 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
249 int32_t filterId = inputAdapter->SetPointerLocation(0, 0);
250 ASSERT_FALSE(filterId > 0);
251 RemovePermission();
252 }
253
254 /**
255 * @tc.name: TestEnableInputDevice
256 * @tc.desc: Test EnableInputDevice
257 * @tc.type: FUNC
258 * @tc.require:
259 */
260 HWTEST_F(InputAdapterTest, TestEnableInputDevice, TestSize.Level1)
261 {
262 CALL_TEST_DEBUG;
263 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
264 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
265 int32_t ret = inputAdapter->EnableInputDevice(true);
266 ASSERT_EQ(ret, RET_OK);
267 RemovePermission();
268 }
269
270 /**
271 * @tc.name: TestSimulateKeyEvent
272 * @tc.desc: Test SimulateKeyEvent
273 * @tc.type: FUNC
274 * @tc.require:
275 */
276 HWTEST_F(InputAdapterTest, TestSimulateKeyEvent, TestSize.Level1)
277 {
278 CALL_TEST_DEBUG;
279 SetPermission(SYSTEM_CORE, g_coresInject, sizeof(g_coresInject) / sizeof(g_coresInject[0]));
280 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
281 ASSERT_NO_FATAL_FAILURE(inputAdapter->SimulateInputEvent(MMI::KeyEvent::Create()));
282 RemovePermission();
283 }
284
285 /**
286 * @tc.name: TestSimulatePointerEvent
287 * @tc.desc: Test SimulatePointerEvent
288 * @tc.type: FUNC
289 * @tc.require:
290 */
291 HWTEST_F(InputAdapterTest, TestSimulatePointerEvent, TestSize.Level1)
292 {
293 CALL_TEST_DEBUG;
294 SetPermission(SYSTEM_CORE, g_coresInject, sizeof(g_coresInject) / sizeof(g_coresInject[0]));
295 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
296 ASSERT_NO_FATAL_FAILURE(inputAdapter->SimulateInputEvent(MMI::PointerEvent::Create()));
297 RemovePermission();
298 }
299
300 /**
301 * @tc.name: TestPointerAddMonitor1
302 * @tc.desc: Test AddMonitor1
303 * @tc.type: FUNC
304 * @tc.require:
305 */
306 HWTEST_F(InputAdapterTest, TestPointerAddMonitor1, TestSize.Level1)
307 {
308 CALL_TEST_DEBUG;
309 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
310 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anona32c6e0f0902(std::shared_ptr<OHOS::MMI::PointerEvent>) 311 auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {};
312 int32_t monitorId = inputAdapter->AddMonitor(callback);
313 ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
314 RemovePermission();
315 }
316
317 /**
318 * @tc.name: TestPointerAddMonitor1
319 * @tc.desc: Test AddMonitor1
320 * @tc.type: FUNC
321 * @tc.require:
322 */
323 HWTEST_F(InputAdapterTest, TestKeyAddMonitor1, TestSize.Level1)
324 {
325 CALL_TEST_DEBUG;
326 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
327 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anona32c6e0f0a02(std::shared_ptr<OHOS::MMI::KeyEvent>) 328 auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {};
329 int32_t monitorId = inputAdapter->AddMonitor(callback);
330 ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
331 RemovePermission();
332 }
333
334 /**
335 * @tc.name: TestAddKeyEventInterceptor1
336 * @tc.desc: Test AddKeyEventInterceptor1
337 * @tc.type: FUNC
338 * @tc.require:
339 */
340 HWTEST_F(InputAdapterTest, AddKeyEventInterceptor1, TestSize.Level1)
341 {
342 CALL_TEST_DEBUG;
343 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
344 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
345 int32_t interceptorId = inputAdapter->AddInterceptor(nullptr, nullptr);
346 ASSERT_EQ(interceptorId, RET_ERR);
347 inputAdapter->RemoveInterceptor(interceptorId);
348 RemovePermission();
349 }
350
351 /**
352 * @tc.name: TestAddFilter1
353 * @tc.desc: Test AddFilter1
354 * @tc.type: FUNC
355 * @tc.require:
356 */
357 HWTEST_F(InputAdapterTest, AddFilter1, TestSize.Level1)
358 {
359 CALL_TEST_DEBUG;
360 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
361 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anona32c6e0f0b02(std::shared_ptr<OHOS::MMI::PointerEvent>) 362 auto filterCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) -> bool {
363 FI_HILOGI("OnEvent");
364 return false;
365 };
366 int32_t filterId = inputAdapter->AddFilter(filterCallback);
367 ASSERT_TRUE(filterId < 0);
368 inputAdapter->RemoveFilter(filterId);
369 RemovePermission();
370 }
371
372 /**
373 * @tc.name: TestOnInputEvent
374 * @tc.desc: Test OnInputEvent
375 * @tc.type: FUNC
376 * @tc.require:
377 */
378 HWTEST_F(InputAdapterTest, TesOnInputEvent, TestSize.Level1)
379 {
380 CALL_TEST_DEBUG;
381 SetPermission(SYSTEM_CORE, g_coresInject, sizeof(g_coresInject) / sizeof(g_coresInject[0]));
__anona32c6e0f0c02(std::shared_ptr<MMI::PointerEvent> pointerEvent) 382 auto pointerCb = [](std::shared_ptr<MMI::PointerEvent> pointerEvent) {
383 pointerEvent = MMI::PointerEvent::Create();
384 return ;
385 };
__anona32c6e0f0d02(std::shared_ptr<MMI::KeyEvent>keyEvent) 386 auto keyCb = [](std::shared_ptr<MMI::KeyEvent>keyEvent) {
387 keyEvent = MMI::KeyEvent::Create();
388 return ;
389 };
390 std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
391 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
392 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
393 InterceptorConsumer interceptorConsumer { pointerCb, keyCb };
394 ASSERT_NO_FATAL_FAILURE(interceptorConsumer.OnInputEvent(pointerEvent));
395 ASSERT_NO_FATAL_FAILURE(interceptorConsumer.OnInputEvent(keyEvent));
396 RemovePermission();
397 }
398
399 /**
400 * @tc.name: TestOnInputEvent1
401 * @tc.desc: Test OnInputEvent1
402 * @tc.type: FUNC
403 * @tc.require:
404 */
405 HWTEST_F(InputAdapterTest, TestOnInputEvent1, TestSize.Level1)
406 {
407 CALL_TEST_DEBUG;
408 SetPermission(SYSTEM_CORE, g_coresInject, sizeof(g_coresInject) / sizeof(g_coresInject[0]));
409 InterceptorConsumer interceptorConsumer1 {
__anona32c6e0f0e02() 410 [](std::shared_ptr<MMI::PointerEvent> cb) -> void {},
__anona32c6e0f0f02() 411 [](std::shared_ptr<MMI::KeyEvent> cb) -> void {}
412 };
413 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
414 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
415 ASSERT_NO_FATAL_FAILURE(interceptorConsumer1.OnInputEvent(pointerEvent));
416 ASSERT_NO_FATAL_FAILURE(interceptorConsumer1.OnInputEvent(keyEvent));
417 RemovePermission();
418 }
419 } // namespace DeviceStatus
420 } // namespace Msdp
421 } // namespace OHOS
422