• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "input_adapter_test.h"
32 #include "nativetoken_kit.h"
33 #include "token_setproc.h"
34 
35 #undef LOG_TAG
36 #define LOG_TAG "InputAdapterTest"
37 
38 namespace OHOS {
39 namespace Msdp {
40 namespace DeviceStatus {
41 using namespace testing::ext;
42 namespace {
43 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
44 std::mutex g_lockSetToken;
45 uint64_t g_shellTokenId = 0;
46 uint64_t g_selfTokenId = 0;
47 static MockNativeToken* g_mock = nullptr;
48 } // namespace
49 
SetTestEvironment(uint64_t shellTokenId)50 void SetTestEvironment(uint64_t shellTokenId)
51 {
52     std::lock_guard<std::mutex> lock(g_lockSetToken);
53     g_shellTokenId = shellTokenId;
54 }
55 
ResetTestEvironment()56 void ResetTestEvironment()
57 {
58     std::lock_guard<std::mutex> lock(g_lockSetToken);
59     g_shellTokenId = 0;
60 }
61 
GetShellTokenId()62 uint64_t GetShellTokenId()
63 {
64     std::lock_guard<std::mutex> lock(g_lockSetToken);
65     return g_shellTokenId;
66 }
67 
GetNativeTokenIdFromProcess(const std::string & process)68 uint64_t GetNativeTokenIdFromProcess(const std::string &process)
69 {
70     uint64_t selfTokenId = GetSelfTokenID();
71     EXPECT_EQ(0, SetSelfTokenID(GetShellTokenId()));
72 
73     std::string dumpInfo;
74     Security::AccessToken::AtmToolsParamInfo info;
75     info.processName = process;
76     Security::AccessToken::AccessTokenKit::DumpTokenInfo(info, dumpInfo);
77     size_t pos = dumpInfo.find("\"tokenID\": ");
78     if (pos == std::string::npos) {
79         FI_HILOGE("tokenid not find");
80         return 0;
81     }
82     pos += std::string("\"tokenID\": ").length();
83     std::string numStr;
84     while (pos < dumpInfo.length() && std::isdigit(dumpInfo[pos])) {
85         numStr += dumpInfo[pos];
86         ++pos;
87     }
88     EXPECT_EQ(0, SetSelfTokenID(selfTokenId));
89 
90     std::istringstream iss(numStr);
91     Security::AccessToken::AccessTokenID tokenID;
92     iss >> tokenID;
93     return tokenID;
94 }
95 
MockNativeToken(const std::string & process)96 MockNativeToken::MockNativeToken(const std::string& process)
97 {
98     selfToken_ = GetSelfTokenID();
99     uint32_t tokenId = GetNativeTokenIdFromProcess(process);
100     FI_HILOGI("selfToken_:%{public}" PRId64 ", tokenId:%{public}u", selfToken_, tokenId);
101     SetSelfTokenID(tokenId);
102 }
103 
~MockNativeToken()104 MockNativeToken::~MockNativeToken()
105 {
106     SetSelfTokenID(selfToken_);
107 }
108 
SetUpTestCase()109 void InputAdapterTest::SetUpTestCase()
110 {
111     g_selfTokenId = GetSelfTokenID();
112     FI_HILOGI("g_selfTokenId:%{public}" PRId64, g_selfTokenId);
113     SetTestEvironment(g_selfTokenId);
114     g_mock = new (std::nothrow) MockNativeToken("msdp_sa");
115 }
116 
TearDownTestCase()117 void InputAdapterTest::TearDownTestCase()
118 {
119     if (g_mock != nullptr) {
120         delete g_mock;
121         g_mock = nullptr;
122     }
123     SetSelfTokenID(g_selfTokenId);
124     ResetTestEvironment();
125 }
126 
SetUp()127 void InputAdapterTest::SetUp() {}
128 
TearDown()129 void InputAdapterTest::TearDown()
130 {
131     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
132 }
133 
134 /**
135  * @tc.name: TestPointerAddMonitor
136  * @tc.desc: Test AddMonitor
137  * @tc.type: FUNC
138  * @tc.require:
139  */
140 HWTEST_F(InputAdapterTest, TestPointerAddMonitor, TestSize.Level1)
141 {
142     CALL_TEST_DEBUG;
143     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb90202(std::shared_ptr<OHOS::MMI::PointerEvent>) 144     auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
145         FI_HILOGI("OnEvent");
146     };
147     int32_t monitorId = inputAdapter->AddMonitor(callback);
148     ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
149 }
150 
151 /**
152  * @tc.name: TestPointerAddMonitor
153  * @tc.desc: Test AddMonitor
154  * @tc.type: FUNC
155  * @tc.require:
156  */
157 HWTEST_F(InputAdapterTest, TestKeyAddMonitor, TestSize.Level1)
158 {
159     CALL_TEST_DEBUG;
160     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb90302(std::shared_ptr<OHOS::MMI::KeyEvent>) 161     auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
162         FI_HILOGI("OnEvent");
163     };
164     int32_t monitorId = inputAdapter->AddMonitor(callback);
165     ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
166 }
167 
168 /**
169  * @tc.name: TestAddKeyEventInterceptor
170  * @tc.desc: Test AddKeyEventInterceptor
171  * @tc.type: FUNC
172  * @tc.require:
173  */
174 HWTEST_F(InputAdapterTest, AddKeyEventInterceptor, TestSize.Level1)
175 {
176     CALL_TEST_DEBUG;
177     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb90402(std::shared_ptr<OHOS::MMI::KeyEvent>) 178     auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
179         FI_HILOGI("OnEvent");
180     };
181     int32_t interceptorId = inputAdapter->AddInterceptor(callback);
182     ASSERT_TRUE(interceptorId > 0);
183     inputAdapter->RemoveInterceptor(interceptorId);
184 }
185 
186 /**
187  * @tc.name: TestAddPointerEventInterceptor
188  * @tc.desc: Test AddPointerEventInterceptor
189  * @tc.type: FUNC
190  * @tc.require:
191  */
192 HWTEST_F(InputAdapterTest, AddPointerEventInterceptor, TestSize.Level1)
193 {
194     CALL_TEST_DEBUG;
195     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb90502(std::shared_ptr<OHOS::MMI::PointerEvent>) 196     auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
197         FI_HILOGI("OnEvent");
198     };
199     int32_t interceptorId = inputAdapter->AddInterceptor(callback);
200     ASSERT_TRUE(interceptorId > 0);
201     inputAdapter->RemoveInterceptor(interceptorId);
202 }
203 
204 /**
205  * @tc.name: TestAddBothEventInterceptor
206  * @tc.desc: Test AddBothEventInterceptor
207  * @tc.type: FUNC
208  * @tc.require:
209  */
210 HWTEST_F(InputAdapterTest, AddBothEventInterceptor, TestSize.Level1)
211 {
212     CALL_TEST_DEBUG;
213     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb90602(std::shared_ptr<OHOS::MMI::PointerEvent>) 214     auto pointerCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {
215         FI_HILOGI("OnEvent");
216     };
__anon04b7ceb90702(std::shared_ptr<OHOS::MMI::KeyEvent>) 217     auto keyCallback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {
218         FI_HILOGI("OnEvent");
219     };
220     int32_t interceptorId = inputAdapter->AddInterceptor(pointerCallback, keyCallback);
221     ASSERT_TRUE(interceptorId > 0);
222     inputAdapter->RemoveInterceptor(interceptorId);
223 }
224 
225 /**
226  * @tc.name: TestAddFilter
227  * @tc.desc: Test AddFilter
228  * @tc.type: FUNC
229  * @tc.require:
230  */
231 HWTEST_F(InputAdapterTest, AddFilter, TestSize.Level1)
232 {
233     CALL_TEST_DEBUG;
234     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb90802(std::shared_ptr<OHOS::MMI::PointerEvent>) 235     auto filterCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) -> bool {
236         FI_HILOGI("OnEvent");
237         return true;
238     };
239     int32_t filterId = inputAdapter->AddFilter(filterCallback);
240     ASSERT_FALSE(filterId > 0);
241     inputAdapter->RemoveFilter(filterId);
242 }
243 
244 /**
245  * @tc.name: TestSetPointerVisibility
246  * @tc.desc: Test SetPointerVisibility
247  * @tc.type: FUNC
248  * @tc.require:
249  */
250 HWTEST_F(InputAdapterTest, TestSetPointerVisibility, TestSize.Level1)
251 {
252     CALL_TEST_DEBUG;
253     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
254     int32_t filterId = inputAdapter->SetPointerVisibility(true);
255     ASSERT_FALSE(filterId > 0);
256 }
257 
258 /**
259  * @tc.name: TestSetPointerLocation
260  * @tc.desc: Test SetPointerLocation
261  * @tc.type: FUNC
262  * @tc.require:
263  */
264 HWTEST_F(InputAdapterTest, TestSetPointerLocation, TestSize.Level1)
265 {
266     CALL_TEST_DEBUG;
267     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
268     int32_t ret= inputAdapter->SetPointerLocation(0, 0);
269     EXPECT_EQ(RET_OK, ret);
270 }
271 
272 /**
273  * @tc.name: TestEnableInputDevice
274  * @tc.desc: Test EnableInputDevice
275  * @tc.type: FUNC
276  * @tc.require:
277  */
278 HWTEST_F(InputAdapterTest, TestEnableInputDevice, TestSize.Level1)
279 {
280     CALL_TEST_DEBUG;
281     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
282     int32_t ret = inputAdapter->EnableInputDevice(true);
283     ASSERT_EQ(ret, RET_OK);
284 }
285 
286 /**
287  * @tc.name: TestSimulateKeyEvent
288  * @tc.desc: Test SimulateKeyEvent
289  * @tc.type: FUNC
290  * @tc.require:
291  */
292 HWTEST_F(InputAdapterTest, TestSimulateKeyEvent, TestSize.Level1)
293 {
294     CALL_TEST_DEBUG;
295     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
296     ASSERT_NO_FATAL_FAILURE(inputAdapter->SimulateInputEvent(MMI::KeyEvent::Create()));
297 }
298 
299 /**
300  * @tc.name: TestSimulatePointerEvent
301  * @tc.desc: Test SimulatePointerEvent
302  * @tc.type: FUNC
303  * @tc.require:
304  */
305 HWTEST_F(InputAdapterTest, TestSimulatePointerEvent, TestSize.Level1)
306 {
307     CALL_TEST_DEBUG;
308     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
309     ASSERT_NO_FATAL_FAILURE(inputAdapter->SimulateInputEvent(MMI::PointerEvent::Create()));
310 }
311 
312 /**
313  * @tc.name: TestPointerAddMonitor1
314  * @tc.desc: Test AddMonitor1
315  * @tc.type: FUNC
316  * @tc.require:
317  */
318 HWTEST_F(InputAdapterTest, TestPointerAddMonitor1, TestSize.Level1)
319 {
320     CALL_TEST_DEBUG;
321     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb90902(std::shared_ptr<OHOS::MMI::PointerEvent>) 322     auto callback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) {};
323     int32_t monitorId = inputAdapter->AddMonitor(callback);
324     ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
325 }
326 
327 /**
328  * @tc.name: TestPointerAddMonitor1
329  * @tc.desc: Test AddMonitor1
330  * @tc.type: FUNC
331  * @tc.require:
332  */
333 HWTEST_F(InputAdapterTest, TestKeyAddMonitor1, TestSize.Level1)
334 {
335     CALL_TEST_DEBUG;
336     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb90a02(std::shared_ptr<OHOS::MMI::KeyEvent>) 337     auto callback = [] (std::shared_ptr<OHOS::MMI::KeyEvent>) {};
338     int32_t monitorId = inputAdapter->AddMonitor(callback);
339     ASSERT_NO_FATAL_FAILURE(inputAdapter->RemoveMonitor(monitorId));
340 }
341 
342 /**
343  * @tc.name: TestAddKeyEventInterceptor1
344  * @tc.desc: Test AddKeyEventInterceptor1
345  * @tc.type: FUNC
346  * @tc.require:
347  */
348 HWTEST_F(InputAdapterTest, AddKeyEventInterceptor1, TestSize.Level1)
349 {
350     CALL_TEST_DEBUG;
351     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
352     int32_t interceptorId = inputAdapter->AddInterceptor(nullptr, nullptr);
353     ASSERT_EQ(interceptorId, RET_ERR);
354     inputAdapter->RemoveInterceptor(interceptorId);
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     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb90b02(std::shared_ptr<OHOS::MMI::PointerEvent>) 367     auto filterCallback = [] (std::shared_ptr<OHOS::MMI::PointerEvent>) -> bool {
368         FI_HILOGI("OnEvent");
369         return false;
370     };
371     int32_t filterId = inputAdapter->AddFilter(filterCallback);
372     ASSERT_TRUE(filterId > 0);
373     inputAdapter->RemoveFilter(filterId);
374 }
375 
376 /**
377  * @tc.name: TestOnInputEvent
378  * @tc.desc: Test OnInputEvent
379  * @tc.type: FUNC
380  * @tc.require:
381  */
382 HWTEST_F(InputAdapterTest, TesOnInputEvent, TestSize.Level1)
383 {
384     CALL_TEST_DEBUG;
__anon04b7ceb90c02(std::shared_ptr<MMI::PointerEvent> pointerEvent) 385     auto pointerCb = [](std::shared_ptr<MMI::PointerEvent> pointerEvent) {
386         pointerEvent =  MMI::PointerEvent::Create();
387         return ;
388     };
__anon04b7ceb90d02(std::shared_ptr<MMI::KeyEvent>keyEvent) 389     auto keyCb = [](std::shared_ptr<MMI::KeyEvent>keyEvent) {
390         keyEvent = MMI::KeyEvent::Create();
391         return ;
392     };
393     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
394     std::shared_ptr<MMI::PointerEvent> pointerEvent =  MMI::PointerEvent::Create();
395     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
396     InterceptorConsumer interceptorConsumer { pointerCb, keyCb };
397     ASSERT_NO_FATAL_FAILURE(interceptorConsumer.OnInputEvent(pointerEvent));
398     ASSERT_NO_FATAL_FAILURE(interceptorConsumer.OnInputEvent(keyEvent));
399 }
400 
401 /**
402  * @tc.name: TestOnInputEvent1
403  * @tc.desc: Test OnInputEvent1
404  * @tc.type: FUNC
405  * @tc.require:
406  */
407 HWTEST_F(InputAdapterTest, TestOnInputEvent1, TestSize.Level1)
408 {
409     CALL_TEST_DEBUG;
410     InterceptorConsumer interceptorConsumer1 {
__anon04b7ceb90e02() 411         [](std::shared_ptr<MMI::PointerEvent> cb) -> void {},
__anon04b7ceb90f02() 412         [](std::shared_ptr<MMI::KeyEvent> cb) -> void {}
413     };
414     std::shared_ptr<MMI::PointerEvent> pointerEvent =  MMI::PointerEvent::Create();
415     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
416     ASSERT_NO_FATAL_FAILURE(interceptorConsumer1.OnInputEvent(pointerEvent));
417     ASSERT_NO_FATAL_FAILURE(interceptorConsumer1.OnInputEvent(keyEvent));
418 }
419 
420 /**
421  * @tc.name: TestRegisterDevListener1
422  * @tc.desc: Test RegisterDevListener
423  * @tc.type: FUNC
424  * @tc.require:
425  */
426 HWTEST_F(InputAdapterTest, RegisterDevListener1, TestSize.Level1)
427 {
428     CALL_TEST_DEBUG;
429     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
430     int32_t ret = inputAdapter->RegisterDevListener(nullptr, nullptr);
431     ASSERT_EQ(ret, RET_ERR);
432     inputAdapter->UnregisterDevListener();
433 }
434 
435 /**
436  * @tc.name: TestRegisterDevListener2
437  * @tc.desc: Test RegisterDevListener
438  * @tc.type: FUNC
439  * @tc.require:
440  */
441 HWTEST_F(InputAdapterTest, RegisterDevListener2, TestSize.Level1)
442 {
443     CALL_TEST_DEBUG;
444     std::shared_ptr<IInputAdapter> inputAdapter = std::make_shared<InputAdapter>();
__anon04b7ceb91002(int32_t deviceId, const std::string &type) 445     auto devAddedCallback = [this](int32_t deviceId, const std::string &type) {
446         FI_HILOGI("Device added");
447     };
__anon04b7ceb91102(int32_t deviceId, const std::string &type) 448     auto devRemovedCallback = [this](int32_t deviceId, const std::string &type) {
449         FI_HILOGI("Device removed");
450     };
451     int32_t ret = inputAdapter->RegisterDevListener(devAddedCallback, devRemovedCallback);
452     ASSERT_EQ(ret, RET_OK);
453     inputAdapter->UnregisterDevListener();
454 }
455 
456 } // namespace DeviceStatus
457 } // namespace Msdp
458 } // namespace OHOS