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 }
54
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)55 void KeyboardListenerTestImpl::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
56 {
57 newBegin_ = newBegin;
58 kdListenerCv_.notify_one();
59 }
60
OnTextChange(const std::string & text)61 void KeyboardListenerTestImpl::OnTextChange(const std::string &text)
62 {
63 text_ = text;
64 kdListenerCv_.notify_one();
65 }
66
OnEditorAttributeChange(const InputAttribute & inputAttribute)67 void KeyboardListenerTestImpl::OnEditorAttributeChange(const InputAttribute &inputAttribute)
68 {
69 inputAttribute_ = inputAttribute;
70 kdListenerCv_.notify_one();
71 }
72
ResetParam()73 void KeyboardListenerTestImpl::ResetParam()
74 {
75 keyCode_ = -1;
76 cursorHeight_ = -1;
77 newBegin_ = -1;
78 text_ = "";
79 inputAttribute_.inputPattern = 0;
80 inputAttribute_.enterKeyType = 0;
81 inputAttribute_.inputOption = 0;
82 }
83
WaitKeyEvent(int32_t keyCode)84 bool KeyboardListenerTestImpl::WaitKeyEvent(int32_t keyCode)
85 {
86 std::unique_lock<std::mutex> lock(kdListenerLock_);
87 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&keyCode]() {
88 return keyCode == keyCode_;
89 });
90 return keyCode == keyCode_;
91 }
92
WaitCursorUpdate()93 bool KeyboardListenerTestImpl::WaitCursorUpdate()
94 {
95 std::unique_lock<std::mutex> lock(kdListenerLock_);
96 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), []() {
97 return cursorHeight_ > 0;
98 });
99 return cursorHeight_ > 0;
100 }
101
WaitSelectionChange(int32_t newBegin)102 bool KeyboardListenerTestImpl::WaitSelectionChange(int32_t newBegin)
103 {
104 std::unique_lock<std::mutex> lock(kdListenerLock_);
105 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&newBegin]() {
106 return newBegin == newBegin_;
107 });
108 return newBegin == newBegin_;
109 }
110
WaitTextChange(const std::string & text)111 bool KeyboardListenerTestImpl::WaitTextChange(const std::string &text)
112 {
113 std::unique_lock<std::mutex> lock(kdListenerLock_);
114 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&text]() {
115 return text == text_;
116 });
117 return text == text_;
118 }
119
WaitEditorAttributeChange(const InputAttribute & inputAttribute)120 bool KeyboardListenerTestImpl::WaitEditorAttributeChange(const InputAttribute &inputAttribute)
121 {
122 std::unique_lock<std::mutex> lock(kdListenerLock_);
123 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&inputAttribute]() {
124 return inputAttribute == inputAttribute_;
125 });
126 return inputAttribute == inputAttribute_;
127 }
128 } // namespace MiscServices
129 } // namespace OHOS
130