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 "ime_setting_listener_test_impl.h"
17
18 namespace OHOS {
19 namespace MiscServices {
20 constexpr int32_t SWITCH_IME_WAIT_TIME = 3;
21 InputWindowStatus ImeSettingListenerTestImpl::status_{ InputWindowStatus::NONE };
22 SubProperty ImeSettingListenerTestImpl::subProperty_{};
23 Property ImeSettingListenerTestImpl::property_{};
24 std::mutex ImeSettingListenerTestImpl::imeSettingListenerLock_;
25 bool ImeSettingListenerTestImpl::isImeChange_{ false };
26 std::condition_variable ImeSettingListenerTestImpl::imeSettingListenerCv_;
ResetParam()27 void ImeSettingListenerTestImpl::ResetParam()
28 {
29 status_ = InputWindowStatus::NONE;
30 subProperty_ = {};
31 property_ = {};
32 isImeChange_ = false;
33 }
WaitPanelHide()34 bool ImeSettingListenerTestImpl::WaitPanelHide()
35 {
36 std::unique_lock<std::mutex> lock(imeSettingListenerLock_);
37 imeSettingListenerCv_.wait_for(lock, std::chrono::seconds(SWITCH_IME_WAIT_TIME),
38 []() { return status_ == InputWindowStatus::HIDE; });
39 return status_ == InputWindowStatus::HIDE;
40 }
WaitPanelShow()41 bool ImeSettingListenerTestImpl::WaitPanelShow()
42 {
43 std::unique_lock<std::mutex> lock(imeSettingListenerLock_);
44 imeSettingListenerCv_.wait_for(lock, std::chrono::seconds(SWITCH_IME_WAIT_TIME),
45 []() { return status_ == InputWindowStatus::SHOW; });
46 return status_ == InputWindowStatus::SHOW;
47 }
48
WaitImeChange()49 bool ImeSettingListenerTestImpl::WaitImeChange()
50 {
51 std::unique_lock<std::mutex> lock(imeSettingListenerLock_);
52 imeSettingListenerCv_.wait_for(lock, std::chrono::seconds(SWITCH_IME_WAIT_TIME), []() { return isImeChange_; });
53 return isImeChange_;
54 }
55
WaitTargetImeChange(const std::string & bundleName)56 bool ImeSettingListenerTestImpl::WaitTargetImeChange(const std::string &bundleName)
57 {
58 std::unique_lock<std::mutex> lock(imeSettingListenerLock_);
59 do {
60 // 3 means 3 seconds.
61 imeSettingListenerCv_.wait_for(lock, std::chrono::seconds(SWITCH_IME_WAIT_TIME),
62 [&bundleName]() { return bundleName == property_.name; });
63 } while (bundleName != property_.name);
64 return isImeChange_ && bundleName == property_.name;
65 }
66
WaitImeChange(const SubProperty & subProperty)67 bool ImeSettingListenerTestImpl::WaitImeChange(const SubProperty &subProperty)
68 {
69 std::unique_lock<std::mutex> lock(imeSettingListenerLock_);
70 imeSettingListenerCv_.wait_for(lock, std::chrono::seconds(SWITCH_IME_WAIT_TIME),
71 [&subProperty]() { return subProperty_.id == subProperty.id && subProperty_.name == subProperty.name; });
72 return subProperty_.id == subProperty.id && subProperty_.name == subProperty.name;
73 }
OnImeChange(const Property & property,const SubProperty & subProperty)74 void ImeSettingListenerTestImpl::OnImeChange(const Property &property, const SubProperty &subProperty)
75 {
76 std::unique_lock<std::mutex> lock(imeSettingListenerLock_);
77 isImeChange_ = true;
78 subProperty_ = subProperty;
79 property_ = property;
80 imeSettingListenerCv_.notify_one();
81 }
OnImeShow(const ImeWindowInfo & info)82 void ImeSettingListenerTestImpl::OnImeShow(const ImeWindowInfo &info)
83 {
84 std::unique_lock<std::mutex> lock(imeSettingListenerLock_);
85 status_ = InputWindowStatus::SHOW;
86 imeSettingListenerCv_.notify_one();
87 }
OnImeHide(const ImeWindowInfo & info)88 void ImeSettingListenerTestImpl::OnImeHide(const ImeWindowInfo &info)
89 {
90 std::unique_lock<std::mutex> lock(imeSettingListenerLock_);
91 status_ = InputWindowStatus::HIDE;
92 imeSettingListenerCv_.notify_one();
93 }
94 } // namespace MiscServices
95 } // namespace OHOS