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