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,bool isAutoToVirtualScreen,int32_t useCoordinate)90 void InputManager::SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent, bool isAutoToVirtualScreen,
91 int32_t useCoordinate)
92 {
93 HILOG_DEBUG();
94 std::lock_guard<ffrt::mutex> lock(g_mtx);
95 int32_t touchAction = pointerEvent->GetPointerAction();
96 mockTouchActions.push_back(touchAction);
97 }
98
AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptorId)99 int32_t InputManager::AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptorId)
100 {
101 HILOG_DEBUG();
102 mockInputEventConsumer = interceptorId;
103 return 0;
104 }
105
AddInterceptor(std::function<void (std::shared_ptr<KeyEvent>)> interceptor)106 int32_t InputManager::AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor)
107 {
108 HILOG_DEBUG();
109 mockKeyEventCallback = interceptor;
110 return 0;
111 }
112
GetKeyEventInterceptor()113 std::function<void(std::shared_ptr<KeyEvent>)> MockInputManager::GetKeyEventInterceptor()
114 {
115 HILOG_DEBUG();
116 return mockKeyEventCallback;
117 }
118
RemoveInterceptor(int32_t interceptorId)119 void InputManager::RemoveInterceptor(int32_t interceptorId)
120 {
121 HILOG_DEBUG();
122 }
123 } // namespace MMI
124 } // namespace OHOS