1 /*
2 * Copyright (c) 2023 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 "keyboard_listener_test_impl.h"
17
18 #include "global.h"
19
20 namespace OHOS {
21 namespace MiscServices {
22 std::mutex KeyboardListenerTestImpl::kdListenerLock_;
23 std::condition_variable KeyboardListenerTestImpl::kdListenerCv_;
24 int32_t KeyboardListenerTestImpl::keyCode_{ -1 };
25 int32_t KeyboardListenerTestImpl::cursorHeight_{ -1 };
26 int32_t KeyboardListenerTestImpl::newBegin_{ -1 };
27 std::string KeyboardListenerTestImpl::text_;
28 InputAttribute KeyboardListenerTestImpl::inputAttribute_{ 0, 0, 0 };
OnKeyEvent(int32_t keyCode,int32_t keyStatus,sptr<KeyEventConsumerProxy> & consumer)29 bool KeyboardListenerTestImpl::OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer)
30 {
31 keyCode_ = keyCode;
32 if (consumer != nullptr) {
33 consumer->OnKeyCodeConsumeResult(true);
34 }
35 return true;
36 }
37
OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,sptr<KeyEventConsumerProxy> & consumer)38 bool KeyboardListenerTestImpl::OnDealKeyEvent(
39 const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer)
40 {
41 bool isKeyCodeConsume = OnKeyEvent(keyEvent->GetKeyCode(), keyEvent->GetKeyAction(), consumer);
42 bool isKeyEventConsume = OnKeyEvent(keyEvent, consumer);
43 if (consumer != nullptr) {
44 consumer->OnKeyEventResult(isKeyEventConsume || isKeyCodeConsume);
45 }
46 return true;
47 }
48
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)49 void KeyboardListenerTestImpl::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
50 {
51 cursorHeight_ = height;
52 kdListenerCv_.notify_one();
53 }
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)54 void KeyboardListenerTestImpl::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
55 {
56 newBegin_ = newBegin;
57 kdListenerCv_.notify_one();
58 }
OnTextChange(const std::string & text)59 void KeyboardListenerTestImpl::OnTextChange(const std::string &text)
60 {
61 text_ = text;
62 kdListenerCv_.notify_one();
63 }
OnEditorAttributeChange(const InputAttribute & inputAttribute)64 void KeyboardListenerTestImpl::OnEditorAttributeChange(const InputAttribute &inputAttribute)
65 {
66 inputAttribute_ = inputAttribute;
67 kdListenerCv_.notify_one();
68 }
ResetParam()69 void KeyboardListenerTestImpl::ResetParam()
70 {
71 keyCode_ = -1;
72 cursorHeight_ = -1;
73 newBegin_ = -1;
74 text_ = "";
75 inputAttribute_.inputPattern = 0;
76 inputAttribute_.enterKeyType = 0;
77 inputAttribute_.inputOption = 0;
78 }
WaitKeyEvent(int32_t keyCode)79 bool KeyboardListenerTestImpl::WaitKeyEvent(int32_t keyCode)
80 {
81 std::unique_lock<std::mutex> lock(kdListenerLock_);
82 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&keyCode]() { return keyCode == keyCode_; });
83 return keyCode == keyCode_;
84 }
WaitCursorUpdate()85 bool KeyboardListenerTestImpl::WaitCursorUpdate()
86 {
87 std::unique_lock<std::mutex> lock(kdListenerLock_);
88 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), []() { return cursorHeight_ > 0; });
89 return cursorHeight_ > 0;
90 }
WaitSelectionChange(int32_t newBegin)91 bool KeyboardListenerTestImpl::WaitSelectionChange(int32_t newBegin)
92 {
93 std::unique_lock<std::mutex> lock(kdListenerLock_);
94 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&newBegin]() { return newBegin == newBegin_; });
95 return newBegin == newBegin_;
96 }
WaitTextChange(const std::string & text)97 bool KeyboardListenerTestImpl::WaitTextChange(const std::string &text)
98 {
99 std::unique_lock<std::mutex> lock(kdListenerLock_);
100 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&text]() { return text == text_; });
101 return text == text_;
102 }
WaitEditorAttributeChange(const InputAttribute & inputAttribute)103 bool KeyboardListenerTestImpl::WaitEditorAttributeChange(const InputAttribute &inputAttribute)
104 {
105 std::unique_lock<std::mutex> lock(kdListenerLock_);
106 kdListenerCv_.wait_for(
107 lock, std::chrono::seconds(1), [&inputAttribute]() { return inputAttribute == inputAttribute_; });
108 return inputAttribute == inputAttribute_;
109 }
110 } // namespace MiscServices
111 } // namespace OHOS
112