• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
83 private:
84     std::string operateInfo_;
85 };
86 
87 class InputMethodDfxTest : public testing::Test {
88 public:
89     using ExecFunc = std::function<void()>;
90     static void SetUpTestCase(void);
91     static void TearDownTestCase(void);
92     static bool WriteAndWatch(std::shared_ptr<Watcher> watcher, InputMethodDfxTest::ExecFunc exec);
93     void SetUp();
94     void TearDown();
95     static sptr<InputMethodController> inputMethodController_;
96     static sptr<OnTextChangedListener> textListener_;
97     static sptr<InputMethodAbility> inputMethodAbility_;
98     static std::shared_ptr<InputMethodEngineListenerImpl> imeListener_;
99 };
100 sptr<InputMethodController> InputMethodDfxTest::inputMethodController_;
101 sptr<OnTextChangedListener> InputMethodDfxTest::textListener_;
102 sptr<InputMethodAbility> InputMethodDfxTest::inputMethodAbility_;
103 std::shared_ptr<InputMethodEngineListenerImpl> InputMethodDfxTest::imeListener_;
104 
WriteAndWatch(std::shared_ptr<Watcher> watcher,InputMethodDfxTest::ExecFunc exec)105 bool InputMethodDfxTest::WriteAndWatch(std::shared_ptr<Watcher> watcher, InputMethodDfxTest::ExecFunc exec)
106 {
107     OHOS::HiviewDFX::ListenerRule listenerRule(DOMAIN, EVENT_NAME, "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
108     std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
109     sysRules.emplace_back(listenerRule);
110     auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
111     if (ret != SUCCESS) {
112         IMSA_HILOGE("AddListener failed! ret = %{public}d", ret);
113         return false;
114     }
115     std::unique_lock<std::mutex> lock(watcher->cvMutex_);
116     exec();
117     bool result = watcher->watcherCv_.wait_for(lock, std::chrono::seconds(1)) != std::cv_status::timeout;
118     ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(watcher);
119     if (ret != SUCCESS || !result) {
120         IMSA_HILOGE("RemoveListener ret = %{public}d, wait_for result = %{public}s", ret, result ? "true" : "false");
121         return false;
122     }
123     return true;
124 }
125 
SetUpTestCase(void)126 void InputMethodDfxTest::SetUpTestCase(void)
127 {
128     IMSA_HILOGI("InputMethodDfxTest::SetUpTestCase");
129     TddUtil::StorageSelfTokenID();
130     std::shared_ptr<Property> property = InputMethodController::GetInstance()->GetCurrentInputMethod();
131     std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest";
132     TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName));
133     inputMethodAbility_ = InputMethodAbility::GetInstance();
134     imeListener_ = std::make_shared<InputMethodEngineListenerImpl>();
135     inputMethodAbility_->SetCoreAndAgent();
136     inputMethodAbility_->SetImeListener(imeListener_);
137 
138     inputMethodController_ = InputMethodController::GetInstance();
139     textListener_ = new TextListener();
140     TddUtil::SetTestTokenID(
141         TddUtil::AllocTestTokenID(true, "undefine", { "ohos.permission.READ_DFX_SYSEVENT", "ohos.permission.DUMP" }));
142     TddUtil::InitWindow(true);
143 }
144 
TearDownTestCase(void)145 void InputMethodDfxTest::TearDownTestCase(void)
146 {
147     IMSA_HILOGI("InputMethodDfxTest::TearDownTestCase");
148     TddUtil::RestoreSelfTokenID();
149     TddUtil::DestroyWindow();
150 }
151 
SetUp(void)152 void InputMethodDfxTest::SetUp(void)
153 {
154     IMSA_HILOGI("InputMethodDfxTest::SetUp");
155 }
156 
TearDown(void)157 void InputMethodDfxTest::TearDown(void)
158 {
159     IMSA_HILOGI("InputMethodDfxTest::TearDown");
160 }
161 
162 /**
163 * @tc.name: InputMethodDfxTest_DumpAllMethod_001
164 * @tc.desc: DumpAllMethod
165 * @tc.type: FUNC
166 * @tc.require: issueI61PMG
167 * @tc.author: chenyu
168 */
169 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_DumpAllMethod_001, TestSize.Level0)
170 {
171     std::string result;
172     auto ret = TddUtil::ExecuteCmd(CMD1, result);
173     EXPECT_TRUE(ret);
174     EXPECT_NE(result.find("imeList"), std::string::npos);
175     EXPECT_NE(result.find("com.example.testIme"), std::string::npos);
176 }
177 
178 /**
179 * @tc.name: InputMethodDfxTest_Dump_ShowHelp_001
180 * @tc.desc: Dump ShowHelp.
181 * @tc.type: FUNC
182 * @tc.require: issueI61PMG
183 * @tc.author: chenyu
184 */
185 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Dump_ShowHelp_001, TestSize.Level0)
186 {
187     std::string result;
188     auto ret = TddUtil::ExecuteCmd(CMD2, result);
189     EXPECT_TRUE(ret);
190     EXPECT_NE(result.find("Description:"), std::string::npos);
191     EXPECT_NE(result.find("-h show help"), std::string::npos);
192     EXPECT_NE(result.find("-a dump all input methods"), std::string::npos);
193 }
194 
195 /**
196 * @tc.name: InputMethodDfxTest_Dump_ShowIllealInformation_001
197 * @tc.desc: Dump ShowIllealInformation.
198 * @tc.type: FUNC
199 * @tc.require: issueI61PMG
200 * @tc.author: chenyu
201 */
202 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Dump_ShowIllealInformation_001, TestSize.Level0)
203 {
204     std::string result;
205     auto ret = TddUtil::ExecuteCmd(CMD3, result);
206     EXPECT_TRUE(ret);
207     EXPECT_NE(result.find("input dump parameter error,enter '-h' for usage."), std::string::npos);
208 }
209 
210 /**
211 * @tc.name: InputMethodDfxTest_Hisysevent_Attach
212 * @tc.desc: Hisysevent attach.
213 * @tc.type: FUNC
214 */
215 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_Attach, TestSize.Level0)
216 {
217     auto watcher = std::make_shared<Watcher>(
218         InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ATTACH)));
__anon7ea1cc660102() 219     auto attach = []() { inputMethodController_->Attach(textListener_, true); };
220     EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, attach));
221 }
222 
223 /**
224 * @tc.name: InputMethodDfxTest_Hisysevent_HideTextInput
225 * @tc.desc: Hisysevent HideTextInput.
226 * @tc.type: FUNC
227 */
228 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_HideTextInput, TestSize.Level0)
229 {
230     auto ret = inputMethodController_->Attach(textListener_, true);
231     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
232     auto watcher = std::make_shared<Watcher>(InputMethodSysEvent::GetInstance().GetOperateInfo(
233         static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_UNEDITABLE)));
__anon7ea1cc660202() 234     auto hideTextInput = []() { inputMethodController_->HideTextInput(); };
235     EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, hideTextInput));
236 }
237 
238 /**
239 * @tc.name: InputMethodDfxTest_Hisysevent_ShowTextInput
240 * @tc.desc: Hisysevent ShowTextInput.
241 * @tc.type: FUNC
242 */
243 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_ShowTextInput, TestSize.Level0)
244 {
245     auto watcher = std::make_shared<Watcher>(InputMethodSysEvent::GetInstance().GetOperateInfo(
246         static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_ENEDITABLE)));
__anon7ea1cc660302() 247     auto showTextInput = []() { inputMethodController_->ShowTextInput(); };
248     EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, showTextInput));
249 }
250 
251 /**
252 * @tc.name: InputMethodDfxTest_Hisysevent_HideCurrentInput
253 * @tc.desc: Hisysevent HideCurrentInput.
254 * @tc.type: FUNC
255 */
256 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_HideCurrentInput, TestSize.Level0)
257 {
258     auto watcher = std::make_shared<Watcher>(
259         InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_NORMAL)));
__anon7ea1cc660402() 260     auto hideCurrentInput = []() { inputMethodController_->HideCurrentInput(); };
261     EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, hideCurrentInput));
262 }
263 
264 /**
265 * @tc.name: InputMethodDfxTest_Hisysevent_ShowCurrentInput
266 * @tc.desc: Hisysevent ShowCurrentInput.
267 * @tc.type: FUNC
268 */
269 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_ShowCurrentInput, TestSize.Level0)
270 {
271     auto watcher = std::make_shared<Watcher>(
272         InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_NORMAL)));
__anon7ea1cc660502() 273     auto showCurrentInput = []() { inputMethodController_->ShowCurrentInput(); };
274     EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, showCurrentInput));
275 }
276 
277 /**
278 * @tc.name: InputMethodDfxTest_Hisysevent_HideSoftKeyboard
279 * @tc.desc: Hisysevent HideSoftKeyboard.
280 * @tc.type: FUNC
281 */
282 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_HideSoftKeyboard, TestSize.Level0)
283 {
284     auto watcher = std::make_shared<Watcher>(
285         InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_NORMAL)));
__anon7ea1cc660602() 286     auto hideSoftKeyboard = []() { inputMethodController_->HideSoftKeyboard(); };
287     EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, hideSoftKeyboard));
288 }
289 
290 /**
291 * @tc.name: InputMethodDfxTest_Hisysevent_ShowSoftKeyboard
292 * @tc.desc: Hisysevent ShowSoftKeyboard.
293 * @tc.type: FUNC
294 */
295 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_ShowSoftKeyboard, TestSize.Level0)
296 {
297     auto watcher = std::make_shared<Watcher>(
298         InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_SHOW_NORMAL)));
__anon7ea1cc660702() 299     auto showSoftKeyboard = []() { inputMethodController_->ShowSoftKeyboard(); };
300     EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, showSoftKeyboard));
301 }
302 
303 /**
304 * @tc.name: InputMethodDfxTest_Hisysevent_HideKeyboardSelf
305 * @tc.desc: Hisysevent HideKeyboardSelf.
306 * @tc.type: FUNC
307 */
308 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_HideKeyboardSelf, TestSize.Level0)
309 {
310     auto watcher = std::make_shared<Watcher>(
311         InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_HIDE_SELF)));
__anon7ea1cc660802() 312     auto hideKeyboardSelf = []() { inputMethodAbility_->HideKeyboardSelf(); };
313     EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, hideKeyboardSelf));
314 }
315 
316 /**
317 * @tc.name: InputMethodDfxTest_Hisysevent_Close
318 * @tc.desc: Hisysevent Close.
319 * @tc.type: FUNC
320 */
321 HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_Close, TestSize.Level0)
322 {
323     auto watcher = std::make_shared<Watcher>(
324         InputMethodSysEvent::GetInstance().GetOperateInfo(static_cast<int32_t>(OperateIMEInfoCode::IME_UNBIND)));
__anon7ea1cc660902() 325     auto close = []() { inputMethodController_->Close(); };
326     EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, close));
327 }
328 } // namespace MiscServices
329 } // namespace OHOS
330