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