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_{ "-1" };
28 InputAttribute KeyboardListenerTestImpl::inputAttribute_ { 0, 0, 0 };
29 int32_t KeyboardListenerTestImpl::funcKey_ { -1 };
OnKeyEvent(int32_t keyCode,int32_t keyStatus,sptr<KeyEventConsumerProxy> & consumer)30 bool KeyboardListenerTestImpl::OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer)
31 {
32 keyCode_ = keyCode;
33 return true;
34 }
35
OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,uint64_t cbId,const sptr<IRemoteObject> & channelObject)36 bool KeyboardListenerTestImpl::OnDealKeyEvent(
37 const std::shared_ptr<MMI::KeyEvent> &keyEvent, uint64_t cbId, const sptr<IRemoteObject> &channelObject)
38 {
39 sptr<KeyEventConsumerProxy> consumer = new (std::nothrow) KeyEventConsumerProxy(nullptr);
40 OnKeyEvent(keyEvent->GetKeyCode(), keyEvent->GetKeyAction(), consumer);
41 OnKeyEvent(keyEvent, consumer);
42 return true;
43 }
44
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)45 void KeyboardListenerTestImpl::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
46 {
47 cursorHeight_ = height;
48 kdListenerCv_.notify_one();
49 }
50
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)51 void KeyboardListenerTestImpl::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
52 {
53 newBegin_ = newBegin;
54 kdListenerCv_.notify_one();
55 }
56
OnTextChange(const std::string & text)57 void KeyboardListenerTestImpl::OnTextChange(const std::string &text)
58 {
59 text_ = text;
60 kdListenerCv_.notify_one();
61 }
62
OnEditorAttributeChange(const InputAttribute & inputAttribute)63 void KeyboardListenerTestImpl::OnEditorAttributeChange(const InputAttribute &inputAttribute)
64 {
65 inputAttribute_ = inputAttribute;
66 kdListenerCv_.notify_one();
67 }
68
OnFunctionKey(int32_t funcKey)69 void KeyboardListenerTestImpl::OnFunctionKey(int32_t funcKey)
70 {
71 funcKey_ = funcKey;
72 kdListenerCv_.notify_one();
73 }
74
ResetParam()75 void KeyboardListenerTestImpl::ResetParam()
76 {
77 keyCode_ = -1;
78 cursorHeight_ = -1;
79 newBegin_ = -1;
80 text_ = "-1";
81 inputAttribute_.inputPattern = 0;
82 inputAttribute_.enterKeyType = 0;
83 inputAttribute_.inputOption = 0;
84 funcKey_ = -1;
85 }
86
WaitKeyEvent(int32_t keyCode)87 bool KeyboardListenerTestImpl::WaitKeyEvent(int32_t keyCode)
88 {
89 std::unique_lock<std::mutex> lock(kdListenerLock_);
90 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&keyCode]() {
91 return keyCode == keyCode_;
92 });
93 return keyCode == keyCode_;
94 }
95
WaitCursorUpdate()96 bool KeyboardListenerTestImpl::WaitCursorUpdate()
97 {
98 std::unique_lock<std::mutex> lock(kdListenerLock_);
99 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), []() {
100 return cursorHeight_ > 0;
101 });
102 return cursorHeight_ > 0;
103 }
104
WaitSelectionChange(int32_t newBegin)105 bool KeyboardListenerTestImpl::WaitSelectionChange(int32_t newBegin)
106 {
107 std::unique_lock<std::mutex> lock(kdListenerLock_);
108 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&newBegin]() {
109 return newBegin == newBegin_;
110 });
111 return newBegin == newBegin_;
112 }
113
WaitTextChange(const std::string & text)114 bool KeyboardListenerTestImpl::WaitTextChange(const std::string &text)
115 {
116 std::unique_lock<std::mutex> lock(kdListenerLock_);
117 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&text]() {
118 return text == text_;
119 });
120 return text == text_;
121 }
122
WaitEditorAttributeChange(const InputAttribute & inputAttribute)123 bool KeyboardListenerTestImpl::WaitEditorAttributeChange(const InputAttribute &inputAttribute)
124 {
125 std::unique_lock<std::mutex> lock(kdListenerLock_);
126 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&inputAttribute]() {
127 return inputAttribute == inputAttribute_;
128 });
129 return inputAttribute == inputAttribute_;
130 }
131
WaitFunctionKey(int32_t funcKey)132 bool KeyboardListenerTestImpl::WaitFunctionKey(int32_t funcKey)
133 {
134 std::unique_lock<std::mutex> lock(kdListenerLock_);
135 kdListenerCv_.wait_for(lock, std::chrono::seconds(1), [&funcKey]() {
136 return funcKey == funcKey_;
137 });
138 return funcKey == funcKey_;
139 }
140 } // namespace MiscServices
141 } // namespace OHOS
142