• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 constexpr int32_t PERMISSION_NUM = 5;
46 constexpr int32_t FIRST_PARAM_INDEX = 0;
47 constexpr int32_t SECOND_PARAM_INDEX = 1;
48 constexpr int32_t THIRD_PARAM_INDEX = 2;
49 constexpr int32_t FOURTH_PARAM_INDEX = 3;
50 constexpr int32_t FIFTH_PARAM_INDEX = 4;
51 } // namespace
52 
53 class InputAdapterTest : public testing::Test {
54 public:
55     void SetUp();
56     void TearDown();
57     static void SetUpTestCase();
58     static void SetPermission();
59     static void RemovePermission();
60 };
61 
SetPermission()62 void InputAdapterTest::SetPermission()
63 {
64     CALL_DEBUG_ENTER;
65     const char** perms = new const char *[PERMISSION_NUM];
66     perms[FIRST_PARAM_INDEX] = "ohos.permission.INPUT_MONITORING";
67     perms[SECOND_PARAM_INDEX] = "ohos.permission.INJECT_INPUT_EVENT";
68     perms[THIRD_PARAM_INDEX] = "ohos.permission.INTERCEPT_INPUT_EVENT";
69     perms[FOURTH_PARAM_INDEX] = "ohos.permission.FILTER_INPUT_EVENT";
70     perms[FIFTH_PARAM_INDEX] = "ohos.permission.MANAGE_MOUSE_CURSOR";
71     NativeTokenInfoParams infoInstance = {
72         .dcapsNum = 0,
73         .permsNum = PERMISSION_NUM,
74         .aclsNum = 0,
75         .dcaps = nullptr,
76         .perms = perms,
77         .acls = nullptr,
78         .processName = "InputAdapterTest",
79         .aplStr = SYSTEM_CORE.c_str(),
80     };
81     g_tokenID = GetAccessTokenId(&infoInstance);
82     EXPECT_EQ(0, SetSelfTokenID(g_tokenID));
83     OHOS::Security::AccessToken::AccessTokenKit::AccessTokenKit::ReloadNativeTokenInfo();
84     delete[] perms;
85 }
86 
RemovePermission()87 void InputAdapterTest::RemovePermission()
88 {
89     CALL_DEBUG_ENTER;
90     int32_t ret = OHOS::Security::AccessToken::AccessTokenKit::DeleteToken(g_tokenID);
91     if (ret != RET_OK) {
92         FI_HILOGE("Failed to remove permission");
93         return;
94     }
95 }
96 
SetUpTestCase()97 void InputAdapterTest::SetUpTestCase() {}
98 
SetUp()99 void InputAdapterTest::SetUp() {}
100 
TearDown()101 void InputAdapterTest::TearDown()
102 {
103     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
104 }
105 
106 /**
107  * @tc.name: TestPointerAddMonitor
108  * @tc.desc: Test AddMonitor
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(InputAdapterTest, TestPointerAddMonitor, TestSize.Level1)
113 {
114     CALL_TEST_DEBUG;
115     SetPermission();
116     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anonb055ed170202(std::shared_ptr<OHOS::MMI::PointerEvent>) 117     auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
118         FI_HILOGI("OnEvent");
119     };
120     int32_t monitorId = inputAdapter->AddMonitor(callback);
121     ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
122     RemovePermission();
123 }
124 
125 /**
126  * @tc.name: TestPointerAddMonitor
127  * @tc.desc: Test AddMonitor
128  * @tc.type: FUNC
129  * @tc.require:
130  */
131 HWTEST_F(InputAdapterTest, TestKeyAddMonitor, TestSize.Level1)
132 {
133     CALL_TEST_DEBUG;
134     SetPermission();
135     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anonb055ed170302(std::shared_ptr<OHOS::MMI::KeyEvent>) 136     auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
137         FI_HILOGI("OnEvent");
138     };
139     int32_t monitorId = inputAdapter->AddMonitor(callback);
140     ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
141     RemovePermission();
142 }
143 
144 /**
145  * @tc.name: TestAddKeyEventInterceptor
146  * @tc.desc: Test AddKeyEventInterceptor
147  * @tc.type: FUNC
148  * @tc.require:
149  */
150 HWTEST_F(InputAdapterTest, AddKeyEventInterceptor, TestSize.Level1)
151 {
152     CALL_TEST_DEBUG;
153     SetPermission();
154     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anonb055ed170402(std::shared_ptr<OHOS::MMI::KeyEvent>) 155     auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
156         FI_HILOGI("OnEvent");
157     };
158     int32_t interceptorId = inputAdapter->AddInterceptor(callback);
159     ASSERT_TRUE(interceptorId > 0);
160     inputAdapter->RemoveInterceptor(interceptorId);
161     RemovePermission();
162 }
163 
164 /**
165  * @tc.name: TestAddPointerEventInterceptor
166  * @tc.desc: Test AddPointerEventInterceptor
167  * @tc.type: FUNC
168  * @tc.require:
169  */
170 HWTEST_F(InputAdapterTest, AddPointerEventInterceptor, TestSize.Level1)
171 {
172     CALL_TEST_DEBUG;
173     SetPermission();
174     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anonb055ed170502(std::shared_ptr<OHOS::MMI::PointerEvent>) 175     auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
176         FI_HILOGI("OnEvent");
177     };
178     int32_t interceptorId = inputAdapter->AddInterceptor(callback);
179     ASSERT_TRUE(interceptorId > 0);
180     inputAdapter->RemoveInterceptor(interceptorId);
181     RemovePermission();
182 }
183 
184 /**
185  * @tc.name: TestAddBothEventInterceptor
186  * @tc.desc: Test AddBothEventInterceptor
187  * @tc.type: FUNC
188  * @tc.require:
189  */
190 HWTEST_F(InputAdapterTest, AddBothEventInterceptor, TestSize.Level1)
191 {
192     CALL_TEST_DEBUG;
193     SetPermission();
194     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anonb055ed170602(std::shared_ptr<OHOS::MMI::PointerEvent>) 195     auto pointerCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
196         FI_HILOGI("OnEvent");
197     };
__anonb055ed170702(std::shared_ptr<OHOS::MMI::KeyEvent>) 198     auto keyCallback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
199         FI_HILOGI("OnEvent");
200     };
201     int32_t interceptorId = inputAdapter->AddInterceptor(pointerCallback, keyCallback);
202     ASSERT_TRUE(interceptorId > 0);
203     inputAdapter->RemoveInterceptor(interceptorId);
204     RemovePermission();
205 }
206 
207 /**
208  * @tc.name: TestAddFilter
209  * @tc.desc: Test AddFilter
210  * @tc.type: FUNC
211  * @tc.require:
212  */
213 HWTEST_F(InputAdapterTest, AddFilter, TestSize.Level1)
214 {
215     CALL_TEST_DEBUG;
216     SetPermission();
217     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anonb055ed170802(std::shared_ptr<OHOS::MMI::PointerEvent>) 218     auto filterCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) -> bool {
219         FI_HILOGI("OnEvent");
220         return true;
221     };
222     int32_t filterId = inputAdapter->AddFilter(filterCallback);
223     ASSERT_FALSE(filterId > 0);
224     inputAdapter->RemoveFilter(filterId);
225     RemovePermission();
226 }
227 
228 /**
229  * @tc.name: TestSetPointerVisibility
230  * @tc.desc: Test SetPointerVisibility
231  * @tc.type: FUNC
232  * @tc.require:
233  */
234 HWTEST_F(InputAdapterTest, TestSetPointerVisibility, TestSize.Level1)
235 {
236     CALL_TEST_DEBUG;
237     SetPermission();
238     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
239     int32_t filterId = inputAdapter->SetPointerVisibility(true);
240     ASSERT_FALSE(filterId > 0);
241     RemovePermission();
242 }
243 
244 /**
245  * @tc.name: TestSetPointerLocation
246  * @tc.desc: Test SetPointerLocation
247  * @tc.type: FUNC
248  * @tc.require:
249  */
250 HWTEST_F(InputAdapterTest, TestSetPointerLocation, TestSize.Level1)
251 {
252     CALL_TEST_DEBUG;
253     SetPermission();
254     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
255     int32_t ret= inputAdapter->SetPointerLocation(0, 0);
256     EXPECT_EQ(RET_OK, ret);
257     RemovePermission();
258 }
259 
260 /**
261  * @tc.name: TestEnableInputDevice
262  * @tc.desc: Test EnableInputDevice
263  * @tc.type: FUNC
264  * @tc.require:
265  */
266 HWTEST_F(InputAdapterTest, TestEnableInputDevice, TestSize.Level1)
267 {
268     CALL_TEST_DEBUG;
269     SetPermission();
270     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
271     int32_t ret = inputAdapter->EnableInputDevice(true);
272     ASSERT_EQ(ret, RET_OK);
273     RemovePermission();
274 }
275 
276 /**
277  * @tc.name: TestSimulateKeyEvent
278  * @tc.desc: Test SimulateKeyEvent
279  * @tc.type: FUNC
280  * @tc.require:
281  */
282 HWTEST_F(InputAdapterTest, TestSimulateKeyEvent, TestSize.Level1)
283 {
284     CALL_TEST_DEBUG;
285     SetPermission();
286     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
287     ASSERT_NO_FATAL_FAILURE(inputAdapter->SimulateInputEvent(MMI::KeyEvent::Create()));
288     RemovePermission();
289 }
290 
291 /**
292  * @tc.name: TestSimulatePointerEvent
293  * @tc.desc: Test SimulatePointerEvent
294  * @tc.type: FUNC
295  * @tc.require:
296  */
297 HWTEST_F(InputAdapterTest, TestSimulatePointerEvent, TestSize.Level1)
298 {
299     CALL_TEST_DEBUG;
300     SetPermission();
301     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
302     ASSERT_NO_FATAL_FAILURE(inputAdapter->SimulateInputEvent(MMI::PointerEvent::Create()));
303     RemovePermission();
304 }
305 
306 /**
307  * @tc.name: TestPointerAddMonitor1
308  * @tc.desc: Test AddMonitor1
309  * @tc.type: FUNC
310  * @tc.require:
311  */
312 HWTEST_F(InputAdapterTest, TestPointerAddMonitor1, TestSize.Level1)
313 {
314     CALL_TEST_DEBUG;
315     SetPermission();
316     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anonb055ed170902(std::shared_ptr<OHOS::MMI::PointerEvent>) 317     auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {};
318     int32_t monitorId = inputAdapter->AddMonitor(callback);
319     ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
320     RemovePermission();
321 }
322 
323 /**
324  * @tc.name: TestPointerAddMonitor1
325  * @tc.desc: Test AddMonitor1
326  * @tc.type: FUNC
327  * @tc.require:
328  */
329 HWTEST_F(InputAdapterTest, TestKeyAddMonitor1, TestSize.Level1)
330 {
331     CALL_TEST_DEBUG;
332     SetPermission();
333     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anonb055ed170a02(std::shared_ptr<OHOS::MMI::KeyEvent>) 334     auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {};
335     int32_t monitorId = inputAdapter->AddMonitor(callback);
336     ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
337     RemovePermission();
338 }
339 
340 /**
341  * @tc.name: TestAddKeyEventInterceptor1
342  * @tc.desc: Test AddKeyEventInterceptor1
343  * @tc.type: FUNC
344  * @tc.require:
345  */
346 HWTEST_F(InputAdapterTest, AddKeyEventInterceptor1, TestSize.Level1)
347 {
348     CALL_TEST_DEBUG;
349     SetPermission();
350     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
351     int32_t interceptorId = inputAdapter->AddInterceptor(nullptr, nullptr);
352     ASSERT_EQ(interceptorId, RET_ERR);
353     inputAdapter->RemoveInterceptor(interceptorId);
354     RemovePermission();
355 }
356 
357 /**
358  * @tc.name: TestAddFilter1
359  * @tc.desc: Test AddFilter1
360  * @tc.type: FUNC
361  * @tc.require:
362  */
363 HWTEST_F(InputAdapterTest, AddFilter1, TestSize.Level1)
364 {
365     CALL_TEST_DEBUG;
366     SetPermission();
367     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anonb055ed170b02(std::shared_ptr<OHOS::MMI::PointerEvent>) 368     auto filterCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) -> bool {
369         FI_HILOGI("OnEvent");
370         return false;
371     };
372     int32_t filterId = inputAdapter->AddFilter(filterCallback);
373     ASSERT_TRUE(filterId > 0);
374     inputAdapter->RemoveFilter(filterId);
375     RemovePermission();
376 }
377 
378 /**
379  * @tc.name: TestOnInputEvent
380  * @tc.desc: Test OnInputEvent
381  * @tc.type: FUNC
382  * @tc.require:
383  */
384 HWTEST_F(InputAdapterTest, TesOnInputEvent, TestSize.Level1)
385 {
386     CALL_TEST_DEBUG;
387     SetPermission();
__anonb055ed170c02(std::shared_ptr<MMI::PointerEvent> pointerEvent) 388     auto pointerCb = [](std::shared_ptr<MMI::PointerEvent> pointerEvent) {
389         pointerEvent =  MMI::PointerEvent::Create();
390         return ;
391     };
__anonb055ed170d02(std::shared_ptr<MMI::KeyEvent>keyEvent) 392     auto keyCb = [](std::shared_ptr<MMI::KeyEvent>keyEvent) {
393         keyEvent = MMI::KeyEvent::Create();
394         return ;
395     };
396     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
397     std::shared_ptr<MMI::PointerEvent> pointerEvent =  MMI::PointerEvent::Create();
398     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
399     InterceptorConsumer interceptorConsumer { pointerCb, keyCb };
400     ASSERT_NO_FATAL_FAILURE(interceptorConsumer.OnInputEvent(pointerEvent));
401     ASSERT_NO_FATAL_FAILURE(interceptorConsumer.OnInputEvent(keyEvent));
402     RemovePermission();
403 }
404 
405 /**
406  * @tc.name: TestOnInputEvent1
407  * @tc.desc: Test OnInputEvent1
408  * @tc.type: FUNC
409  * @tc.require:
410  */
411 HWTEST_F(InputAdapterTest, TestOnInputEvent1, TestSize.Level1)
412 {
413     CALL_TEST_DEBUG;
414     SetPermission();
415     InterceptorConsumer interceptorConsumer1 {
__anonb055ed170e02() 416         [](std::shared_ptr<MMI::PointerEvent> cb) -> void {},
__anonb055ed170f02() 417         [](std::shared_ptr<MMI::KeyEvent> cb) -> void {}
418     };
419     std::shared_ptr<MMI::PointerEvent> pointerEvent =  MMI::PointerEvent::Create();
420     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
421     ASSERT_NO_FATAL_FAILURE(interceptorConsumer1.OnInputEvent(pointerEvent));
422     ASSERT_NO_FATAL_FAILURE(interceptorConsumer1.OnInputEvent(keyEvent));
423     RemovePermission();
424 }
425 } // namespace DeviceStatus
426 } // namespace Msdp
427 } // namespace OHOS
428