1 /*
2 * Copyright (C) 2022 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 #define private public
17 #define protected public
18 #include "inputmethod_sysevent.h"
19 #undef private
20
21 #include <gtest/gtest.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24
25 #include <cstdint>
26 #include <string>
27
28 #include "global.h"
29 #include "hisysevent_base_manager.h"
30 #include "hisysevent_listener.h"
31 #include "hisysevent_manager.h"
32 #include "hisysevent_query_callback.h"
33 #include "hisysevent_record.h"
34 #include "input_method_ability.h"
35 #include "input_method_controller.h"
36 #include "input_method_engine_listener_impl.h"
37 #include "tdd_util.h"
38 #include "text_listener.h"
39
40 using namespace testing::ext;
41 using namespace OHOS::HiviewDFX;
42 namespace OHOS {
43 namespace MiscServices {
44 using WindowMgr = TddUtil::WindowManager;
45 constexpr const char *CMD1 = "hidumper -s 3703 -a -a";
46 constexpr const char *CMD2 = "hidumper -s 3703 -a -h";
47 constexpr const char *CMD3 = "hidumper -s 3703 -a -test";
48 constexpr const char *PARAM_KEY = "OPERATE_INFO";
49 constexpr const char *DOMAIN = "INPUTMETHOD";
50 constexpr const char *EVENT_NAME = "OPERATE_SOFTKEYBOARD";
51
52 class Watcher : public HiSysEventListener {
53 public:
Watcher(const std::string & operateInfo)54 explicit Watcher(const std::string &operateInfo) : operateInfo_(operateInfo)
55 {
56 }
~Watcher()57 virtual ~Watcher()
58 {
59 }
OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)60 void OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent) final
61 {
62 if (sysEvent == nullptr) {
63 IMSA_HILOGE("sysEvent is nullptr!");
64 return;
65 }
66 std::string result;
67 sysEvent->GetParamValue(PARAM_KEY, result);
68 IMSA_HILOGD("result = %{public}s", result.c_str());
69 if (result != operateInfo_) {
70 IMSA_HILOGE("string is not matched.");
71 return;
72 }
73 std::unique_lock<std::mutex> lock(cvMutex_);
74 watcherCv_.notify_all();
75 }
OnServiceDied()76 void OnServiceDied() final
77 {
78 IMSA_HILOGE("Watcher::OnServiceDied");
79 }
80 std::mutex cvMutex_;
81 std::condition_variable watcherCv_;
82 private:
83 std::string operateInfo_;
84 };
85
86 class InputMethodDfxTest : public testing::Test {
87 public:
88 using ExecFunc = std::function<void()>;
89 static void SetUpTestCase(void);
90 static void TearDownTestCase(void);
91 static bool WriteAndWatch(std::shared_ptr<Watcher> watcher, InputMethodDfxTest::ExecFunc exec);
92 void SetUp();
93 void TearDown();
94 static sptr<InputMethodController> inputMethodController_;
95 static sptr<OnTextChangedListener> textListener_;
96 static sptr<InputMethodAbility> inputMethodAbility_;
97 static std::shared_ptr<InputMethodEngineListenerImpl> imeListener_;
98 };
99 sptr<InputMethodController> InputMethodDfxTest::inputMethodController_;
100 sptr<OnTextChangedListener> InputMethodDfxTest::textListener_;
101 sptr<InputMethodAbility> InputMethodDfxTest::inputMethodAbility_;
102 std::shared_ptr<InputMethodEngineListenerImpl> InputMethodDfxTest::imeListener_;
103
WriteAndWatch(std::shared_ptr<Watcher> watcher,InputMethodDfxTest::ExecFunc exec)104 bool InputMethodDfxTest::WriteAndWatch(std::shared_ptr<Watcher> watcher, InputMethodDfxTest::ExecFunc exec)
105 {
106 OHOS::HiviewDFX::ListenerRule listenerRule(DOMAIN, EVENT_NAME, "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
107 std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
108 sysRules.emplace_back(listenerRule);
109 auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
110 if (ret != SUCCESS) {
111 IMSA_HILOGE("AddListener failed! ret = %{public}d", ret);
112 return false;
113 }
114 std::unique_lock<std::mutex> lock(watcher->cvMutex_);
115 exec();
116 bool result = watcher->watcherCv_.wait_for(lock, std::chrono::seconds(1)) != std::cv_status::timeout;
117 ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(watcher);
118 if (ret != SUCCESS || !result) {
119 IMSA_HILOGE("RemoveListener ret = %{public}d, wait_for result = %{public}s", ret, result ? "true" : "false");
120 return false;
121 }
122 return true;
123 }
124
SetUpTestCase(void)125 void InputMethodDfxTest::SetUpTestCase(void)
126 {
127 IMSA_HILOGI("InputMethodDfxTest::SetUpTestCase");
128 TddUtil::StorageSelfTokenID();
129 std::shared_ptr<Property> property = InputMethodController::GetInstance()->GetCurrentInputMethod();
130 std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest";
131 TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName));
132 inputMethodAbility_ = InputMethodAbility::GetInstance();
133 imeListener_ = std::make_shared<InputMethodEngineListenerImpl>();
134 inputMethodAbility_->OnImeReady();
135 inputMethodAbility_->SetCoreAndAgent();
136 inputMethodAbility_->SetImeListener(imeListener_);
137
138 inputMethodController_ = InputMethodController::GetInstance();
139 textListener_ = new TextListener();
140 TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, false, "undefine"));
141 TddUtil::WindowManager::RegisterFocusChangeListener();
142 WindowMgr::CreateWindow();
143 WindowMgr::ShowWindow();
144 bool isFocused = FocusChangedListenerTestImpl::isFocused_->GetValue();
145 IMSA_HILOGI("getFocus end, isFocused = %{public}d", isFocused);
146 }
147
TearDownTestCase(void)148 void InputMethodDfxTest::TearDownTestCase(void)
149 {
150 IMSA_HILOGI("InputMethodDfxTest::TearDownTestCase");
151 TddUtil::RestoreSelfTokenID();
152 WindowMgr::HideWindow();
153 WindowMgr::DestroyWindow();
154 }
155
SetUp(void)156 void InputMethodDfxTest::SetUp(void)
157 {
158 IMSA_HILOGI("InputMethodDfxTest::SetUp");
159 }
160
TearDown(void)161 void InputMethodDfxTest::TearDown(void)
162 {
163 IMSA_HILOGI("InputMethodDfxTest::TearDown");
164 }
165
166 /**
167 * @tc.name: InputMethodDfxTest_DumpAllMethod_001
168 * @tc.desc: DumpAllMethod
169 * @tc.type: FUNC
170 * @tc.require: issueI61PMG
171 * @tc.author: chenyu
172 */
173 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_DumpAllMethod_001, TestSize.Level0)
174 {
175 std::string result;
176 auto ret = TddUtil::ExecuteCmd(CMD1, result);
177 EXPECT_TRUE(ret);
178 EXPECT_NE(result.find("imeList"), std::string::npos);
179 EXPECT_NE(result.find("com.example.testIme"), std::string::npos);
180 }
181
182 /**
183 * @tc.name: InputMethodDfxTest_Dump_ShowHelp_001
184 * @tc.desc: Dump ShowHelp.
185 * @tc.type: FUNC
186 * @tc.require: issueI61PMG
187 * @tc.author: chenyu
188 */
189 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Dump_ShowHelp_001, TestSize.Level0)
190 {
191 std::string result;
192 auto ret = TddUtil::ExecuteCmd(CMD2, result);
193 EXPECT_TRUE(ret);
194 EXPECT_NE(result.find("Description:"), std::string::npos);
195 EXPECT_NE(result.find("-h show help"), std::string::npos);
196 EXPECT_NE(result.find("-a dump all input methods"), std::string::npos);
197 }
198
199 /**
200 * @tc.name: InputMethodDfxTest_Dump_ShowIllealInformation_001
201 * @tc.desc: Dump ShowIllealInformation.
202 * @tc.type: FUNC
203 * @tc.require: issueI61PMG
204 * @tc.author: chenyu
205 */
206 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Dump_ShowIllealInformation_001, TestSize.Level0)
207 {
208 std::string result;
209 auto ret = TddUtil::ExecuteCmd(CMD3, result);
210 EXPECT_TRUE(ret);
211 EXPECT_NE(result.find("input dump parameter error,enter '-h' for usage."), std::string::npos);
212 }
213
214 /**
215 * @tc.name: InputMethodDfxTest_Hisysevent_Attach
216 * @tc.desc: Hisysevent attach.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_Attach, TestSize.Level0)
220 {
221 auto watcher = std::make_shared<Watcher>(
222 InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ATTACH)));
__anon1d35de250102() 223 auto attach = []() { inputMethodController_->Attach(textListener_, true); };
224 EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, attach));
225 }
226
227 /**
228 * @tc.name: InputMethodDfxTest_Hisysevent_HideTextInput
229 * @tc.desc: Hisysevent HideTextInput.
230 * @tc.type: FUNC
231 */
232 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_HideTextInput, TestSize.Level0)
233 {
234 auto ret = inputMethodController_->Attach(textListener_, true);
235 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
236 auto watcher = std::make_shared<Watcher>(InputMethodSysEvent::GetInstance().GetOperateInfo(
237 static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNEDITABLE)));
__anon1d35de250202() 238 auto hideTextInput = []() { inputMethodController_->HideTextInput(); };
239 EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, hideTextInput));
240 }
241
242 /**
243 * @tc.name: InputMethodDfxTest_Hisysevent_ShowTextInput
244 * @tc.desc: Hisysevent ShowTextInput.
245 * @tc.type: FUNC
246 */
247 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_ShowTextInput, TestSize.Level0)
248 {
249 auto watcher = std::make_shared<Watcher>(InputMethodSysEvent::GetInstance().GetOperateInfo(
250 static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ENEDITABLE)));
__anon1d35de250302() 251 auto showTextInput = []() { inputMethodController_->ShowTextInput(); };
252 EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, showTextInput));
253 }
254
255 /**
256 * @tc.name: InputMethodDfxTest_Hisysevent_HideCurrentInput
257 * @tc.desc: Hisysevent HideCurrentInput.
258 * @tc.type: FUNC
259 */
260 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_HideCurrentInput, TestSize.Level0)
261 {
262 auto watcher = std::make_shared<Watcher>(
263 InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_NORMAL)));
__anon1d35de250402() 264 auto hideCurrentInput = []() { inputMethodController_->HideCurrentInput(); };
265 EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, hideCurrentInput));
266 }
267
268 /**
269 * @tc.name: InputMethodDfxTest_Hisysevent_ShowCurrentInput
270 * @tc.desc: Hisysevent ShowCurrentInput.
271 * @tc.type: FUNC
272 */
273 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_ShowCurrentInput, TestSize.Level0)
274 {
275 auto watcher = std::make_shared<Watcher>(
276 InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_NORMAL)));
__anon1d35de250502() 277 auto showCurrentInput = []() { inputMethodController_->ShowCurrentInput(); };
278 EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, showCurrentInput));
279 }
280
281 /**
282 * @tc.name: InputMethodDfxTest_Hisysevent_HideSoftKeyboard
283 * @tc.desc: Hisysevent HideSoftKeyboard.
284 * @tc.type: FUNC
285 */
286 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_HideSoftKeyboard, TestSize.Level0)
287 {
288 auto watcher = std::make_shared<Watcher>(
289 InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_NORMAL)));
__anon1d35de250602() 290 auto hideSoftKeyboard = []() { inputMethodController_->HideSoftKeyboard(); };
291 EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, hideSoftKeyboard));
292 }
293
294 /**
295 * @tc.name: InputMethodDfxTest_Hisysevent_ShowSoftKeyboard
296 * @tc.desc: Hisysevent ShowSoftKeyboard.
297 * @tc.type: FUNC
298 */
299 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_ShowSoftKeyboard, TestSize.Level0)
300 {
301 auto watcher = std::make_shared<Watcher>(
302 InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_NORMAL)));
__anon1d35de250702() 303 auto showSoftKeyboard = []() { inputMethodController_->ShowSoftKeyboard(); };
304 EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, showSoftKeyboard));
305 }
306
307 /**
308 * @tc.name: InputMethodDfxTest_Hisysevent_HideKeyboardSelf
309 * @tc.desc: Hisysevent HideKeyboardSelf.
310 * @tc.type: FUNC
311 */
312 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_HideKeyboardSelf, TestSize.Level0)
313 {
314 auto watcher = std::make_shared<Watcher>(
315 InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_SELF)));
__anon1d35de250802() 316 auto hideKeyboardSelf = []() { inputMethodAbility_->HideKeyboardSelf(); };
317 EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, hideKeyboardSelf));
318 }
319
320 /**
321 * @tc.name: InputMethodDfxTest_Hisysevent_Close
322 * @tc.desc: Hisysevent Close.
323 * @tc.type: FUNC
324 */
325 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_Close, TestSize.Level0)
326 {
327 auto watcher = std::make_shared<Watcher>(
328 InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_UNBIND)));
__anon1d35de250902() 329 auto close = []() { inputMethodController_->Close(); };
330 EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, close));
331 }
332 } // namespace MiscServices
333 } // namespace OHOS
334