1 /*
2  * Copyright (C) 2022 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 "ffrt.h"
17 #include "mock_input_manager.h"
18 #include "hilog_wrapper.h"
19 
20 namespace OHOS {
21 namespace MMI {
22 static int mockKeyCode = -1;
23 static std::vector<int32_t> mockTouchActions;
24 static std::function<void(std::shared_ptr<MMI::KeyEvent>)> mockKeyEventCallback = nullptr;
25 static std::shared_ptr<MMI::IInputEventConsumer> mockInputEventConsumer = nullptr;
26 static ffrt::mutex g_mtx;
27 
GetKeyCode()28 int MockInputManager::GetKeyCode()
29 {
30     return mockKeyCode;
31 }
32 
ClearTouchActions()33 void MockInputManager::ClearTouchActions()
34 {
35     std::lock_guard<ffrt::mutex> lock(g_mtx);
36     mockTouchActions.clear();
37 }
38 
GetTouchActions()39 std::vector<int32_t> MockInputManager::GetTouchActions()
40 {
41     std::lock_guard<ffrt::mutex> lock(g_mtx);
42     return mockTouchActions;
43 }
44 
GetTouchActionOfTargetIndex(int32_t index)45 int32_t MockInputManager::GetTouchActionOfTargetIndex(int32_t index)
46 {
47     std::lock_guard<ffrt::mutex> lock(g_mtx);
48     int32_t size = static_cast<int32_t>(mockTouchActions.size());
49     if (size > index) {
50         return mockTouchActions[index];
51     }
52     return -1;
53 }
54 
ClearInputEventConsumer()55 void MockInputManager::ClearInputEventConsumer()
56 {
57     HILOG_DEBUG();
58     mockInputEventConsumer = nullptr;
59 }
60 
GetInputEventConsumer()61 std::shared_ptr<IInputEventConsumer> MockInputManager::GetInputEventConsumer()
62 {
63     HILOG_DEBUG();
64     return mockInputEventConsumer;
65 }
66 
67 InputManager *InputManager::instance_ = new(std::nothrow) InputManager();
GetInstance()68 InputManager *InputManager::GetInstance()
69 {
70     HILOG_DEBUG();
71     if (instance_ == nullptr) {
72         instance_ = new(std::nothrow) InputManager();
73     }
74     return instance_;
75 }
76 
MoveMouse(int32_t offsetX,int32_t offsetY)77 void InputManager::MoveMouse(int32_t offsetX, int32_t offsetY)
78 {
79     HILOG_DEBUG();
80     (void)offsetX;
81     (void)offsetY;
82 }
83 
SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent)84 void InputManager::SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent)
85 {
86     HILOG_DEBUG();
87     mockKeyCode = keyEvent->GetKeyCode();
88 }
89 
SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent)90 void InputManager::SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent)
91 {
92     HILOG_DEBUG();
93     std::lock_guard<ffrt::mutex> lock(g_mtx);
94     int32_t touchAction = pointerEvent->GetPointerAction();
95     mockTouchActions.push_back(touchAction);
96 }
97 
AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptorId)98 int32_t InputManager::AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptorId)
99 {
100     HILOG_DEBUG();
101     mockInputEventConsumer = interceptorId;
102     return 0;
103 }
104 
AddInterceptor(std::function<void (std::shared_ptr<KeyEvent>)> interceptor)105 int32_t InputManager::AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor)
106 {
107     HILOG_DEBUG();
108     mockKeyEventCallback = interceptor;
109     return 0;
110 }
111 
GetKeyEventInterceptor()112 std::function<void(std::shared_ptr<KeyEvent>)> MockInputManager::GetKeyEventInterceptor()
113 {
114     HILOG_DEBUG();
115     return mockKeyEventCallback;
116 }
117 
RemoveInterceptor(int32_t interceptorId)118 void InputManager::RemoveInterceptor(int32_t interceptorId)
119 {
120     HILOG_DEBUG();
121 }
122 } // namespace MMI
123 } // namespace OHOS