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